Example #1
0
        public string FormatClipboard()
        {
            // WPF and Windows Forms Clipboard behavior differs when it comes
            // to DataFormats.CommaSeparatedValue.
            // WPF will always return the data as a string, no matter how it
            // was set, but Windows Forms may return a Stream or a string.
            // Use WPF Clipboard fully qualified name to ensure we don't
            // accidentally end up using the wrong clipboard implementation
            // if this code is moved.
            if (System.Windows.Clipboard.ContainsData(System.Windows.DataFormats.CommaSeparatedValue))
            {
                string data = System.Windows.Clipboard.GetData(System.Windows.DataFormats.CommaSeparatedValue) as string;
                if (data != null)
                {
                    string[]      lines = data.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    StringBuilder res   = new StringBuilder();
                    res.AppendLine("[");
                    foreach (var line in lines)
                    {
                        string[] items = _splitLineRegex.Split(line);

                        res.Append("  [");
                        for (int i = 0; i < items.Length; i++)
                        {
                            res.Append(FormatItem(items[i]));

                            if (i != items.Length - 1)
                            {
                                res.Append(", ");
                            }
                        }
                        res.AppendLine("],");
                    }
                    res.AppendLine("]");
                    return(res.ToString());
                }
            }

            var txt = System.Windows.Clipboard.GetText();

            if (!_serviceProvider.GetPythonToolsService().AdvancedOptions.PasteRemovesReplPrompts)
            {
                return(txt);
            }


            return(ReplPromptHelpers.RemovePrompts(
                       txt,
                       _window.TextView.Options.GetNewLineCharacter()
                       ));
        }
Example #2
0
        public void RemoveReplPrompts()
        {
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts(">>>", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts(">>> ", null));
            Assert.AreEqual("    ", ReplPromptHelpers.RemovePrompts(">>>     ", null));
            Assert.AreEqual("pass", ReplPromptHelpers.RemovePrompts(">>> pass", null));
            Assert.AreEqual(" pass", ReplPromptHelpers.RemovePrompts(">>>  pass", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("...", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("... ", null));
            Assert.AreEqual("    ", ReplPromptHelpers.RemovePrompts("...     ", null));
            Assert.AreEqual("pass", ReplPromptHelpers.RemovePrompts("... pass", null));
            Assert.AreEqual(" pass", ReplPromptHelpers.RemovePrompts("...  pass", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("In[1]:", null));
            Assert.AreEqual("    ", ReplPromptHelpers.RemovePrompts("In [ 2 ]  :     ", null));
            Assert.AreEqual("pass", ReplPromptHelpers.RemovePrompts("In [ 2 ]  : pass", null));
            Assert.AreEqual(" pass", ReplPromptHelpers.RemovePrompts("In [ 2 ]  :  pass", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("...:", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("    ...:", null));
            Assert.AreEqual("", ReplPromptHelpers.RemovePrompts("  ...: ", null));
            Assert.AreEqual("    ", ReplPromptHelpers.RemovePrompts("  ...:     ", null));
            Assert.AreEqual("pass", ReplPromptHelpers.RemovePrompts("  ...: pass", null));
            Assert.AreEqual(" pass", ReplPromptHelpers.RemovePrompts("  ...:  pass", null));

            Assert.AreEqual(@"x = 1
print(x)
if True:
    pass


print(x)
1

if True:
    print(x)

1".Replace("\r\n", "\n"), ReplPromptHelpers.RemovePrompts(@">>> x = 1
>>> print(x)
>>> if True:
...     pass
...

In [2]: print(x)
1

In [3]: if True:
   ...:     print(x)
   ...: 
1", "\n"));
        }
Example #3
0
        public string FormatClipboard()
        {
            if (Clipboard.ContainsData(DataFormats.CommaSeparatedValue))
            {
                string data = Clipboard.GetData(DataFormats.CommaSeparatedValue) as string;
                if (data != null)
                {
                    string[]      lines = data.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    StringBuilder res   = new StringBuilder();
                    res.AppendLine("[");
                    foreach (var line in lines)
                    {
                        string[] items = _splitLineRegex.Split(line);

                        res.Append("  [");
                        for (int i = 0; i < items.Length; i++)
                        {
                            res.Append(FormatItem(items[i]));

                            if (i != items.Length - 1)
                            {
                                res.Append(", ");
                            }
                        }
                        res.AppendLine("],");
                    }
                    res.AppendLine("]");
                    return(res.ToString());
                }
            }

            var txt = Clipboard.GetText();

            if (!_serviceProvider.GetPythonToolsService().AdvancedOptions.PasteRemovesReplPrompts)
            {
                return(txt);
            }


            return(ReplPromptHelpers.RemovePrompts(
                       txt,
                       _window.TextView.Options.GetNewLineCharacter()
                       ));
        }
Example #4
0
        private int ExecWorker(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            // preprocessing
            if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
            {
                switch ((VSConstants.VSStd97CmdID)nCmdID)
                {
                case VSConstants.VSStd97CmdID.Paste:
                    if (!_editorServices.Python.AdvancedOptions.PasteRemovesReplPrompts)
                    {
                        // Not stripping prompts, so don't use our logic
                        break;
                    }
                    var beforePaste = _textView.TextSnapshot;
                    if (_editorServices.EditOperationsFactory.GetEditorOperations(_textView).Paste())
                    {
                        var afterPaste = _textView.TextSnapshot;
                        var um         = _editorServices.UndoManagerFactory.GetTextBufferUndoManager(afterPaste.TextBuffer);
                        using (var undo = um.TextBufferUndoHistory.CreateTransaction(Strings.RemoveReplPrompts)) {
                            if (ReplPromptHelpers.RemovePastedPrompts(beforePaste, afterPaste))
                            {
                                undo.Complete();
                            }
                        }
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd97CmdID.GotoDefn: GotoDefinition(); return(VSConstants.S_OK);

                case VSConstants.VSStd97CmdID.FindReferences: FindAllReferences(); return(VSConstants.S_OK);
                }
            }
            else if (pguidCmdGroup == VSConstants.VsStd12)
            {
                switch ((VSConstants.VSStd12CmdID)nCmdID)
                {
                case VSConstants.VSStd12CmdID.PeekDefinition:
                    if (_editorServices.PeekBroker != null &&
                        !_textView.Roles.Contains(PredefinedTextViewRoles.EmbeddedPeekTextView) &&
                        !_textView.Roles.Contains(PredefinedTextViewRoles.CodeDefinitionView))
                    {
                        _editorServices.PeekBroker.TriggerPeekSession(_textView, PredefinedPeekRelationships.Definitions.Name);
                        return(VSConstants.S_OK);
                    }
                    break;
                }
            }
            else if (pguidCmdGroup == CommonConstants.Std2KCmdGroupGuid)
            {
                SnapshotPoint?pyPoint;
                switch ((VSConstants.VSStd2KCmdID)nCmdID)
                {
                case VSConstants.VSStd2KCmdID.RETURN:
                    pyPoint = _textView.GetPythonCaret();
                    if (pyPoint != null)
                    {
                        // https://github.com/Microsoft/PTVS/issues/241
                        // If the current line is a full line comment and we
                        // are splitting the text, automatically insert the
                        // comment marker on the new line.
                        var line     = pyPoint.Value.GetContainingLine();
                        var lineText = pyPoint.Value.Snapshot.GetText(line.Start, pyPoint.Value - line.Start);
                        int comment  = lineText.IndexOf('#');
                        if (comment >= 0 &&
                            pyPoint.Value < line.End &&
                            line.Start + comment < pyPoint.Value &&
                            string.IsNullOrWhiteSpace(lineText.Remove(comment))
                            )
                        {
                            int extra = lineText.Skip(comment + 1).TakeWhile(char.IsWhiteSpace).Count() + 1;
                            using (var edit = line.Snapshot.TextBuffer.CreateEdit()) {
                                edit.Insert(
                                    pyPoint.Value.Position,
                                    _textView.Options.GetNewLineCharacter() + lineText.Substring(0, comment + extra)
                                    );
                                edit.Apply();
                            }

                            return(VSConstants.S_OK);
                        }
                    }
                    break;

                case VSConstants.VSStd2KCmdID.FORMATDOCUMENT:
                    pyPoint = _textView.GetPythonCaret();
                    if (pyPoint != null)
                    {
                        FormatCode(new SnapshotSpan(pyPoint.Value.Snapshot, 0, pyPoint.Value.Snapshot.Length), false);
                    }
                    return(VSConstants.S_OK);

                case VSConstants.VSStd2KCmdID.FORMATSELECTION:
                    foreach (var span in _textView.BufferGraph.MapDownToFirstMatch(
                                 _textView.Selection.StreamSelectionSpan.SnapshotSpan,
                                 SpanTrackingMode.EdgeInclusive,
                                 EditorExtensions.IsPythonContent
                                 ))
                    {
                        FormatCode(span, true);
                    }
                    return(VSConstants.S_OK);

                case VSConstants.VSStd2KCmdID.SHOWMEMBERLIST:
                case VSConstants.VSStd2KCmdID.COMPLETEWORD:
                    var controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController));
                    if (controller != null)
                    {
                        controller.TriggerCompletionSession(
                            (VSConstants.VSStd2KCmdID)nCmdID == VSConstants.VSStd2KCmdID.COMPLETEWORD,
                            '\0',
                            true
                            ).DoNotWait();
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.QUICKINFO:
                    controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController));
                    if (controller != null)
                    {
                        controller.TriggerQuickInfoAsync().DoNotWait();
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.PARAMINFO:
                    controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController));
                    if (controller != null)
                    {
                        controller.TriggerSignatureHelp();
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.OUTLN_STOP_HIDING_ALL:
                    _textView.GetOutliningTagger()?.Disable(_textView.TextSnapshot);
                    // let VS get the event as well
                    break;

                case VSConstants.VSStd2KCmdID.OUTLN_START_AUTOHIDING:
                    _textView.GetOutliningTagger()?.Enable(_textView.TextSnapshot);
                    // let VS get the event as well
                    break;

                case VSConstants.VSStd2KCmdID.COMMENT_BLOCK:
                case VSConstants.VSStd2KCmdID.COMMENTBLOCK:
                    if (_textView.CommentOrUncommentBlock(comment: true))
                    {
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK:
                case VSConstants.VSStd2KCmdID.UNCOMMENTBLOCK:
                    if (_textView.CommentOrUncommentBlock(comment: false))
                    {
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.EXTRACTMETHOD:
                    ExtractMethod();
                    return(VSConstants.S_OK);

                case VSConstants.VSStd2KCmdID.RENAME:
                    RefactorRename();
                    return(VSConstants.S_OK);
                }
            }
            else if (pguidCmdGroup == GuidList.guidPythonToolsCmdSet)
            {
                switch (nCmdID)
                {
                case PkgCmdIDList.cmdidRefactorRenameIntegratedShell:
                    RefactorRename();
                    return(VSConstants.S_OK);

                case PkgCmdIDList.cmdidExtractMethodIntegratedShell:
                    ExtractMethod();
                    return(VSConstants.S_OK);

                case CommonConstants.StartDebuggingCmdId:
                case CommonConstants.StartWithoutDebuggingCmdId:
                    PythonToolsPackage.LaunchFile(_editorServices.Site, _textView.GetFilePath(), nCmdID == CommonConstants.StartDebuggingCmdId, true);
                    return(VSConstants.S_OK);
                }
            }

            return(_next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
        }