Browse Source

Fixed SD2-458: Tab is always replaced with 4 spaces

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@551 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
abd9f2604d
  1. 65
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/MiscActions.cs
  2. 4
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs

65
src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/MiscActions.cs

@ -17,11 +17,22 @@ namespace ICSharpCode.TextEditor.Actions
public class Tab : AbstractEditAction public class Tab : AbstractEditAction
{ {
public static string GetIndentationString(IDocument document) public static string GetIndentationString(IDocument document)
{
return GetIndentationString(document, null);
}
public static string GetIndentationString(IDocument document, TextArea textArea)
{ {
StringBuilder indent = new StringBuilder(); StringBuilder indent = new StringBuilder();
if (document.TextEditorProperties.ConvertTabsToSpaces) { if (document.TextEditorProperties.ConvertTabsToSpaces) {
indent.Append(new String(' ', document.TextEditorProperties.TabIndent)); int tabIndent = document.TextEditorProperties.TabIndent;
if (textArea != null) {
int column = textArea.TextView.GetVisualColumn(textArea.Caret.Line, textArea.Caret.Column);
indent.Append(new String(' ', tabIndent - column % tabIndent));
} else {
indent.Append(new String(' ', tabIndent));
}
} else { } else {
indent.Append('\t'); indent.Append('\t');
} }
@ -57,10 +68,10 @@ namespace ICSharpCode.TextEditor.Actions
{ {
switch (textArea.Caret.CaretMode) { switch (textArea.Caret.CaretMode) {
case CaretMode.InsertMode: case CaretMode.InsertMode:
textArea.InsertString(GetIndentationString(textArea.Document)); textArea.InsertString(GetIndentationString(textArea.Document, textArea));
break; break;
case CaretMode.OverwriteMode: case CaretMode.OverwriteMode:
string indentStr = GetIndentationString(textArea.Document); string indentStr = GetIndentationString(textArea.Document, textArea);
textArea.ReplaceChar(indentStr[0]); textArea.ReplaceChar(indentStr[0]);
if (indentStr.Length > 1) { if (indentStr.Length > 1) {
textArea.InsertString(indentStr.Substring(1)); textArea.InsertString(indentStr.Substring(1));
@ -103,7 +114,7 @@ namespace ICSharpCode.TextEditor.Actions
public class ShiftTab : AbstractEditAction public class ShiftTab : AbstractEditAction
{ {
void RemoveTabs(IDocument document, ISelection selection, int y1, int y2) void RemoveTabs(IDocument document, ISelection selection, int y1, int y2)
{ {
int redocounter = 0; int redocounter = 0;
for (int i = y2; i >= y1; --i) { for (int i = y2; i >= y1; --i) {
@ -119,7 +130,7 @@ namespace ICSharpCode.TextEditor.Actions
document.Replace(line.Offset,line.Length,newLine.Substring(1)); document.Replace(line.Offset,line.Length,newLine.Substring(1));
++redocounter; ++redocounter;
} }
else if(newLine.Length > 0 && newLine[0] == ' ') { else if(newLine.Length > 0 && newLine[0] == ' ') {
/// there were just some leading spaces but less than TabIndent of them /// there were just some leading spaces but less than TabIndent of them
int leadingSpaces = 1; int leadingSpaces = 1;
for(leadingSpaces = 1; leadingSpaces < newLine.Length && newLine[leadingSpaces] == ' '; leadingSpaces++) { for(leadingSpaces = 1; leadingSpaces < newLine.Length && newLine[leadingSpaces] == ' '; leadingSpaces++) {
@ -131,7 +142,7 @@ namespace ICSharpCode.TextEditor.Actions
/// else /// else
/// there were no leading tabs or spaces on this line so do nothing /// there were no leading tabs or spaces on this line so do nothing
/// MS Visual Studio 6 strategy: /// MS Visual Studio 6 strategy:
****/ ****/
// string temp = document.GetText(line.Offset,line.Length); // string temp = document.GetText(line.Offset,line.Length);
if (line.Length > 0) { if (line.Length > 0) {
int charactersToRemove = 0; int charactersToRemove = 0;
@ -185,11 +196,11 @@ namespace ICSharpCode.TextEditor.Actions
textArea.Document.UpdateQueue.Clear(); textArea.Document.UpdateQueue.Clear();
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, startLine, endLine)); textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, startLine, endLine));
textArea.EndUpdate(); textArea.EndUpdate();
} }
textArea.AutoClearSelection = false; textArea.AutoClearSelection = false;
} else { } else {
// Pressing Shift-Tab with nothing selected the cursor will move back to the // Pressing Shift-Tab with nothing selected the cursor will move back to the
// previous tab stop. It will stop at the beginning of the line. Also, the desired // previous tab stop. It will stop at the beginning of the line. Also, the desired
// column is updated to that column. // column is updated to that column.
LineSegment line = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset); LineSegment line = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
@ -204,7 +215,7 @@ namespace ICSharpCode.TextEditor.Actions
} }
textArea.SetCaretToDesiredColumn(textArea.Caret.Line); textArea.SetCaretToDesiredColumn(textArea.Caret.Line);
} }
} }
} }
public class ToggleComment : AbstractEditAction public class ToggleComment : AbstractEditAction
@ -217,14 +228,14 @@ namespace ICSharpCode.TextEditor.Actions
{ {
if (textArea.Document.ReadOnly) { if (textArea.Document.ReadOnly) {
return; return;
} }
if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("LineComment")) { if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("LineComment")) {
new ToggleLineComment().Execute(textArea); new ToggleLineComment().Execute(textArea);
} else if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentBegin") && } else if (textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentBegin") &&
textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentBegin")) { textArea.Document.HighlightingStrategy.Properties.ContainsKey("BlockCommentBegin")) {
new ToggleBlockComment().Execute(textArea); new ToggleBlockComment().Execute(textArea);
} }
} }
} }
@ -250,7 +261,7 @@ namespace ICSharpCode.TextEditor.Actions
if (lineText.Trim().StartsWith(comment)) { if (lineText.Trim().StartsWith(comment)) {
document.Remove(line.Offset + lineText.IndexOf(comment), comment.Length); document.Remove(line.Offset + lineText.IndexOf(comment), comment.Length);
++redocounter; ++redocounter;
} }
} }
if (redocounter > 0) { if (redocounter > 0) {
@ -354,7 +365,7 @@ namespace ICSharpCode.TextEditor.Actions
} }
public class ToggleBlockComment : AbstractEditAction public class ToggleBlockComment : AbstractEditAction
{ {
/// <remarks> /// <remarks>
/// Executes this edit action /// Executes this edit action
/// </remarks> /// </remarks>
@ -438,7 +449,7 @@ namespace ICSharpCode.TextEditor.Actions
offset = document.TextLength; offset = document.TextLength;
} }
string text = document.GetText(0, offset); string text = document.GetText(0, offset);
commentStartOffset = text.LastIndexOf(commentStart); commentStartOffset = text.LastIndexOf(commentStart);
} }
// Find end of comment after selected text. // Find end of comment after selected text.
@ -452,7 +463,7 @@ namespace ICSharpCode.TextEditor.Actions
commentEndOffset = text.IndexOf(commentEnd); commentEndOffset = text.IndexOf(commentEnd);
if (commentEndOffset >= 0) { if (commentEndOffset >= 0) {
commentEndOffset += offset; commentEndOffset += offset;
} }
} }
if (commentStartOffset != -1 && commentEndOffset != -1) { if (commentStartOffset != -1 && commentEndOffset != -1) {
@ -521,7 +532,7 @@ namespace ICSharpCode.TextEditor.Actions
} }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
BlockCommentRegion commentRegion = obj as BlockCommentRegion; BlockCommentRegion commentRegion = obj as BlockCommentRegion;
if (commentRegion != null) { if (commentRegion != null) {
if (commentRegion.commentStart == commentStart && if (commentRegion.commentStart == commentStart &&
@ -587,7 +598,7 @@ namespace ICSharpCode.TextEditor.Actions
textArea.BeginUpdate(); textArea.BeginUpdate();
int curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset); int curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset);
int curLineOffset = textArea.Document.GetLineSegment(curLineNr).Offset; int curLineOffset = textArea.Document.GetLineSegment(curLineNr).Offset;
if (curLineOffset == textArea.Caret.Offset) { if (curLineOffset == textArea.Caret.Offset) {
LineSegment line = textArea.Document.GetLineSegment(curLineNr - 1); LineSegment line = textArea.Document.GetLineSegment(curLineNr - 1);
bool lastLine = curLineNr == textArea.Document.TotalNumberOfLines; bool lastLine = curLineNr == textArea.Document.TotalNumberOfLines;
@ -628,7 +639,7 @@ namespace ICSharpCode.TextEditor.Actions
textArea.ScrollToCaret(); textArea.ScrollToCaret();
textArea.EndUpdate(); textArea.EndUpdate();
} else { } else {
if (textArea.Caret.Offset < textArea.Document.TextLength) { if (textArea.Caret.Offset < textArea.Document.TextLength) {
textArea.BeginUpdate(); textArea.BeginUpdate();
int curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset); int curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset);
@ -666,7 +677,7 @@ namespace ICSharpCode.TextEditor.Actions
if (curLineNr != requestedLineNumber) { if (curLineNr != requestedLineNumber) {
textArea.Caret.Position = new Point(textArea.Caret.DesiredColumn, requestedLineNumber); textArea.Caret.Position = new Point(textArea.Caret.DesiredColumn, requestedLineNumber);
} }
} }
} }
public class MovePageUp : AbstractEditAction public class MovePageUp : AbstractEditAction
@ -698,7 +709,7 @@ namespace ICSharpCode.TextEditor.Actions
} }
textArea.BeginUpdate(); textArea.BeginUpdate();
textArea.InsertString(Environment.NewLine); textArea.InsertString(Environment.NewLine);
int curLineNr = textArea.Caret.Line; int curLineNr = textArea.Caret.Line;
textArea.Caret.Column = textArea.Document.FormattingStrategy.FormatLine(textArea, curLineNr, textArea.Caret.Offset, '\n'); textArea.Caret.Column = textArea.Document.FormattingStrategy.FormatLine(textArea, curLineNr, textArea.Caret.Offset, '\n');
@ -762,7 +773,7 @@ namespace ICSharpCode.TextEditor.Actions
/// I will implement this as deleting back to the point that ctrl-leftarrow would /// I will implement this as deleting back to the point that ctrl-leftarrow would
/// take you to /// take you to
/// </summary> /// </summary>
public class WordBackspace : AbstractEditAction public class WordBackspace : AbstractEditAction
{ {
/// <remarks> /// <remarks>
/// Executes this edit action /// Executes this edit action
@ -778,7 +789,7 @@ namespace ICSharpCode.TextEditor.Actions
} }
// now delete from the caret to the beginning of the word // now delete from the caret to the beginning of the word
LineSegment line = LineSegment line =
textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset); textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
// if we are not at the beginning of a line // if we are not at the beginning of a line
if(textArea.Caret.Offset > line.Offset) { if(textArea.Caret.Offset > line.Offset) {
int prevWordStart = TextUtilities.FindPrevWordStart(textArea.Document, int prevWordStart = TextUtilities.FindPrevWordStart(textArea.Document,
@ -793,7 +804,7 @@ namespace ICSharpCode.TextEditor.Actions
if(textArea.Caret.Offset == line.Offset) { if(textArea.Caret.Offset == line.Offset) {
// if we are not on the first line // if we are not on the first line
int curLineNr = int curLineNr =
textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset); textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset);
if(curLineNr > 0) { if(curLineNr > 0) {
// move to the end of the line above // move to the end of the line above
LineSegment lineAbove = textArea.Document.GetLineSegment(curLineNr - LineSegment lineAbove = textArea.Document.GetLineSegment(curLineNr -
@ -815,10 +826,10 @@ namespace ICSharpCode.TextEditor.Actions
/// <summary> /// <summary>
/// handles the ctrl-delete key /// handles the ctrl-delete key
/// functionality attempts to mimic MS Developer studio /// functionality attempts to mimic MS Developer studio
/// I will implement this as deleting forwardto the point that /// I will implement this as deleting forwardto the point that
/// ctrl-leftarrow would take you to /// ctrl-leftarrow would take you to
/// </summary> /// </summary>
public class DeleteWord : Delete public class DeleteWord : Delete
{ {
/// <remarks> /// <remarks>
/// Executes this edit action /// Executes this edit action
@ -834,7 +845,7 @@ namespace ICSharpCode.TextEditor.Actions
} }
// now delete from the caret to the beginning of the word // now delete from the caret to the beginning of the word
LineSegment line = LineSegment line =
textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset); textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
if(textArea.Caret.Offset == line.Offset + line.Length) { if(textArea.Caret.Offset == line.Offset + line.Length) {
// if we are at the end of a line // if we are at the end of a line
base.Execute(textArea); base.Execute(textArea);

4
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs

@ -900,7 +900,9 @@ namespace ICSharpCode.TextEditor
for (int j = currentLine.Length; j < end; j++) { for (int j = currentLine.Length; j < end; j++) {
drawingPos += WideSpaceWidth; drawingPos += WideSpaceWidth;
} }
column += (int)(drawingPos / WideSpaceWidth); // add one pixel in column calculation to account for floating point calculation errors
column += (int)((drawingPos + 1) / WideSpaceWidth);
/* OLD Code (does not work for fonts like Verdana) /* OLD Code (does not work for fonts like Verdana)
for (int j = start; j < end; ++j) { for (int j = start; j < end; ++j) {
char ch; char ch;

Loading…
Cancel
Save