コード例 #1
0
        private AdvancedEditorOptions CreateAdvancedEditorOptions()
        {
            var opts = new AdvancedEditorOptions(this);

            opts.Load();
            return(opts);
        }
コード例 #2
0
        internal PythonToolsService(IServiceContainer container)
        {
            _container = container;

            var langService = new PythonLanguageInfo(container);

            _container.AddService(langService.GetType(), langService, true);

            IVsTextManager textMgr = (IVsTextManager)container.GetService(typeof(SVsTextManager));

            if (textMgr != null)
            {
                var langPrefs = new LANGPREFERENCES[1];
                langPrefs[0].guidLang = typeof(PythonLanguageInfo).GUID;
                ErrorHandler.ThrowOnFailure(textMgr.GetUserPreferences(null, null, langPrefs, null));
                _langPrefs = new LanguagePreferences(this, langPrefs[0]);

                Guid             guid = typeof(IVsTextManagerEvents2).GUID;
                IConnectionPoint connectionPoint;
                ((IConnectionPointContainer)textMgr).FindConnectionPoint(ref guid, out connectionPoint);
                connectionPoint.Advise(_langPrefs, out _langPrefsTextManagerCookie);
            }

            _optionsService = (IPythonToolsOptionsService)container.GetService(typeof(IPythonToolsOptionsService));
            var compModel = (IComponentModel)container.GetService(typeof(SComponentModel));

            _interpreterRegistry = compModel.GetService <IInterpreterRegistryService>();
            if (_interpreterRegistry != null)
            {
                _interpreterRegistry.InterpretersChanged += InterpretersChanged;
            }

            _interpreterOptionsService = compModel.GetService <IInterpreterOptionsService>();
            if (_interpreterOptionsService != null)     // not available in some test cases...
            {
                _interpreterOptionsService.DefaultInterpreterChanged += UpdateDefaultAnalyzer;
                LoadInterpreterOptions();
            }

            _idleManager              = new IdleManager(container);
            _advancedOptions          = new AdvancedEditorOptions(this);
            _debuggerOptions          = new DebuggerOptions(this);
            _generalOptions           = new GeneralOptions(this);
            _surveyNews               = new SurveyNewsService(container);
            _suppressDialogOptions    = new SuppressDialogOptions(this);
            _globalInterpreterOptions = new GlobalInterpreterOptions(this, _interpreterOptionsService, _interpreterRegistry);
            _globalInterpreterOptions.Load();
            _interactiveOptions = new PythonInteractiveOptions(this, "Interactive");
            _interactiveOptions.Load();
            _debugInteractiveOptions = new PythonInteractiveOptions(this, "Debug Interactive Window");
            _debuggerOptions.Load();
            _factoryProviders = ComponentModel.DefaultExportProvider.GetExports <IPythonInterpreterFactoryProvider, Dictionary <string, object> >();
            _logger           = new PythonToolsLogger(ComponentModel.GetExtensions <IPythonToolsLogger>().ToArray());
            InitializeLogging();
        }
コード例 #3
0
        internal PythonToolsService(IServiceContainer container)
        {
            _container = container;

            var langService = new PythonLanguageInfo(container);

            _container.AddService(langService.GetType(), langService, true);

            _langPrefs = new Lazy <LanguagePreferences>(() => new LanguagePreferences(this, typeof(PythonLanguageInfo).GUID));

            _optionsService = (IPythonToolsOptionsService)container.GetService(typeof(IPythonToolsOptionsService));
            var compModel = (IComponentModel)container.GetService(typeof(SComponentModel));

            _interpreterRegistry = compModel.GetService <IInterpreterRegistryService>();
            if (_interpreterRegistry != null)
            {
                _interpreterRegistry.InterpretersChanged += InterpretersChanged;
            }

            _interpreterOptionsService = compModel.GetService <IInterpreterOptionsService>();
            if (_interpreterOptionsService != null)     // not available in some test cases...
            {
                _interpreterOptionsService.DefaultInterpreterChanged += UpdateDefaultAnalyzer;
                LoadInterpreterOptions();
            }

            _idleManager              = new IdleManager(container);
            _advancedOptions          = new AdvancedEditorOptions(this);
            _debuggerOptions          = new DebuggerOptions(this);
            _generalOptions           = new GeneralOptions(this);
            _surveyNews               = new SurveyNewsService(container);
            _suppressDialogOptions    = new SuppressDialogOptions(this);
            _globalInterpreterOptions = new GlobalInterpreterOptions(this, _interpreterOptionsService, _interpreterRegistry);
            _globalInterpreterOptions.Load();
            _interactiveOptions = new PythonInteractiveOptions(this, "Interactive");
            _interactiveOptions.Load();
            _debugInteractiveOptions = new PythonInteractiveOptions(this, "Debug Interactive Window");
            _debuggerOptions.Load();
            _factoryProviders = ComponentModel.DefaultExportProvider.GetExports <IPythonInterpreterFactoryProvider, Dictionary <string, object> >();
            _logger           = new PythonToolsLogger(ComponentModel.GetExtensions <IPythonToolsLogger>().ToArray());
            InitializeLogging();
        }
コード例 #4
0
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedEditorOptions advancedOptions = null;
                vs.InvokeSync(() => {
                    advancedOptions = vs.GetPyService().AdvancedOptions;
                    advancedOptions.AutoListMembers     = true;
                    advancedOptions.AutoListIdentifiers = false;
                });
                AdvancedOptions = advancedOptions;

                if (factory == null)
                {
                    _disposeFactory = true;
                    factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    vs.InvokeSync(() => {
                        analyzer = new VsProjectAnalyzer(vs.ComponentModel.GetService <PythonEditorServices>(), factory, outOfProcAnalyzer: false);
                    });
                    var task = analyzer.ReloadTask;
                    if (task != null)
                    {
                        task.WaitAndUnwrapExceptions();
                    }
                }
                if (string.IsNullOrEmpty(filename))
                {
                    do
                    {
                        filename = PathUtils.GetAbsoluteFilePath(TestData.GetTempPath(), Path.GetRandomFileName()) + ".py";
                    } while (File.Exists(filename));
                }

                var cancel = CancellationTokens.After60s;
                using (var mre = new ManualResetEventSlim()) {
                    view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "",
                                             v => {
                        v.TextView.TextBuffer.Properties[BufferParser.ParseImmediately]             = true;
                        v.TextView.TextBuffer.Properties[IntellisenseController.SuppressErrorLists] = IntellisenseController.SuppressErrorLists;
                        v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testAnalyzer]           = analyzer;
                        v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testFilename]           = filename;
                    },
                                             filename);

                    var entry = analyzer.GetAnalysisEntryFromPath(filename);
                    while (entry == null && !cancel.IsCancellationRequested)
                    {
                        Thread.Sleep(50);
                        entry = analyzer.GetAnalysisEntryFromPath(filename);
                    }

                    if (!string.IsNullOrEmpty(content) && !cancel.IsCancellationRequested && !entry.IsAnalyzed)
                    {
                        EventHandler evt = (s, e) => mre.SetIfNotDisposed();

                        try {
                            entry.AnalysisComplete += evt;
                            while (!mre.Wait(50, cancel) && !vs.HasPendingException)
                            {
                            }
                        } catch (OperationCanceledException) {
                        } finally {
                            analyzer.AnalysisStarted -= evt;
                        }
                    }
                    if (cancel.IsCancellationRequested)
                    {
                        Assert.Fail("Timed out waiting for code analysis");
                    }

                    vs.ThrowPendingException();
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }
コード例 #5
0
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename     = null,
            bool?inProcAnalyzer = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedEditorOptions advancedOptions = null;
                vs.InvokeSync(() => {
                    advancedOptions = vs.GetPyService().AdvancedOptions;
                    advancedOptions.AutoListMembers     = true;
                    advancedOptions.AutoListIdentifiers = false;
                });
                AdvancedOptions = advancedOptions;

                if (factory == null)
                {
                    vs.InvokeSync(() => {
                        factory = vs.ComponentModel.GetService <IInterpreterRegistryService>()
                                  .Interpreters
                                  .FirstOrDefault(c => c.GetLanguageVersion() == version && c.Configuration.Id.StartsWith("Global|PythonCore"));
                        if (factory != null)
                        {
                            Console.WriteLine($"Using interpreter {factory.Configuration.InterpreterPath}");
                        }
                    });
                    if (factory == null)
                    {
                        _disposeFactory = true;
                        factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                        Console.WriteLine("Using analysis-only interpreter");
                    }
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    analyzer         = vs.InvokeTask(() => VsProjectAnalyzer.CreateForTestsAsync(vs.ComponentModel.GetService <PythonEditorServices>(), factory, inProcAnalyzer ?? Debugger.IsAttached), 10000);
                }
                Uri uri;
                if (string.IsNullOrEmpty(filename))
                {
                    filename = Path.ChangeExtension(Path.GetRandomFileName(), ".py");
                }
                if (Path.IsPathRooted(filename))
                {
                    uri = new Uri(filename);
                }
                else
                {
                    var d = Path.GetRandomFileName();
                    uri      = new Uri($"python://test/{d}/{filename}");
                    filename = $"_:\\PYTHON\\{d}\\{filename}";
                }

                var cancel = CancellationTokens.After60s;
                view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "",
                                         v => {
                    v.TextView.TextBuffer.Properties[BufferParser.ParseImmediately]             = true;
                    v.TextView.TextBuffer.Properties[IntellisenseController.SuppressErrorLists] = IntellisenseController.SuppressErrorLists;
                    v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testAnalyzer]           = analyzer;
                    v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testFilename]           = filename;
                    v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testDocumentUri]        = uri;
                },
                                         filename);

                var services = vs.ComponentModel.GetService <PythonEditorServices>();
                var bi       = services.GetBufferInfo(view.TextView.TextBuffer);
                var entry    = bi.GetAnalysisEntryAsync(cancel).WaitAndUnwrapExceptions();
                Assert.IsNotNull(entry, "failed to get analysis entry");

                if (!string.IsNullOrEmpty(content) && !cancel.IsCancellationRequested && !entry.IsAnalyzed)
                {
                    var task = entry.Analyzer.WaitForNextCompleteAnalysis();
                    var bp   = entry.TryGetBufferParser();
                    while (bp == null)
                    {
                        Thread.Sleep(50);
                        cancel.ThrowIfCancellationRequested();
                        bp = entry.TryGetBufferParser();
                    }
                    try {
                        bp.EnsureCodeSyncedAsync(bi.Buffer, true).Wait(cancel);
                        task.Wait(cancel);
                    } catch (AggregateException ex) when(ex.InnerException != null)
                    {
                        throw ex.InnerException;
                    } catch (OperationCanceledException) {
                    }
                }
                if (cancel.IsCancellationRequested)
                {
                    Assert.Fail("Timed out waiting for code analysis");
                }

                vs.ThrowPendingException();

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }
コード例 #6
0
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedOptions = vs.GetPyService().AdvancedOptions;
                AdvancedOptions.AutoListMembers     = true;
                AdvancedOptions.AutoListIdentifiers = false;

                if (factory == null)
                {
                    _disposeFactory = true;
                    factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    analyzer         = new VsProjectAnalyzer(vs.ServiceProvider, factory, new[] { factory });
                    var task = analyzer.ReloadTask;
                    if (task != null)
                    {
                        task.WaitAndUnwrapExceptions();
                    }
                }

                var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
                using (var mre = new ManualResetEventSlim()) {
                    EventHandler evt = (s, e) => mre.Set();
                    analyzer.AnalysisStarted += evt;
                    view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "", v => {
                        v.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                    }, filename);

                    try {
                        mre.Wait(cts.Token);
                        analyzer.WaitForCompleteAnalysis(x => !cts.IsCancellationRequested);
                    } catch (OperationCanceledException) {
                    } finally {
                        analyzer.AnalysisStarted -= evt;
                    }
                    if (cts.IsCancellationRequested)
                    {
                        Assert.Fail("Timed out waiting for code analysis");
                    }
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }
コード例 #7
0
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedOptions = vs.GetPyService().AdvancedOptions;
                AdvancedOptions.AutoListMembers     = true;
                AdvancedOptions.AutoListIdentifiers = false;

                if (factory == null)
                {
                    _disposeFactory = true;
                    factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    analyzer         = new VsProjectAnalyzer(vs.ServiceProvider, factory, new[] { factory });
                }

                view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "", v => {
                    v.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                });
                var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
                analyzer.WaitForCompleteAnalysis(x => !cts.IsCancellationRequested);
                if (cts.IsCancellationRequested)
                {
                    Assert.Fail("Timed out waiting for code analysis");
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }
コード例 #8
0
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename     = null,
            bool?inProcAnalyzer = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedEditorOptions advancedOptions = null;
                vs.InvokeSync(() => {
                    advancedOptions = vs.GetPyService().AdvancedOptions;
                    advancedOptions.AutoListMembers     = true;
                    advancedOptions.AutoListIdentifiers = false;
                });
                AdvancedOptions = advancedOptions;

                if (factory == null)
                {
                    vs.InvokeSync(() => {
                        factory = vs.ComponentModel.GetService <IInterpreterRegistryService>()
                                  .Interpreters
                                  .FirstOrDefault(c => c.GetLanguageVersion() == version && c.Configuration.Id.StartsWith("Global|PythonCore"));
                        if (factory != null)
                        {
                            Console.WriteLine($"Using interpreter {factory.Configuration.InterpreterPath}");
                        }
                    });
                    if (factory == null)
                    {
                        _disposeFactory = true;
                        factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                        Console.WriteLine("Using analysis-only interpreter");
                    }
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    analyzer         = vs.InvokeTask(() => VsProjectAnalyzer.CreateForTestsAsync(vs.ComponentModel.GetService <PythonEditorServices>(), factory, inProcAnalyzer ?? Debugger.IsAttached));
                }
                if (string.IsNullOrEmpty(filename))
                {
                    do
                    {
                        filename = PathUtils.GetAbsoluteFilePath(TestData.GetTempPath(), Path.GetRandomFileName()) + ".py";
                    } while (File.Exists(filename));
                }

                var cancel = CancellationTokens.After60s;
                using (var mre = new ManualResetEventSlim()) {
                    view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "",
                                             v => {
                        v.TextView.TextBuffer.Properties[BufferParser.ParseImmediately]             = true;
                        v.TextView.TextBuffer.Properties[IntellisenseController.SuppressErrorLists] = IntellisenseController.SuppressErrorLists;
                        v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testAnalyzer]           = analyzer;
                        v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testFilename]           = filename;
                    },
                                             filename);

                    var entry = analyzer.GetAnalysisEntryFromPath(filename);
                    while (entry == null && !cancel.IsCancellationRequested)
                    {
                        Thread.Sleep(50);
                        entry = analyzer.GetAnalysisEntryFromPath(filename);
                    }

                    if (!string.IsNullOrEmpty(content) && !cancel.IsCancellationRequested && !entry.IsAnalyzed)
                    {
                        EventHandler evt = (s, e) => mre.SetIfNotDisposed();

                        try {
                            entry.AnalysisComplete += evt;
                            while (!mre.Wait(50, cancel) && !vs.HasPendingException && !entry.IsAnalyzed)
                            {
                                if (!analyzer.IsAnalyzing && !entry.IsAnalyzed)
                                {
                                    var bp = entry.TryGetBufferParser();
                                    Assert.IsNotNull(bp, "No buffer parser was ever created");
                                    var bi = PythonTextBufferInfo.TryGetForBuffer(view.TextView.TextBuffer);
                                    Assert.IsNotNull(bi, "No BufferInfo was ever created");
                                    bi.LastSentSnapshot = null;
                                    bp.EnsureCodeSyncedAsync(view.TextView.TextBuffer).WaitAndUnwrapExceptions();
                                }
                            }
                        } catch (OperationCanceledException) {
                        } finally {
                            entry.AnalysisComplete -= evt;
                        }
                    }
                    if (cancel.IsCancellationRequested)
                    {
                        Assert.Fail("Timed out waiting for code analysis");
                    }

                    vs.ThrowPendingException();
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }