public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, AstRoot ast, ITextRange formatRange) { ICompoundUndoAction undoAction = EditorShell.Current.CreateCompoundAction(textView, textView.TextBuffer); undoAction.Open(Resources.AutoFormat); bool changed = false; try { // Now format the scope changed = RangeFormatter.FormatRange(textView, textBuffer, formatRange, ast, REditorSettings.FormatOptions); } finally { undoAction.Close(!changed); } }
public static void DoActionOnLines(ITextView textView, ITextBuffer textBuffer, ITextRange range, Func <ITextSnapshotLine, bool> action, string actionName) { // When user clicks editor margin to select a line, selection actually // ends in the beginning of the next line. In order to prevent commenting // of the next line that user did not select, we need to shrink span to // format and exclude the trailing line break. ITextSnapshot snapshot = textBuffer.CurrentSnapshot; ITextSnapshotLine line = snapshot.GetLineFromPosition(range.End); int start = range.Start; int end = range.End; if (line.Start.Position == range.End && range.Length > 0) { if (line.LineNumber > 0) { line = snapshot.GetLineFromLineNumber(line.LineNumber - 1); end = line.End.Position; start = Math.Min(start, end); } } int startLineNumber = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(start); int endLineNumber = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(end); ICompoundUndoAction undoAction = EditorShell.Current.CreateCompoundAction(textView, textBuffer); undoAction.Open(actionName); bool changed = false; try { for (int i = startLineNumber; i <= endLineNumber; i++) { line = textBuffer.CurrentSnapshot.GetLineFromLineNumber(i); changed |= action(line); } } finally { undoAction.Close(!changed); } }
/// <summary> /// Formats specific scope /// </summary> private static void FormatScope(ITextView textView, ITextBuffer textBuffer, AstRoot ast, IScope scope, int baseIndentPosition, bool indentCaret) { ICompoundUndoAction undoAction = EditorShell.Current.CreateCompoundAction(textView, textView.TextBuffer); undoAction.Open(Resources.AutoFormat); bool changed = false; try { // Now format the scope changed = RangeFormatter.FormatRangeExact(textView, textBuffer, scope, ast, REditorSettings.FormatOptions, baseIndentPosition, indentCaret); if (indentCaret) { IndentCaretInNewScope(textView, textBuffer, scope, REditorSettings.FormatOptions); changed = true; } } finally { undoAction.Close(!changed); } }