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

Move few CodeFixProviders to Analyzers layer #60524

Merged
merged 8 commits into from
Apr 5, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,37 @@ internal record struct AddImportPlacementOptions(
[property: DataMember(Order = 1)] bool PlaceImportsInsideNamespaces,
[property: DataMember(Order = 2)] bool AllowInHiddenRegions)
{
#if !CODE_STYLE
public static async Task<AddImportPlacementOptions> FromDocumentAsync(Document document, CancellationToken cancellationToken)
=> FromDocument(document, await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false));

public static AddImportPlacementOptions FromDocument(Document document, OptionSet documentOptions)
{
var service = document.GetRequiredLanguageService<IAddImportsService>();

return new(
PlaceSystemNamespaceFirst: documentOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst, document.Project.Language),
PlaceImportsInsideNamespaces: service.PlaceImportsInsideNamespaces(documentOptions),
AllowInHiddenRegions: CanAddImportsInHiddenRegions(document));
#if CODE_STYLE
var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false), cancellationToken);
#else
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif
return FromDocument(document, options);
}

private static bool CanAddImportsInHiddenRegions(Document document)
{
#if CODE_STYLE
return false;
mavasani marked this conversation as resolved.
Show resolved Hide resolved
#else
// Normally we don't allow generation into a hidden region in the file. However, if we have a
// modern span mapper at our disposal, we do allow it as that host span mapper can handle mapping
// our edit to their domain appropriate.
var spanMapper = document.Services.GetService<ISpanMappingService>();
return spanMapper != null && spanMapper.SupportsMappingImportDirectives;
#endif
}
#else
public static async Task<AddImportPlacementOptions> FromDocumentAsync(Document document, CancellationToken cancellationToken)

public static AddImportPlacementOptions FromDocument(Document document, OptionSet documentOptions)
{
var service = document.GetRequiredLanguageService<IAddImportsService>();
var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false), cancellationToken);
return new(
PlaceSystemNamespaceFirst: options.GetOption(GenerationOptions.PlaceSystemNamespaceFirst),
PlaceImportsInsideNamespaces: service.PlaceImportsInsideNamespaces(options),
AllowInHiddenRegions: false);
PlaceSystemNamespaceFirst: documentOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst, document.Project.Language),
PlaceImportsInsideNamespaces: service.PlaceImportsInsideNamespaces(documentOptions),
AllowInHiddenRegions: CanAddImportsInHiddenRegions(document));
}
#endif
}

internal interface IAddImportsService : ILanguageService
Expand Down