Skip to content

Commit

Permalink
Merge pull request #9151 from davidwengier/MapDiagnosticsOnServer
Browse files Browse the repository at this point in the history
Remap and translate diagnostics on the server
  • Loading branch information
davidwengier authored Aug 23, 2023
2 parents 004a706 + 3674ad0 commit a3213d6
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static class CustomMessageNames
public const string RazorUriPresentationEndpoint = "razor/uriPresentation";
public const string RazorSpellCheckEndpoint = "razor/spellCheck";
public const string RazorProjectContextsEndpoint = "razor/projectContexts";
public const string RazorPullDiagnosticEndpointName = "razor/pullDiagnostics";

// VS Windows and VS Code
public const string RazorUpdateCSharpBufferEndpoint = "razor/updateCSharpBuffer";
Expand All @@ -29,7 +30,6 @@ internal static class CustomMessageNames
public const string RazorResolveCodeActionsEndpoint = "razor/resolveCodeActions";
public const string RazorProvideHtmlColorPresentationEndpoint = "razor/provideHtmlColorPresentation";
public const string RazorProvideHtmlDocumentColorEndpoint = "razor/provideHtmlDocumentColor";
public const string RazorPullDiagnosticEndpointName = "razor/pullDiagnostics";
public const string RazorProvideSemanticTokensRangeEndpoint = "razor/provideSemanticTokensRange";
public const string RazorFoldingRangeEndpoint = "razor/foldingRange";
public const string RazorHtmlFormattingEndpoint = "razor/htmlFormatting";
Expand All @@ -53,4 +53,7 @@ internal static class CustomMessageNames
public const string RazorReferencesEndpointName = "razor/references";

public const string RazorSimplifyMethodEndpointName = "razor/simplifyMethod";

// Called to get C# diagnostics from Roslyn when publishing diagnostics for VS Code
public const string RazorCSharpPullDiagnosticsEndpointName = "razor/csharpPullDiagnostics";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class ConfigurableLanguageServerFeatureOptions : LanguageServerFeatureO
private readonly bool? _singleServerCompletionSupport;
private readonly bool? _singleServerSupport;
private readonly bool? _supportsDelegatedCodeActions;
private readonly bool? _supportsDelegatedDiagnostics;
private readonly bool? _delegateToCSharpOnDiagnosticPublish;
private readonly bool? _returnCodeActionAndRenamePathsWithPrefixedSlash;
private readonly bool? _showAllCSharpCodeActions;
private readonly bool? _updateBuffersForClosedDocuments;
Expand All @@ -29,7 +29,7 @@ internal class ConfigurableLanguageServerFeatureOptions : LanguageServerFeatureO
public override bool SingleServerCompletionSupport => _singleServerCompletionSupport ?? _defaults.SingleServerCompletionSupport;
public override bool SingleServerSupport => _singleServerSupport ?? _defaults.SingleServerSupport;
public override bool SupportsDelegatedCodeActions => _supportsDelegatedCodeActions ?? _defaults.SupportsDelegatedCodeActions;
public override bool SupportsDelegatedDiagnostics => _supportsDelegatedDiagnostics ?? _defaults.SupportsDelegatedDiagnostics;
public override bool DelegateToCSharpOnDiagnosticPublish => _delegateToCSharpOnDiagnosticPublish ?? _defaults.DelegateToCSharpOnDiagnosticPublish;
public override bool ReturnCodeActionAndRenamePathsWithPrefixedSlash => _returnCodeActionAndRenamePathsWithPrefixedSlash ?? _defaults.ReturnCodeActionAndRenamePathsWithPrefixedSlash;
public override bool ShowAllCSharpCodeActions => _showAllCSharpCodeActions ?? _defaults.ShowAllCSharpCodeActions;
public override bool UpdateBuffersForClosedDocuments => _updateBuffersForClosedDocuments ?? _defaults.UpdateBuffersForClosedDocuments;
Expand All @@ -50,7 +50,7 @@ public ConfigurableLanguageServerFeatureOptions(string[] args)
TryProcessBoolOption(nameof(SingleServerCompletionSupport), ref _singleServerCompletionSupport, option, args, i);
TryProcessBoolOption(nameof(SingleServerSupport), ref _singleServerSupport, option, args, i);
TryProcessBoolOption(nameof(SupportsDelegatedCodeActions), ref _supportsDelegatedCodeActions, option, args, i);
TryProcessBoolOption(nameof(SupportsDelegatedDiagnostics), ref _supportsDelegatedDiagnostics, option, args, i);
TryProcessBoolOption(nameof(DelegateToCSharpOnDiagnosticPublish), ref _delegateToCSharpOnDiagnosticPublish, option, args, i);
TryProcessBoolOption(nameof(ReturnCodeActionAndRenamePathsWithPrefixedSlash), ref _returnCodeActionAndRenamePathsWithPrefixedSlash, option, args, i);
TryProcessBoolOption(nameof(ShowAllCSharpCodeActions), ref _showAllCSharpCodeActions, option, args, i);
TryProcessBoolOption(nameof(UpdateBuffersForClosedDocuments), ref _updateBuffersForClosedDocuments, option, args, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class DefaultLanguageServerFeatureOptions : LanguageServerFeatureOption

public override bool SupportsDelegatedCodeActions => false;

public override bool SupportsDelegatedDiagnostics => false;
public override bool DelegateToCSharpOnDiagnosticPublish => false;

public override bool UpdateBuffersForClosedDocuments => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ internal class RazorDiagnosticsPublisher : DocumentProcessedListener
private readonly ILogger<RazorDiagnosticsPublisher> _logger;
private ProjectSnapshotManager? _projectManager;
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions;
private readonly Lazy<RazorTranslateDiagnosticsService> _razorTranslateDiagnosticsService;
private readonly Lazy<DocumentContextFactory> _documentContextFactory;

public RazorDiagnosticsPublisher(
ProjectSnapshotManagerDispatcher projectSnapshotManagerDispatcher,
ClientNotifierServiceBase languageServer,
LanguageServerFeatureOptions languageServerFeatureOptions,
Lazy<RazorTranslateDiagnosticsService> razorTranslateDiagnosticsService,
Lazy<DocumentContextFactory> documentContextFactory,
ILoggerFactory loggerFactory)
{
if (projectSnapshotManagerDispatcher is null)
Expand All @@ -57,6 +61,16 @@ public RazorDiagnosticsPublisher(
throw new ArgumentNullException(nameof(languageServerFeatureOptions));
}

if (razorTranslateDiagnosticsService is null)
{
throw new ArgumentNullException(nameof(razorTranslateDiagnosticsService));
}

if (documentContextFactory is null)
{
throw new ArgumentNullException(nameof(documentContextFactory));
}

if (loggerFactory is null)
{
throw new ArgumentNullException(nameof(loggerFactory));
Expand All @@ -65,6 +79,8 @@ public RazorDiagnosticsPublisher(
_projectSnapshotManagerDispatcher = projectSnapshotManagerDispatcher;
_languageServer = languageServer;
_languageServerFeatureOptions = languageServerFeatureOptions;
_razorTranslateDiagnosticsService = razorTranslateDiagnosticsService;
_documentContextFactory = documentContextFactory;
PublishedRazorDiagnostics = new Dictionary<string, IReadOnlyList<RazorDiagnostic>>(FilePathComparer.Instance);
PublishedCSharpDiagnostics = new Dictionary<string, IReadOnlyList<Diagnostic>>(FilePathComparer.Instance);
_work = new Dictionary<string, IDocumentSnapshot>(FilePathComparer.Instance);
Expand Down Expand Up @@ -193,8 +209,8 @@ internal async Task PublishDiagnosticsAsync(IDocumentSnapshot document)
{
var result = await document.GetGeneratedOutputAsync().ConfigureAwait(false);

SumType<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>? delegatedResponse = null;
if (_languageServerFeatureOptions.SupportsDelegatedDiagnostics)
Diagnostic[]? csharpDiagnostics = null;
if (_languageServerFeatureOptions.DelegateToCSharpOnDiagnosticPublish)
{
var uriBuilder = new UriBuilder()
{
Expand All @@ -208,23 +224,26 @@ internal async Task PublishDiagnosticsAsync(IDocumentSnapshot document)
TextDocument = new TextDocumentIdentifier { Uri = uriBuilder.Uri },
};

delegatedResponse = await _languageServer.SendRequestAsync<DocumentDiagnosticParams, SumType<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>?>(
CustomMessageNames.RazorPullDiagnosticEndpointName,
var delegatedResponse = await _languageServer.SendRequestAsync<DocumentDiagnosticParams, SumType<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>?>(
CustomMessageNames.RazorCSharpPullDiagnosticsEndpointName,
delegatedParams,
CancellationToken.None).ConfigureAwait(false);

if (delegatedResponse.HasValue &&
delegatedResponse.Value.TryGetFirst(out var fullDiagnostics) &&
fullDiagnostics.Items is not null &&
_documentContextFactory.Value.TryCreate(delegatedParams.TextDocument.Uri, projectContext: null) is { } documentContext)
{
csharpDiagnostics = await _razorTranslateDiagnosticsService.Value.TranslateAsync(Protocol.RazorLanguageKind.CSharp, fullDiagnostics.Items, documentContext, CancellationToken.None).ConfigureAwait(false);
}
}

var razorDiagnostics = result.GetCSharpDocument().Diagnostics;
IReadOnlyList<Diagnostic>? csharpDiagnostics = null;

lock (PublishedRazorDiagnostics)
lock (PublishedCSharpDiagnostics)
{
var filePath = document.FilePath.AssumeNotNull();
if (delegatedResponse != null && delegatedResponse.Value.TryGetFirst(out var fullDocumentDiagnosticReport))
{
csharpDiagnostics = fullDocumentDiagnosticReport.Items;
}

if (PublishedRazorDiagnostics.TryGetValue(filePath, out var previousRazorDiagnostics) && razorDiagnostics.SequenceEqual(previousRazorDiagnostics)
&& (csharpDiagnostics == null || (PublishedCSharpDiagnostics.TryGetValue(filePath, out var previousCsharpDiagnostics) && csharpDiagnostics.SequenceEqual(previousCsharpDiagnostics))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static void AddDiagnosticServices(this IServiceCollection services)
services.AddHandlerWithCapabilities<DocumentPullDiagnosticsEndpoint>();
services.AddHandler<WorkspacePullDiagnosticsEndpoint>();
services.AddSingleton<RazorTranslateDiagnosticsService>();
services.AddSingleton(sp => new Lazy<RazorTranslateDiagnosticsService>(sp.GetRequiredService<RazorTranslateDiagnosticsService>));
}

public static void AddHoverServices(this IServiceCollection services)
Expand Down Expand Up @@ -191,6 +192,7 @@ public static void AddDocumentManagementServices(this IServiceCollection service
services.AddSingleton<GeneratedDocumentPublisher, DefaultGeneratedDocumentPublisher>();
services.AddSingleton<IProjectSnapshotChangeTrigger>((services) => services.GetRequiredService<GeneratedDocumentPublisher>());
services.AddSingleton<DocumentContextFactory, DefaultDocumentContextFactory>();
services.AddSingleton(sp => new Lazy<DocumentContextFactory>(sp.GetRequiredService<DocumentContextFactory>));

services.AddSingleton<DocumentVersionCache, DefaultDocumentVersionCache>();
services.AddSingleton<IProjectSnapshotChangeTrigger>((services) => services.GetRequiredService<DocumentVersionCache>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal abstract class LanguageServerFeatureOptions

public abstract bool SupportsDelegatedCodeActions { get; }

public abstract bool SupportsDelegatedDiagnostics { get; }
public abstract bool DelegateToCSharpOnDiagnosticPublish { get; }

public abstract bool ShowAllCSharpCodeActions { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public VisualStudioWindowsLanguageServerFeatureOptions(LSPEditorFeatureDetector

public override bool SupportsDelegatedCodeActions => true;

public override bool SupportsDelegatedDiagnostics => false;
public override bool DelegateToCSharpOnDiagnosticPublish => false;

public override bool ReturnCodeActionAndRenamePathsWithPrefixedSlash => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public VisualStudioMacLanguageServerFeatureOptions(LSPEditorFeatureDetector lspE

public override bool SupportsDelegatedCodeActions => true;

public override bool SupportsDelegatedDiagnostics => false;
public override bool DelegateToCSharpOnDiagnosticPublish => false;

public override bool ShowAllCSharpCodeActions => false;

Expand Down
Loading

0 comments on commit a3213d6

Please sign in to comment.