public override void DoCommand(object sender, EventArgs args)
        {
            VsProjectAnalyzer analyzer;
            string            filename, dir;
            var pyProj = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;

            if (!PythonToolsPackage.TryGetStartupFileAndDirectory(_serviceProvider, out filename, out dir, out analyzer))
            {
                // TODO: Error reporting
                return;
            }

            var            window      = (IReplWindow)EnsureReplWindow(_serviceProvider, analyzer, pyProj);
            IVsWindowFrame windowFrame = (IVsWindowFrame)((ToolWindowPane)window).Frame;

            ErrorHandler.ThrowOnFailure(windowFrame.Show());
            window.Focus();

            // The interpreter may take some time to startup, do this off the UI thread.
            ThreadPool.QueueUserWorkItem(x => {
                window.Reset();

                window.WriteLine(String.Format("Running {0}", filename));
                string scopeName = Path.GetFileNameWithoutExtension(filename);

                window.Evaluator.ExecuteFile(filename);
            });
        }
Ejemplo n.º 2
0
        private void QueryStatusMethod(object sender, EventArgs args)
        {
            var oleMenu = sender as OleMenuCommand;

            IWpfTextView textView;
            var          pyProj = CommonPackage.GetStartupProject() as PythonProjectNode;

            if (pyProj != null)
            {
                // startup project, enabled in Start in REPL mode.
                oleMenu.Visible   = true;
                oleMenu.Enabled   = true;
                oleMenu.Supported = true;
                oleMenu.Text      = "Execute Project in P&ython Interactive";
            }
            else if ((textView = CommonPackage.GetActiveTextView()) != null &&
                     textView.TextBuffer.ContentType == PythonToolsPackage.Instance.ContentType)
            {
                // enabled in Execute File mode...
                oleMenu.Visible   = true;
                oleMenu.Enabled   = true;
                oleMenu.Supported = true;
                oleMenu.Text      = "Execute File in P&ython Interactive";
            }
            else
            {
                oleMenu.Visible   = false;
                oleMenu.Enabled   = false;
                oleMenu.Supported = false;
            }
        }
        public override void DoCommand(object sender, EventArgs args)
        {
            if (!Utilities.SaveDirtyFiles())
            {
                // Abort
                return;
            }

            // Launch with project context if there is one and it contains the active document
            // Fallback to using default python project
            var file = CommonPackage.GetActiveTextView(_serviceProvider).GetFilePath();
            var pythonProjectNode = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;

            if ((pythonProjectNode != null) && (pythonProjectNode.FindNodeByFullPath(file) == null))
            {
                pythonProjectNode = null;
            }
            IPythonProject pythonProject = pythonProjectNode as IPythonProject ?? new DefaultPythonProject(_serviceProvider, file);

            var launcher = PythonToolsPackage.GetLauncher(_serviceProvider, pythonProject);

            try {
                launcher.LaunchFile(file, CommandId == CommonConstants.StartDebuggingCmdId);
            } catch (NoInterpretersException ex) {
                PythonToolsPackage.OpenNoInterpretersHelpPage(_serviceProvider, ex.HelpPage);
            }
        }
Ejemplo n.º 4
0
        private void QueryStatusMethod(object sender, EventArgs args)
        {
            var oleMenu = sender as OleMenuCommand;

            if (oleMenu == null)
            {
                Debug.Fail("Unexpected command type " + sender == null ? "(null)" : sender.GetType().FullName);
                return;
            }

            var workspace = _serviceProvider.GetWorkspace();
            var pyProj    = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;
            var textView  = CommonPackage.GetActiveTextView(_serviceProvider);

            oleMenu.Supported = true;

            if (pyProj != null)
            {
                // startup project, so visible in Project mode
                oleMenu.Visible = true;
                oleMenu.Text    = Strings.ExecuteInReplCommand_ExecuteProject;

                // Only enable if runnable
                oleMenu.Enabled = pyProj.GetInterpreterFactory().IsRunnable();
            }
            else if (textView != null && textView.TextBuffer.ContentType.IsOfType(PythonCoreConstants.ContentType))
            {
                // active file, so visible in File mode
                oleMenu.Visible = true;
                oleMenu.Text    = Strings.ExecuteInReplCommand_ExecuteFile;

                // Only enable if runnable
                if (workspace != null)
                {
                    oleMenu.Enabled = workspace.CurrentFactory.IsRunnable();
                }
                else
                {
                    var interpreterService = _serviceProvider.GetComponentModel().GetService <IInterpreterOptionsService>();
                    oleMenu.Enabled = interpreterService != null && interpreterService.DefaultInterpreter.IsRunnable();
                }
            }
            else
            {
                // Python is not active, so hide the command
                oleMenu.Visible = false;
                oleMenu.Enabled = false;
            }
        }
Ejemplo n.º 5
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var pyProj   = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;
            var textView = CommonPackage.GetActiveTextView(_serviceProvider);

            VsProjectAnalyzer analyzer;
            string            filename, dir = null;

            if (pyProj != null)
            {
                analyzer = pyProj.GetAnalyzer();
                filename = pyProj.GetStartupFile();
                dir      = pyProj.GetWorkingDirectory();
            }
            else if (textView != null)
            {
                var pyService = _serviceProvider.GetPythonToolsService();
                analyzer = pyService.DefaultAnalyzer;
                filename = textView.GetFilePath();
            }
            else
            {
                Debug.Fail("Should not be executing command when it is invisible");
                return;
            }
            if (string.IsNullOrEmpty(filename))
            {
                // TODO: Error reporting
                return;
            }
            if (string.IsNullOrEmpty(dir))
            {
                dir = PathUtils.GetParent(filename);
            }

            var window = EnsureReplWindow(_serviceProvider, analyzer, pyProj);

            window.Show(true);

            // The interpreter may take some time to startup, do this off the UI thread.
            ThreadPool.QueueUserWorkItem(x => {
                window.InteractiveWindow.Evaluator.ResetAsync().WaitAndUnwrapExceptions();

                window.InteractiveWindow.WriteLine(String.Format("Running {0}", filename));
                string scopeName = Path.GetFileNameWithoutExtension(filename);

                ((PythonReplEvaluator)window.InteractiveWindow.Evaluator).ExecuteFile(filename);
            });
        }
Ejemplo n.º 6
0
        private void QueryStatusMethod(object sender, EventArgs args)
        {
            var oleMenu = sender as OleMenuCommand;
            VsProjectAnalyzer analyzer;
            var    interpreterService = _serviceProvider.GetComponentModel().GetService <IInterpreterOptionsService>();
            string filename, dir;

            if (!PythonToolsPackage.TryGetStartupFileAndDirectory(_serviceProvider, out filename, out dir, out analyzer) ||
                interpreterService == null ||
                interpreterService.NoInterpretersValue == analyzer.InterpreterFactory)
            {
                // no interpreters installed, disable the command.
                oleMenu.Visible   = true;
                oleMenu.Enabled   = false;
                oleMenu.Supported = true;
            }
            else
            {
                IWpfTextView textView;
                var          pyProj = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;
                var          window = (IReplWindow)EnsureReplWindow(_serviceProvider, analyzer, pyProj);
                if (pyProj != null)
                {
                    // startup project, enabled in Start in REPL mode.
                    oleMenu.Visible   = true;
                    oleMenu.Enabled   = true;
                    oleMenu.Supported = true;
                    oleMenu.Text      = "Execute Project in P&ython Interactive";
                }
                else if ((textView = CommonPackage.GetActiveTextView(_serviceProvider)) != null &&
                         textView.TextBuffer.ContentType == _serviceProvider.GetPythonContentType())
                {
                    // enabled in Execute File mode...
                    oleMenu.Visible   = true;
                    oleMenu.Enabled   = true;
                    oleMenu.Supported = true;
                    oleMenu.Text      = "Execute File in P&ython Interactive";
                }
                else
                {
                    oleMenu.Visible   = false;
                    oleMenu.Enabled   = false;
                    oleMenu.Supported = false;
                }
            }
        }
Ejemplo n.º 7
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var file = CommonPackage.GetActiveTextView().GetFilePath();

            // Launch with project context if there is one and it contains the active document
            // Fallback to using default python project
            var pythonProjectNode = CommonPackage.GetStartupProject() as PythonProjectNode;

            if ((pythonProjectNode != null) && (pythonProjectNode.FindNodeByFullPath(file) == null))
            {
                pythonProjectNode = null;
            }
            IPythonProject pythonProject = pythonProjectNode as IPythonProject ?? new DefaultPythonProject(file);

            var launcher = PythonToolsPackage.GetLauncher(pythonProject);

            launcher.LaunchFile(file, true);
        }
Ejemplo n.º 8
0
        private async System.Threading.Tasks.Task DoCommand()
        {
            var pyProj   = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;
            var textView = CommonPackage.GetActiveTextView(_serviceProvider);

            LaunchConfiguration config = null;

            try {
                config = pyProj?.GetLaunchConfigurationOrThrow();
            } catch (MissingInterpreterException ex) {
                MessageBox.Show(ex.Message, Strings.ProductTitle);
                return;
            }
            if (config == null && textView != null)
            {
                var interpreters = _serviceProvider.GetComponentModel().GetService <IInterpreterOptionsService>();
                config = new LaunchConfiguration(interpreters.DefaultInterpreter.Configuration)
                {
                    ScriptName       = textView.GetFilePath(),
                    WorkingDirectory = PathUtils.GetParent(textView.GetFilePath())
                };
            }
            if (config == null)
            {
                Debug.Fail("Should not be executing command when it is invisible");
                return;
            }

            var window = EnsureReplWindow(_serviceProvider, config.Interpreter, pyProj);

            window.Show(true);

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

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

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

                await eval.ExecuteFileAsync(config.ScriptName, config.ScriptArguments);
            });
        }
Ejemplo n.º 9
0
        public override async void DoCommand(object sender, EventArgs e)
        {
            var pyProj   = CommonPackage.GetStartupProject(_serviceProvider) as PythonProjectNode;
            var textView = CommonPackage.GetActiveTextView(_serviceProvider);

            var config = pyProj?.GetLaunchConfigurationOrThrow();

            if (config == null && textView != null)
            {
                var pyService = _serviceProvider.GetPythonToolsService();
                config = new LaunchConfiguration(pyService.DefaultInterpreterConfiguration)
                {
                    ScriptName       = textView.GetFilePath(),
                    WorkingDirectory = PathUtils.GetParent(textView.GetFilePath())
                };
            }
            if (config == null)
            {
                Debug.Fail("Should not be executing command when it is invisible");
                return;
            }

            var window = EnsureReplWindow(_serviceProvider, config.Interpreter, pyProj);

            window.Show(true);

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

            // The interpreter may take some time to startup, do this off the UI thread.
            await ThreadHelper.JoinableTaskFactory.RunAsync(async() => {
                await eval.ResetAsync();

                window.InteractiveWindow.WriteLine(string.Format("Running {0}", config.ScriptName));

                await eval.ExecuteFileAsync(config.ScriptName, config.ScriptArguments);
            });
        }
Ejemplo n.º 10
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);
            });
        }