| | | 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(TypeNotSupportedForCallbackAttributeCodeFixProvider))] |
| | | 15 | | [Shared] |
| | | 16 | | public sealed class TypeNotSupportedForCallbackAttributeCodeFixProvider : CodeFixProvider |
| | | 17 | | { |
| | 0 | 18 | | private static readonly ImmutableArray<string> fixableDiagnosticIds = ImmutableArray.Create(DiagnosticIdentifiers.Ty |
| | | 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]; |
| | 0 | 36 | | if (!diagnostic.Properties.TryGetValue("correctAttributeName", out string? attributeName) || |
| | 0 | 37 | | string.IsNullOrEmpty(attributeName)) |
| | | 38 | | { |
| | 0 | 39 | | return Task.CompletedTask; |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | context.RegisterCodeFix(CodeAction.Create( |
| | 0 | 43 | | string.Format(Resources.HSV0100CodeFixTitle, attributeName), |
| | 0 | 44 | | token => CreateChangedDocument(context, token), |
| | 0 | 45 | | nameof(Resources.HSV0100CodeFixTitle)), diagnostic); |
| | | 46 | | |
| | 0 | 47 | | return Task.CompletedTask; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | private static async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToke |
| | | 51 | | { |
| | 0 | 52 | | Diagnostic diagnostic = context.Diagnostics[0]; |
| | 0 | 53 | | if (!diagnostic.Properties.TryGetValue("correctAttributeName", out string? attributeName) || |
| | 0 | 54 | | string.IsNullOrEmpty(attributeName)) |
| | | 55 | | { |
| | 0 | 56 | | return context.Document; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | SyntaxNode? root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 60 | | if (root == null) |
| | | 61 | | { |
| | 0 | 62 | | return context.Document; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | TextSpan diagnosticSpan = context.Diagnostics[0].Location.SourceSpan; |
| | 0 | 66 | | SyntaxNode node = root.FindNode(diagnosticSpan); |
| | | 67 | | |
| | 0 | 68 | | if (node is not AttributeSyntax attribute) |
| | | 69 | | { |
| | 0 | 70 | | return context.Document; |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | SemanticModel? semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(fa |
| | 0 | 74 | | if (semanticModel == null) |
| | | 75 | | { |
| | 0 | 76 | | return context.Document; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // Get symbol for the new attribute |
| | 0 | 80 | | string fullName = "Hertzole.ScriptableValues." + attributeName; |
| | 0 | 81 | | INamedTypeSymbol? newAttributeSymbol = semanticModel.Compilation.GetTypeByMetadataName(fullName); |
| | 0 | 82 | | if (newAttributeSymbol == null) |
| | | 83 | | { |
| | | 84 | | Log.Info("Could not find symbol for new attribute: " + fullName); |
| | 0 | 85 | | return context.Document; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | DocumentEditor? editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(fa |
| | 0 | 89 | | SyntaxNode newAttribute = editor.Generator.Attribute(editor.Generator.TypeExpression(newAttributeSymbol)); |
| | | 90 | | |
| | 0 | 91 | | editor.ReplaceNode(attribute, newAttribute); |
| | | 92 | | |
| | 0 | 93 | | return editor.GetChangedDocument(); |
| | 0 | 94 | | } |
| | | 95 | | } |