Skip to content

Commit

Permalink
Addrest test feedback
Browse files Browse the repository at this point in the history
Address feedback from dotnet#70068 (comment) and move `AdditionalAnalyzers` to `InitializationOptions` for LSP pull diagnostics tests
  • Loading branch information
mavasani committed Sep 26, 2023
1 parent dbf4e99 commit 0700d9f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.Options;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
Expand All @@ -23,6 +25,7 @@ internal readonly record struct InitializationOptions()
internal bool CallInitialized { get; init; } = true;
internal object? ClientTarget { get; init; } = null;
internal string? Locale { get; init; } = null;
internal IEnumerable<DiagnosticAnalyzer>? AdditionalAnalyzers { get; init; } = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,19 @@ private protected static CodeActionResolveData CreateCodeActionResolveData(strin
=> new(uniqueIdentifier, customTags.ToImmutableArrayOrEmpty(), location.Range, CreateTextDocumentIdentifier(location.Uri));

private protected Task<TestLspServer> CreateTestLspServerAsync(string markup, bool mutatingLspWorkspace, LSP.ClientCapabilities clientCapabilities, bool callInitialized = true, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
=> CreateTestLspServerAsync(new string[] { markup }, LanguageNames.CSharp, mutatingLspWorkspace, new InitializationOptions { ClientCapabilities = clientCapabilities, CallInitialized = callInitialized }, additionalAnalyzers: additionalAnalyzers);
=> CreateTestLspServerAsync(new string[] { markup }, LanguageNames.CSharp, mutatingLspWorkspace, new InitializationOptions { ClientCapabilities = clientCapabilities, CallInitialized = callInitialized, AdditionalAnalyzers = additionalAnalyzers });

private protected Task<TestLspServer> CreateTestLspServerAsync(string markup, bool mutatingLspWorkspace, InitializationOptions? initializationOptions = null, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
=> CreateTestLspServerAsync(new string[] { markup }, LanguageNames.CSharp, mutatingLspWorkspace, initializationOptions, additionalAnalyzers: additionalAnalyzers);
private protected Task<TestLspServer> CreateTestLspServerAsync(string markup, bool mutatingLspWorkspace, InitializationOptions? initializationOptions = null)
=> CreateTestLspServerAsync(new string[] { markup }, LanguageNames.CSharp, mutatingLspWorkspace, initializationOptions);

private protected Task<TestLspServer> CreateTestLspServerAsync(string[] markups, bool mutatingLspWorkspace, InitializationOptions? initializationOptions = null, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
=> CreateTestLspServerAsync(markups, LanguageNames.CSharp, mutatingLspWorkspace, initializationOptions, additionalAnalyzers: additionalAnalyzers);
private protected Task<TestLspServer> CreateTestLspServerAsync(string[] markups, bool mutatingLspWorkspace, InitializationOptions? initializationOptions = null)
=> CreateTestLspServerAsync(markups, LanguageNames.CSharp, mutatingLspWorkspace, initializationOptions);

private protected Task<TestLspServer> CreateVisualBasicTestLspServerAsync(string markup, bool mutatingLspWorkspace, InitializationOptions? initializationOptions = null)
=> CreateTestLspServerAsync(new string[] { markup }, LanguageNames.VisualBasic, mutatingLspWorkspace, initializationOptions);

private protected Task<TestLspServer> CreateTestLspServerAsync(
string[] markups, string languageName, bool mutatingLspWorkspace, InitializationOptions? initializationOptions, List<Type>? excludedTypes = null, List<Type>? extraExportedTypes = null, bool commonReferences = true, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
string[] markups, string languageName, bool mutatingLspWorkspace, InitializationOptions? initializationOptions, List<Type>? excludedTypes = null, List<Type>? extraExportedTypes = null, bool commonReferences = true)
{
var lspOptions = initializationOptions ?? new InitializationOptions();

Expand All @@ -325,10 +325,10 @@ private protected Task<TestLspServer> CreateTestLspServerAsync(
TestWorkspace.CreateWorkspaceElement(languageName, files: markups, fileContainingFolders: lspOptions.DocumentFileContainingFolders, sourceGeneratedFiles: lspOptions.SourceGeneratedMarkups, commonReferences: commonReferences),
openDocuments: false);

return CreateTestLspServerAsync(workspace, lspOptions, languageName, additionalAnalyzers);
return CreateTestLspServerAsync(workspace, lspOptions, languageName);
}

private async Task<TestLspServer> CreateTestLspServerAsync(TestWorkspace workspace, InitializationOptions initializationOptions, string languageName, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers)
private async Task<TestLspServer> CreateTestLspServerAsync(TestWorkspace workspace, InitializationOptions initializationOptions, string languageName)
{
var solution = workspace.CurrentSolution;

Expand All @@ -350,8 +350,8 @@ private async Task<TestLspServer> CreateTestLspServerAsync(TestWorkspace workspa
}

var analyzerReferencesByLanguage = CreateTestAnalyzersReference();
if (additionalAnalyzers != null)
analyzerReferencesByLanguage = analyzerReferencesByLanguage.WithAdditionalAnalyzers(languageName, additionalAnalyzers);
if (initializationOptions.AdditionalAnalyzers != null)
analyzerReferencesByLanguage = analyzerReferencesByLanguage.WithAdditionalAnalyzers(languageName, initializationOptions.AdditionalAnalyzers);

solution = solution.WithAnalyzerReferences(new[] { analyzerReferencesByLanguage });
await workspace.ChangeSolutionAsync(solution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,22 @@ static DocumentDiagnosticParams CreateProposedDocumentDiagnosticParams(
}

private protected Task<TestLspServer> CreateTestWorkspaceWithDiagnosticsAsync(string markup, bool mutatingLspWorkspace, BackgroundAnalysisScope analyzerDiagnosticsScope, bool useVSDiagnostics, bool pullDiagnostics = true, CompilerDiagnosticsScope? compilerDiagnosticsScope = null, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
=> CreateTestLspServerAsync(markup, mutatingLspWorkspace, GetInitializationOptions(analyzerDiagnosticsScope, compilerDiagnosticsScope, useVSDiagnostics, pullDiagnostics ? DiagnosticMode.LspPull : DiagnosticMode.SolutionCrawlerPush), additionalAnalyzers);
=> CreateTestLspServerAsync(markup, mutatingLspWorkspace, GetInitializationOptions(analyzerDiagnosticsScope, compilerDiagnosticsScope, useVSDiagnostics, pullDiagnostics ? DiagnosticMode.LspPull : DiagnosticMode.SolutionCrawlerPush, additionalAnalyzers: additionalAnalyzers));

private protected Task<TestLspServer> CreateTestWorkspaceWithDiagnosticsAsync(string[] markups, bool mutatingLspWorkspace, BackgroundAnalysisScope analyzerDiagnosticsScope, bool useVSDiagnostics, bool pullDiagnostics = true, CompilerDiagnosticsScope? compilerDiagnosticsScope = null, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
=> CreateTestLspServerAsync(markups, mutatingLspWorkspace, GetInitializationOptions(analyzerDiagnosticsScope, compilerDiagnosticsScope, useVSDiagnostics, pullDiagnostics ? DiagnosticMode.LspPull : DiagnosticMode.SolutionCrawlerPush), additionalAnalyzers);
=> CreateTestLspServerAsync(markups, mutatingLspWorkspace, GetInitializationOptions(analyzerDiagnosticsScope, compilerDiagnosticsScope, useVSDiagnostics, pullDiagnostics ? DiagnosticMode.LspPull : DiagnosticMode.SolutionCrawlerPush, additionalAnalyzers: additionalAnalyzers));

private protected Task<TestLspServer> CreateTestWorkspaceFromXmlAsync(string xmlMarkup, bool mutatingLspWorkspace, BackgroundAnalysisScope analyzerDiagnosticsScope, bool useVSDiagnostics, bool pullDiagnostics = true, CompilerDiagnosticsScope? compilerDiagnosticsScope = null)
=> CreateXmlTestLspServerAsync(xmlMarkup, mutatingLspWorkspace, initializationOptions: GetInitializationOptions(analyzerDiagnosticsScope, compilerDiagnosticsScope, useVSDiagnostics, pullDiagnostics ? DiagnosticMode.LspPull : DiagnosticMode.SolutionCrawlerPush));
private protected Task<TestLspServer> CreateTestWorkspaceFromXmlAsync(string xmlMarkup, bool mutatingLspWorkspace, BackgroundAnalysisScope analyzerDiagnosticsScope, bool useVSDiagnostics, bool pullDiagnostics = true, CompilerDiagnosticsScope? compilerDiagnosticsScope = null, IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
=> CreateXmlTestLspServerAsync(xmlMarkup, mutatingLspWorkspace, initializationOptions: GetInitializationOptions(analyzerDiagnosticsScope, compilerDiagnosticsScope, useVSDiagnostics, pullDiagnostics ? DiagnosticMode.LspPull : DiagnosticMode.SolutionCrawlerPush, additionalAnalyzers: additionalAnalyzers));

private protected static InitializationOptions GetInitializationOptions(
BackgroundAnalysisScope analyzerDiagnosticsScope,
CompilerDiagnosticsScope? compilerDiagnosticsScope,
bool useVSDiagnostics,
DiagnosticMode mode,
WellKnownLspServerKinds serverKind = WellKnownLspServerKinds.AlwaysActiveVSLspServer,
string[]? sourceGeneratedMarkups = null)
string[]? sourceGeneratedMarkups = null,
IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
{
// If no explicit compiler diagnostics scope has been provided, match it with the provided analyzer diagnostics scope
compilerDiagnosticsScope ??= analyzerDiagnosticsScope switch
Expand All @@ -346,7 +347,8 @@ private protected static InitializationOptions GetInitializationOptions(
globalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.EnableDiagnosticsInSourceGeneratedFiles, true);
},
ServerKind = serverKind,
SourceGeneratedMarkups = sourceGeneratedMarkups ?? Array.Empty<string>()
SourceGeneratedMarkups = sourceGeneratedMarkups ?? Array.Empty<string>(),
AdditionalAnalyzers = additionalAnalyzers
};
}

Expand Down

0 comments on commit 0700d9f

Please sign in to comment.