Skip to content

Commit 5c69a9f

Browse files
author
Andrew Hall
authored
Add project information to razor diagnostics (#9595)
1 parent 9d67a98 commit 5c69a9f

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/DocumentPullDiagnosticsEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno
126126
return null;
127127
}
128128

129-
var convertedDiagnostics = RazorDiagnosticConverter.Convert(diagnostics, sourceText);
129+
var convertedDiagnostics = RazorDiagnosticConverter.Convert(diagnostics, sourceText, documentContext.Snapshot);
130130

131131
return
132132
[

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticConverter.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
using System.Collections.Generic;
66
using System.Globalization;
77
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
89
using Microsoft.CodeAnalysis.Text;
910
using Microsoft.VisualStudio.LanguageServer.Protocol;
1011

1112
namespace Microsoft.AspNetCore.Razor.LanguageServer.Diagnostics;
1213

1314
internal static class RazorDiagnosticConverter
1415
{
15-
public static Diagnostic Convert(RazorDiagnostic razorDiagnostic, SourceText sourceText)
16+
public static VSDiagnostic Convert(RazorDiagnostic razorDiagnostic, SourceText sourceText, IDocumentSnapshot? documentSnapshot)
1617
{
1718
if (razorDiagnostic is null)
1819
{
@@ -24,7 +25,18 @@ public static Diagnostic Convert(RazorDiagnostic razorDiagnostic, SourceText sou
2425
throw new ArgumentNullException(nameof(sourceText));
2526
}
2627

27-
var diagnostic = new Diagnostic()
28+
var projects = documentSnapshot is null
29+
? Array.Empty<VSDiagnosticProjectInformation>()
30+
: [
31+
new VSDiagnosticProjectInformation()
32+
{
33+
Context = null,
34+
ProjectIdentifier = documentSnapshot.Project.Key.Id,
35+
ProjectName = documentSnapshot.Project.DisplayName
36+
}
37+
];
38+
39+
var diagnostic = new VSDiagnostic()
2840
{
2941
Message = razorDiagnostic.GetMessage(CultureInfo.InvariantCulture),
3042
Code = razorDiagnostic.Id,
@@ -33,19 +45,20 @@ public static Diagnostic Convert(RazorDiagnostic razorDiagnostic, SourceText sou
3345
// This is annotated as not null, but we have tests that validate the behaviour when
3446
// we pass in null here
3547
Range = ConvertSpanToRange(razorDiagnostic.Span, sourceText)!,
48+
Projects = projects
3649
};
3750

3851
return diagnostic;
3952
}
4053

41-
internal static Diagnostic[] Convert(IReadOnlyList<RazorDiagnostic> diagnostics, SourceText sourceText)
54+
internal static Diagnostic[] Convert(IReadOnlyList<RazorDiagnostic> diagnostics, SourceText sourceText, IDocumentSnapshot documentSnapshot)
4255
{
4356
var convertedDiagnostics = new Diagnostic[diagnostics.Count];
4457

4558
var i = 0;
4659
foreach (var diagnostic in diagnostics)
4760
{
48-
convertedDiagnostics[i++] = Convert(diagnostic, sourceText);
61+
convertedDiagnostics[i++] = Convert(diagnostic, sourceText, documentSnapshot);
4962
}
5063

5164
return convertedDiagnostics;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fullDiagnostics.Items is not null &&
264264
return;
265265
}
266266

267-
var convertedDiagnostics = razorDiagnostics.Select(razorDiagnostic => RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText));
267+
var convertedDiagnostics = razorDiagnostics.Select(razorDiagnostic => RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText, document));
268268
var combinedDiagnostics = csharpDiagnostics == null ? convertedDiagnostics : convertedDiagnostics.Concat(csharpDiagnostics);
269269
PublishDiagnosticsForFilePath(document.FilePath, combinedDiagnostics);
270270

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticConverterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void Convert_Converts()
2222
var sourceText = SourceText.From(string.Empty);
2323

2424
// Act
25-
var diagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);
25+
var diagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText, documentSnapshot: null);
2626

2727
// Assert
2828
Assert.Equal(razorDiagnostic.Id, diagnostic.Code);

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Linq;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Razor.Language;
@@ -193,10 +194,17 @@ public async Task PublishDiagnosticsAsync_NewDocumentDiagnosticsGetPublished(boo
193194
var resultRazorDiagnostic = @params.Diagnostics[0];
194195
var razorDiagnostic = s_singleRazorDiagnostic[0];
195196
Assert.True(processedOpenDocument.TryGetText(out var sourceText));
196-
var expectedRazorDiagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);
197+
var expectedRazorDiagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText, _openedDocument);
197198
Assert.Equal(expectedRazorDiagnostic.Message, resultRazorDiagnostic.Message);
198199
Assert.Equal(expectedRazorDiagnostic.Severity, resultRazorDiagnostic.Severity);
199200
Assert.Equal(expectedRazorDiagnostic.Range, resultRazorDiagnostic.Range);
201+
Assert.NotNull(expectedRazorDiagnostic.Projects);
202+
Assert.Single(expectedRazorDiagnostic.Projects);
203+
204+
var project = expectedRazorDiagnostic.Projects.Single();
205+
Assert.Equal(_openedDocument.Project.DisplayName, project.ProjectName);
206+
Assert.Equal(_openedDocument.Project.Key.Id, project.ProjectIdentifier);
207+
200208
}
201209
})
202210
.Returns(Task.CompletedTask);
@@ -247,10 +255,15 @@ public async Task PublishDiagnosticsAsync_NewRazorDiagnosticsGetPublished()
247255
var diagnostic = Assert.Single(@params.Diagnostics);
248256
var razorDiagnostic = s_singleRazorDiagnostic[0];
249257
Assert.True(processedOpenDocument.TryGetText(out var sourceText));
250-
var expectedDiagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);
258+
var expectedDiagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText, _openedDocument);
251259
Assert.Equal(expectedDiagnostic.Message, diagnostic.Message);
252260
Assert.Equal(expectedDiagnostic.Severity, diagnostic.Severity);
253261
Assert.Equal(expectedDiagnostic.Range, diagnostic.Range);
262+
263+
Assert.NotNull(expectedDiagnostic.Projects);
264+
var project = expectedDiagnostic.Projects.Single();
265+
Assert.Equal(_openedDocument.Project.DisplayName, project.ProjectName);
266+
Assert.Equal(_openedDocument.Project.Key.Id, project.ProjectIdentifier);
254267
})
255268
.Returns(Task.CompletedTask);
256269

0 commit comments

Comments
 (0)