| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Composition; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CodeActions; |
| | | 7 | | using Microsoft.CodeAnalysis.CodeFixes; |
| | | 8 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 9 | | using Microsoft.CodeAnalysis.Editing; |
| | | 10 | | using Microsoft.CodeAnalysis.Text; |
| | | 11 | | |
| | | 12 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 13 | | |
| | | 14 | | [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(NoMarkerAttributeCodeFixProvider))] |
| | | 15 | | [Shared] |
| | | 16 | | public sealed class NoMarkerAttributeCodeFixProvider : CodeFixProvider |
| | | 17 | | { |
| | 0 | 18 | | private static readonly ImmutableArray<string> fixableDiagnosticIds = ImmutableArray.Create(DiagnosticIdentifiers.No |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public override ImmutableArray<string> FixableDiagnosticIds |
| | | 22 | | { |
| | 0 | 23 | | get { return fixableDiagnosticIds; } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public override FixAllProvider? GetFixAllProvider() |
| | | 28 | | { |
| | 0 | 29 | | return WellKnownFixAllProviders.BatchFixer; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override Task RegisterCodeFixesAsync(CodeFixContext context) |
| | | 34 | | { |
| | 0 | 35 | | Diagnostic diagnostic = context.Diagnostics[0]; |
| | | 36 | | |
| | 0 | 37 | | context.RegisterCodeFix( |
| | 0 | 38 | | CodeAction.Create(Resources.HSV0002CodeFixTitle, token => CreateChangedDocument(context, token), nameof(Reso |
| | 0 | 39 | | diagnostic); |
| | | 40 | | |
| | 0 | 41 | | return Task.CompletedTask; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private static async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToke |
| | | 45 | | { |
| | 0 | 46 | | SemanticModel? semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(fa |
| | 0 | 47 | | if (semanticModel == null) |
| | | 48 | | { |
| | 0 | 49 | | return context.Document; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | DocumentEditor editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(fal |
| | 0 | 53 | | SyntaxGenerator generator = editor.Generator; |
| | | 54 | | |
| | 0 | 55 | | INamedTypeSymbol? attributeSymbol = semanticModel.Compilation.GetTypeByMetadataName(Types.MARKER_ATTRIBUTE); |
| | 0 | 56 | | if (attributeSymbol == null) |
| | | 57 | | { |
| | 0 | 58 | | return context.Document; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | SyntaxNode? root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 62 | | if (root == null) |
| | | 63 | | { |
| | 0 | 64 | | return context.Document; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | TextSpan diagnosticSpan = context.Diagnostics[0].Location.SourceSpan; |
| | 0 | 68 | | SyntaxNode node = root.FindNode(diagnosticSpan); |
| | 0 | 69 | | if (node is not AttributeSyntax attribute) |
| | | 70 | | { |
| | 0 | 71 | | return context.Document; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | // Find the type declaration |
| | 0 | 75 | | TypeDeclarationSyntax? typeDeclaration = attribute.FirstAncestorOrSelf<TypeDeclarationSyntax>(); |
| | 0 | 76 | | if (typeDeclaration == null) |
| | | 77 | | { |
| | 0 | 78 | | return context.Document; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | SyntaxNode attributeDeclaration = generator.Attribute(generator.TypeExpression(attributeSymbol, true)); |
| | 0 | 82 | | editor.AddAttribute(typeDeclaration, attributeDeclaration); |
| | | 83 | | |
| | 0 | 84 | | return editor.GetChangedDocument(); |
| | 0 | 85 | | } |
| | | 86 | | } |