diff options
Diffstat (limited to 'main/src/core/Mono.Texteditor/Mono.TextEditor/Actions')
-rw-r--r-- | main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/DeleteActions.cs | 46 | ||||
-rw-r--r-- | main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs | 62 |
2 files changed, 49 insertions, 59 deletions
diff --git a/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/DeleteActions.cs b/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/DeleteActions.cs index ce3e4c3b98..1c1cb81ad4 100644 --- a/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/DeleteActions.cs +++ b/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/DeleteActions.cs @@ -220,7 +220,35 @@ namespace Mono.TextEditor // Virtual indentation needs to be fixed before to have the same behavior // if it's there or not (otherwise user has to press multiple backspaces in some cases) data.EnsureCaretIsNotVirtual (); - DocumentLine line = data.Document.GetLine (data.Caret.Line); + + var line = data.Document.GetLine (data.Caret.Line); + // smart backspace (delete indentation) + if (data.Options.IndentStyle == IndentStyle.Smart || data.Options.IndentStyle == IndentStyle.Virtual) { + if (data.Caret.Column == data.GetVirtualIndentationColumn (data.Caret.Location)) { + bool isAllIndent = line.GetIndentation (data.Document).Length == data.Caret.Column - 1; + + if (isAllIndent) { + var prevLine = line.PreviousLine; + var prevLineIsEmpty = prevLine != null && prevLine.Length == 0; + + var startOffset = prevLine != null ? prevLine.EndOffset : 0; + data.Remove (startOffset, data.Caret.Offset - startOffset); + if (prevLine != null) { + if (prevLineIsEmpty) { + if (line.Length - data.Caret.Column - 1 > 0 && data.HasIndentationTracker) { + data.InsertAtCaret (data.IndentationTracker.GetIndentationString (data.Caret.Offset)); + } else { + data.Caret.Column = data.GetVirtualIndentationColumn (prevLine.Offset); + } + } + data.FixVirtualIndentation (); + } + return; + } + } + } + + // normal backspace. if (data.Caret.Column > line.Length + 1) { data.Caret.Column = line.Length + 1; } else if (data.Caret.Offset == line.Offset) { @@ -287,7 +315,21 @@ namespace Mono.TextEditor DocumentLine line = data.Document.GetLine (data.Caret.Line); if (data.Caret.Column == line.Length + 1) { if (data.Caret.Line < data.Document.LineCount) { - data.Remove (line.EndOffsetIncludingDelimiter - line.DelimiterLength, line.DelimiterLength); + var deletionLength = line.DelimiterLength; + // smart backspace (delete indentation) + if (data.Options.IndentStyle == IndentStyle.Smart || data.Options.IndentStyle == IndentStyle.Virtual) { + var next = line.NextLine; + if (next != null) { + if (data.HasIndentationTracker) { + var lineIndentation = next.GetIndentation (data.Document); + if (lineIndentation.StartsWith (data.IndentationTracker.GetIndentationString (next.Offset))) { + deletionLength += lineIndentation.Length; + } + } + } + } + + data.Remove (line.EndOffsetIncludingDelimiter - line.DelimiterLength, deletionLength); if (line.EndOffsetIncludingDelimiter == data.Document.TextLength) line.UnicodeNewline = UnicodeNewline.Unknown; } diff --git a/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs b/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs index 8a42f8b0a0..37b5dd2c81 100644 --- a/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs +++ b/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs @@ -272,9 +272,11 @@ namespace Mono.TextEditor break; case IndentStyle.Auto: data.EnsureCaretIsNotVirtual (); - var sb = new StringBuilder (data.EolMarker); - sb.Append (data.Document.GetLineIndent (data.Caret.Line)); - data.InsertAtCaret (sb.ToString ()); + var indent = data.Document.GetLineIndent (data.Caret.Line); + data.InsertAtCaret (data.EolMarker); + data.EnsureCaretIsNotVirtual (); + if (data.GetLine (data.Caret.Line).Length == 0) + data.InsertAtCaret (indent); break; case IndentStyle.Smart: if (!data.HasIndentationTracker) @@ -511,59 +513,5 @@ namespace Mono.TextEditor } } - public static void SortSelectedLines (TextEditorData data) - { - var start = data.MainSelection.Start; - var end = data.MainSelection.End; - var caret = data.Caret.Location; - - int startLine = start.Line; - int endLine = end.Line; - if (startLine == endLine) - return; - - int length = 0; - var lines = new string[endLine - startLine + 1]; - for (int i = startLine; i <= endLine; i++) { - //get lines *with* line endings - var lineText = data.GetLineText (i, true); - lines [i - startLine] = lineText; - length += lineText.Length; - } - - var linesUnsorted = new string[lines.Length]; - - Array.Sort (lines, StringComparer.Ordinal); - - bool changed = false; - for (int i = 0; i <= lines.Length; i++) { - //can't simply use reference comparison as Array.Sort is not stable - if (string.Equals (lines [i], linesUnsorted [i], StringComparison.Ordinal)) { - continue; - } - changed = true; - break; - } - if (!changed) { - return; - } - - - var sb = new StringBuilder (); - for (int i = 0; i < lines.Length; i++) { - sb.Append (lines [i]); - } - - var startOffset = data.Document.LocationToOffset (new TextLocation (startLine, 0)); - data.Replace (startOffset, length, sb.ToString ()); - - data.Caret.Location = LimitColumn (data, caret); - data.SetSelection (LimitColumn (data, start), LimitColumn (data, end)); - } - - static DocumentLocation LimitColumn (TextEditorData data, DocumentLocation loc) - { - return new DocumentLocation (loc.Line, System.Math.Min (loc.Column, data.GetLine (loc.Line).Length + 1)); - } } }
\ No newline at end of file |