|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +using System.Collections.Immutable; |
| 19 | +using System.Composition; |
| 20 | +using System.Linq; |
| 21 | +using System.Threading; |
| 22 | +using System.Threading.Tasks; |
| 23 | +using Lucene.Net.CodeAnalysis.Dev.CodeFixes; |
| 24 | +using Lucene.Net.CodeAnalysis.Dev.CodeFixes.Utility; |
| 25 | +using Lucene.Net.CodeAnalysis.Dev.Utility; |
| 26 | +using Microsoft.CodeAnalysis; |
| 27 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 28 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 29 | +using SyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 30 | +using SyntaxKind = Microsoft.CodeAnalysis.CSharp.SyntaxKind; |
| 31 | + |
| 32 | +namespace Lucene.Net.CodeAnalysis.Dev; |
| 33 | + |
| 34 | +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(LuceneDev1005_LuceneNetSupportPublicTypesCSCodeFixProvider)), Shared] |
| 35 | +public class LuceneDev1005_LuceneNetSupportPublicTypesCSCodeFixProvider : CodeFixProvider |
| 36 | +{ |
| 37 | + // Specify the diagnostic IDs of analyzers that are expected to be linked. |
| 38 | + public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = |
| 39 | + [Descriptors.LuceneDev1005_LuceneNetSupportPublicTypes.Id]; |
| 40 | + |
| 41 | + public override FixAllProvider? GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| 42 | + |
| 43 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 44 | + { |
| 45 | + // We link only one diagnostic and assume there is only one diagnostic in the context. |
| 46 | + var diagnostic = context.Diagnostics.Single(); |
| 47 | + |
| 48 | + // 'SourceSpan' of 'Location' is the highlighted area. We're going to use this area to find the 'SyntaxNode' to rename. |
| 49 | + var diagnosticSpan = diagnostic.Location.SourceSpan; |
| 50 | + |
| 51 | + // Get the root of Syntax Tree that contains the highlighted diagnostic. |
| 52 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 53 | + |
| 54 | + // Find SyntaxNode corresponding to the diagnostic. |
| 55 | + var diagnosticNode = root?.FindNode(diagnosticSpan); |
| 56 | + |
| 57 | + if (diagnosticNode is MemberDeclarationSyntax declaration) |
| 58 | + { |
| 59 | + var name = declaration switch |
| 60 | + { |
| 61 | + BaseTypeDeclarationSyntax baseTypeDeclaration => baseTypeDeclaration.Identifier.ToString(), |
| 62 | + DelegateDeclarationSyntax delegateDeclaration => delegateDeclaration.Identifier.ToString(), |
| 63 | + _ => null |
| 64 | + }; |
| 65 | + |
| 66 | + if (name == null) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Register a code action that will invoke the fix. |
| 72 | + context.RegisterCodeFix( |
| 73 | + CodeActionHelper.CreateFromResource( |
| 74 | + CodeFixResources.MakeXInternal, |
| 75 | + createChangedSolution: c => MakeDeclarationInternal(context.Document, declaration, c), |
| 76 | + "MakeDeclarationInternal", |
| 77 | + name), |
| 78 | + diagnostic); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static async Task<Solution> MakeDeclarationInternal(Document document, |
| 83 | + MemberDeclarationSyntax memberDeclaration, |
| 84 | + CancellationToken cancellationToken) |
| 85 | + { |
| 86 | + var solution = document.Project.Solution; |
| 87 | + var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); |
| 88 | + if (semanticModel == null) return solution; |
| 89 | + |
| 90 | + // Get the symbol for this type declaration |
| 91 | + var symbol = semanticModel.GetDeclaredSymbol(memberDeclaration, cancellationToken); |
| 92 | + if (symbol == null) return solution; |
| 93 | + |
| 94 | + // Find all partial declarations of this symbol |
| 95 | + var declaringSyntaxReferences = symbol.DeclaringSyntaxReferences; |
| 96 | + |
| 97 | + // Update all partial declarations across all documents |
| 98 | + foreach (var syntaxReference in declaringSyntaxReferences) |
| 99 | + { |
| 100 | + var declarationSyntax = await syntaxReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false); |
| 101 | + if (declarationSyntax is not MemberDeclarationSyntax declaration) continue; |
| 102 | + |
| 103 | + var declarationDocument = solution.GetDocument(syntaxReference.SyntaxTree); |
| 104 | + if (declarationDocument == null) continue; |
| 105 | + |
| 106 | + var syntaxRoot = await declarationDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 107 | + if (syntaxRoot == null) continue; |
| 108 | + |
| 109 | + // Get leading trivia from the first modifier (which contains license headers/comments) |
| 110 | + var leadingTrivia = declaration.Modifiers.Count > 0 |
| 111 | + ? declaration.Modifiers[0].LeadingTrivia |
| 112 | + : SyntaxTriviaList.Empty; |
| 113 | + |
| 114 | + // Get trailing trivia from the accessibility modifier we're removing (typically whitespace) |
| 115 | + var accessibilityModifier = declaration.Modifiers |
| 116 | + .FirstOrDefault(m => m.IsKind(SyntaxKind.PublicKeyword) || |
| 117 | + m.IsKind(SyntaxKind.InternalKeyword) || |
| 118 | + m.IsKind(SyntaxKind.ProtectedKeyword) || |
| 119 | + m.IsKind(SyntaxKind.PrivateKeyword)); |
| 120 | + var trailingTrivia = accessibilityModifier.TrailingTrivia; |
| 121 | + |
| 122 | + // Remove existing accessibility modifiers |
| 123 | + var newModifiers = SyntaxFactory.TokenList( |
| 124 | + declaration.Modifiers |
| 125 | + .Where(modifier => !modifier.IsKind(SyntaxKind.PrivateKeyword) && |
| 126 | + !modifier.IsKind(SyntaxKind.ProtectedKeyword) && |
| 127 | + !modifier.IsKind(SyntaxKind.InternalKeyword) && |
| 128 | + !modifier.IsKind(SyntaxKind.PublicKeyword)) |
| 129 | + ).Insert(0, SyntaxFactory.Token(leadingTrivia, SyntaxKind.InternalKeyword, trailingTrivia)); // Ensure 'internal' is the first modifier with preserved trivia |
| 130 | + |
| 131 | + var newDeclaration = declaration.WithModifiers(newModifiers); |
| 132 | + var newRoot = syntaxRoot.ReplaceNode(declaration, newDeclaration); |
| 133 | + solution = solution.WithDocumentSyntaxRoot(declarationDocument.Id, newRoot); |
| 134 | + } |
| 135 | + |
| 136 | + return solution; |
| 137 | + } |
| 138 | +} |
0 commit comments