Example #1
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var activeView = CommonPackage.GetActiveTextView(_serviceProvider);
            var project    = activeView.TextBuffer.GetProject(_serviceProvider);
            var analyzer   = activeView.GetAnalyzer(_serviceProvider);

            ToolWindowPane window = (ToolWindowPane)ExecuteInReplCommand.EnsureReplWindow(_serviceProvider, analyzer, project);

            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;

            ErrorHandler.ThrowOnFailure(windowFrame.Show());
            var repl = (IVsReplWindow)window;

#if DEV14_OR_LATER
            PythonReplEvaluator eval = repl.InteractiveWindow.Evaluator as PythonReplEvaluator;
#else
            PythonReplEvaluator eval = repl.Evaluator as PythonReplEvaluator;
#endif

            eval.EnsureConnected();
#if DEV14_OR_LATER
            repl.InteractiveWindow.Submit(GetActiveInputs(activeView, eval));
#else
            repl.Submit(GetActiveInputs(activeView, eval));
#endif

            repl.Focus();
        }
Example #2
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var activeView = PythonToolsPackage.GetActiveTextView(_serviceProvider);
            var pyProj     = activeView.TextBuffer.GetProject(_serviceProvider);
            var analyzer   = activeView.GetAnalyzerAtCaret(_serviceProvider);
            var window     = ExecuteInReplCommand.EnsureReplWindow(_serviceProvider, analyzer, pyProj);
            var eval       = window.InteractiveWindow.Evaluator as PythonReplEvaluator;

            string path = activeView.GetFilePath();
            string scope;

            if (path != null && (scope = eval.GetScopeByFilename(path)) != null)
            {
                // we're now in the correct module, execute the code
                window.InteractiveWindow.Operations.Cancel();
                // TODO: get correct prompt
                window.InteractiveWindow.WriteLine(">>>" + " $module " + scope);
                eval.SetScope(scope);

                base.DoCommand(sender, args);
            }
            else
            {
                window.InteractiveWindow.WriteLine("Could not find defining module.");
            }
        }
        public override void DoCommand(object sender, EventArgs args)
        {
            var activeView = PythonToolsPackage.GetActiveTextView();
            var analyzer   = activeView.GetAnalyzer();
            var window     = ExecuteInReplCommand.EnsureReplWindow(analyzer);
            var eval       = window.Evaluator as PythonReplEvaluator;

            string path = activeView.GetFilePath();
            string scope;

            if (path != null && (scope = eval.GetScopeByFilename(path)) != null)
            {
                // we're now in the correct module, execute the code
                eval.SetScope(scope);
                window.Cancel();
                window.WriteLine(window.GetOptionValue(ReplOptions.PrimaryPrompt) + " $module " + scope);
                window.WriteLine(String.Format("Current scope changed to {0}", scope));

                base.DoCommand(sender, args);
            }
            else
            {
                window.WriteLine("Could not find defining module.");
            }
        }
Example #4
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var window = (ToolWindowPane)ExecuteInReplCommand.EnsureReplWindow(_factory);

            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;

            ErrorHandler.ThrowOnFailure(windowFrame.Show());
            ((IReplWindow)window).Focus();
        }
Example #5
0
        public override void DoCommand(object sender, EventArgs e)
        {
            // _factory is never null, but if a specific factory or command line
            // is passed as an argument, use that instead.
            var factory = _factory;
            var oe      = e as OleMenuCmdEventArgs;

            if (oe != null)
            {
                IPythonInterpreterFactory asFactory;
                string args;
                if ((asFactory = oe.InValue as IPythonInterpreterFactory) != null)
                {
                    factory = asFactory;
                }
                else if (!string.IsNullOrEmpty(args = oe.InValue as string))
                {
                    string description;
                    var    parse = _serviceProvider.GetService(typeof(SVsParseCommandLine)) as IVsParseCommandLine;
                    if (ErrorHandler.Succeeded(parse.ParseCommandTail(args, -1)) &&
                        ErrorHandler.Succeeded(parse.EvaluateSwitches("e,env,environment:")) &&
                        ErrorHandler.Succeeded(parse.GetSwitchValue(0, out description)) &&
                        !string.IsNullOrEmpty(description)
                        )
                    {
                        var service = _serviceProvider.GetComponentModel().GetService <IInterpreterOptionsService>();
                        asFactory = service.Interpreters.FirstOrDefault(
                            // Descriptions are localized strings, hence CCIC
                            f => description.Equals(f.Description, StringComparison.CurrentCultureIgnoreCase)
                            );
                        if (asFactory != null)
                        {
                            factory = asFactory;
                        }
                    }
                }
            }

            // These commands are project-insensitive, so pass null for project.
            var window = (ToolWindowPane)ExecuteInReplCommand.EnsureReplWindow(_serviceProvider, factory, null);

#if DEV14_OR_LATER
            ((IVsInteractiveWindow)window).Show(true);
#else
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            ErrorHandler.ThrowOnFailure(windowFrame.Show());
            ((IReplWindow)window).Focus();
#endif
        }
Example #6
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var activeView = CommonPackage.GetActiveTextView(_serviceProvider);
            var project    = activeView.TextBuffer.GetProject(_serviceProvider);
            var analyzer   = activeView.GetAnalyzer(_serviceProvider);

            var repl = ExecuteInReplCommand.EnsureReplWindow(_serviceProvider, analyzer, project);

            repl.Show(true);

            PythonReplEvaluator eval = repl.InteractiveWindow.Evaluator as PythonReplEvaluator;

            eval.EnsureConnected();
            repl.InteractiveWindow.Submit(GetActiveInputs(activeView, eval));

            repl.Show(true);
        }
Example #7
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var activeView = CommonPackage.GetActiveTextView();
            var analyzer   = activeView.GetAnalyzer();

            ToolWindowPane window = (ToolWindowPane)ExecuteInReplCommand.EnsureReplWindow(analyzer);

            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;

            ErrorHandler.ThrowOnFailure(windowFrame.Show());


            foreach (var span in activeView.Selection.SelectedSpans)
            {
                var text = span.GetText();
                ((IReplWindow)window).PasteText(text);
            }

            ((IReplWindow)window).Focus();
        }
Example #8
0
        public override void DoCommand(object sender, EventArgs e)
        {
            // _factory is never null, but if a specific factory or command line
            // is passed as an argument, use that instead.
            var config = _config;
            var oe     = e as OleMenuCmdEventArgs;

            if (oe != null)
            {
                IPythonInterpreterFactory asFactory;
                string args;
                if ((asFactory = oe.InValue as IPythonInterpreterFactory) != null)
                {
                    config = asFactory.Configuration;
                }
                else if (!string.IsNullOrEmpty(args = oe.InValue as string))
                {
                    string description;
                    var    parse = _serviceProvider.GetService(typeof(SVsParseCommandLine)) as IVsParseCommandLine;
                    if (ErrorHandler.Succeeded(parse.ParseCommandTail(args, -1)) &&
                        ErrorHandler.Succeeded(parse.EvaluateSwitches("e,env,environment:")) &&
                        ErrorHandler.Succeeded(parse.GetSwitchValue(0, out description)) &&
                        !string.IsNullOrEmpty(description)
                        )
                    {
                        var service        = _serviceProvider.GetComponentModel().GetService <IInterpreterRegistryService>();
                        var matchingConfig = service.Configurations.FirstOrDefault(
                            // Descriptions are localized strings, hence CCIC
                            f => description.Equals(f.FullDescription, StringComparison.CurrentCultureIgnoreCase)
                            );
                        if (matchingConfig != null)
                        {
                            config = matchingConfig;
                        }
                    }
                }
            }

            // These commands are project-insensitive, so pass null for project.
            ExecuteInReplCommand.EnsureReplWindow(_serviceProvider, config, null).Show(true);
        }
Example #9
0
        public override async void DoCommand(object sender, EventArgs args)
        {
            var            activeView = CommonPackage.GetActiveTextView(_serviceProvider);
            var            project    = activeView.GetProjectAtCaret(_serviceProvider);
            var            analyzer   = activeView.GetAnalyzerAtCaret(_serviceProvider);
            ITextSelection selection  = activeView.Selection;
            ITextSnapshot  snapshot   = activeView.TextBuffer.CurrentSnapshot;

            IVsInteractiveWindow repl;

            try {
                repl = ExecuteInReplCommand.EnsureReplWindow(_serviceProvider, analyzer, project);
            } catch (MissingInterpreterException ex) {
                MessageBox.Show(ex.Message, Strings.ProductTitle);
                return;
            }

            string input;
            bool   focusRepl = false, alwaysSubmit = false;

            if (selection.StreamSelectionSpan.Length > 0)
            {
                // Easy, just send the selection to the interactive window.
                input = activeView.Selection.StreamSelectionSpan.GetText();
                if (!input.EndsWithOrdinal("\n") && !input.EndsWithOrdinal("\r"))
                {
                    input += activeView.Options.GetNewLineCharacter();
                }
                focusRepl = true;
            }
            else if (!activeView.Properties.ContainsProperty(_executedLastLine))
            {
                // No selection, and we haven't hit the end of the file in line-by-line mode.
                // Send the current line, and then move the caret to the next non-blank line.
                ITextSnapshotLine targetLine = snapshot.GetLineFromPosition(selection.Start.Position);
                var targetSpan = targetLine.Extent;

                // If the line is inside a code cell, expand the target span to
                // contain the entire cell.
                var cellStart = CodeCellAnalysis.FindStartOfCell(targetLine);
                if (cellStart != null)
                {
                    var cellEnd = CodeCellAnalysis.FindEndOfCell(cellStart, targetLine);
                    targetSpan   = new SnapshotSpan(cellStart.Start, cellEnd.End);
                    targetLine   = CodeCellAnalysis.FindEndOfCell(cellEnd, targetLine, includeWhitespace: true);
                    alwaysSubmit = true;
                }
                input = targetSpan.GetText();

                bool moved = false;
                while (targetLine.LineNumber < snapshot.LineCount - 1)
                {
                    targetLine = snapshot.GetLineFromLineNumber(targetLine.LineNumber + 1);
                    // skip over blank lines, unless it's the last line, in which case we want to land on it no matter what
                    if (!string.IsNullOrWhiteSpace(targetLine.GetText()) ||
                        targetLine.LineNumber == snapshot.LineCount - 1)
                    {
                        activeView.Caret.MoveTo(new SnapshotPoint(snapshot, targetLine.Start));
                        activeView.Caret.EnsureVisible();
                        moved = true;
                        break;
                    }
                }

                if (!moved)
                {
                    // There's no where for the caret to go, don't execute the line if
                    // we've already executed it.
                    activeView.Caret.PositionChanged        += Caret_PositionChanged;
                    activeView.Properties[_executedLastLine] = _executedLastLine;
                }
            }
            else if ((repl.InteractiveWindow.CurrentLanguageBuffer?.CurrentSnapshot.Length ?? 0) != 0)
            {
                // We reached the end of the file but have some text buffered.  Execute it now.
                input = activeView.Options.GetNewLineCharacter();
            }
            else
            {
                // We've hit the end of the current text view and executed everything
                input = null;
            }

            if (input != null)
            {
                repl.Show(focusRepl);

                var inputs = repl.InteractiveWindow.Properties.GetOrCreateSingletonProperty(
                    () => new InteractiveInputs(repl.InteractiveWindow, _serviceProvider, alwaysSubmit)
                    );

                await inputs.EnqueueAsync(input);
            }

            // Take focus back if REPL window has stolen it and we're in line-by-line mode.
            if (!focusRepl && !activeView.HasAggregateFocus)
            {
                var adapterService = _serviceProvider.GetComponentModel().GetService <VisualStudio.Editor.IVsEditorAdaptersFactoryService>();
                var tv             = adapterService.GetViewAdapter(activeView);
                tv.SendExplicitFocus();
            }
        }