-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abec33d
commit 9dfcc1c
Showing
5 changed files
with
87 additions
and
12 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/ArgumentParsing.CodeFixes/UseArgsParameterNameCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using ArgumentParsing.Generators.Diagnostics; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace ArgumentParsing.CodeFixes; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public sealed class UseArgsParameterNameCodeFixProvider : CodeFixProvider | ||
Check warning on line 13 in src/ArgumentParsing.CodeFixes/UseArgsParameterNameCodeFixProvider.cs GitHub Actions / build-and-test
Check warning on line 13 in src/ArgumentParsing.CodeFixes/UseArgsParameterNameCodeFixProvider.cs GitHub Actions / build-and-test
|
||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticDescriptors.PreferArgsParameterName.Id); | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var document = context.Document; | ||
var root = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
|
||
if (root?.FindNode(context.Span) is ParameterSyntax parameter) | ||
{ | ||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
"Use 'args' name for the parameter", | ||
_ => ChangeNameToArgs(document, root, parameter)), | ||
context.Diagnostics[0]); | ||
} | ||
} | ||
|
||
private static Task<Document> ChangeNameToArgs(Document document, SyntaxNode root, ParameterSyntax parameter) | ||
{ | ||
var fixedParameter = parameter | ||
.WithIdentifier( | ||
SyntaxFactory.Identifier("args") | ||
.WithTriviaFrom(parameter.Identifier)); | ||
|
||
var fixedRoot = root.ReplaceNode(parameter, fixedParameter); | ||
return Task.FromResult(document.WithSyntaxRoot(fixedRoot)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters