-
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.
Add code fix for
ARGP0005
and modify analyzer to report hidden diag…
…nostic in case of an error type, so we can offer the fix in all scenarios
- Loading branch information
1 parent
544e951
commit 1a3762e
Showing
4 changed files
with
148 additions
and
31 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/ArgumentParsing.CodeFixes/WrapReturnTypeIntoParseResultCodeFixProvider.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,66 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Diagnostics; | ||
using ArgumentParsing.Generators.Diagnostics; | ||
using ArgumentParsing.Generators.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Editing; | ||
|
||
namespace ArgumentParsing.CodeFixes; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public sealed class WrapReturnTypeIntoParseResultCodeFixProvider : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticDescriptors.ReturnTypeMustBeParseResult.Id); | ||
|
||
public override FixAllProvider? GetFixAllProvider() | ||
=> WellKnownFixAllProviders.BatchFixer; | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var document = context.Document; | ||
var cancellationToken = context.CancellationToken; | ||
|
||
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
if (root?.FindNode(context.Span) is TypeSyntax returnTypeSyntax) | ||
{ | ||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
"Wrap return type into ParseResult<T>", | ||
ct => WrapReturnTypeIntoParseResult(document, root, returnTypeSyntax, ct), | ||
nameof(WrapReturnTypeIntoParseResultCodeFixProvider)), | ||
context.Diagnostics[0]); | ||
} | ||
} | ||
|
||
private static async Task<Document> WrapReturnTypeIntoParseResult(Document document, SyntaxNode root, TypeSyntax returnTypeSyntax, CancellationToken cancellationToken) | ||
{ | ||
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
if (semanticModel is null) | ||
{ | ||
Debug.Fail("Shouldn't really happen"); | ||
return document; | ||
} | ||
|
||
var parseResultOfTType = semanticModel.Compilation.ParseResultOfTType(); | ||
if (parseResultOfTType is null) | ||
{ | ||
Debug.Fail("Shouldn't really happen"); | ||
return document; | ||
} | ||
|
||
var returnType = semanticModel.GetTypeInfo(returnTypeSyntax, cancellationToken).Type!; | ||
var parseResultOfReturnType = parseResultOfTType.Construct(returnType); | ||
|
||
var generator = SyntaxGenerator.GetGenerator(document); | ||
var wrappedReturnTypeSyntax = generator.TypeExpression(parseResultOfReturnType, addImport: true); | ||
|
||
var changedRoot = root.ReplaceNode(returnTypeSyntax, wrappedReturnTypeSyntax); | ||
return document.WithSyntaxRoot(changedRoot); | ||
} | ||
} |
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