Skip to content

Commit

Permalink
Pass options to FixAllAsync, simplify CodeAction registration (#60665)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Apr 10, 2022
1 parent beb6f39 commit 48ef538
Show file tree
Hide file tree
Showing 110 changed files with 348 additions and 942 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,13 @@ public override ImmutableArray<string> FixableDiagnosticIds

public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
context.RegisterCodeFix(
CodeAction.Create(
CSharpAnalyzersResources.Add_braces,
c => FixAsync(context.Document, context.Diagnostics.First(), c),
nameof(CSharpAnalyzersResources.Add_braces)),
context.Diagnostics);

RegisterCodeFix(context, CSharpAnalyzersResources.Add_braces, nameof(CSharpAnalyzersResources.Add_braces));
return Task.CompletedTask;
}

protected override Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
var root = editor.OriginalRoot;
foreach (var diagnostic in diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
if (symbol.IsOverride ||
symbol.ImplicitInterfaceImplementations().Any())
{
context.RegisterCodeFix(new MyCodeAction(CSharpCodeFixesResources.Explicitly_inherit_documentation,
c => FixAsync(context.Document, diagnostic, c)), context.Diagnostics);
RegisterCodeFix(context, CSharpCodeFixesResources.Explicitly_inherit_documentation, nameof(CSharpCodeFixesResources.Explicitly_inherit_documentation), diagnostic);
}
}
}
}

protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
string? newLine = null;
SourceText? sourceText = null;
Expand All @@ -99,11 +98,11 @@ protected override async Task FixAllAsync(Document document, ImmutableArray<Diag
}

#if CODE_STYLE
var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(node.SyntaxTree, cancellationToken);
var optionSet = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(node.SyntaxTree, cancellationToken);
#else
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif
newLine ??= options.GetOption(FormattingOptions2.NewLine);
newLine ??= optionSet.GetOption(FormattingOptions2.NewLine);
// We can safely assume, that there is no leading doc comment, because that is what CS1591 is telling us.
// So we create a new /// <inheritdoc/> comment.
var xmlSpaceAfterTripleSlash = Token(leading: TriviaList(DocumentationCommentExterior("///")), SyntaxKind.XmlTextLiteralToken, text: " ", valueText: " ", trailing: default);
Expand Down Expand Up @@ -131,13 +130,5 @@ protected override async Task FixAllAsync(Document document, ImmutableArray<Diag
editor.ReplaceNode(node, node.WithPrependedLeadingTrivia(newLeadingTrivia));
}
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument)
: base(title, createChangedDocument, equivalenceKey: nameof(CSharpCodeFixesResources.Explicitly_inherit_documentation))
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
});

context.RegisterCodeFix(
CodeAction.Create(title, c => FixAsync(context.Document, diagnostic, c), equivalenceKey),
CodeAction.Create(title, GetDocumentUpdater(context), equivalenceKey),
context.Diagnostics);

return Task.CompletedTask;
}

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
var diagnostic = diagnostics.First();

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

#if CODE_STYLE
var configOptions = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(namespaceDecl.SyntaxTree, cancellationToken);
var options = CSharpSyntaxFormattingOptions.Create(configOptions);
var formattingOptions = CSharpSyntaxFormattingOptions.Create(configOptions);
#else
var options = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
#endif
var converted = await ConvertAsync(document, namespaceDecl, options, cancellationToken).ConfigureAwait(false);
var converted = await ConvertAsync(document, namespaceDecl, formattingOptions, cancellationToken).ConfigureAwait(false);

editor.ReplaceNode(
editor.OriginalRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,13 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return Task.CompletedTask;
}

context.RegisterCodeFix(
CodeAction.Create(
CSharpAnalyzersResources.Convert_switch_statement_to_expression,
c => FixAsync(context.Document, context.Diagnostics.First(), c),
nameof(CSharpAnalyzersResources.Convert_switch_statement_to_expression)),
context.Diagnostics);
RegisterCodeFix(context, CSharpAnalyzersResources.Convert_switch_statement_to_expression, nameof(CSharpAnalyzersResources.Convert_switch_statement_to_expression));
return Task.CompletedTask;
}

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
using var spansDisposer = ArrayBuilder<TextSpan>.GetInstance(diagnostics.Length, out var spans);
foreach (var diagnostic in diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var cancellationToken = context.CancellationToken;

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (CanFix(
semanticModel, diagnostic, cancellationToken,
out _, out _, out var title))
if (CanFix(semanticModel, diagnostic, cancellationToken, out _, out _, out var title))
{
context.RegisterCodeFix(new MyCodeAction(
title,
c => FixAsync(document, diagnostic, c)),
context.Diagnostics);
RegisterCodeFix(context, title, nameof(CSharpDisambiguateSameVariableCodeFixProvider));
}
}

Expand Down Expand Up @@ -141,7 +136,7 @@ where m.IsAccessibleWithin(enclosingType)

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
var syntaxFacts = CSharpSyntaxFacts.Instance;
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -167,13 +162,5 @@ protected override async Task FixAllAsync(
editor.ReplaceNode(nameNode, newExpr);
}
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument)
: base(title, createChangedDocument, nameof(CSharpDisambiguateSameVariableCodeFixProvider))
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,20 @@ private static bool TryGetConstraint(

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
var cancellationToken = context.CancellationToken;

var diagnostic = context.Diagnostics.First();
if (TryGetConstraint(diagnostic, cancellationToken, out _, out _))
{
context.RegisterCodeFix(
new MyCodeAction(c => this.FixAsync(document, diagnostic, c)),
diagnostic);
RegisterCodeFix(context, CSharpCodeFixesResources.Fix_constraint, nameof(CSharpFixIncorrectConstraintCodeFixProvider));
}

return Task.CompletedTask;
}

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -118,14 +115,5 @@ protected override async Task FixAllAsync(
}
}
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(
Func<CancellationToken, Task<Document>> createChangedDocument)
: base(CSharpCodeFixesResources.Fix_constraint, createChangedDocument, nameof(CSharpFixIncorrectConstraintCodeFixProvider))
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

context.RegisterCodeFix(
new MyCodeAction(c => FixAsync(document, diagnostics.First(), c)),
diagnostics);
RegisterCodeFix(context, CSharpCodeFixesResources.Fix_return_type, nameof(CSharpCodeFixesResources.Fix_return_type));

return;

Expand Down Expand Up @@ -103,7 +101,7 @@ static bool IsVoid(TypeSyntax typeSyntax)
return (declarationTypeToFix, fixedDeclaration);
}

protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
var (declarationTypeToFix, fixedDeclaration) =
await TryGetOldAndNewReturnTypeAsync(document, diagnostics, cancellationToken).ConfigureAwait(false);
Expand All @@ -129,13 +127,5 @@ private static (TypeSyntax type, bool useTask) TryGetDeclarationTypeToFix(Syntax
};
}
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument)
: base(CSharpCodeFixesResources.Fix_return_type, createChangedDocument, nameof(CSharpCodeFixesResources.Fix_return_type))
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,18 @@ public override ImmutableArray<string> FixableDiagnosticIds

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
context.RegisterCodeFix(CodeAction.Create(
CSharpAnalyzersResources.Inline_variable_declaration,
c => FixAsync(context.Document, context.Diagnostics.First(), c),
nameof(CSharpAnalyzersResources.Inline_variable_declaration)),
context.Diagnostics);
RegisterCodeFix(context, CSharpAnalyzersResources.Inline_variable_declaration, nameof(CSharpAnalyzersResources.Inline_variable_declaration));
return Task.CompletedTask;
}

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
#if CODE_STYLE
var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(editor.OriginalRoot.SyntaxTree, cancellationToken);
var optionSet = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(editor.OriginalRoot.SyntaxTree, cancellationToken);
#else
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif

// Gather all statements to be removed
Expand Down Expand Up @@ -97,7 +93,7 @@ await editor.ApplyExpressionLevelSemanticEditsAsync(
(_1, _2, _3) => true,
(semanticModel, currentRoot, t, currentNode)
=> ReplaceIdentifierWithInlineDeclaration(
options, semanticModel, currentRoot, t.declarator,
optionSet, semanticModel, currentRoot, t.declarator,
t.identifier, currentNode, declarationsToRemove, document.Project.Solution.Workspace.Services,
cancellationToken),
cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ protected override bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic)

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
context.RegisterCodeFix(CodeAction.Create(
CSharpAnalyzersResources.Simplify_delegate_invocation,
c => FixAsync(context.Document, context.Diagnostics.First(), c),
nameof(CSharpAnalyzersResources.Simplify_delegate_invocation)),
context.Diagnostics);
RegisterCodeFix(context, CSharpAnalyzersResources.Simplify_delegate_invocation, nameof(CSharpAnalyzersResources.Simplify_delegate_invocation));
return Task.CompletedTask;
}

protected override Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
foreach (var diagnostic in diagnostics)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public override ImmutableArray<string> FixableDiagnosticIds
.WithAdditionalAnnotations(Formatter.Annotation);

root = root.ReplaceNode(returnStatement, yieldStatement);
return new MyCodeAction(document.WithSyntaxRoot(root));

return CodeAction.Create(
CSharpCodeFixesResources.Replace_return_with_yield_return,
_ => Task.FromResult(document.WithSyntaxRoot(root)),
nameof(CSharpCodeFixesResources.Replace_return_with_yield_return));
}

private static bool TryGetExpressionType(
Expand Down Expand Up @@ -219,13 +223,5 @@ protected override bool TryGetNode(
node = ancestors.FirstOrDefault(n => n.Span.Contains(span) && n != root && n.IsKind(SyntaxKind.ReturnStatement));
return node != null;
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(Document newDocument)
: base(CSharpCodeFixesResources.Replace_return_with_yield_return, c => Task.FromResult(newDocument), nameof(CSharpCodeFixesResources.Replace_return_with_yield_return))
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ protected override async Task<CodeAction> GetCodeFixAsync(SyntaxNode root, Synta
return null;
}

return new MyCodeAction(
string.Format(CSharpCodeFixesResources.Change_return_type_from_0_to_1,
type.ToMinimalDisplayString(model, node.SpanStart),
ienumerableGenericSymbol.ToMinimalDisplayString(model, node.SpanStart)), newDocument);
var title = string.Format(CSharpCodeFixesResources.Change_return_type_from_0_to_1,
type.ToMinimalDisplayString(model, node.SpanStart),
ienumerableGenericSymbol.ToMinimalDisplayString(model, node.SpanStart));

return CodeAction.Create(title, _ => Task.FromResult(newDocument), title);
}

private static bool TryGetIEnumerableSymbols(SemanticModel model, out INamedTypeSymbol ienumerableSymbol, out INamedTypeSymbol ienumerableGenericSymbol)
Expand All @@ -129,13 +130,5 @@ private static bool TryGetIEnumerableSymbols(SemanticModel model, out INamedType

return true;
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(string title, Document newDocument)
: base(title, c => Task.FromResult(newDocument), title)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

context.RegisterCodeFix(new MyCodeAction(
c => FixAsync(context.Document, diagnostic, c)),
context.Diagnostics);
RegisterCodeFix(context, CSharpCodeFixesResources.Add_await, nameof(CSharpCodeFixesResources.Add_await));
}

protected override Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
SyntaxEditor editor, CodeActionOptionsProvider options, CancellationToken cancellationToken)
{
foreach (var diagnostic in diagnostics)
{
Expand Down Expand Up @@ -114,15 +112,5 @@ private static SyntaxNode TryGetStatementToFix(SyntaxNode node)

return null;
}

private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument)
: base(CSharpCodeFixesResources.Add_await,
createChangedDocument,
CSharpCodeFixesResources.Add_await)
{
}
}
}
}
Loading

0 comments on commit 48ef538

Please sign in to comment.