Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hint diagnostics in vscode for better rendering in the editor #69403

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ LSP.VSDiagnostic CreateLspDiagnostic(
Code = diagnosticData.Id,
CodeDescription = ProtocolConversions.HelpLinkToCodeDescription(diagnosticData.GetValidHelpLinkUri()),
Message = diagnosticData.Message,
Severity = ConvertDiagnosticSeverity(diagnosticData.Severity),
Severity = ConvertDiagnosticSeverity(diagnosticData.Severity, capabilities),
Tags = ConvertTags(diagnosticData),
DiagnosticRank = ConvertRank(diagnosticData),
};
Expand Down Expand Up @@ -501,13 +501,14 @@ static bool ShouldIncludeHiddenDiagnostic(DiagnosticData diagnosticData, ClientC
return null;
}

private static LSP.DiagnosticSeverity ConvertDiagnosticSeverity(DiagnosticSeverity severity)
private static LSP.DiagnosticSeverity ConvertDiagnosticSeverity(DiagnosticSeverity severity, ClientCapabilities clientCapabilities)
=> severity switch
{
// Hidden is translated in ConvertTags to pass along appropriate _ms tags
// that will hide the item in a client that knows about those tags.
DiagnosticSeverity.Hidden => LSP.DiagnosticSeverity.Hint,
DiagnosticSeverity.Info => LSP.DiagnosticSeverity.Information,
// VSCode shows information diagnostics as blue squiggles, and hint diagnostics as 3 dots. We prefer the latter rendering so we return hint diagnostics in vscode.
DiagnosticSeverity.Info => clientCapabilities.HasVisualStudioLspCapability() ? LSP.DiagnosticSeverity.Information : LSP.DiagnosticSeverity.Hint,
DiagnosticSeverity.Warning => LSP.DiagnosticSeverity.Warning,
DiagnosticSeverity.Error => LSP.DiagnosticSeverity.Error,
_ => throw ExceptionUtilities.UnexpectedValue(severity),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryParentheses;
using Microsoft.CodeAnalysis.CSharp.RemoveUnnecessarySuppressions;
using Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues;
using Microsoft.CodeAnalysis.CSharp.UseImplicitObjectCreation;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.LanguageServer.Handler.Diagnostics.Public;
Expand Down Expand Up @@ -46,7 +47,8 @@ private protected override TestAnalyzerReferenceByLanguage CreateTestAnalyzersRe
new CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer(),
new CSharpRemoveUnnecessaryExpressionParenthesesDiagnosticAnalyzer(),
new CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer(),
new CSharpRemoveUnnecessaryInlineSuppressionsDiagnosticAnalyzer()));
new CSharpRemoveUnnecessaryInlineSuppressionsDiagnosticAnalyzer(),
new CSharpUseImplicitObjectCreationDiagnosticAnalyzer()));
builder.Add(LanguageNames.VisualBasic, ImmutableArray.Create(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.VisualBasic)));
builder.Add(InternalLanguageNames.TypeScript, ImmutableArray.Create<DiagnosticAnalyzer>(new MockTypescriptDiagnosticAnalyzer()));
return new(builder.ToImmutableDictionary());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,54 @@ void M()
Assert.Empty(results.Single().Diagnostics);
}

[Theory, CombinatorialData]
public async Task TestInfoDiagnosticsAreReportedAsInformationInVS(bool mutatingLspWorkspace)
{
var markup =
@"class A
{
public A SomeA = new A();
}";
await using var testLspServer = await CreateTestWorkspaceWithDiagnosticsAsync(markup, mutatingLspWorkspace, BackgroundAnalysisScope.OpenFiles, useVSDiagnostics: true);

// Calling GetTextBuffer will effectively open the file.
testLspServer.TestWorkspace.Documents.Single().GetTextBuffer();

var document = testLspServer.GetCurrentSolution().Projects.Single().Documents.Single();

await OpenDocumentAsync(testLspServer, document);

var results = await RunGetDocumentPullDiagnosticsAsync(
testLspServer, document.GetURI(), useVSDiagnostics: true);

Assert.Equal("IDE0090", results.Single().Diagnostics.Single().Code);
Assert.Equal(LSP.DiagnosticSeverity.Information, results.Single().Diagnostics.Single().Severity);
}

[Theory, CombinatorialData]
public async Task TestInfoDiagnosticsAreReportedAsHintInVSCode(bool mutatingLspWorkspace)
{
var markup =
@"class A
{
public A SomeA = new A();
}";
await using var testLspServer = await CreateTestWorkspaceWithDiagnosticsAsync(markup, mutatingLspWorkspace, BackgroundAnalysisScope.OpenFiles, useVSDiagnostics: false);

// Calling GetTextBuffer will effectively open the file.
testLspServer.TestWorkspace.Documents.Single().GetTextBuffer();

var document = testLspServer.GetCurrentSolution().Projects.Single().Documents.Single();

await OpenDocumentAsync(testLspServer, document);

var results = await RunGetDocumentPullDiagnosticsAsync(
testLspServer, document.GetURI(), useVSDiagnostics: false);

Assert.Equal("IDE0090", results.Single().Diagnostics.Single().Code);
Assert.Equal(LSP.DiagnosticSeverity.Hint, results.Single().Diagnostics.Single().Severity);
}

#endregion

#region Workspace Diagnostics
Expand Down