Exemplo n.º 1
0
        public void Test_TagSourceDiffer()
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFiles(new string[] { "class A { }", "class E { }" }, CSharpParseOptions.Default))
            {
                var registrationService = workspace.Services.GetService <ISolutionCrawlerRegistrationService>();
                registrationService.Register(workspace);

                var analyzer        = new Analyzer();
                var analyzerService = new TestDiagnosticAnalyzerService(
                    new Dictionary <string, ImmutableArray <DiagnosticAnalyzer> >()
                {
                    { LanguageNames.CSharp, ImmutableArray.Create <DiagnosticAnalyzer>(analyzer) }
                }.ToImmutableDictionary());

                var listener  = new AsynchronousOperationListener();
                var listeners = AsynchronousOperationListener.CreateListeners(
                    ValueTuple.Create(FeatureAttribute.DiagnosticService, listener),
                    ValueTuple.Create(FeatureAttribute.ErrorSquiggles, listener));

                var diagnosticService = new DiagnosticService(SpecializedCollections.SingletonEnumerable <IDiagnosticUpdateSource>(analyzerService), listeners);

                var provider = new DiagnosticsSquiggleTaggerProvider(
                    workspace.Services.GetService <IOptionService>(), diagnosticService,
                    workspace.GetService <IForegroundNotificationService>(), listeners);
                var tagger = provider.CreateTagger <IErrorTag>(workspace.Documents.First().GetTextBuffer());
                using (var disposable = tagger as IDisposable)
                {
                    var service = workspace.Services.GetService <ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
                    var incrementalAnalyzers = ImmutableArray.Create(analyzerService.CreateIncrementalAnalyzer(workspace));

                    // test first update
                    service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);

                    listener.CreateWaitTask().PumpingWait();

                    var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                    var spans    = tagger.GetTags(new NormalizedSnapshotSpanCollection(new SnapshotSpan(snapshot, 0, snapshot.Length))).ToList();
                    Assert.True(spans.First().Span.Contains(new Span(0, 1)));

                    // test second update
                    analyzer.ChangeSeverity();

                    var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
                    var text     = document.GetTextAsync().Result;
                    workspace.TryApplyChanges(document.WithText(text.WithChanges(new TextChange(new TextSpan(text.Length - 1, 1), string.Empty))).Project.Solution);

                    service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);

                    listener.CreateWaitTask().PumpingWait();

                    snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                    spans    = tagger.GetTags(new NormalizedSnapshotSpanCollection(new SnapshotSpan(snapshot, 0, snapshot.Length))).ToList();
                    Assert.True(spans.First().Span.Contains(new Span(0, 1)));

                    registrationService.Unregister(workspace);
                }
            }
        }
Exemplo n.º 2
0
        private TestWorkspace SetupWorkspace(params string[] files)
        {
            var workspace         = CSharpWorkspaceFactory.CreateWorkspaceFromFiles(files, parseOptions: Options.Script);
            var aggregateListener = AggregateAsynchronousOperationListener.CreateEmptyListener();

            _provider = new NavigateToItemProvider(
                workspace,
                _glyphServiceMock.Object,
                aggregateListener);
            _aggregator = new NavigateToTestAggregator(_provider);

            return(workspace);
        }
        public void Test_TagSourceDiffer()
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFiles(new string[] { "class A { }", "class E { }" }, CSharpParseOptions.Default))
            {
                var registrationService = workspace.Services.GetService <ISolutionCrawlerRegistrationService>();
                registrationService.Register(workspace);

                var diagnosticWaiter = new DiagnosticServiceWaiter();
                var squiggleWaiter   = new ErrorSquiggleWaiter();

                Analyzer analyzer;
                DiagnosticAnalyzerService analyzerService;
                DiagnosticsSquiggleTaggerProvider.TagSource taggerSource;
                GetTagSource(workspace, diagnosticWaiter, squiggleWaiter, out analyzer, out analyzerService, out taggerSource);

                taggerSource.TagsChangedForBuffer += (o, arg) =>
                {
                    Assert.True(arg.Spans.First().Span.Contains(new Span(0, 1)));
                };

                var service = workspace.Services.GetService <ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
                var incrementalAnalyzers = ImmutableArray.Create(analyzerService.CreateIncrementalAnalyzer(workspace));

                // test first update
                service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);

                diagnosticWaiter.CreateWaitTask().PumpingWait();
                squiggleWaiter.CreateWaitTask().PumpingWait();

                // test second update
                analyzer.ChangeSeverity();

                var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
                var text     = document.GetTextAsync().Result;
                workspace.TryApplyChanges(document.WithText(text.WithChanges(new TextChange(new TextSpan(text.Length - 1, 1), string.Empty))).Project.Solution);

                service.WaitUntilCompletion_ForTestingPurposesOnly(workspace, incrementalAnalyzers);

                diagnosticWaiter.CreateWaitTask().PumpingWait();
                squiggleWaiter.CreateWaitTask().PumpingWait();

                taggerSource.TestOnly_Dispose();

                registrationService.Unregister(workspace);
            }
        }
        public void MultipleTaggersAndDispose()
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFiles(new string[] { "class A {" }, CSharpParseOptions.Default))
                using (var wrapper = new DiagnosticTaggerWrapper(workspace))
                {
                    // Make two taggers.
                    var tagger1 = wrapper.TaggerProvider.CreateTagger <IErrorTag>(workspace.Documents.First().GetTextBuffer());
                    var tagger2 = wrapper.TaggerProvider.CreateTagger <IErrorTag>(workspace.Documents.First().GetTextBuffer());

                    // But dispose the first one. We still want the second one to work.
                    ((IDisposable)tagger1).Dispose();

                    using (var disposable = tagger2 as IDisposable)
                    {
                        wrapper.WaitForTags();

                        var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        var spans    = tagger2.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.False(spans.IsEmpty());
                    }
                }
        }
        public void Test_TagSourceDiffer()
        {
            var analyzer    = new Analyzer();
            var analyzerMap = new Dictionary <string, DiagnosticAnalyzer[]>
            {
                { LanguageNames.CSharp, new DiagnosticAnalyzer[] { analyzer } }
            };

            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFiles(new string[] { "class A { }", "class E { }" }, CSharpParseOptions.Default))
                using (var wrapper = new DiagnosticTaggerWrapper(workspace, analyzerMap))
                {
                    var tagger = wrapper.TaggerProvider.CreateTagger <IErrorTag>(workspace.Documents.First().GetTextBuffer());
                    using (var disposable = tagger as IDisposable)
                    {
                        // test first update
                        wrapper.WaitForTags();

                        var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        var spans    = tagger.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.True(spans.First().Span.Contains(new Span(0, 1)));

                        // test second update
                        analyzer.ChangeSeverity();

                        var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
                        var text     = document.GetTextAsync().Result;
                        workspace.TryApplyChanges(document.WithText(text.WithChanges(new TextChange(new TextSpan(text.Length - 1, 1), string.Empty))).Project.Solution);

                        wrapper.WaitForTags();

                        snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        spans    = tagger.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.True(spans.First().Span.Contains(new Span(0, 1)));
                    }
                }
        }
Exemplo n.º 6
0
        public void TaggerProviderCreatedAfterInitialDiagnosticsReported()
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFiles(new string[] { "class C {" }, CSharpParseOptions.Default))
                using (var wrapper = new DiagnosticTaggerWrapper(workspace, analyzerMap: null, createTaggerProvider: false))
                {
                    // First, make sure all diagnostics have been reported.
                    wrapper.WaitForTags();

                    // Now make the tagger.
                    var taggerProvider = wrapper.TaggerProvider;

                    // Make a taggers.
                    var tagger1 = wrapper.TaggerProvider.CreateTagger <IErrorTag>(workspace.Documents.First().GetTextBuffer());
                    using (var disposable = tagger1 as IDisposable)
                    {
                        wrapper.WaitForTags();

                        // We should have tags at this point.
                        var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        var spans    = tagger1.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.False(spans.IsEmpty());
                    }
                }
        }