Пример #1
0
        private async Task <TestWorkspace> SetupWorkspaceAsync(params string[] files)
        {
            var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFilesAsync(files, parseOptions : Options.Script);

            var aggregateListener = AggregateAsynchronousOperationListener.CreateEmptyListener();

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

            return(workspace);
        }
        public async Task TestWithMockDiagnosticService_TaggerProviderCreatedBeforeInitialDiagnosticsReported()
        {
            // This test produces diagnostics from a mock service so that we are disconnected from
            // all teh asynchrony of hte actual async analyzer engine.  If this fails, then the
            // issue is almost certainly in the DiagnosticsSquiggleTaggerProvider code.  If this
            // succeed, but other squiggle tests fail, then it is likely an issue with the
            // diagnostics engine not actually reporting all diagnostics properly.

            using (var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFilesAsync(new string[] { "class A { }" }, CSharpParseOptions.Default))
                using (var wrapper = new DiagnosticTaggerWrapper(workspace))
                {
                    var asyncListener = new AsynchronousOperationListener();
                    var listeners     = AsynchronousOperationListener.CreateListeners(
                        ValueTuple.Create(FeatureAttribute.DiagnosticService, asyncListener),
                        ValueTuple.Create(FeatureAttribute.ErrorSquiggles, asyncListener));

                    var diagnosticService = new MockDiagnosticService(workspace);
                    var provider          = new DiagnosticsSquiggleTaggerProvider(
                        workspace.Services.GetService <IOptionService>(), diagnosticService,
                        workspace.GetService <IForegroundNotificationService>(), listeners);

                    // Create the tagger before the first diagnostic event has been fired.
                    var tagger = provider.CreateTagger <IErrorTag>(workspace.Documents.First().GetTextBuffer());

                    // Now product hte first diagnostic and fire the events.
                    var tree = await workspace.CurrentSolution.Projects.Single().Documents.Single().GetSyntaxTreeAsync();

                    var span = TextSpan.FromBounds(0, 5);
                    diagnosticService.CreateDiagnosticAndFireEvents(Location.Create(tree, span));

                    using (var disposable = tagger as IDisposable)
                    {
                        await asyncListener.CreateWaitTask();

                        var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        var spans    = tagger.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.Equal(1, spans.Count);
                        Assert.Equal(span.ToSpan(), spans[0].Span.Span);
                    }
                }
        }
        public async Task MultipleTaggersAndDispose()
        {
            using (var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFilesAsync(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)
                    {
                        await wrapper.WaitForTags();

                        var snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        var spans    = tagger2.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.False(spans.IsEmpty());
                    }
                }
        }
Пример #4
0
        public async Task Test_TagSourceDiffer()
        {
            var analyzer    = new Analyzer();
            var analyzerMap = new Dictionary <string, DiagnosticAnalyzer[]>
            {
                { LanguageNames.CSharp, new DiagnosticAnalyzer[] { analyzer } }
            };

            using (var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFilesAsync(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
                        await 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     = await document.GetTextAsync();

                        workspace.TryApplyChanges(document.WithText(text.WithChanges(new TextChange(new TextSpan(text.Length - 1, 1), string.Empty))).Project.Solution);

                        await wrapper.WaitForTags();

                        snapshot = workspace.Documents.First().GetTextBuffer().CurrentSnapshot;
                        spans    = tagger.GetTags(snapshot.GetSnapshotSpanCollection()).ToList();
                        Assert.True(spans.First().Span.Contains(new Span(0, 1)));
                    }
                }
        }
        public async Task TaggerProviderCreatedAfterInitialDiagnosticsReported()
        {
            using (var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFilesAsync(new string[] { "class C {" }, CSharpParseOptions.Default))
                using (var wrapper = new DiagnosticTaggerWrapper(workspace, analyzerMap: null, createTaggerProvider: false))
                {
                    // First, make sure all diagnostics have been reported.
                    await 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)
                    {
                        await 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());
                    }
                }
        }