-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathStepDefinitionExpressionCodeFixProvider.cs
More file actions
101 lines (85 loc) · 4.02 KB
/
Copy pathStepDefinitionExpressionCodeFixProvider.cs
File metadata and controls
101 lines (85 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
using System.Composition;
namespace Reqnroll.Analyzers.CodeFixes.StepDefinitions;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(StepDefinitionExpressionCodeFixProvider)), Shared]
public class StepDefinitionExpressionCodeFixProvider : CodeFixProvider
{
public StepDefinitionExpressionCodeFixProvider()
{
System.Diagnostics.Debug.WriteLine(">>> CodeFixProvider instantiated");
}
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
DiagnosticIds.StepDefinitionExpressionCannotBeNull,
DiagnosticIds.StepDefinitionExpressionCannotBeEmptyOrWhitespace,
DiagnosticIds.StepDefinitionExpressionShouldNotHaveLeadingOrTrailingWhitespace);
public override FixAllProvider? GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
if (root == null)
{
return;
}
var diagnostic = context.Diagnostics.First();
var argument = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent!
.AncestorsAndSelf().OfType<AttributeArgumentSyntax>().First();
if (diagnostic.Descriptor.Id == DiagnosticIds.StepDefinitionExpressionCannotBeNull ||
diagnostic.Descriptor.Id == DiagnosticIds.StepDefinitionExpressionCannotBeEmptyOrWhitespace)
{
context.RegisterCodeFix(
CodeAction.Create(
title: CodeFixResources.RemoveStepTextArgumentTitle,
createChangedDocument: _ => RemoveStepTextArgumentAsync(context.Document, root, argument),
equivalenceKey: "RemoveStepTextArgument"),
diagnostic);
}
if (diagnostic.Descriptor.Id == DiagnosticIds.StepDefinitionExpressionShouldNotHaveLeadingOrTrailingWhitespace)
{
context.RegisterCodeFix(
CodeAction.Create(
title: CodeFixResources.TrimStepTextArgumentTitle,
createChangedDocument: _ => TrimStepTextArgumentAsync(context.Document, root, argument),
equivalenceKey: "TrimStepTextArgument"),
diagnostic);
}
}
private async Task<Document> TrimStepTextArgumentAsync(
Document document,
SyntaxNode root,
AttributeArgumentSyntax argument)
{
// Create a new argument with trimmed text.
var literalExpression = (LiteralExpressionSyntax)argument.Expression;
var originalText = (string)literalExpression.Token.Value!;
var trimmedText = originalText.Trim();
var newLiteralExpression = literalExpression.WithToken(SyntaxFactory.Literal(trimmedText));
// Replace the argument expression.
var newRoot = root.ReplaceNode(literalExpression, newLiteralExpression);
return document.WithSyntaxRoot(newRoot);
}
private async Task<Document> RemoveStepTextArgumentAsync(
Document document,
SyntaxNode root,
AttributeArgumentSyntax argument)
{
// Create a new argument list without the specified argument.
var argumentList = (AttributeArgumentListSyntax)argument.Parent!;
var newArgumentList = argumentList.RemoveNode(argument, SyntaxRemoveOptions.KeepNoTrivia);
// Replace the argument list.
SyntaxNode newRoot;
if (newArgumentList == null || newArgumentList.Arguments.Count == 0)
{
newRoot = root.RemoveNode(argumentList, SyntaxRemoveOptions.KeepNoTrivia) ?? SyntaxFactory.CompilationUnit();
}
else
{
newRoot = root.ReplaceNode(argumentList, newArgumentList);
}
return document.WithSyntaxRoot(newRoot);
}
}