Exemplo n.º 1
0
        private static async Task <IEnumerable <TrackingTagSpan <ErrorTag> > > AnalyzeTextBufferAsync(
            MockTextBuffer buffer,
            PythonLanguageVersion version = PythonLanguageVersion.V27
            )
        {
            var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());

            try {
                var serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
                var errorProvider   = serviceProvider.ComponentModel.GetService <IErrorProviderFactory>();
                Assert.IsNotNull(errorProvider, "Error provider factory is not available");
                var analyzer = new VsProjectAnalyzer(serviceProvider, fact, new[] { fact });
                buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                var classifierProvider = new PythonClassifierProvider(new MockContentTypeRegistryService(PythonCoreConstants.ContentType), serviceProvider);
                classifierProvider._classificationRegistry = new MockClassificationTypeRegistryService();
                classifierProvider.GetClassifier(buffer);
                var squiggles       = errorProvider.GetErrorTagger(buffer);
                var textView        = new MockTextView(buffer);
                var monitoredBuffer = analyzer.MonitorTextBuffer(textView, buffer);

                var tcs = new TaskCompletionSource <object>();
                buffer.GetPythonProjectEntry().OnNewAnalysis += (s, e) => tcs.SetResult(null);
                await tcs.Task;

                var snapshot = buffer.CurrentSnapshot;

                // Ensure all tasks have been updated
                var taskProvider = (ErrorTaskProvider)serviceProvider.GetService(typeof(ErrorTaskProvider));
                var time         = await taskProvider.FlushAsync();

                Console.WriteLine("TaskProvider.FlushAsync took {0}ms", time.TotalMilliseconds);

                var spans = squiggles.GetTaggedSpans(new SnapshotSpan(snapshot, 0, snapshot.Length));

                analyzer.StopMonitoringTextBuffer(monitoredBuffer.BufferParser, textView);

                return(spans);
            } finally {
            }
        }
Exemplo n.º 2
0
        private static ISmartTagSession GetSmartTagSession(
            MockTextBuffer buffer,
            int index,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            bool assertIfNoSession        = true
            )
        {
            if (index < 0)
            {
                index += buffer.CurrentSnapshot.Length;
            }

            var fact     = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var sp       = PythonToolsTestUtilities.CreateMockServiceProvider();
            var analyzer = new VsProjectAnalyzer(sp, fact, new[] { fact });

            buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer);

            var classifierProvider = new PythonClassifierProvider(
                new MockContentTypeRegistryService(PythonCoreConstants.ContentType),
                sp
                );

            classifierProvider._classificationRegistry = new MockClassificationTypeRegistryService();
            classifierProvider.GetClassifier(buffer);

            var view            = new MockTextView(buffer);
            var monitoredBuffer = analyzer.MonitorTextBuffer(view, buffer);

            analyzer.WaitForCompleteAnalysis(x => true);
            while (((IPythonProjectEntry)buffer.GetProjectEntry()).Analysis == null)
            {
                System.Threading.Thread.Sleep(500);
            }
            analyzer.StopMonitoringTextBuffer(monitoredBuffer.BufferParser, view);

            view.TextViewModel = new MockTextViewModel {
                DataBuffer = buffer, EditBuffer = buffer
            };
            var snapshot = buffer.CurrentSnapshot;
            var broker   = new MockSmartTagBroker();

            broker.SourceProviders.Add(new SmartTagSourceProvider(sp));

            var cont = new SmartTagController(broker, view);

            view.Caret.MoveTo(new SnapshotPoint(snapshot, index));
            cont.ShowSmartTag();

            var session = broker.GetSessions(view).FirstOrDefault();

            Assert.IsNull(session, "Session should not be active");

            var task = Volatile.Read(ref cont._curTask);

            if (assertIfNoSession)
            {
                Assert.IsNotNull(task, "Session should have task running");
            }
            else if (task == null)
            {
                // No session and no task means we won't ever get a session here
                return(null);
            }

            // Simulate repeatedly starting a session like the idle loop would
            for (int retries = 1000; session == null && retries > 0; --retries)
            {
                Thread.Sleep(10);
                cont.ShowSmartTag();

                session = broker.GetSessions(view).FirstOrDefault();
            }

            if (assertIfNoSession)
            {
                Assert.IsNotNull(session, "No session is active");
                Assert.IsFalse(session.IsDismissed, "Session should not be dismissed");
            }

            if (session != null)
            {
                Console.WriteLine("Found session with following tags:");
                foreach (var t in session.ActionSets.SelectMany(s => s.Actions).Select(s => s.DisplayText))
                {
                    Console.WriteLine("  {0}", t);
                }
            }

            return(session);
        }