Microsoft.R.Editor.Formatting.FormatOnPasteCommand.Invoke C# (CSharp) Method

Invoke() public method

public Invoke ( System.Guid group, int id, object inputArg, object &outputArg ) : Microsoft.R.Components.Controller.CommandResult
group System.Guid
id int
inputArg object
outputArg object
return Microsoft.R.Components.Controller.CommandResult
        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;
                        document.TextBuffer.Replace(targetSpan, text);
                        document.EditorTree.EnsureTreeReady();

                        // We don't want to format inside strings
                        if (!document.EditorTree.AstRoot.IsPositionInsideString(insertionPoint)) {
                            RangeFormatter.FormatRange(TextView, document.TextBuffer,
                                new TextRange(insertionPoint, text.Length), REditorSettings.FormatOptions, EditorShell);
                        }
                    }
                }
            }
            return CommandResult.Executed;
        }
    }

Usage Example

Example #1
0
        private string FormatFromClipboard(string content) {
            ITextBuffer textBuffer = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
            ITextView textView = new TextViewMock(textBuffer);
            var clipboard = new ClipboardDataProvider();

            using (var command = new FormatOnPasteCommand(textView, textBuffer, _editorShell)) {
                command.ClipboardDataProvider = clipboard;

                clipboard.Format = DataFormats.UnicodeText;
                clipboard.Data = content;

                var ast = RParser.Parse(textBuffer.CurrentSnapshot.GetText());
                using (var document = new EditorDocumentMock(new EditorTreeMock(textBuffer, ast))) {
                    object o = new object();
                    command.Invoke(VSConstants.GUID_VSStandardCommandSet97, (int)VSConstants.VSStd97CmdID.Paste, null, ref o);
                }
            }

            return textBuffer.CurrentSnapshot.GetText();
        }