Esempio n. 1
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            SnapshotSpan selectionSpan = TextView.Selection.StreamSelectionSpan.SnapshotSpan;
            var          rSpans        = TextView.BufferGraph.MapDownToFirstMatch(
                selectionSpan,
                SpanTrackingMode.EdgeInclusive,
                snapshot => snapshot.TextBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType)
                );

            foreach (var spanToFormat in rSpans)
            {
                IREditorDocument document = REditorDocument.TryFromTextBuffer(spanToFormat.Snapshot.TextBuffer);
                AstRoot          ast;
                if (document == null)
                {
                    // For unit test purposes
                    ast = inputArg as AstRoot;
                }
                else
                {
                    ast = document.EditorTree.AstRoot;
                }

                if (ast != null)
                {
                    RangeFormatter.FormatRange(TextView,
                                               spanToFormat.Snapshot.TextBuffer,
                                               new TextRange(spanToFormat.Start.Position, spanToFormat.Length),
                                               ast, REditorSettings.FormatOptions);
                }
            }
            return(new CommandResult(CommandStatus.Supported, 0));
        }
Esempio n. 2
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            if (!_settings.FormatOnPaste || TextView.Selection.Mode != TextSelectionMode.Stream)
            {
                return(CommandResult.NotSupported);
            }

            var text = ClipboardDataProvider.GetData(DataFormats.UnicodeText) as string ??
                       ClipboardDataProvider.GetData(DataFormats.Text) as string;

            if (string.IsNullOrEmpty(text))
            {
                return(CommandResult.NotSupported);
            }

            var viewSpan = TextView.Selection.StreamSelectionSpan.SnapshotSpan;
            // Locate non-readonly R buffer. In REPL previous entries
            // are read-only and only prompt line is writeable.
            var insertionPoint = TextView.BufferGraph.MapDownToInsertionPoint(viewSpan.Start, PointTrackingMode.Positive,
                                                                              s => s.TextBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType) &&
                                                                              s.TextBuffer.GetReadOnlyExtents(new Span(0, s.Length)).Count == 0);

            if (!insertionPoint.HasValue)
            {
                return(CommandResult.NotSupported); // Proceed with default action.
            }

            var targetBuffer = insertionPoint.Value.Snapshot.TextBuffer;

            // In REPL target buffer may be just "text", such as when readline() is called.
            // We do not format non-R code content. Proceed with default action.
            if (!targetBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType))
            {
                return(CommandResult.NotSupported);
            }

            var document = targetBuffer.GetEditorDocument <IREditorDocument>();

            if (document == null)
            {
                return(CommandResult.NotSupported);
            }

            // Make sure change actually did happen
            var editorBuffer = document.EditorBuffer;

            if (editorBuffer.Replace(new TextRange(insertionPoint.Value, viewSpan.Length), text))
            {
                // Make sure AST is up to date so we can determine if position is inside a string.
                // Since we don't want to format inside strings.
                document.EditorTree.EnsureTreeReady();
                if (!document.EditorTree.AstRoot.IsPositionInsideString(insertionPoint.Value))
                {
                    var formatter = new RangeFormatter(Services, TextView.ToEditorView(), editorBuffer);
                    formatter.FormatRange(new TextRange(insertionPoint.Value, text.Length));
                }
                return(CommandResult.Executed);
            }
            return(CommandResult.NotSupported);
        }
Esempio n. 3
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();
         }
     }
 }
Esempio n. 4
0
 public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange)
 {
     using (var undoAction = EditorShell.Current.CreateCompoundAction(textView, textView.TextBuffer)) {
         undoAction.Open(Resources.AutoFormat);
         // Now format the scope
         if (RangeFormatter.FormatRange(textView, textBuffer, formatRange, REditorSettings.FormatOptions))
         {
             undoAction.Commit();
         }
     }
 }
Esempio n. 5
0
        public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, ICoreShell shell)
        {
            var es = shell.GetService <IApplicationEditorSupport>();

            using (var undoAction = es.CreateCompoundAction(textView, textView.TextBuffer)) {
                undoAction.Open(Resources.AutoFormat);
                var result = RangeFormatter.FormatRange(textView, textBuffer, formatRange, shell.GetService <IREditorSettings>().FormatOptions, shell);
                if (result)
                {
                    undoAction.Commit();
                }
            }
        }
Esempio n. 6
0
        public static void UndoableFormatRange(IEditorView editorView, IEditorBuffer textBuffer, ITextRange formatRange, IServiceContainer services)
        {
            var es = services.GetService <IEditorSupport>();

            using (var undoAction = es.CreateUndoAction(editorView)) {
                undoAction.Open(Resources.AutoFormat);
                var formatter = new RangeFormatter(services);
                var result    = formatter.FormatRange(editorView, textBuffer, formatRange);
                if (result)
                {
                    undoAction.Commit();
                }
            }
        }
Esempio n. 7
0
        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);
            }
        }
Esempio n. 8
0
        public void UndoableFormatRange(ITextRange formatRange)
        {
            var es = _services.GetService <IEditorSupport>();

            using (var undoAction = es.CreateUndoAction(_editorView)) {
                undoAction.Open(Resources.AutoFormat);
                var formatter = new RangeFormatter(_services, _editorView, _editorBuffer, _changeHandler);
                var result    = formatter.FormatRange(formatRange);
                if (result)
                {
                    undoAction.Commit();
                }
            }
        }
Esempio n. 9
0
        public static void FormatCurrentScope(IEditorView editorView, IEditorBuffer textBuffer, IServiceContainer services, bool indentCaret)
        {
            // Figure out caret position in the document text buffer
            var document = textBuffer.GetEditorDocument <IREditorDocument>();

            if (document == null)
            {
                return;
            }
            var caretPoint = editorView.GetCaretPosition(textBuffer);

            if (caretPoint == null)
            {
                return;
            }

            // Make sure AST is up to date
            document.EditorTree.EnsureTreeReady();
            var ast = document.EditorTree.AstRoot;
            // Find scope to format
            var scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Position);

            var es = services.GetService <IEditorSupport>();

            using (var undoAction = es.CreateUndoAction(editorView)) {
                var settings = services.GetService <IREditorSettings>();
                undoAction.Open(Resources.AutoFormat);
                // Now format the scope
                var formatter = new RangeFormatter(services);
                var changed   = formatter.FormatRange(editorView, textBuffer, scope);
                if (indentCaret)
                {
                    // Formatting may change AST and the caret position so we need to reacquire both
                    caretPoint = editorView.GetCaretPosition(textBuffer);
                    if (caretPoint != null)
                    {
                        document.EditorTree.EnsureTreeReady();
                        ast   = document.EditorTree.AstRoot;
                        scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Position);
                        IndentCaretInNewScope(editorView, scope, caretPoint, settings.FormatOptions);
                    }
                }
                if (changed)
                {
                    undoAction.Commit();
                }
            }
        }
Esempio n. 10
0
        public static void FormatCurrentScope(ITextView textView, ITextBuffer textBuffer, ICoreShell shell, bool indentCaret)
        {
            // Figure out caret position in the document text buffer
            var caretPoint = REditorDocument.MapCaretPositionFromView(textView);

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

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

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

                var es = shell.GetService <IApplicationEditorSupport>();
                using (var undoAction = es.CreateCompoundAction(textView, textView.TextBuffer)) {
                    var settings = shell.GetService <IREditorSettings>();
                    undoAction.Open(Resources.AutoFormat);
                    // Now format the scope
                    bool changed = RangeFormatter.FormatRange(textView, textBuffer, scope, settings.FormatOptions, shell);
                    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, settings.FormatOptions);
                        }
                    }
                    if (changed)
                    {
                        undoAction.Commit();
                    }
                }
            }
        }
Esempio n. 11
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            SnapshotSpan selectionSpan = TextView.Selection.StreamSelectionSpan.SnapshotSpan;
            var          rSpans        = TextView.BufferGraph.MapDownToFirstMatch(
                selectionSpan,
                SpanTrackingMode.EdgeInclusive,
                snapshot => snapshot.TextBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType)
                );

            foreach (var spanToFormat in rSpans)
            {
                RangeFormatter.FormatRange(TextView, spanToFormat.Snapshot.TextBuffer,
                                           new TextRange(spanToFormat.Start.Position, spanToFormat.Length),
                                           Shell.GetService <IREditorSettings>().FormatOptions, Shell);
            }

            return(new CommandResult(CommandStatus.Supported, 0));
        }
Esempio n. 12
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            if (!REditorSettings.FormatOnPaste || TextView.Selection.Mode != TextSelectionMode.Stream)
            {
                return(CommandResult.NotSupported);
            }

            string text = ClipboardDataProvider.GetData(DataFormats.UnicodeText) as string;

            if (text == null)
            {
                text = ClipboardDataProvider.GetData(DataFormats.Text) as string;
            }

            if (text != null)
            {
                var rSpans = TextView.BufferGraph.MapDownToFirstMatch(
                    TextView.Selection.StreamSelectionSpan.SnapshotSpan,
                    SpanTrackingMode.EdgeInclusive,
                    snapshot => snapshot.TextBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType)
                    );
                if (rSpans.Count > 0)
                {
                    var targetSpan = rSpans[rSpans.Count - 1];

                    IREditorDocument document = REditorDocument.TryFromTextBuffer(targetSpan.Snapshot.TextBuffer);
                    if (document != null)
                    {
                        int insertionPoint = targetSpan.Start;
                        targetSpan.Snapshot.TextBuffer.Replace(targetSpan, text);
                        document.EditorTree.EnsureTreeReady();

                        // We don't want to format inside strings
                        if (!document.EditorTree.AstRoot.IsPositionInsideString(insertionPoint))
                        {
                            RangeFormatter.FormatRange(TextView, targetSpan.Snapshot.TextBuffer,
                                                       new TextRange(insertionPoint, text.Length), document.EditorTree.AstRoot,
                                                       REditorSettings.FormatOptions);
                        }
                    }
                }
            }
            return(CommandResult.Executed);
        }
Esempio n. 13
0
 public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, bool exactRange = false)
 {
     using (var undoAction = EditorShell.Current.CreateCompoundAction(textView, textView.TextBuffer)) {
         undoAction.Open(Resources.AutoFormat);
         bool result;
         if (exactRange)
         {
             result = RangeFormatter.FormatRangeExact(textView, textBuffer, formatRange, REditorSettings.FormatOptions);
         }
         else
         {
             result = RangeFormatter.FormatRange(textView, textBuffer, formatRange, REditorSettings.FormatOptions);
         }
         if (result)
         {
             undoAction.Commit();
         }
     }
 }
Esempio n. 14
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            var selectionSpan = TextView.Selection.StreamSelectionSpan.SnapshotSpan;
            var rSpans        = TextView.BufferGraph.MapDownToFirstMatch(
                selectionSpan,
                SpanTrackingMode.EdgeInclusive,
                snapshot => snapshot.TextBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType)
                );

            if (rSpans.Count > 0)
            {
                var formatter = new RangeFormatter(Services, TextView.ToEditorView(), rSpans[0].Snapshot.TextBuffer.ToEditorBuffer());
                foreach (var spanToFormat in rSpans)
                {
                    formatter.FormatRange(new TextRange(spanToFormat.Start.Position, spanToFormat.Length));
                }
            }
            return(new CommandResult(CommandStatus.Supported, 0));
        }
Esempio n. 15
0
        /// <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);
            }
        }