public PythonSuggestedActionsSource(
     IServiceProvider provider,
     ITextView textView,
     ITextBuffer textBuffer
 ) {
     _provider = provider;
     _view = textView;
     _textBuffer = textBuffer;
     _textBuffer.RegisterForNewAnalysis(OnNewAnalysisEntry);
     _uiThread = provider.GetUIThread();
 }
Esempio n. 2
0
        public DropDownBarClient(IServiceProvider serviceProvider, IWpfTextView textView, AnalysisEntry analysisEntry) {
            Utilities.ArgumentNotNull(nameof(serviceProvider), serviceProvider);
            Utilities.ArgumentNotNull(nameof(textView), textView);
            Utilities.ArgumentNotNull(nameof(analysisEntry), analysisEntry);

            _serviceProvider = serviceProvider;
            _uiThread = _serviceProvider.GetUIThread();
            _analysisEntry = analysisEntry;
            textView.TextBuffer.RegisterForParseTree(ParserOnNewParseTree);
            _textView = textView;
            _dispatcher = Dispatcher.CurrentDispatcher;
            _textView.Caret.PositionChanged += CaretPositionChanged;
            for (int i = 0; i < NavigationLevels; i++) {
                _curSelection[i] = -1;
            }
        }
Esempio n. 3
0
        private string GetData(UIThreadBase ui, bool skipAnalysisLog, CancellationToken cancel) {
            StringBuilder res = new StringBuilder();

            if (PythonToolsPackage.IsIpyToolsInstalled()) {
                res.AppendLine("WARNING: IpyTools is installed on this machine.  Having both IpyTools and Python Tools for Visual Studio installed will break Python editing.");
            }

            string pythonPathIsMasked = "";
            EnvDTE.DTE dte = null;
            IPythonInterpreterFactoryProvider[] knownProviders = null;
            IPythonLauncherProvider[] launchProviders = null;
            InMemoryLogger inMemLogger = null;
            ui.Invoke((Action)(() => {
                pythonPathIsMasked = _serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath
                    ? " (masked)"
                    : "";
                dte = (EnvDTE.DTE)_serviceProvider.GetService(typeof(EnvDTE.DTE));
                var model = _serviceProvider.GetComponentModel();
                knownProviders = model.GetExtensions<IPythonInterpreterFactoryProvider>().ToArray();
                launchProviders = model.GetExtensions<IPythonLauncherProvider>().ToArray();
                inMemLogger = model.GetService<InMemoryLogger>();
            }));

            res.AppendLine("Projects: ");

            var projects = dte.Solution.Projects;

            foreach (EnvDTE.Project project in projects) {
                cancel.ThrowIfCancellationRequested();

                string name;
                try {
                    // Some projects will throw rather than give us a unique
                    // name. They are not ours, so we will ignore them.
                    name = project.UniqueName;
                } catch (Exception ex) when (!ex.IsCriticalException()) {
                    bool isPythonProject = false;
                    try {
                        isPythonProject = Utilities.GuidEquals(PythonConstants.ProjectFactoryGuid, project.Kind);
                    } catch (Exception ex2) when (!ex2.IsCriticalException()) {
                    }

                    if (isPythonProject) {
                        // Actually, it was one of our projects, so we do care
                        // about the exception. We'll add it to the output,
                        // rather than crashing.
                        res.AppendLine("    Project: " + ex.Message);
                        res.AppendLine("        Kind: Python");
                    }
                    continue;
                }
                res.AppendLine("    Project: " + name);

                if (Utilities.GuidEquals(PythonConstants.ProjectFactoryGuid, project.Kind)) {
                    res.AppendLine("        Kind: Python");

                    foreach (var prop in InterestingDteProperties) {
                        res.AppendLine("        " + prop + ": " + GetProjectProperty(project, prop));
                    }

                    var pyProj = project.GetPythonProject();
                    if (pyProj != null) {
                        ui.Invoke((Action)(() => {
                            foreach (var prop in InterestingProjectProperties) {
                                var propValue = pyProj.GetProjectProperty(prop);
                                if (propValue != null) {
                                    res.AppendLine("        " + prop + ": " + propValue);
                                }
                            }
                        }));

                        foreach (var factory in pyProj.InterpreterFactories) {
                            res.AppendLine();
                            res.AppendLine("        Interpreter: " + factory.Configuration.Description);
                            res.AppendLine("            Id: " + factory.Configuration.Id);
                            res.AppendLine("            Version: " + factory.Configuration.Version);
                            res.AppendLine("            Arch: " + factory.Configuration.Architecture);
                            res.AppendLine("            Prefix Path: " + factory.Configuration.PrefixPath ?? "(null)");
                            res.AppendLine("            Path: " + factory.Configuration.InterpreterPath ?? "(null)");
                            res.AppendLine("            Windows Path: " + factory.Configuration.WindowsInterpreterPath ?? "(null)");
                            res.AppendLine(string.Format("            Path Env: {0}={1}{2}",
                                factory.Configuration.PathEnvironmentVariable ?? "(null)",
                                Environment.GetEnvironmentVariable(factory.Configuration.PathEnvironmentVariable ?? ""),
                                pythonPathIsMasked
                            ));
                        }
                    }
                } else {
                    res.AppendLine("        Kind: " + project.Kind);
                }

                res.AppendLine();
            }

            res.AppendLine("Environments: ");
            foreach (var provider in knownProviders.MaybeEnumerate()) {
                cancel.ThrowIfCancellationRequested();

                res.AppendLine("    " + provider.GetType().FullName);
                foreach (var config in provider.GetInterpreterConfigurations()) {
                    res.AppendLine("        Id: " + config.Id);
                    res.AppendLine("        Factory: " + config.Description);
                    res.AppendLine("        Version: " + config.Version);
                    res.AppendLine("        Arch: " + config.Architecture);
                    res.AppendLine("        Prefix Path: " + config.PrefixPath ?? "(null)");
                    res.AppendLine("        Path: " + config.InterpreterPath ?? "(null)");
                    res.AppendLine("        Windows Path: " + config.WindowsInterpreterPath ?? "(null)");
                    res.AppendLine("        Path Env: " + config.PathEnvironmentVariable ?? "(null)");
                    res.AppendLine();
                }
            }

            res.AppendLine("Launchers:");
            foreach (var launcher in launchProviders.MaybeEnumerate()) {
                cancel.ThrowIfCancellationRequested();

                res.AppendLine("    Launcher: " + launcher.GetType().FullName);
                res.AppendLine("        " + launcher.Description);
                res.AppendLine("        " + launcher.Name);
                res.AppendLine();
            }

            try {
                res.AppendLine("Logged events/stats:");
                res.AppendLine(inMemLogger.ToString());
                res.AppendLine();
            } catch (Exception ex) when (!ex.IsCriticalException()) {
                res.AppendLine("  Failed to access event log.");
                res.AppendLine(ex.ToString());
                res.AppendLine();
            }

            try {
                res.AppendLine("System events:");

                var application = new EventLog("Application");
                var lastWeek = DateTime.Now.Subtract(TimeSpan.FromDays(7));
                foreach (var entry in application.Entries.Cast<EventLogEntry>()
                    .Where(e => e.InstanceId == 1026L)  // .NET Runtime
                    .Where(e => e.TimeGenerated >= lastWeek)
                    .Where(e => InterestingApplicationLogEntries.IsMatch(e.Message))
                    .OrderByDescending(e => e.TimeGenerated)
                ) {
                    res.AppendLine(string.Format("Time: {0:s}", entry.TimeGenerated));
                    using (var reader = new StringReader(entry.Message.TrimEnd())) {
                        for (var line = reader.ReadLine(); line != null; line = reader.ReadLine()) {
                            res.AppendLine(line);
                        }
                    }
                    res.AppendLine();
                }

            } catch (Exception ex) when (!ex.IsCriticalException()) {
                res.AppendLine("  Failed to access event log.");
                res.AppendLine(ex.ToString());
                res.AppendLine();
            }

            res.AppendLine("Loaded assemblies:");
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().OrderBy(assem => assem.FullName)) {
                cancel.ThrowIfCancellationRequested();

                AssemblyFileVersionAttribute assemFileVersion;
                var error = "(null)";
                try {
                    assemFileVersion = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                        .OfType<AssemblyFileVersionAttribute>()
                        .FirstOrDefault();
                } catch (Exception e) when (!e.IsCriticalException()) {
                    assemFileVersion = null;
                    error = string.Format("{0}: {1}", e.GetType().Name, e.Message);
                }

                res.AppendLine(string.Format("  {0}, FileVersion={1}",
                    assembly.FullName,
                    assemFileVersion?.Version ?? error
                ));
            }
            res.AppendLine();

            string globalAnalysisLog = PythonTypeDatabase.GlobalLogFilename;
            if (File.Exists(globalAnalysisLog)) {
                res.AppendLine("Global Analysis:");
                try {
                    res.AppendLine(File.ReadAllText(globalAnalysisLog));
                } catch (Exception ex) when (!ex.IsCriticalException()) {
                    res.AppendLine("Error reading the global analysis log.");
                    res.AppendLine("Please wait for analysis to complete and try again.");
                    res.AppendLine(ex.ToString());
                }
            }
            res.AppendLine();

            if (!skipAnalysisLog) {
                res.AppendLine("Environment Analysis Logs: ");
                foreach (var provider in knownProviders) {
                    foreach (var factory in provider.GetInterpreterFactories().OfType<IPythonInterpreterFactoryWithDatabase>()) {
                        cancel.ThrowIfCancellationRequested();

                        res.AppendLine(factory.Configuration.Description);
                        string analysisLog = factory.GetAnalysisLogContent(CultureInfo.InvariantCulture);
                        if (!string.IsNullOrEmpty(analysisLog)) {
                            res.AppendLine(analysisLog);
                        }
                        res.AppendLine();
                    }
                }
            }

            return res.ToString();
        }
Esempio n. 4
0
 public VariableRenamer(ITextView textView, IServiceProvider serviceProvider) {
     _view = textView;
     _serviceProvider = serviceProvider;
     _uiThread = _serviceProvider.GetUIThread();
 }
Esempio n. 5
0
        private string GetData(UIThreadBase ui, bool skipAnalysisLog, CancellationToken cancel)
        {
            StringBuilder res = new StringBuilder();

            if (PythonToolsPackage.IsIpyToolsInstalled())
            {
                res.AppendLine("WARNING: IpyTools is installed on this machine.  Having both IpyTools and Python Tools for Visual Studio installed will break Python editing.");
            }

            string pythonPathIsMasked = "";

            EnvDTE.DTE dte = null;
            IPythonInterpreterFactoryProvider[] knownProviders  = null;
            IPythonLauncherProvider[]           launchProviders = null;
            InMemoryLogger inMemLogger = null;

            ui.Invoke((Action)(() => {
                pythonPathIsMasked = _serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath
                    ? " (masked)"
                    : "";
                dte = (EnvDTE.DTE)_serviceProvider.GetService(typeof(EnvDTE.DTE));
                var model = _serviceProvider.GetComponentModel();
                knownProviders = model.GetExtensions <IPythonInterpreterFactoryProvider>().ToArray();
                launchProviders = model.GetExtensions <IPythonLauncherProvider>().ToArray();
                inMemLogger = model.GetService <InMemoryLogger>();
            }));

            res.AppendLine("Projects: ");

            var projects = dte.Solution.Projects;

            foreach (EnvDTE.Project project in projects)
            {
                cancel.ThrowIfCancellationRequested();

                string name;
                try {
                    // Some projects will throw rather than give us a unique
                    // name. They are not ours, so we will ignore them.
                    name = project.UniqueName;
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    bool isPythonProject = false;

                    try {
                        isPythonProject = Utilities.GuidEquals(PythonConstants.ProjectFactoryGuid, project.Kind);
                    } catch (Exception ex2) when(!ex2.IsCriticalException())
                    {
                    }

                    if (isPythonProject)
                    {
                        // Actually, it was one of our projects, so we do care
                        // about the exception. We'll add it to the output,
                        // rather than crashing.
                        res.AppendLine("    Project: " + ex.Message);
                        res.AppendLine("        Kind: Python");
                    }
                    continue;
                }
                res.AppendLine("    Project: " + name);

                if (Utilities.GuidEquals(PythonConstants.ProjectFactoryGuid, project.Kind))
                {
                    res.AppendLine("        Kind: Python");

                    foreach (var prop in InterestingDteProperties)
                    {
                        res.AppendLine("        " + prop + ": " + GetProjectProperty(project, prop));
                    }

                    var pyProj = project.GetPythonProject();
                    if (pyProj != null)
                    {
                        ui.Invoke((Action)(() => {
                            foreach (var prop in InterestingProjectProperties)
                            {
                                var propValue = pyProj.GetProjectProperty(prop);
                                if (propValue != null)
                                {
                                    res.AppendLine("        " + prop + ": " + propValue);
                                }
                            }
                        }));

                        foreach (var factory in pyProj.InterpreterFactories)
                        {
                            res.AppendLine();
                            res.AppendLine("        Interpreter: " + factory.Configuration.Description);
                            res.AppendLine("            Id: " + factory.Configuration.Id);
                            res.AppendLine("            Version: " + factory.Configuration.Version);
                            res.AppendLine("            Arch: " + factory.Configuration.Architecture);
                            res.AppendLine("            Prefix Path: " + factory.Configuration.PrefixPath ?? "(null)");
                            res.AppendLine("            Path: " + factory.Configuration.InterpreterPath ?? "(null)");
                            res.AppendLine("            Windows Path: " + factory.Configuration.WindowsInterpreterPath ?? "(null)");
                            res.AppendLine(string.Format("            Path Env: {0}={1}{2}",
                                                         factory.Configuration.PathEnvironmentVariable ?? "(null)",
                                                         Environment.GetEnvironmentVariable(factory.Configuration.PathEnvironmentVariable ?? ""),
                                                         pythonPathIsMasked
                                                         ));
                        }
                    }
                }
                else
                {
                    res.AppendLine("        Kind: " + project.Kind);
                }

                res.AppendLine();
            }

            res.AppendLine("Environments: ");
            foreach (var provider in knownProviders.MaybeEnumerate())
            {
                cancel.ThrowIfCancellationRequested();

                res.AppendLine("    " + provider.GetType().FullName);
                foreach (var config in provider.GetInterpreterConfigurations())
                {
                    res.AppendLine("        Id: " + config.Id);
                    res.AppendLine("        Factory: " + config.Description);
                    res.AppendLine("        Version: " + config.Version);
                    res.AppendLine("        Arch: " + config.Architecture);
                    res.AppendLine("        Prefix Path: " + config.PrefixPath ?? "(null)");
                    res.AppendLine("        Path: " + config.InterpreterPath ?? "(null)");
                    res.AppendLine("        Windows Path: " + config.WindowsInterpreterPath ?? "(null)");
                    res.AppendLine("        Path Env: " + config.PathEnvironmentVariable ?? "(null)");
                    res.AppendLine();
                }
            }

            res.AppendLine("Launchers:");
            foreach (var launcher in launchProviders.MaybeEnumerate())
            {
                cancel.ThrowIfCancellationRequested();

                res.AppendLine("    Launcher: " + launcher.GetType().FullName);
                res.AppendLine("        " + launcher.Description);
                res.AppendLine("        " + launcher.Name);
                res.AppendLine();
            }

            try {
                res.AppendLine("Logged events/stats:");
                res.AppendLine(inMemLogger.ToString());
                res.AppendLine();
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                res.AppendLine("  Failed to access event log.");
                res.AppendLine(ex.ToString());
                res.AppendLine();
            }

            if (!skipAnalysisLog)
            {
                try {
                    res.AppendLine("System events:");

                    var application = new EventLog("Application");
                    var lastWeek    = DateTime.Now.Subtract(TimeSpan.FromDays(7));
                    foreach (var entry in application.Entries.Cast <EventLogEntry>()
                             .Where(e => e.InstanceId == 1026L) // .NET Runtime
                             .Where(e => e.TimeGenerated >= lastWeek)
                             .Where(e => InterestingApplicationLogEntries.IsMatch(e.Message))
                             .OrderByDescending(e => e.TimeGenerated)
                             )
                    {
                        res.AppendLine(string.Format("Time: {0:s}", entry.TimeGenerated));
                        using (var reader = new StringReader(entry.Message.TrimEnd())) {
                            for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
                            {
                                res.AppendLine(line);
                            }
                        }
                        res.AppendLine();
                    }
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    res.AppendLine("  Failed to access event log.");
                    res.AppendLine(ex.ToString());
                    res.AppendLine();
                }
            }

            res.AppendLine("Loaded assemblies:");
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().OrderBy(assem => assem.FullName))
            {
                cancel.ThrowIfCancellationRequested();

                AssemblyFileVersionAttribute assemFileVersion;
                var error = "(null)";
                try {
                    assemFileVersion = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                                       .OfType <AssemblyFileVersionAttribute>()
                                       .FirstOrDefault();
                } catch (Exception e) when(!e.IsCriticalException())
                {
                    assemFileVersion = null;
                    error            = string.Format("{0}: {1}", e.GetType().Name, e.Message);
                }

                res.AppendLine(string.Format("  {0}, FileVersion={1}",
                                             assembly.FullName,
                                             assemFileVersion?.Version ?? error
                                             ));
            }
            res.AppendLine();

            string globalAnalysisLog = PythonTypeDatabase.GlobalLogFilename;

            if (File.Exists(globalAnalysisLog))
            {
                res.AppendLine("Global Analysis:");
                try {
                    res.AppendLine(File.ReadAllText(globalAnalysisLog));
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    res.AppendLine("Error reading the global analysis log.");
                    res.AppendLine("Please wait for analysis to complete and try again.");
                    res.AppendLine(ex.ToString());
                }
            }
            res.AppendLine();

            if (!skipAnalysisLog)
            {
                res.AppendLine("Environment Analysis Logs: ");
                foreach (var provider in knownProviders)
                {
                    foreach (var factory in provider.GetInterpreterFactories().OfType <IPythonInterpreterFactoryWithDatabase>())
                    {
                        cancel.ThrowIfCancellationRequested();

                        res.AppendLine(factory.Configuration.Description);
                        string analysisLog = factory.GetAnalysisLogContent(CultureInfo.InvariantCulture);
                        if (!string.IsNullOrEmpty(analysisLog))
                        {
                            res.AppendLine(analysisLog);
                        }
                        res.AppendLine();
                    }
                }
            }

            return(res.ToString());
        }
Esempio n. 6
0
        protected override void OnCreate()
        {
            base.OnCreate();

            _pyService = _site.GetPythonToolsService();
            _uiThread  = _site.GetUIThread();

            _pyService.InteractiveOptions.Changed += InteractiveOptions_Changed;

            // TODO: Get PYEnvironment added to image list
            BitmapImageMoniker = KnownMonikers.DockPanel;
            Caption            = Strings.Environments;

            _outputWindow = OutputWindowRedirector.GetGeneral(_site);
            Debug.Assert(_outputWindow != null);
            _statusBar = _site.GetService(typeof(SVsStatusbar)) as IVsStatusbar;

            var list = new ToolWindow();

            list.ViewCreated  += List_ViewCreated;
            list.ViewSelected += List_ViewSelected;
            list.Site          = _site;
            try {
                list.TelemetryLogger = _pyService.Logger;
            } catch (Exception ex) {
                Debug.Fail(ex.ToUnhandledExceptionMessage(GetType()));
            }

            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInteractiveWindow,
                                         OpenInteractiveWindow_Executed,
                                         OpenInteractiveWindow_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInteractiveScripts,
                                         OpenInteractiveScripts_Executed,
                                         OpenInteractiveScripts_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.StartInterpreter,
                                         StartInterpreter_Executed,
                                         StartInterpreter_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.StartWindowsInterpreter,
                                         StartInterpreter_Executed,
                                         StartInterpreter_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         ApplicationCommands.Help,
                                         OnlineHelp_Executed,
                                         OnlineHelp_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         ToolWindow.UnhandledException,
                                         UnhandledException_Executed,
                                         UnhandledException_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInPowerShell,
                                         OpenInPowerShell_Executed,
                                         OpenInPowerShell_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInCommandPrompt,
                                         OpenInCommandPrompt_Executed,
                                         OpenInCommandPrompt_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.OpenInBrowser,
                                         OpenInBrowser_Executed,
                                         OpenInBrowser_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.Delete,
                                         DeleteEnvironment_Executed,
                                         DeleteEnvironment_CanExecute
                                         ));

            RegisterCommands(
                CommandAsyncToOleMenuCommandShimFactory.CreateCommand(GuidList.guidPythonToolsCmdSet, (int)PkgCmdIDList.cmdidAddEnvironment, new AddEnvironmentCommand(this))
                );

            Content = list;
        }
 public PythonSuggestedActionsSource(PythonEditorServices services)
 {
     _services = services;
     _uiThread = _services.Site.GetUIThread();
 }
 public PythonLanguageServiceProviderCallback(IServiceProvider serviceProvider)
 {
     _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
     _uiThread        = _serviceProvider.GetUIThread();
     _analyzerCache   = new ConcurrentDictionary <Uri, VsProjectAnalyzer>(UriEqualityComparer.Default);
 }