public static void ConvertSelectionToList(TextEditor textEditor) { var selection = textEditor.TextArea.Selection; var start = Math.Min(selection.StartPosition.Line, selection.EndPosition.Line); if (start == 0) return; var end = Math.Max(selection.StartPosition.Line, selection.EndPosition.Line); var document = textEditor.Document; var ordered = new Regex(@"^\s*\d+\.\s{1,4}"); var unordered = new Regex(@"^\s*[-|\*|\+]\s{1,4}"); textEditor.BeginChange(); var index = 1; try { foreach (var num in Enumerable.Range(start, end - start + 1)) { var line = document.GetLineByNumber(num); var offset = line.Offset; var text = document.GetText(line); if (string.IsNullOrWhiteSpace(text)) continue; if (unordered.IsMatch(text)) { var numbered = Regex.Replace(text, @"[-|\*|\+]", $"{index++}."); document.Remove(line); document.Insert(offset, numbered); } else if (ordered.IsMatch(text)) { var unnumbered = Regex.Replace(text, @"\d+\.", "-"); document.Remove(line); document.Insert(offset, unnumbered); } else { var indexOfFirstChar = text.TakeWhile(char.IsWhiteSpace).Count(); document.Insert(line.Offset + indexOfFirstChar, "- "); } } } finally { textEditor.EndChange(); } }
public static void MoveSegmentUp(TextEditor textEditor) { var textArea = textEditor.TextArea; var document = textEditor.Document; int currentLine; DocumentLine segment; var selection = textArea.Selection; if (selection != null && selection.StartPosition.Line > 1) { currentLine = Math.Max(selection.StartPosition.Line, selection.EndPosition.Line); segment = document.GetLineByNumber(Math.Min(selection.StartPosition.Line, selection.EndPosition.Line)).PreviousLine; } else { currentLine = textArea.Caret.Line; if (currentLine == 1) return; segment = document.GetLineByNumber(currentLine).PreviousLine; } textEditor.BeginChange(); try { var text = document.GetText(segment.Offset, segment.TotalLength); document.Remove(segment.Offset, segment.TotalLength); var offset = currentLine >= document.LineCount ? document.TextLength : document.GetOffset(currentLine, 0); document.Insert(offset, text); textArea.Caret.Line = currentLine - 1; textArea.Caret.BringCaretToView(); } finally { textEditor.EndChange(); textArea.TextView.Redraw(DispatcherPriority.ApplicationIdle); } }
public static void InsertBlockQuote(TextEditor textEditor) { var textArea = textEditor.TextArea; var document = textEditor.Document; var selection = textEditor.TextArea.Selection; var start = Math.Min(selection.StartPosition.Line, selection.EndPosition.Line); if (start == 0) start = textArea.Caret.Line; var end = Math.Max(selection.StartPosition.Line, selection.EndPosition.Line); if (end == 0) end = textArea.Caret.Line; textEditor.BeginChange(); try { foreach (var line in Enumerable.Range(start, end - start + 1)) { var offset = document.GetOffset(line, 0); document.Insert(offset, "> "); } } finally { textEditor.EndChange(); } }