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

Unify access to options across code style, code style fixes and feature layers #74450

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -89,8 +90,8 @@ protected override async Task FixAllAsync(Document document, ImmutableArray<Diag

if (newLine == null)
{
var optionsProvider = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
newLine = optionsProvider.GetLineFormattingOptions().NewLine;
var options = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
newLine = options.NewLine;
}

// We can safely assume, that there is no leading doc comment, because that is what CS1591 is telling us.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
Expand Down Expand Up @@ -55,8 +56,8 @@ protected override async Task FixAllAsync(

var namespaceDecl = (BaseNamespaceDeclarationSyntax)diagnostic.AdditionalLocations[0].FindNode(cancellationToken);

var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var converted = await ConvertNamespaceTransform.ConvertAsync(document, namespaceDecl, options.GetFormattingOptions(), cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var converted = await ConvertNamespaceTransform.ConvertAsync(document, namespaceDecl, options, cancellationToken).ConfigureAwait(false);

editor.ReplaceNode(
editor.OriginalRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ await RefactorInitializersAsync(type, solutionEditor, propertiesToAssign, cancel
}
}

var optionsProvider = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var lineFormattingOptions = optionsProvider.GetLineFormattingOptions();
var lineFormattingOptions = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

var modifiedClassTrivia = GetModifiedClassTrivia(
positionalParameterInfos, typeDeclaration, lineFormattingOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.LanguageService;
using Microsoft.CodeAnalysis.CSharp.OrderModifiers;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.OrderModifiers;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
Expand All @@ -28,9 +30,9 @@ private class AddNewKeywordAction(Document document, SyntaxNode node) : CodeActi
protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
var root = await _document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var options = await _document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var configOptions = await _document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);

var newNode = GetNewNode(_node, options.PreferredModifierOrder.Value);
var newNode = GetNewNode(_node, configOptions.GetOption(CSharpCodeStyleOptions.PreferredModifierOrder).Value);
var newRoot = root.ReplaceNode(_node, newNode);

return _document.WithSyntaxRoot(newRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Simplification;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
{
var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

// Gather all statements to be removed
// We need this to find the statements we can safely attach trivia to
Expand Down Expand Up @@ -92,7 +93,7 @@ private static (VariableDeclaratorSyntax declarator, IdentifierNameSyntax identi

private static SyntaxNode ReplaceIdentifierWithInlineDeclaration(
Document document,
CSharpCodeFixOptionsProvider options, SemanticModel semanticModel,
CSharpSimplifierOptions options, SemanticModel semanticModel,
SyntaxNode currentRoot, VariableDeclaratorSyntax declarator,
IdentifierNameSyntax identifier, SyntaxNode currentNode,
HashSet<StatementSyntax> declarationsToRemove,
Expand Down Expand Up @@ -238,7 +239,7 @@ private static SyntaxNode ReplaceIdentifierWithInlineDeclaration(
}

public static TypeSyntax GenerateTypeSyntaxOrVar(
ITypeSymbol symbol, CSharpCodeFixOptionsProvider options)
ITypeSymbol symbol, CSharpSimplifierOptions options)
{
var useVar = IsVarDesired(symbol, options);

Expand All @@ -251,7 +252,7 @@ public static TypeSyntax GenerateTypeSyntaxOrVar(
: symbol.GenerateTypeSyntax();
}

private static bool IsVarDesired(ITypeSymbol type, CSharpCodeFixOptionsProvider options)
private static bool IsVarDesired(ITypeSymbol type, CSharpSimplifierOptions options)
{
// If they want it for intrinsics, and this is an intrinsic, then use var.
if (type.IsSpecialType() == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Simplification;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;

namespace Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives;

Expand Down Expand Up @@ -57,12 +60,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var syntaxRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var compilationUnit = (CompilationUnitSyntax)syntaxRoot;

var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var simplifierOptions = options.GetSimplifierOptions();
var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);
var simplifierOptions = new CSharpSimplifierOptions(configOptions);

// Read the preferred placement option and verify if it can be applied to this code file. There are cases
// where we will not be able to fix the diagnostic and the user will need to resolve it manually.
var (placement, preferPreservation) = DeterminePlacement(compilationUnit, options.UsingDirectivePlacement);
var (placement, preferPreservation) = DeterminePlacement(compilationUnit, configOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement));
if (preferPreservation)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
Expand Down Expand Up @@ -47,7 +48,7 @@ public static async Task<Document> FixAllAsync(Document document, ImmutableArray
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var editor = new SyntaxEditor(root, document.Project.Solution.Services);

var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

var endOfLineTrivia = ElasticEndOfLine(options.NewLine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionA

var document = await ParsedDocument.CreateAsync(workspaceDocument, cancellationToken).ConfigureAwait(false);

var options = await workspaceDocument.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = (CSharpSyntaxFormattingOptions)options.GetFormattingOptions(CSharpSyntaxFormatting.Instance);
var formattingOptions = (CSharpSyntaxFormattingOptions)await workspaceDocument.GetSyntaxFormattingOptionsAsync(CSharpSyntaxFormatting.Instance, cancellationToken).ConfigureAwait(false);
var indentationOptions = new IndentationOptions(formattingOptions);

var wrappingLength = formattingOptions.CollectionExpressionWrappingLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var syntaxGenerator = document.GetRequiredLanguageService<SyntaxGenerator>();
var compilation = semanticModel.Compilation;

var placementOption = await document.GetAddImportPlacementOptionsAsync(addImportService, cancellationToken).ConfigureAwait(false);
var placementOption = await document.GetAddImportPlacementOptionsAsync(cancellationToken).ConfigureAwait(false);

using var _ = ArrayBuilder<CodeAction>.GetInstance(out var actions);
foreach (var symbol in Sort(symbolInfo.CandidateSymbols.Cast<ITypeSymbol>(), placementOption.PlaceSystemNamespaceFirst))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private async Task<Document> GetTransformedDocumentAsync(Document document, Canc

private async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken)
{
var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var generator = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var newLineTrivia = generator.EndOfLine(options.NewLine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ private async Task<Document> FixOneAsync(CodeFixContext context, Diagnostic diag
text.Lines[diagnosticLinePositionSpan.Start.Line].Start,
text.Lines[diagnosticLinePositionSpan.End.Line].End);

var options = await context.Document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = options.GetFormattingOptions(SyntaxFormatting);
var formattingOptions = await context.Document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false);
var formattedRoot = SyntaxFormatting.GetFormattingResult(root, [spanToFormat], formattingOptions, rules: default, cancellationToken: cancellationToken).GetFormattedRoot(cancellationToken);

return context.Document.WithSyntaxRoot(formattedRoot);
}

protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = options.GetFormattingOptions(SyntaxFormatting);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false);
var updatedRoot = SyntaxFormatting.GetFormattingResult(editor.OriginalRoot, [editor.OriginalRoot.FullSpan], formattingOptions, rules: default, cancellationToken).GetFormattedRoot(cancellationToken);
editor.ReplaceNode(editor.OriginalRoot, updatedRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;

Expand Down Expand Up @@ -43,7 +44,7 @@ private static Task<Document> UpdateDocumentAsync(Document document, Diagnostic
public static async Task<Document> FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

var generator = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var endOfLineTrivia = generator.EndOfLine(options.NewLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ private static async Task<Document> PreprocessDocumentAsync(Document document, I

protected sealed override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = options.GetFormattingOptions(SyntaxFormatting);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false);
var preprocessedDocument = await PreprocessDocumentAsync(document, diagnostics, cancellationToken).ConfigureAwait(false);
var newRoot = await GetNewRootAsync(preprocessedDocument, formattingOptions, diagnostics, cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(editor.OriginalRoot, newRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ protected override async Task FixAllAsync(
{
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = options.GetFormattingOptions(SyntaxFormatting);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false);

// Defer to our callback to actually make the edits for each diagnostic. In turn, it
// will return 'true' if it made a multi-line conditional expression. In that case,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
Expand Down Expand Up @@ -159,7 +160,7 @@ private static (SyntaxNode root, SyntaxToken firstToken) RewriteExistingDirectiv

private static async Task<SyntaxNode> DisableNullableReferenceTypesInExistingDocumentIfNecessaryAsync(Document document, SyntaxNode root, SyntaxToken firstToken, CancellationToken cancellationToken)
{
var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var newLine = SyntaxFactory.EndOfLine(options.NewLine);

// Add a new '#nullable disable' to the top of each file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand Down Expand Up @@ -47,18 +48,18 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
if (!IsValidPosition(namespaceDecl, position))
return;

var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
if (!CanOfferRefactoring(namespaceDecl, root, options, out var info))
return;

context.RegisterRefactoring(CodeAction.Create(
info.Value.title, c => ConvertAsync(document, namespaceDecl, options.GetFormattingOptions(), c), info.Value.equivalenceKey));
info.Value.title, c => ConvertAsync(document, namespaceDecl, options, c), info.Value.equivalenceKey));
}

private static bool CanOfferRefactoring(
[NotNullWhen(true)] BaseNamespaceDeclarationSyntax? namespaceDecl,
CompilationUnitSyntax root,
CSharpCodeFixOptionsProvider options,
CSharpSyntaxFormattingOptions options,
[NotNullWhen(true)] out (string title, string equivalenceKey)? info)
{
info =
Expand Down Expand Up @@ -91,15 +92,15 @@ protected override async Task FixAllAsync(
CancellationToken cancellationToken)
{
var root = (CompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false);
var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var namespaceDecl = root.DescendantNodes().OfType<BaseNamespaceDeclarationSyntax>().FirstOrDefault();
if (!CanOfferRefactoring(namespaceDecl, root, options, out var info)
|| info.Value.equivalenceKey != equivalenceKey)
{
return;
}

document = await ConvertAsync(document, namespaceDecl, options.GetFormattingOptions(), cancellationToken).ConfigureAwait(false);
document = await ConvertAsync(document, namespaceDecl, options, cancellationToken).ConfigureAwait(false);
var newRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(editor.OriginalRoot, newRoot);
}
Expand Down
Loading