-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathAttributeArgumentCodeFixProvider.cs
83 lines (69 loc) · 3.12 KB
/
AttributeArgumentCodeFixProvider.cs
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
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CSharp;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Roslynator.CSharp.CSharpFactory;
namespace Roslynator.CodeAnalysis.CSharp;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AttributeArgumentCodeFixProvider))]
[Shared]
public sealed class AttributeArgumentCodeFixProvider : BaseCodeFixProvider
{
private static ImmutableDictionary<string, string> _languageNames;
public override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(CodeAnalysisDiagnosticIdentifiers.UnknownLanguageName); }
}
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);
if (!TryFindFirstAncestorOrSelf(root, context.Span, out AttributeArgumentSyntax attributeArgument))
return;
Document document = context.Document;
Diagnostic diagnostic = context.Diagnostics[0];
switch (diagnostic.Id)
{
case CodeAnalysisDiagnosticIdentifiers.UnknownLanguageName:
{
foreach (string languageName in RoslynUtility.WellKnownLanguageNames)
{
CodeAction codeAction = CodeAction.Create(
$"Change language name to '{languageName}'",
ct => ChangeLanguageNameAsync(document, attributeArgument, languageName, ct),
GetEquivalenceKey(diagnostic, languageName));
context.RegisterCodeFix(codeAction, diagnostic);
}
break;
}
}
}
private static Task<Document> ChangeLanguageNameAsync(
Document document,
AttributeArgumentSyntax attributeArgument,
string languageName,
CancellationToken cancellationToken)
{
if (_languageNames is null)
Interlocked.CompareExchange(ref _languageNames, LoadLanguageNames(), null);
AttributeArgumentSyntax newAttributeArgument = AttributeArgument(
SimpleMemberAccessExpression(
ParseName("global::Microsoft.CodeAnalysis.LanguageNames").WithSimplifierAnnotation(),
IdentifierName(_languageNames[languageName])));
return document.ReplaceNodeAsync(attributeArgument, newAttributeArgument, cancellationToken);
static ImmutableDictionary<string, string> LoadLanguageNames()
{
return typeof(LanguageNames)
.GetRuntimeFields()
.Where(f => f.IsPublic)
.ToImmutableDictionary(f => (string)f.GetValue(null), f => f.Name);
}
}
}