-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGetNamesCodeFixProvider.cs
More file actions
58 lines (50 loc) · 2.23 KB
/
GetNamesCodeFixProvider.cs
File metadata and controls
58 lines (50 loc) · 2.23 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
using System.Collections.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Simplification;
namespace NetEscapades.EnumGenerators.Diagnostics.UsageAnalyzers;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(GetNamesCodeFixProvider)), Shared]
public class GetNamesCodeFixProvider : CodeFixProviderBase
{
private const string Title = "Replace with generated GetNames()";
public sealed override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(GetNamesAnalyzer.DiagnosticId);
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
if (!context.Diagnostics.IsDefaultOrEmpty)
{
// Register a code action for GetNames() replacement
context.RegisterCodeFix(
CodeAction.Create(
title: Title,
createChangedDocument: c => FixAllAsync(context.Document, context.Diagnostics, c),
equivalenceKey: Title),
context.Diagnostics);
}
return Task.CompletedTask;
}
protected override Task FixWithEditor(DocumentEditor editor, Diagnostic diagnostic,
INamedTypeSymbol extensionTypeSymbol,
CancellationToken cancellationToken)
{
// Find the invocation node at the diagnostic location
var node = editor.OriginalRoot.FindNode(diagnostic.Location.SourceSpan);
if (node is not InvocationExpressionSyntax invocation)
{
return Task.CompletedTask;
}
// Create new invocation: ExtensionsClass.GetNames()
var generator = editor.Generator;
var newInvocation = generator.InvocationExpression(
generator.MemberAccessExpression(generator.TypeExpression(extensionTypeSymbol), "GetNames"))
.WithTriviaFrom(invocation)
.WithAdditionalAnnotations(Simplifier.AddImportsAnnotation, Simplifier.Annotation);
editor.ReplaceNode(invocation, newInvocation);
return Task.CompletedTask;
}
}