Exemplo n.º 1
0
        public static void DoActionOnLines(ITextView textView, ITextBuffer textBuffer, ITextRange range, IEditorShell editorShell, 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);

            using (var undoAction = editorShell.CreateCompoundAction(textView, textBuffer)) {
                undoAction.Open(actionName);
                bool changed = false;
                for (int i = startLineNumber; i <= endLineNumber; i++) {
                    line = textBuffer.CurrentSnapshot.GetLineFromLineNumber(i);
                    changed |= action(line);
                }
                if (changed) {
                    undoAction.Commit();
                }
            }
        }
Exemplo n.º 2
0
 public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, IEditorShell editorShell)
 {
     using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
         undoAction.Open(Resources.AutoFormat);
         var result = RangeFormatter.FormatRange(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell);
         if (result)
         {
             undoAction.Commit();
         }
     }
 }
Exemplo n.º 3
0
        public static void FormatCurrentScope(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, bool indentCaret)
        {
            // Figure out caret position in the document text buffer
            SnapshotPoint?caretPoint = REditorDocument.MapCaretPositionFromView(textView);

            if (!caretPoint.HasValue)
            {
                return;
            }
            IREditorDocument document = REditorDocument.TryFromTextBuffer(textBuffer);

            if (document != null)
            {
                // Make sure AST is up to date
                document.EditorTree.EnsureTreeReady();
                var           ast      = document.EditorTree.AstRoot;
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;

                // Find scope to format
                IScope scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Value);

                using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                    undoAction.Open(Resources.AutoFormat);
                    // Now format the scope
                    bool changed = RangeFormatter.FormatRange(textView, textBuffer, scope, REditorSettings.FormatOptions, editorShell);
                    if (indentCaret)
                    {
                        // Formatting may change AST and the caret position so we need to reacquire both
                        caretPoint = REditorDocument.MapCaretPositionFromView(textView);
                        if (caretPoint.HasValue)
                        {
                            document.EditorTree.EnsureTreeReady();
                            ast   = document.EditorTree.AstRoot;
                            scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Value);
                            IndentCaretInNewScope(textView, scope, caretPoint.Value, REditorSettings.FormatOptions);
                        }
                    }
                    if (changed)
                    {
                        undoAction.Commit();
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static void DoActionOnLines(ITextView textView, ITextBuffer textBuffer, ITextRange range, IEditorShell editorShell, 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);

            using (var undoAction = editorShell.CreateCompoundAction(textView, textBuffer)) {
                undoAction.Open(actionName);
                bool changed = false;
                for (int i = startLineNumber; i <= endLineNumber; i++)
                {
                    line     = textBuffer.CurrentSnapshot.GetLineFromLineNumber(i);
                    changed |= action(line);
                }
                if (changed)
                {
                    undoAction.Commit();
                }
            }
        }
Exemplo n.º 5
0
        public static void FormatCurrentScope(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, bool indentCaret) {
            // Figure out caret position in the document text buffer
            SnapshotPoint? caretPoint = REditorDocument.MapCaretPositionFromView(textView);
            if (!caretPoint.HasValue) {
                return;
            }
            IREditorDocument document = REditorDocument.TryFromTextBuffer(textBuffer);
            if (document != null) {
                // Make sure AST is up to date
                document.EditorTree.EnsureTreeReady();
                var ast = document.EditorTree.AstRoot;
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;

                // Find scope to format
                IScope scope = ast.GetNodeOfTypeFromPosition<IScope>(caretPoint.Value);

                using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                    undoAction.Open(Resources.AutoFormat);
                    // Now format the scope
                    bool changed = RangeFormatter.FormatRange(textView, textBuffer, scope, REditorSettings.FormatOptions, editorShell);
                    if (indentCaret) {
                        // Formatting may change AST and the caret position so we need to reacquire both
                        caretPoint = REditorDocument.MapCaretPositionFromView(textView);
                        if (caretPoint.HasValue) {
                            document.EditorTree.EnsureTreeReady();
                            ast = document.EditorTree.AstRoot;
                            scope = ast.GetNodeOfTypeFromPosition<IScope>(caretPoint.Value);
                            IndentCaretInNewScope(textView, scope, caretPoint.Value, REditorSettings.FormatOptions);
                        }
                    }
                    if (changed) {
                        undoAction.Commit();
                    }
                }
            }
        }
Exemplo n.º 6
0
        public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, IEditorShell editorShell, bool exactRange = false) {
            using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                undoAction.Open(Resources.AutoFormat);
                var result = exactRange
                    ? RangeFormatter.FormatRangeExact(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell)
                    : RangeFormatter.FormatRange(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell);

                if (result) {
                    undoAction.Commit();
                }
            }
        }