Exemple #1
0
        private async System.Threading.Tasks.Task DoCommand()
        {
            var workspace = _serviceProvider.GetWorkspace();
            var pyProj    = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;
            var textView  = CommonPackage.GetActiveTextView(_serviceProvider);

            var scriptName = textView?.GetFilePath();

            if (!string.IsNullOrEmpty(scriptName) && pyProj != null)
            {
                if (pyProj.FindNodeByFullPath(scriptName) == null)
                {
                    // Starting a script that isn't in the project.
                    // Try and find the project. If we fail, we will
                    // use the default environment.
                    pyProj = _serviceProvider.GetProjectFromFile(scriptName);
                }
            }

            LaunchConfiguration config = null;

            try {
                if (workspace != null)
                {
                    config = PythonCommonInteractiveEvaluator.GetWorkspaceLaunchConfigurationOrThrow(workspace);
                }
                else
                {
                    config = pyProj?.GetLaunchConfigurationOrThrow();
                }
            } catch (MissingInterpreterException ex) {
                MessageBox.Show(ex.Message, Strings.ProductTitle);
                return;
            }
            if (config == null)
            {
                var interpreters = _serviceProvider.GetComponentModel().GetService <IInterpreterOptionsService>();
                config = new LaunchConfiguration(interpreters.DefaultInterpreter.Configuration);
            }
            else
            {
                config = config.Clone();
            }

            if (!string.IsNullOrEmpty(scriptName))
            {
                config.ScriptName = scriptName;
                // Only overwrite the working dir for a loose file, don't do it for workspaces
                if (workspace == null)
                {
                    config.WorkingDirectory = PathUtils.GetParent(scriptName);
                }
            }

            if (config == null)
            {
                Debug.Fail("Should not be executing command when it is invisible");
                return;
            }

            IVsInteractiveWindow window;

            try {
                window = EnsureReplWindow(_serviceProvider, config.Interpreter, pyProj, workspace);
            } catch (MissingInterpreterException ex) {
                MessageBox.Show(ex.Message, Strings.ProductTitle);
                return;
            }

            window.Show(true);
            var eval = (IPythonInteractiveEvaluator)window.InteractiveWindow.Evaluator;

            // The interpreter may take some time to startup, do this off the UI thread.
            await _serviceProvider.GetPythonToolsService().UIThread.InvokeAsync(async() => {
                await((IInteractiveEvaluator)eval).ResetAsync();

                window.InteractiveWindow.WriteLine(Strings.ExecuteInReplCommand_RunningMessage.FormatUI(config.ScriptName));

                await eval.ExecuteFileAsync(config.ScriptName, config.ScriptArguments);
            });
        }
 public PythonLanguageClientContextRepl(PythonCommonInteractiveEvaluator evaluator)
 {
     _evaluator = evaluator ?? throw new ArgumentNullException(nameof(evaluator));
 }