Exemplo n.º 1
0
        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         sln  = (IVsSolution)_serviceProvider.GetService(typeof(SVsSolution));
            IEnumerable projects;

            try {
                projects = _serviceProvider.GetDTE().ActiveSolutionProjects as IEnumerable;
            } catch (COMException) {
                // ActiveSolutionProjects can fail if Solution Explorer has not been loaded
                projects = Enumerable.Empty <EnvDTE.Project>();
            }

            var pythonProject = (projects == null ? null : projects.OfType <EnvDTE.Project>()
                                 .Select(p => p.GetPythonProject())
                                 .FirstOrDefault(p => p != null && p.FindNodeByFullPath(file) != null) as IPythonProject)
                                ?? new DefaultPythonProject(_serviceProvider, file);

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

            try {
                var launcher2 = launcher as IProjectLauncher2;
                if (launcher2 != null)
                {
                    launcher2.LaunchFile(
                        file,
                        CommandId == CommonConstants.StartDebuggingCmdId,
                        new LaunchFileProperties(
                            null,
                            PathUtils.GetParent(file),
                            pythonProject.GetInterpreterFactory().Configuration.PathEnvironmentVariable,
                            pythonProject.GetWorkingDirectory()
                            )
                        );
                }
                else
                {
                    launcher.LaunchFile(file, CommandId == CommonConstants.StartDebuggingCmdId);
                }
            } catch (MissingInterpreterException ex) {
                MessageBox.Show(ex.Message, Strings.ProductTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            } catch (NoInterpretersException ex) {
                PythonToolsPackage.OpenNoInterpretersHelpPage(_serviceProvider, ex.HelpPage);
            }
        }