Skip to content

Commit

Permalink
Update InlineDiagnosticsTaggerProvider to work for source generated d…
Browse files Browse the repository at this point in the history
…ocuments
  • Loading branch information
sharwell committed Feb 3, 2022
1 parent a7165bc commit 49170ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Editor.InlineDiagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.Extensions;
using Microsoft.CodeAnalysis.Editor.UnitTests.Squiggles;
Expand Down Expand Up @@ -32,12 +33,33 @@ public async Task ErrorTagGeneratedForError()
Assert.Equal(PredefinedErrorTypeNames.SyntaxError, firstSpan.Tag.ErrorType);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.ErrorSquiggles)]
public async Task ErrorTagGeneratedForErrorInSourceGeneratedDocument()
{
var spans = await GetTagSpansInSourceGeneratedDocumentAsync("class C {");
Assert.Equal(1, spans.Count());

var firstSpan = spans.First();
Assert.Equal(PredefinedErrorTypeNames.SyntaxError, firstSpan.Tag.ErrorType);
}

private static async Task<ImmutableArray<ITagSpan<InlineDiagnosticsTag>>> GetTagSpansAsync(string content)
{
using var workspace = TestWorkspace.CreateCSharp(content, composition: SquiggleUtilities.WpfCompositionWithSolutionCrawler);
return await GetTagSpansAsync(workspace);
}

private static async Task<ImmutableArray<ITagSpan<InlineDiagnosticsTag>>> GetTagSpansInSourceGeneratedDocumentAsync(string content)
{
var workspaceElement = $@"<Workspace>
<Project AssemblyName=""Test"" Language=""C#"" CommonReferences=""true"">
<DocumentFromSourceGenerator FilePath=""test1.cs"">{new XText(content)}</DocumentFromSourceGenerator>
</Project>
</Workspace>";
using var workspace = TestWorkspace.Create(workspaceElement, composition: SquiggleUtilities.WpfCompositionWithSolutionCrawler);
return await GetTagSpansAsync(workspace);
}

private static async Task<ImmutableArray<ITagSpan<InlineDiagnosticsTag>>> GetTagSpansAsync(TestWorkspace workspace)
{
workspace.ApplyOptions(new[] { KeyValuePairUtil.Create(new OptionKey2(InlineDiagnosticsOptions.EnableInlineDiagnostics, LanguageNames.CSharp), (object)true) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ diagnostic.Severity is DiagnosticSeverity.Warning or DiagnosticSeverity.Error &&
return null;
}

if (diagnostic.DocumentId is null)
if (diagnostic.DocumentId is not { ProjectId: var projectId })
{
return null;
}

var document = workspace.CurrentSolution.GetDocument(diagnostic.DocumentId);
if (document is null)
var project = workspace.CurrentSolution.GetProject(projectId);
if (project is null)
{
return null;
}

var locationOption = GlobalOptions.GetOption(InlineDiagnosticsOptions.Location, document.Project.Language);
var locationOption = GlobalOptions.GetOption(InlineDiagnosticsOptions.Location, project.Language);
var navigateService = workspace.Services.GetRequiredService<INavigateToLinkService>();
return new InlineDiagnosticsTag(errorType, diagnostic, _editorFormatMap, _classificationFormatMapService,
_classificationTypeRegistryService, locationOption, navigateService);
Expand Down

0 comments on commit 49170ce

Please sign in to comment.