|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CodeActions; |
| 8 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.Rename; |
| 11 | +using SyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 12 | +using SyntaxKind = Microsoft.CodeAnalysis.CSharp.SyntaxKind; |
| 13 | + |
| 14 | +namespace Lucene.Net.CodeAnalysis.Dev; |
| 15 | + |
| 16 | +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(LuceneDev1005_LuceneNetSupportPublicTypesCSCodeFix)), Shared] |
| 17 | +public class LuceneDev1005_LuceneNetSupportPublicTypesCSCodeFix : CodeFixProvider |
| 18 | +{ |
| 19 | + // Specify the diagnostic IDs of analyzers that are expected to be linked. |
| 20 | + public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = |
| 21 | + [LuceneDev1005_LuceneNetSupportPublicTypesCSCodeAnalyzer.DiagnosticId]; |
| 22 | + |
| 23 | + public override FixAllProvider? GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| 24 | + |
| 25 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 26 | + { |
| 27 | + // We link only one diagnostic and assume there is only one diagnostic in the context. |
| 28 | + var diagnostic = context.Diagnostics.Single(); |
| 29 | + |
| 30 | + // 'SourceSpan' of 'Location' is the highlighted area. We're going to use this area to find the 'SyntaxNode' to rename. |
| 31 | + var diagnosticSpan = diagnostic.Location.SourceSpan; |
| 32 | + |
| 33 | + // Get the root of Syntax Tree that contains the highlighted diagnostic. |
| 34 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 35 | + |
| 36 | + // Find SyntaxNode corresponding to the diagnostic. |
| 37 | + var diagnosticNode = root?.FindNode(diagnosticSpan); |
| 38 | + |
| 39 | + if (diagnosticNode is MemberDeclarationSyntax declaration) |
| 40 | + { |
| 41 | + var name = declaration switch |
| 42 | + { |
| 43 | + BaseTypeDeclarationSyntax baseTypeDeclaration => baseTypeDeclaration.Identifier.ToString(), |
| 44 | + DelegateDeclarationSyntax delegateDeclaration => delegateDeclaration.Identifier.ToString(), |
| 45 | + _ => null |
| 46 | + }; |
| 47 | + |
| 48 | + if (string.IsNullOrEmpty(name)) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // Register a code action that will invoke the fix. |
| 54 | + context.RegisterCodeFix( |
| 55 | + CodeAction.Create( |
| 56 | + title: string.Format(Resources.LuceneDev1005_CodeFixTitle, name), |
| 57 | + createChangedSolution: c => MakeDeclarationInternal(context.Document, declaration, c), |
| 58 | + equivalenceKey: nameof(Resources.LuceneDev1005_CodeFixTitle)), |
| 59 | + diagnostic); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private async Task<Solution> MakeDeclarationInternal(Document document, |
| 64 | + MemberDeclarationSyntax memberDeclaration, |
| 65 | + CancellationToken cancellationToken) |
| 66 | + { |
| 67 | + var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 68 | + if (syntaxRoot == null) return document.Project.Solution; |
| 69 | + |
| 70 | + // Remove existing accessibility modifiers |
| 71 | + var newModifiers = SyntaxFactory.TokenList( |
| 72 | + memberDeclaration.Modifiers |
| 73 | + .Where(modifier => !modifier.IsKind(SyntaxKind.PrivateKeyword) && |
| 74 | + !modifier.IsKind(SyntaxKind.ProtectedKeyword) && |
| 75 | + !modifier.IsKind(SyntaxKind.InternalKeyword) && |
| 76 | + !modifier.IsKind(SyntaxKind.PublicKeyword)) |
| 77 | + ).Insert(0, SyntaxFactory.Token(SyntaxKind.InternalKeyword)); // Ensure 'internal' is the first modifier |
| 78 | + |
| 79 | + var newMemberDeclaration = memberDeclaration.WithModifiers(newModifiers); |
| 80 | + var newRoot = syntaxRoot.ReplaceNode(memberDeclaration, newMemberDeclaration); |
| 81 | + return document.Project.Solution.WithDocumentSyntaxRoot(document.Id, newRoot); |
| 82 | + } |
| 83 | +} |
0 commit comments