Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Feb 10, 2022
1 parent f493e97 commit f078ffd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.SymbolSearch;
Expand All @@ -20,15 +16,15 @@ internal abstract partial class AbstractAddImportCodeFixProvider : CodeFixProvid
{
private const int MaxResults = 5;

private readonly IPackageInstallerService _packageInstallerService;
private readonly ISymbolSearchService _symbolSearchService;
private readonly IPackageInstallerService? _packageInstallerService;
private readonly ISymbolSearchService? _symbolSearchService;

/// <summary>
/// Values for these parameters can be provided (during testing) for mocking purposes.
/// </summary>
protected AbstractAddImportCodeFixProvider(
IPackageInstallerService packageInstallerService = null,
ISymbolSearchService symbolSearchService = null)
IPackageInstallerService? packageInstallerService = null,
ISymbolSearchService? symbolSearchService = null)
{
_packageInstallerService = packageInstallerService;
_symbolSearchService = symbolSearchService;
Expand All @@ -42,7 +38,7 @@ protected AbstractAddImportCodeFixProvider(
private protected override CodeActionRequestPriority ComputeRequestPriority()
=> CodeActionRequestPriority.High;

public sealed override FixAllProvider GetFixAllProvider()
public sealed override FixAllProvider? GetFixAllProvider()
{
// Currently Fix All is not supported for this provider
// https://github.com/dotnet/roslyn/issues/34457
Expand All @@ -56,28 +52,34 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var cancellationToken = context.CancellationToken;
var diagnostics = context.Diagnostics;

var addImportService = document.GetLanguageService<IAddImportFeatureService>();
var addImportService = document.GetRequiredLanguageService<IAddImportFeatureService>();
var services = document.Project.Solution.Workspace.Services;

var solution = document.Project.Solution;
var searchOptions = context.Options.SearchOptions;

var placement = await AddImportPlacementOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var symbolSearchService = _symbolSearchService ?? services.GetRequiredService<ISymbolSearchService>();

var options = new AddImportOptions(
context.Options.SearchOptions,
context.Options.HideAdvancedMembers,
placement);
var installerService = searchOptions.SearchNuGetPackages ?
_packageInstallerService ?? services.GetService<IPackageInstallerService>() : null;

var symbolSearchService = options.SearchOptions.SearchReferenceAssemblies
? _symbolSearchService ?? solution.Workspace.Services.GetService<ISymbolSearchService>()
: null;

var installerService = GetPackageInstallerService(document);
var packageSources = options.SearchOptions.SearchNuGetPackages && symbolSearchService != null && installerService?.IsEnabled(document.Project.Id) == true
var packageSources = installerService?.IsEnabled(document.Project.Id) == true
? installerService.TryGetPackageSources()
: ImmutableArray<PackageSource>.Empty;

if (packageSources.IsEmpty)
{
searchOptions = searchOptions with { SearchNuGetPackages = false };
}

var placement = await AddImportPlacementOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

var addImportOptions = new AddImportOptions(
searchOptions,
context.Options.HideAdvancedMembers,
placement);

var fixesForDiagnostic = await addImportService.GetFixesForDiagnosticsAsync(
document, span, diagnostics, MaxResults, symbolSearchService, options, packageSources, cancellationToken).ConfigureAwait(false);
document, span, diagnostics, MaxResults, symbolSearchService, addImportOptions, packageSources, cancellationToken).ConfigureAwait(false);

foreach (var (diagnostic, fixes) in fixesForDiagnostic)
{
Expand All @@ -86,8 +88,5 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterFixes(codeActions, diagnostic);
}
}

private IPackageInstallerService GetPackageInstallerService(Document document)
=> _packageInstallerService ?? document.Project.Solution.Workspace.Services.GetService<IPackageInstallerService>();
}
}
6 changes: 0 additions & 6 deletions src/Features/Core/Portable/AddImport/SymbolReferenceFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ public SymbolReferenceFinder(
_symbolSearchService = symbolSearchService;
_options = options;
_packageSources = packageSources;

if (options.SearchOptions.SearchReferenceAssemblies || packageSources.Length > 0)
{
Contract.ThrowIfNull(symbolSearchService);
}

_syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();

_namespacesInScope = GetNamespacesInScope(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IdeAnalyzerOptions GetIdeOptions(string language)
language,
static (language, solution) => new IdeAnalyzerOptions(
FadeOutUnusedImports: solution.Options.GetOption(Fading.FadingOptions.Metadata.FadeOutUnusedImports, language),
FadeOutUnreachableCode: solution.Options.GetOption(Fading.FadingOptions.Metadata.FadeOutUnusedImports, language),
FadeOutUnreachableCode: solution.Options.GetOption(Fading.FadingOptions.Metadata.FadeOutUnreachableCode, language),
ReportInvalidPlaceholdersInStringDotFormatCalls: solution.Options.GetOption(ValidateFormatString.ValidateFormatStringOption.ReportInvalidPlaceholdersInStringDotFormatCalls, language),
ReportInvalidRegexPatterns: solution.Options.GetOption(Features.EmbeddedLanguages.RegularExpressions.LanguageServices.RegularExpressionsOptions.ReportInvalidRegexPatterns, language)),
_solution);
Expand Down

0 comments on commit f078ffd

Please sign in to comment.