Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/main/src
diff options
context:
space:
mode:
authorMichael Hutchinson <m.j.hutchinson@gmail.com>2015-02-08 08:31:00 +0300
committerMichael Hutchinson <m.j.hutchinson@gmail.com>2015-02-08 08:41:46 +0300
commit75645d74e23e960d8af66ed97639b72b6fcc0656 (patch)
treeadf5fc76eae2b1cdf5223a387ce3b931a2811a0e /main/src
parent9f29560a79ea9bff73ed7f4af0b8c176ce2327fc (diff)
[TextEditor] Add action to sort selected lines
Diffstat (limited to 'main/src')
-rw-r--r--main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs57
1 files changed, 57 insertions, 0 deletions
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 716dffbf53..42c1044a06 100644
--- a/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs
+++ b/main/src/core/Mono.Texteditor/Mono.TextEditor/Actions/MiscActions.cs
@@ -29,6 +29,8 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
+using System.Linq;
+using ICSharpCode.NRefactory;
namespace Mono.TextEditor
@@ -508,5 +510,60 @@ 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, Math.Min (loc.Column, data.GetLine (loc.Line).Length + 1));
+ }
}
} \ No newline at end of file