Example #1
0
        public void TypeAndWaitForAnalysis(string text)
        {
            using (var mre = new ManualResetEventSlim()) {
                EventHandler evt = (s, e) => mre.SetIfNotDisposed();
                Analyzer.AnalysisStarted += evt;

                Type(text);

                var cts = CancellationTokens.After60s;
                try {
                    while (!mre.Wait(500, cts) && !VS.HasPendingException)
                    {
                    }
                    Analyzer.WaitForCompleteAnalysis(x => !cts.IsCancellationRequested && !VS.HasPendingException);
                } catch (OperationCanceledException) {
                } finally {
                    Analyzer.AnalysisStarted -= evt;
                }
                if (cts.IsCancellationRequested)
                {
                    Assert.Fail("Timed out waiting for code analysis");
                }
                VS.ThrowPendingException();
            }
        }
Example #2
0
            public ClassifierHelper(string code, PythonLanguageVersion version)
            {
                _view = new PythonEditor("", version);

                var providers = _view.VS.ComponentModel.GetExtensions <IClassifierProvider>().ToArray();

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

                _classificationsReady1 = new ManualResetEventSlim();
                _classificationsReady2 = new ManualResetEventSlim();

                AstClassifier.ClassificationChanged += (s, e) => _classificationsReady1.SetIfNotDisposed();
                var startVersion = _view.CurrentSnapshot.Version;

                AnalysisClassifier.ClassificationChanged += (s, e) => {
                    try {
                        var bi = PythonTextBufferInfo.TryGetForBuffer(_view.View.TextView.TextBuffer);
                        if (bi?.LastAnalysisReceivedVersion == null)
                        {
                            return;
                        }
                        // make sure we have classifications from the version we analyzed after
                        // setting the text below.
                        if (bi.LastAnalysisReceivedVersion.VersionNumber > startVersion.VersionNumber)
                        {
                            _classificationsReady2.SetIfNotDisposed();
                        }
                    } catch (Exception ex) {
                        _excInfo = ExceptionDispatchInfo.Capture(ex);
                    }
                };

                _view.Text = code;
            }
Example #3
0
        public void Type(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            using (var mre = new ManualResetEventSlim()) {
                EventHandler <TextContentChangedEventArgs> evt = (s, e) => mre.SetIfNotDisposed();
                _view.TextBuffer.ChangedLowPriority += evt;
                _commandTarget.Type(text);
                Assert.IsTrue(mre.Wait(1000), "No change event seen");
                _view.TextBuffer.ChangedLowPriority -= evt;
            }
        }
Example #4
0
            public ClassifierHelper(string code, PythonLanguageVersion version)
            {
                _view = new PythonEditor("", version);

                var providers = _view.VS.ComponentModel.GetExtensions <IClassifierProvider>().ToArray();

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

                _classificationsReady1 = new ManualResetEventSlim();
                _classificationsReady2 = new ManualResetEventSlim();

                AstClassifier.ClassificationChanged      += (s, e) => _classificationsReady1.SetIfNotDisposed();
                AnalysisClassifier.ClassificationChanged += (s, e) => _classificationsReady2.SetIfNotDisposed();

                _view.Text = code;
            }
Example #5
0
            public void WaitForSessionDismissed(TimeSpan?timeout = null)
            {
                if (Session.IsDismissed)
                {
                    return;
                }

                using (var evt = new ManualResetEventSlim()) {
                    Session.Dismissed += (s, e) => evt.SetIfNotDisposed();
                    if (Session.IsDismissed)
                    {
                        evt.Set();
                    }

                    Assert.IsTrue(
                        evt.Wait(timeout ?? TimeSpan.FromSeconds(10)),
                        string.Format("Timeout waiting for {0} to dismiss", typeof(T).Name)
                        );
                }
            }
Example #6
0
        private void WaitForAnyLineContainsTextInternal(string expected, TimeSpan?timeout = null)
        {
            using (var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(15)))
                using (var changed = new ManualResetEventSlim()) {
                    EventHandler <TextContentChangedEventArgs> handler = (s, e) => changed.SetIfNotDisposed();
                    Window.TextView.TextBuffer.Changed += handler;
                    try {
                        while (!MatchAnyLineContainsTextInternal(expected, true))
                        {
                            changed.Wait(cts.Token);
                            changed.Reset();
                        }
                        return;
                    } catch (OperationCanceledException) {
                    } finally {
                        Window.TextView.TextBuffer.Changed -= handler;
                    }
                }

            Assert.Fail("Failed to find a line that contains the following text:\n{0}", expected);
        }
Example #7
0
        private void WaitForTextInternal(IList <string> expected, bool matchAtStart, bool matchAtEnd, TimeSpan?timeout = null)
        {
            using (var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(15)))
                using (var changed = new ManualResetEventSlim()) {
                    EventHandler <TextContentChangedEventArgs> handler = (s, e) => changed.SetIfNotDisposed();
                    Window.TextView.TextBuffer.Changed += handler;
                    try {
                        while (!MatchTextInternal(expected, matchAtStart, matchAtEnd, true))
                        {
                            changed.Wait(cts.Token);
                            changed.Reset();
                        }
                        return;
                    } catch (OperationCanceledException) {
                    } finally {
                        Window.TextView.TextBuffer.Changed -= handler;
                    }
                }

            MatchTextInternal(expected, matchAtStart, matchAtEnd, true);
            Assert.Fail("Failed to match text. See Output for details.");
        }
Example #8
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();
                }
            }
        }
Example #9
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.ServiceProvider, factory);
                    });
                    var task = analyzer.ReloadTask;
                    if (task != null)
                    {
                        task.WaitAndUnwrapExceptions();
                    }
                }

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

                    try {
                        while (!mre.Wait(500, cts.Token) && !vs.HasPendingException)
                        {
                        }
                        analyzer.WaitForCompleteAnalysis(x => !cts.IsCancellationRequested && !vs.HasPendingException);
                    } catch (OperationCanceledException) {
                    } finally {
                        analyzer.AnalysisStarted -= evt;
                    }
                    if (cts.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();
                }
            }
        }
Example #10
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();
                }
            }
        }