|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CodeActions; |
| 7 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.Formatting; |
| 11 | +using Microsoft.CodeAnalysis.Simplification; |
| 12 | +using Microsoft.CodeAnalysis.Text; |
| 13 | + |
| 14 | +namespace Flow.Launcher.Localization.Analyzers.Localize |
| 15 | +{ |
| 16 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ContextAvailabilityAnalyzerCodeFixProvider)), Shared] |
| 17 | + public class ContextAvailabilityAnalyzerCodeFixProvider : CodeFixProvider |
| 18 | + { |
| 19 | + public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create( |
| 20 | + AnalyzerDiagnostics.ContextIsAField.Id, |
| 21 | + AnalyzerDiagnostics.ContextIsNotStatic.Id, |
| 22 | + AnalyzerDiagnostics.ContextAccessIsTooRestrictive.Id, |
| 23 | + AnalyzerDiagnostics.ContextIsNotDeclared.Id |
| 24 | + ); |
| 25 | + |
| 26 | + public sealed override FixAllProvider GetFixAllProvider() |
| 27 | + { |
| 28 | + return WellKnownFixAllProviders.BatchFixer; |
| 29 | + } |
| 30 | + |
| 31 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 32 | + { |
| 33 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 34 | + |
| 35 | + var diagnostic = context.Diagnostics.First(); |
| 36 | + var diagnosticSpan = diagnostic.Location.SourceSpan; |
| 37 | + |
| 38 | + if (diagnostic.Id == AnalyzerDiagnostics.ContextIsAField.Id) |
| 39 | + { |
| 40 | + context.RegisterCodeFix( |
| 41 | + CodeAction.Create( |
| 42 | + title: "Replace with static property", |
| 43 | + createChangedDocument: _ => Task.FromResult(FixContextIsAFieldError(context, root, diagnosticSpan)), |
| 44 | + equivalenceKey: AnalyzerDiagnostics.ContextIsAField.Id |
| 45 | + ), |
| 46 | + diagnostic |
| 47 | + ); |
| 48 | + } |
| 49 | + else if (diagnostic.Id == AnalyzerDiagnostics.ContextIsNotStatic.Id) |
| 50 | + { |
| 51 | + context.RegisterCodeFix( |
| 52 | + CodeAction.Create( |
| 53 | + title: "Make static", |
| 54 | + createChangedDocument: _ => Task.FromResult(FixContextIsNotStaticError(context, root, diagnosticSpan)), |
| 55 | + equivalenceKey: AnalyzerDiagnostics.ContextIsNotStatic.Id |
| 56 | + ), |
| 57 | + diagnostic |
| 58 | + ); |
| 59 | + } |
| 60 | + else if (diagnostic.Id == AnalyzerDiagnostics.ContextAccessIsTooRestrictive.Id) |
| 61 | + { |
| 62 | + context.RegisterCodeFix( |
| 63 | + CodeAction.Create( |
| 64 | + title: "Make internal", |
| 65 | + createChangedDocument: _ => Task.FromResult(FixContextIsTooRestricted(context, root, diagnosticSpan)), |
| 66 | + equivalenceKey: AnalyzerDiagnostics.ContextAccessIsTooRestrictive.Id |
| 67 | + ), |
| 68 | + diagnostic |
| 69 | + ); |
| 70 | + } |
| 71 | + else if (diagnostic.Id == AnalyzerDiagnostics.ContextIsNotDeclared.Id) |
| 72 | + { |
| 73 | + context.RegisterCodeFix( |
| 74 | + CodeAction.Create( |
| 75 | + title: "Declare context property", |
| 76 | + createChangedDocument: _ => Task.FromResult(FixContextNotDeclared(context, root, diagnosticSpan)), |
| 77 | + equivalenceKey: AnalyzerDiagnostics.ContextIsNotDeclared.Id |
| 78 | + ), |
| 79 | + diagnostic |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static MemberDeclarationSyntax GetStaticContextPropertyDeclaration(string propertyName = "Context") => |
| 85 | + SyntaxFactory.ParseMemberDeclaration( |
| 86 | + $"internal static PluginInitContext {propertyName} {{ get; private set; }} = null!;" |
| 87 | + ); |
| 88 | + |
| 89 | + private static Document GetFormattedDocument(CodeFixContext context, SyntaxNode root) |
| 90 | + { |
| 91 | + var formattedRoot = Formatter.Format( |
| 92 | + root, |
| 93 | + Formatter.Annotation, |
| 94 | + context.Document.Project.Solution.Workspace |
| 95 | + ); |
| 96 | + |
| 97 | + return context.Document.WithSyntaxRoot(formattedRoot); |
| 98 | + } |
| 99 | + |
| 100 | + private static Document FixContextNotDeclared(CodeFixContext context, SyntaxNode root, TextSpan diagnosticSpan) |
| 101 | + { |
| 102 | + var classDeclaration = GetDeclarationSyntax<ClassDeclarationSyntax>(root, diagnosticSpan); |
| 103 | + if (classDeclaration?.BaseList is null) return context.Document; |
| 104 | + |
| 105 | + var newPropertyDeclaration = GetStaticContextPropertyDeclaration(); |
| 106 | + if (newPropertyDeclaration is null) return context.Document; |
| 107 | + |
| 108 | + var annotatedNewPropertyDeclaration = newPropertyDeclaration |
| 109 | + .WithLeadingTrivia(SyntaxFactory.ElasticLineFeed) |
| 110 | + .WithTrailingTrivia(SyntaxFactory.ElasticLineFeed) |
| 111 | + .WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation); |
| 112 | + |
| 113 | + var newMembers = classDeclaration.Members.Insert(0, annotatedNewPropertyDeclaration); |
| 114 | + var newClassDeclaration = classDeclaration.WithMembers(newMembers); |
| 115 | + |
| 116 | + var newRoot = root.ReplaceNode(classDeclaration, newClassDeclaration); |
| 117 | + |
| 118 | + return GetFormattedDocument(context, newRoot); |
| 119 | + } |
| 120 | + |
| 121 | + private static Document FixContextIsNotStaticError(CodeFixContext context, SyntaxNode root, TextSpan diagnosticSpan) |
| 122 | + { |
| 123 | + var propertyDeclaration = GetDeclarationSyntax<PropertyDeclarationSyntax>(root, diagnosticSpan); |
| 124 | + if (propertyDeclaration is null) return context.Document; |
| 125 | + |
| 126 | + var newPropertyDeclaration = FixRestrictivePropertyModifiers(propertyDeclaration).AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword)); |
| 127 | + |
| 128 | + var newRoot = root.ReplaceNode(propertyDeclaration, newPropertyDeclaration); |
| 129 | + return context.Document.WithSyntaxRoot(newRoot); |
| 130 | + } |
| 131 | + |
| 132 | + private static Document FixContextIsTooRestricted(CodeFixContext context, SyntaxNode root, TextSpan diagnosticSpan) |
| 133 | + { |
| 134 | + var propertyDeclaration = GetDeclarationSyntax<PropertyDeclarationSyntax>(root, diagnosticSpan); |
| 135 | + if (propertyDeclaration is null) return context.Document; |
| 136 | + |
| 137 | + var newPropertyDeclaration = FixRestrictivePropertyModifiers(propertyDeclaration); |
| 138 | + |
| 139 | + var newRoot = root.ReplaceNode(propertyDeclaration, newPropertyDeclaration); |
| 140 | + return context.Document.WithSyntaxRoot(newRoot); |
| 141 | + } |
| 142 | + |
| 143 | + private static Document FixContextIsAFieldError(CodeFixContext context, SyntaxNode root, TextSpan diagnosticSpan) { |
| 144 | + var fieldDeclaration = GetDeclarationSyntax<FieldDeclarationSyntax>(root, diagnosticSpan); |
| 145 | + if (fieldDeclaration is null) return context.Document; |
| 146 | + |
| 147 | + var field = fieldDeclaration.Declaration.Variables.First(); |
| 148 | + var fieldIdentifier = field.Identifier.ToString(); |
| 149 | + |
| 150 | + var propertyDeclaration = GetStaticContextPropertyDeclaration(fieldIdentifier); |
| 151 | + if (propertyDeclaration is null) return context.Document; |
| 152 | + |
| 153 | + var annotatedNewPropertyDeclaration = propertyDeclaration |
| 154 | + .WithTriviaFrom(fieldDeclaration) |
| 155 | + .WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation); |
| 156 | + |
| 157 | + var newRoot = root.ReplaceNode(fieldDeclaration, annotatedNewPropertyDeclaration); |
| 158 | + |
| 159 | + return GetFormattedDocument(context, newRoot); |
| 160 | + } |
| 161 | + |
| 162 | + private static PropertyDeclarationSyntax FixRestrictivePropertyModifiers(PropertyDeclarationSyntax propertyDeclaration) |
| 163 | + { |
| 164 | + var newModifiers = SyntaxFactory.TokenList(); |
| 165 | + foreach (var modifier in propertyDeclaration.Modifiers) |
| 166 | + { |
| 167 | + if (modifier.IsKind(SyntaxKind.PrivateKeyword) || modifier.IsKind(SyntaxKind.ProtectedKeyword)) |
| 168 | + { |
| 169 | + newModifiers = newModifiers.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword)); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + newModifiers = newModifiers.Add(modifier); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return propertyDeclaration.WithModifiers(newModifiers); |
| 178 | + } |
| 179 | + |
| 180 | + private static T GetDeclarationSyntax<T>(SyntaxNode root, TextSpan diagnosticSpan) where T : SyntaxNode => |
| 181 | + root |
| 182 | + .FindToken(diagnosticSpan.Start) |
| 183 | + .Parent |
| 184 | + ?.AncestorsAndSelf() |
| 185 | + .OfType<T>() |
| 186 | + .First(); |
| 187 | + } |
| 188 | +} |
0 commit comments