|
16 | 16 |
|
17 | 17 | namespace ILLink.CodeFix |
18 | 18 | { |
19 | | - public abstract class BaseAttributeCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider |
20 | | - { |
21 | | - private protected abstract LocalizableString CodeFixTitle { get; } |
22 | | - |
23 | | - private protected abstract string FullyQualifiedAttributeName { get; } |
24 | | - |
25 | | - private protected abstract AttributeableParentTargets AttributableParentTargets { get; } |
26 | | - |
27 | | - public sealed override FixAllProvider GetFixAllProvider () |
28 | | - { |
29 | | - // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers |
30 | | - return WellKnownFixAllProviders.BatchFixer; |
31 | | - } |
32 | | - |
33 | | - protected async Task BaseRegisterCodeFixesAsync (CodeFixContext context) |
34 | | - { |
35 | | - var document = context.Document; |
36 | | - var diagnostic = context.Diagnostics.First (); |
37 | | - var codeFixTitle = CodeFixTitle.ToString (); |
38 | | - |
39 | | - if (await document.GetSyntaxRootAsync (context.CancellationToken).ConfigureAwait (false) is not { } root) |
40 | | - return; |
41 | | - |
42 | | - SyntaxNode targetNode = root.FindNode (diagnostic.Location.SourceSpan, getInnermostNodeForTie: true); |
43 | | - if (FindAttributableParent (targetNode, AttributableParentTargets) is not SyntaxNode attributableNode) |
44 | | - return; |
45 | | - |
46 | | - context.RegisterCodeFix (CodeAction.Create ( |
47 | | - title: codeFixTitle, |
48 | | - createChangedDocument: ct => AddAttributeAsync ( |
49 | | - document, diagnostic, targetNode, attributableNode, ct), |
50 | | - equivalenceKey: codeFixTitle), diagnostic); |
51 | | - } |
52 | | - |
53 | | - private async Task<Document> AddAttributeAsync ( |
54 | | - Document document, |
55 | | - Diagnostic diagnostic, |
56 | | - SyntaxNode targetNode, |
57 | | - SyntaxNode attributableNode, |
58 | | - CancellationToken cancellationToken) |
59 | | - { |
60 | | - if (await document.GetSemanticModelAsync (cancellationToken).ConfigureAwait (false) is not { } model) |
61 | | - return document; |
62 | | - if (model.GetSymbolInfo (targetNode, cancellationToken).Symbol is not { } targetSymbol) |
63 | | - return document; |
64 | | - if (model.Compilation.GetBestTypeByMetadataName (FullyQualifiedAttributeName) is not { } attributeSymbol) |
65 | | - return document; |
66 | | - |
67 | | - // N.B. May be null for FieldDeclaration, since field declarations can declare multiple variables |
68 | | - var attributableSymbol = model.GetDeclaredSymbol (attributableNode, cancellationToken); |
69 | | - |
70 | | - var attributeArguments = GetAttributeArguments (attributableSymbol, targetSymbol, SyntaxGenerator.GetGenerator (document), diagnostic); |
71 | | - |
72 | | - var editor = await DocumentEditor.CreateAsync (document, cancellationToken).ConfigureAwait (false); |
73 | | - var generator = editor.Generator; |
74 | | - var attribute = generator.Attribute ( |
75 | | - generator.TypeExpression (attributeSymbol), attributeArguments) |
76 | | - .WithAdditionalAnnotations (Simplifier.Annotation, Simplifier.AddImportsAnnotation); |
77 | | - |
78 | | - editor.AddAttribute (attributableNode, attribute); |
79 | | - return editor.GetChangedDocument (); |
80 | | - } |
81 | | - |
82 | | - [Flags] |
83 | | - protected enum AttributeableParentTargets |
84 | | - { |
85 | | - MethodOrConstructor = 0x0001, |
86 | | - Property = 0x0002, |
87 | | - Field = 0x0004, |
88 | | - Event = 0x0008, |
89 | | - Class = 0x0010, |
90 | | - All = MethodOrConstructor | Property | Field | Event | Class |
91 | | - } |
92 | | - |
93 | | - private static CSharpSyntaxNode? FindAttributableParent (SyntaxNode node, AttributeableParentTargets targets) |
94 | | - { |
95 | | - SyntaxNode? parentNode = node.Parent; |
96 | | - while (parentNode is not null) { |
97 | | - switch (parentNode) { |
98 | | - case LambdaExpressionSyntax: |
99 | | - return null; |
100 | | - |
101 | | - case PropertyDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Property): |
102 | | - case EventDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Event): |
103 | | - return (CSharpSyntaxNode) parentNode; |
104 | | - case PropertyDeclarationSyntax: |
105 | | - case EventDeclarationSyntax: |
106 | | - // If the attribute can be placed on a method but not directly on a property/event, we don't want to keep walking up |
107 | | - // the syntax tree to annotate the class. Instead the correct thing to do is to add accessor methods and annotate those. |
108 | | - // The code fixer doesn't support doing this automatically, so return null to indicate that the attribute can't be added. |
109 | | - if (targets.HasFlag (AttributeableParentTargets.MethodOrConstructor)) |
110 | | - return null; |
111 | | - |
112 | | - parentNode = parentNode.Parent; |
113 | | - break; |
114 | | - case LocalFunctionStatementSyntax or BaseMethodDeclarationSyntax or AccessorDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.MethodOrConstructor): |
115 | | - case FieldDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Field): |
116 | | - case ClassDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Class): |
117 | | - return (CSharpSyntaxNode) parentNode; |
118 | | - |
119 | | - default: |
120 | | - parentNode = parentNode.Parent; |
121 | | - break; |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - return null; |
126 | | - } |
127 | | - |
128 | | - protected abstract SyntaxNode[] GetAttributeArguments ( |
129 | | - ISymbol? attributableSymbol, |
130 | | - ISymbol targetSymbol, |
131 | | - SyntaxGenerator syntaxGenerator, |
132 | | - Diagnostic diagnostic); |
133 | | - |
134 | | - protected static bool HasPublicAccessibility (ISymbol? m) |
135 | | - { |
136 | | - if (m is not { DeclaredAccessibility: Accessibility.Public or Accessibility.Protected }) { |
137 | | - return false; |
138 | | - } |
139 | | - for (var t = m.ContainingType; t is not null; t = t.ContainingType) { |
140 | | - if (t.DeclaredAccessibility != Accessibility.Public) { |
141 | | - return false; |
142 | | - } |
143 | | - } |
144 | | - return true; |
145 | | - } |
146 | | - } |
| 19 | + public abstract class BaseAttributeCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider |
| 20 | + { |
| 21 | + private protected abstract LocalizableString CodeFixTitle { get; } |
| 22 | + |
| 23 | + private protected abstract string FullyQualifiedAttributeName { get; } |
| 24 | + |
| 25 | + private protected abstract AttributeableParentTargets AttributableParentTargets { get; } |
| 26 | + |
| 27 | + public sealed override FixAllProvider GetFixAllProvider() |
| 28 | + { |
| 29 | + // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers |
| 30 | + return WellKnownFixAllProviders.BatchFixer; |
| 31 | + } |
| 32 | + |
| 33 | + protected async Task BaseRegisterCodeFixesAsync(CodeFixContext context) |
| 34 | + { |
| 35 | + var document = context.Document; |
| 36 | + var diagnostic = context.Diagnostics.First(); |
| 37 | + var codeFixTitle = CodeFixTitle.ToString(); |
| 38 | + |
| 39 | + if (await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) is not { } root) |
| 40 | + return; |
| 41 | + |
| 42 | + SyntaxNode targetNode = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true); |
| 43 | + if (FindAttributableParent(targetNode, AttributableParentTargets) is not SyntaxNode attributableNode) |
| 44 | + return; |
| 45 | + |
| 46 | + context.RegisterCodeFix(CodeAction.Create( |
| 47 | + title: codeFixTitle, |
| 48 | + createChangedDocument: ct => AddAttributeAsync( |
| 49 | + document, diagnostic, targetNode, attributableNode, ct), |
| 50 | + equivalenceKey: codeFixTitle), diagnostic); |
| 51 | + } |
| 52 | + |
| 53 | + private async Task<Document> AddAttributeAsync( |
| 54 | + Document document, |
| 55 | + Diagnostic diagnostic, |
| 56 | + SyntaxNode targetNode, |
| 57 | + SyntaxNode attributableNode, |
| 58 | + CancellationToken cancellationToken) |
| 59 | + { |
| 60 | + if (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false) is not { } model) |
| 61 | + return document; |
| 62 | + if (model.GetSymbolInfo(targetNode, cancellationToken).Symbol is not { } targetSymbol) |
| 63 | + return document; |
| 64 | + if (model.Compilation.GetBestTypeByMetadataName(FullyQualifiedAttributeName) is not { } attributeSymbol) |
| 65 | + return document; |
| 66 | + |
| 67 | + // N.B. May be null for FieldDeclaration, since field declarations can declare multiple variables |
| 68 | + var attributableSymbol = model.GetDeclaredSymbol(attributableNode, cancellationToken); |
| 69 | + |
| 70 | + var attributeArguments = GetAttributeArguments(attributableSymbol, targetSymbol, SyntaxGenerator.GetGenerator(document), diagnostic); |
| 71 | + |
| 72 | + var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); |
| 73 | + var generator = editor.Generator; |
| 74 | + var attribute = generator.Attribute( |
| 75 | + generator.TypeExpression(attributeSymbol), attributeArguments) |
| 76 | + .WithAdditionalAnnotations(Simplifier.Annotation, Simplifier.AddImportsAnnotation); |
| 77 | + |
| 78 | + editor.AddAttribute(attributableNode, attribute); |
| 79 | + return editor.GetChangedDocument(); |
| 80 | + } |
| 81 | + |
| 82 | + [Flags] |
| 83 | + protected enum AttributeableParentTargets |
| 84 | + { |
| 85 | + MethodOrConstructor = 0x0001, |
| 86 | + Property = 0x0002, |
| 87 | + Field = 0x0004, |
| 88 | + Event = 0x0008, |
| 89 | + Class = 0x0010, |
| 90 | + All = MethodOrConstructor | Property | Field | Event | Class |
| 91 | + } |
| 92 | + |
| 93 | + private static CSharpSyntaxNode? FindAttributableParent(SyntaxNode node, AttributeableParentTargets targets) |
| 94 | + { |
| 95 | + SyntaxNode? parentNode = node.Parent; |
| 96 | + while (parentNode is not null) |
| 97 | + { |
| 98 | + switch (parentNode) |
| 99 | + { |
| 100 | + case LambdaExpressionSyntax: |
| 101 | + return null; |
| 102 | + |
| 103 | + case PropertyDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Property): |
| 104 | + case EventDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Event): |
| 105 | + return (CSharpSyntaxNode)parentNode; |
| 106 | + case PropertyDeclarationSyntax: |
| 107 | + case EventDeclarationSyntax: |
| 108 | + // If the attribute can be placed on a method but not directly on a property/event, we don't want to keep walking up |
| 109 | + // the syntax tree to annotate the class. Instead the correct thing to do is to add accessor methods and annotate those. |
| 110 | + // The code fixer doesn't support doing this automatically, so return null to indicate that the attribute can't be added. |
| 111 | + if (targets.HasFlag(AttributeableParentTargets.MethodOrConstructor)) |
| 112 | + return null; |
| 113 | + |
| 114 | + parentNode = parentNode.Parent; |
| 115 | + break; |
| 116 | + case LocalFunctionStatementSyntax or BaseMethodDeclarationSyntax or AccessorDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.MethodOrConstructor): |
| 117 | + case FieldDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Field): |
| 118 | + case ClassDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Class): |
| 119 | + return (CSharpSyntaxNode)parentNode; |
| 120 | + |
| 121 | + default: |
| 122 | + parentNode = parentNode.Parent; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + protected abstract SyntaxNode[] GetAttributeArguments( |
| 131 | + ISymbol? attributableSymbol, |
| 132 | + ISymbol targetSymbol, |
| 133 | + SyntaxGenerator syntaxGenerator, |
| 134 | + Diagnostic diagnostic); |
| 135 | + |
| 136 | + protected static bool HasPublicAccessibility(ISymbol? m) |
| 137 | + { |
| 138 | + if (m is not { DeclaredAccessibility: Accessibility.Public or Accessibility.Protected }) |
| 139 | + { |
| 140 | + return false; |
| 141 | + } |
| 142 | + for (var t = m.ContainingType; t is not null; t = t.ContainingType) |
| 143 | + { |
| 144 | + if (t.DeclaredAccessibility != Accessibility.Public) |
| 145 | + { |
| 146 | + return false; |
| 147 | + } |
| 148 | + } |
| 149 | + return true; |
| 150 | + } |
| 151 | + } |
147 | 152 | } |
0 commit comments