internal ReplWindowProxy(PythonVisualStudioApp app, ReplWindow window, ReplWindowProxySettings settings)
 {
     Assert.IsNotNull(app, "app is required");
     Assert.IsNotNull(window, "window is required");
     _app                   = app;
     _window                = window;
     _settings              = settings;
     _replWindowInfo        = _replWindows.GetOrCreateValue(_window);
     _window.ReadyForInput += _replWindowInfo.OnReadyForInput;
     _editorOperations      = _app.ComponentModel.GetService <IEditorOperationsFactoryService>()
                              .GetEditorOperations(_window.TextView);
 }
        public static ReplWindowProxy Prepare(
            ReplWindowProxySettings settings,
            bool useIPython = false
            )
        {
            settings.Version.AssertInstalled();

            var             app    = new PythonVisualStudioApp();
            ReplWindowProxy result = null;

            try {
                var wnd = OpenInteractive(app, settings, useIPython ? "IPython" : "Standard");
                result = new ReplWindowProxy(app, wnd, settings);
                app    = null;

                result.Window.Reset();
                result.ClearInput();

                for (int retries = 10; retries > 0; --retries)
                {
                    result.Window.Reset();
                    try {
                        var task = result.Window.Evaluator.ExecuteText("print('READY')");
                        Assert.IsTrue(task.Wait(useIPython ? 30000 : 10000), "ReplWindow did not initialize in time");
                        if (!task.Result.IsSuccessful)
                        {
                            continue;
                        }
                    } catch (TaskCanceledException) {
                        continue;
                    }

                    result.WaitForTextEnd("READY", ">");
                    if (result.TextView.TextBuffer.CurrentSnapshot.Lines
                        .Any(l => l.GetText().Contains("Error using selected REPL back-end")) &&
                        useIPython)
                    {
                        Assert.Inconclusive("IPython is not available");
                    }
                    result.ClearScreen();
                    result.ClearHistory();
                    return(result);
                }
                Assert.Fail("ReplWindow did not initialize");
                return(null);
            } finally {
                if (app != null)
                {
                    app.Dispose();
                }
            }
        }
예제 #3
0
        private static void Run(PythonVisualStudioApp app, SendToStep[] inputs, ReplWindowProxySettings settings, EditorWindow doc, EnvDTE.Window window, string projectName = null, string workspaceName = null)
        {
            window.Activate();
            doc.MoveCaret(new SnapshotPoint(doc.TextView.TextBuffer.CurrentSnapshot, 0));
            app.WaitForCommandAvailable("Python.SendSelectionToInteractive", TimeSpan.FromSeconds(15));

            var interactive = ReplWindowProxy.Prepare(app, settings, projectName, workspaceName, useIPython: false);

            interactive.ExecuteText("42").Wait();
            interactive.ClearScreen();

            WaitForText(interactive.TextView, ">>> ");

            var state = new StepState(interactive, app, doc, window);

            state.Content.Append(">>> ");

            foreach (var input in inputs)
            {
                input.Execute(state);
            }
        }
        private static ReplWindow OpenInteractive(
            PythonVisualStudioApp app,
            ReplWindowProxySettings settings,
            string executionMode
            )
        {
            string description = null;

            if (settings.Version.IsCPython)
            {
                description = string.Format("{0} {1}",
                                            settings.Version.Isx64 ? CPythonInterpreterFactoryConstants.Description64 : CPythonInterpreterFactoryConstants.Description32,
                                            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).GetInteractiveOptions(description);

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

            options.InlinePrompts         = settings.InlinePrompts;
            options.UseInterpreterPrompts = settings.UseInterpreterPrompts;
            options.PrimaryPrompt         = settings.PrimaryPrompt;
            options.SecondaryPrompt       = settings.SecondaryPrompt;
            options.EnableAttach          = settings.EnableAttach;

            var oldExecutionMode = options.ExecutionMode;

            app.OnDispose(() => options.ExecutionMode = oldExecutionMode);
            options.ExecutionMode = executionMode;

            bool success = false;

            for (int retries = 1; retries < 20; ++retries)
            {
                try {
                    app.ExecuteCommand("Python.Interactive", "/e:\"" + description + "\"");
                    success = true;
                    break;
                } catch (AggregateException) {
                }
                app.DismissAllDialogs();
                app.SetFocus();
                Thread.Sleep(retries * 100);
            }
            Assert.IsTrue(success, "Unable to open " + description + " through DTE");
            var provider     = app.ComponentModel.GetService <IReplWindowProvider>();
            var interpreters = app.ComponentModel.GetService <IInterpreterOptionsService>();
            var replId       = PythonReplEvaluatorProvider.GetReplId(
                interpreters.FindInterpreter(settings.Version.Id, settings.Version.Version.ToVersion())
                );

            var interactive = provider.FindReplWindow(replId) as ReplWindow;

            if (interactive == null)
            {
                // This is a failure, since we check if the environment is
                // installed in TestInitialize().
                Assert.Fail("Need " + description);
            }

            return(interactive);
        }