Beispiel #1
0
        public static void Clear(this MockVsTextView view)
        {
            var snapshot = view.View.TextSnapshot;

            using (var edit = snapshot.TextBuffer.CreateEdit()) {
                edit.Delete(new Microsoft.VisualStudio.Text.Span(0, snapshot.Length));
                edit.Apply();
            }
        }
Beispiel #2
0
            public ClassifierHelper(string code, PythonLanguageVersion version)
            {
                _vs = new MockVs();

                var reg       = _vs.ContentTypeRegistry;
                var providers = _vs.ComponentModel.GetExtensions <IClassifierProvider>().ToArray();

                _provider1 = providers.OfType <PythonClassifierProvider>().Single();
                _provider2 = providers.OfType <PythonAnalysisClassifierProvider>().Single();

                var factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());

                _analyzer = new VsProjectAnalyzer(_vs.ServiceProvider, factory, new[] { factory });

                _view = _vs.CreateTextView(PythonCoreConstants.ContentType, code, v => {
                    v.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), _analyzer);
                });
            }
Beispiel #3
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();
                }
            }
        }
Beispiel #4
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();
                }
            }
        }
Beispiel #5
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();
                }
            }
        }
Beispiel #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);
                    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();
                }
            }
        }
Beispiel #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();
                }
            }
        }
Beispiel #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();
                }
            }
        }