예제 #1
0
        public ReplWindowProxy ExecuteInInteractive(Project project, ReplWindowProxySettings settings = null)
        {
            // Prepare makes sure that IPython mode is disabled, and that the REPL is reset and cleared
            var window = ReplWindowProxy.Prepare(this, settings, project.Name);

            OpenSolutionExplorer().SelectProject(project);
            ExecuteCommand("Python.ExecuteInInteractive");
            return(window);
        }
예제 #2
0
        private static ToolWindowPane ActivateInteractiveWindow(ReplWindowProxySettings settings, VisualStudioApp app, string projectName, string workspaceName, string backend)
        {
            string description = null;

            if (settings.Version.IsCPython)
            {
                description = string.Format("{0} {1}",
                                            settings.Version.Isx64 ? "Python 64-bit" : "Python 32-bit",
                                            settings.Version.Version.ToVersion()
                                            );
            }
            else if (settings.Version.IsIronPython)
            {
                description = string.Format("{0} {1}",
                                            settings.Version.Isx64 ? "IronPython 64-bit" : "IronPython",
                                            settings.Version.Version.ToVersion()
                                            );
            }
            Assert.IsNotNull(description, "Unknown interpreter");

            var automation  = (IVsPython)app.Dte.GetObject("VsPython");
            var options     = (IPythonOptions)automation;
            var replOptions = options.Interactive;

            Assert.IsNotNull(replOptions, "Could not find options for " + description);

            var oldAddNewLineAtEndOfFullyTypedWord = options.Intellisense.AddNewLineAtEndOfFullyTypedWord;

            app.OnDispose(() => options.Intellisense.AddNewLineAtEndOfFullyTypedWord = oldAddNewLineAtEndOfFullyTypedWord);
            options.Intellisense.AddNewLineAtEndOfFullyTypedWord = settings.AddNewLineAtEndOfFullyTypedWord;

            var interpreters = app.ComponentModel.GetService <IInterpreterRegistryService>();
            var replId       = PythonReplEvaluatorProvider.GetEvaluatorId(
                interpreters.FindConfiguration(settings.Version.Id)
                );

            if (!string.IsNullOrEmpty(projectName))
            {
                var dteProj = app.GetProject(projectName);
                var proj    = (PythonProjectNode)dteProj.GetCommonProject();
                replId = PythonReplEvaluatorProvider.GetEvaluatorId(proj);
            }
            else if (!string.IsNullOrEmpty(workspaceName))
            {
                var workspaceContextProvider = app.ComponentModel.GetService <IPythonWorkspaceContextProvider>();
                replId = PythonReplEvaluatorProvider.GetEvaluatorId(workspaceContextProvider.Workspace);
            }

            return(app.ServiceProvider.GetUIThread().Invoke(() => {
                app.ServiceProvider.GetPythonToolsService().InteractiveBackendOverride = backend;
                var provider = app.ComponentModel.GetService <InteractiveWindowProvider>();
                return (ToolWindowPane)provider.OpenOrCreate(replId);
            }));
        }
예제 #3
0
 internal ReplWindowProxy(VisualStudioApp app, IInteractiveWindow window, ToolWindowPane toolWindow, ReplWindowProxySettings settings)
 {
     Assert.IsNotNull(app, "app is required");
     Assert.IsNotNull(window, "window is required");
     _app                   = app;
     _window                = window;
     _toolWindow            = toolWindow;
     _settings              = settings;
     _replWindowInfo        = _replWindows.GetOrCreateValue(toolWindow);
     _window.ReadyForInput += _replWindowInfo.OnReadyForInput;
     _editorOperations      = _app.ComponentModel.GetService <IEditorOperationsFactoryService>()
                              .GetEditorOperations(_window.TextView);
 }
예제 #4
0
        private static ReplWindowProxy OpenInteractive(
            VisualStudioApp app,
            ReplWindowProxySettings settings,
            string projectName,
            string backend
            )
        {
            var toolWindow = ActivateInteractiveWindow(settings, app, projectName, backend);

            var interactive = toolWindow != null ? ((IVsInteractiveWindow)toolWindow).InteractiveWindow : null;

            Assert.IsNotNull(interactive, "Could not find interactive window");
            return(new ReplWindowProxy(app, interactive, toolWindow, settings));
        }
예제 #5
0
        public ReplWindowProxy GetInteractiveWindow(string title, ReplWindowProxySettings settings = null)
        {
            var iwp    = GetService <IComponentModel>(typeof(SComponentModel))?.GetService <InteractiveWindowProvider>();
            var window = iwp?.AllOpenWindows.FirstOrDefault(w => ((ToolWindowPane)w).Caption == title);

            if (window == null)
            {
                Trace.TraceWarning(
                    "Failed to find {0} in {1}",
                    title,
                    string.Join(", ", iwp?.AllOpenWindows.Select(w => ((ToolWindowPane)w).Caption) ?? Enumerable.Empty <string>())
                    );
                return(null);
            }
            return(new ReplWindowProxy(this, window.InteractiveWindow, (ToolWindowPane)window, settings ?? new ReplWindowProxySettings()));
        }
예제 #6
0
        public ReplWindowProxy WaitForInteractiveWindow(string title, ReplWindowProxySettings settings = null)
        {
            var iwp = GetService <IComponentModel>(typeof(SComponentModel))?.GetService <InteractiveWindowProvider>();
            IVsInteractiveWindow window = null;

            for (int retries = 20; retries > 0 && window == null; --retries)
            {
                System.Threading.Thread.Sleep(100);
                window = iwp?.AllOpenWindows.FirstOrDefault(w => ((ToolWindowPane)w).Caption == title);
            }
            if (window == null)
            {
                Trace.TraceWarning(
                    "Failed to find {0} in {1}",
                    title,
                    string.Join(", ", iwp?.AllOpenWindows.Select(w => ((ToolWindowPane)w).Caption) ?? Enumerable.Empty <string>())
                    );
                return(null);
            }
            return(new ReplWindowProxy(this, window.InteractiveWindow, (ToolWindowPane)window, settings ?? new ReplWindowProxySettings()));
        }
예제 #7
0
 public ReplWindowProxy GetInteractiveWindow(Project project, ReplWindowProxySettings settings = null)
 {
     return(GetInteractiveWindow(project.Name + " Interactive", settings));
 }
예제 #8
0
        public static ReplWindowProxy Prepare(
            PythonVisualStudioApp app,
            ReplWindowProxySettings settings,
            string projectName,
            string workspaceName,
            bool useIPython = false
            )
        {
            settings.AssertValid();

            ReplWindowProxy result = null;

            try {
                result = OpenInteractive(app, settings, projectName, workspaceName, useIPython ? IPythonBackend : StandardBackend);
                app    = null;

                for (int retries = 10; retries > 0; --retries)
                {
                    result.Reset();
                    result.ClearScreen();
                    result.ClearInput();

                    try {
                        var task = result.ExecuteText("print('READY')");
                        Assert.IsTrue(task.Wait(useIPython ? 30000 : 15000), "ReplWindow did not initialize in time");
                        if (!task.Result.IsSuccessful)
                        {
                            continue;
                        }
                    } catch (TaskCanceledException) {
                        continue;
                    }


                    if (useIPython)
                    {
                        // The longer we wait, the better are the chances of detecting this error
                        // This seems long enough to detect it when running locally
                        Thread.Sleep(500);

                        if (result.TextView.TextBuffer.CurrentSnapshot.Lines
                            .Any(l => l.GetText().Contains("Error using selected REPL back-end"))
                            )
                        {
                            Assert.Inconclusive("IPython is not available");
                        }

                        // In IPython mode, a help header appears at startup,
                        // but the output order is inconsistent, so we can't WaitForTextEnd
                        // (sometimes READY appears before help, sometimes after)
                        result.WaitForAnyLineContainsTextInternal("READY");
                        result.WaitForReadyForInput(TimeSpan.FromSeconds(5));
                    }
                    else
                    {
                        result.WaitForTextEnd("READY", ">");
                    }

                    result.ClearScreen();
                    return(result);
                }
                Assert.Fail("ReplWindow did not initialize");
                return(null);
            } finally {
                if (app != null)
                {
                    app.Dispose();
                }
            }
        }