| | | 1 | | using System; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | using System.Composition; |
| | | 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; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 11 | | using Microsoft.CodeAnalysis.Editing; |
| | | 12 | | using Microsoft.CodeAnalysis.Text; |
| | | 13 | | |
| | | 14 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 15 | | |
| | | 16 | | [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(NoCallbackImplementationCodeFixProvider))] |
| | | 17 | | [Shared] |
| | | 18 | | public sealed class NoCallbackImplementationCodeFixProvider : CodeFixProvider |
| | | 19 | | { |
| | 0 | 20 | | private static readonly ImmutableArray<string> fixableDiagnosticIds = ImmutableArray.Create(DiagnosticIdentifiers.No |
| | | 21 | | |
| | | 22 | | public override ImmutableArray<string> FixableDiagnosticIds |
| | | 23 | | { |
| | 0 | 24 | | get { return fixableDiagnosticIds; } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public override FixAllProvider GetFixAllProvider() |
| | | 28 | | { |
| | 0 | 29 | | return WellKnownFixAllProviders.BatchFixer; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public override Task RegisterCodeFixesAsync(CodeFixContext context) |
| | | 33 | | { |
| | 0 | 34 | | Diagnostic diagnostic = context.Diagnostics[0]; |
| | 0 | 35 | | if (!diagnostic.Properties.TryGetValue("callbackName", out string? callbackName)) |
| | | 36 | | { |
| | 0 | 37 | | callbackName = string.Empty; |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | context.RegisterCodeFix(CodeAction.Create( |
| | 0 | 41 | | string.Format(Resources.HSV0001CodeFixTitle, callbackName), |
| | 0 | 42 | | token => CreateChangedDocument(context, token), |
| | 0 | 43 | | nameof(Resources.HSV0001CodeFixTitle)), diagnostic); |
| | | 44 | | |
| | 0 | 45 | | return Task.CompletedTask; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | private static async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToke |
| | | 49 | | { |
| | 0 | 50 | | SyntaxNode? root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 51 | | if (root == null) |
| | | 52 | | { |
| | 0 | 53 | | return context.Document; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Find the declaration |
| | 0 | 57 | | TextSpan diagnosticSpan = context.Diagnostics[0].Location.SourceSpan; |
| | 0 | 58 | | SyntaxNode node = root.FindNode(diagnosticSpan); |
| | | 59 | | |
| | 0 | 60 | | if (node is not AttributeSyntax attribute) |
| | | 61 | | { |
| | 0 | 62 | | return context.Document; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // Get the member declaration |
| | 0 | 66 | | MemberDeclarationSyntax? memberDeclaration = attribute.FirstAncestorOrSelf<MemberDeclarationSyntax>(); |
| | 0 | 67 | | if (memberDeclaration == null) |
| | | 68 | | { |
| | 0 | 69 | | return context.Document; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | SemanticModel? semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(fa |
| | 0 | 73 | | if (semanticModel == null) |
| | | 74 | | { |
| | 0 | 75 | | return context.Document; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // Get the field/property wrapper |
| | 0 | 79 | | if (!memberDeclaration.TryGetFieldOrPropertyWrapper(in semanticModel, out ImmutableArray<FieldOrPropertyWrapper> |
| | | 80 | | { |
| | 0 | 81 | | return context.Document; |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | Diagnostic diagnostic = context.Diagnostics[0]; |
| | 0 | 85 | | if (!diagnostic.Properties.TryGetValue("fieldName", out string? fieldName)) |
| | | 86 | | { |
| | 0 | 87 | | return context.Document; |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | FieldOrPropertyWrapper? targetWrapper = null; |
| | 0 | 91 | | for (int i = 0; i < wrappers.Length; i++) |
| | | 92 | | { |
| | 0 | 93 | | FieldOrPropertyWrapper wrapper = wrappers.ItemRef(i); |
| | 0 | 94 | | if (wrapper.Name == fieldName) |
| | | 95 | | { |
| | 0 | 96 | | targetWrapper = wrapper; |
| | 0 | 97 | | break; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if (targetWrapper == null) |
| | | 102 | | { |
| | 0 | 103 | | return context.Document; |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | if (!ScriptableValueHelper.TryGetScriptableType(in semanticModel, (INamedTypeSymbol?) targetWrapper.Value.Type, |
| | 0 | 107 | | out ITypeSymbol? genericType) || |
| | 0 | 108 | | !diagnostic.Properties.TryGetValue("callbackName", out string? callbackName)) |
| | | 109 | | { |
| | 0 | 110 | | return context.Document; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | DocumentEditor editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(fal |
| | | 114 | | |
| | 0 | 115 | | SyntaxNode? newMethod = GetImplementationMethod(in editor, in semanticModel, in callbackName!, in scriptableType |
| | | 116 | | |
| | 0 | 117 | | editor.InsertAfter(memberDeclaration, newMethod); |
| | | 118 | | |
| | 0 | 119 | | return editor.GetChangedDocument(); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private static SyntaxNode GetImplementationMethod(in DocumentEditor editor, |
| | | 123 | | in SemanticModel semanticModel, |
| | | 124 | | in string callbackName, |
| | | 125 | | in ScriptableType scriptableType, |
| | | 126 | | in ITypeSymbol? genericType) |
| | | 127 | | { |
| | 0 | 128 | | SyntaxGenerator generator = editor.Generator; |
| | 0 | 129 | | using ArrayBuilder<SyntaxNode> parametersBuilder = new ArrayBuilder<SyntaxNode>(); |
| | | 130 | | |
| | 0 | 131 | | switch (scriptableType) |
| | | 132 | | { |
| | | 133 | | case ScriptableType.Value: |
| | 0 | 134 | | SyntaxNode genericTypeExpression = generator.TypeExpression(genericType!, true); |
| | | 135 | | |
| | 0 | 136 | | parametersBuilder.Add(generator.ParameterDeclaration("oldValue", genericTypeExpression)); |
| | 0 | 137 | | parametersBuilder.Add(generator.ParameterDeclaration("newValue", genericTypeExpression)); |
| | 0 | 138 | | break; |
| | | 139 | | case ScriptableType.GenericEvent: |
| | | 140 | | case ScriptableType.Event: |
| | 0 | 141 | | SyntaxNode senderExpression = generator.TypeExpression(semanticModel.Compilation.GetSpecialType(SpecialT |
| | 0 | 142 | | SyntaxNode argsExpression = generator.TypeExpression(genericType ?? semanticModel.Compilation.GetTypeByM |
| | | 143 | | |
| | 0 | 144 | | parametersBuilder.Add(generator.ParameterDeclaration("sender", senderExpression)); |
| | 0 | 145 | | parametersBuilder.Add(generator.ParameterDeclaration("args", argsExpression)); |
| | 0 | 146 | | break; |
| | | 147 | | case ScriptableType.Pool: |
| | 0 | 148 | | SyntaxNode poolActionExpression = |
| | 0 | 149 | | generator.TypeExpression(semanticModel.Compilation.GetTypeByMetadataName("Hertzole.ScriptableValues. |
| | | 150 | | |
| | 0 | 151 | | SyntaxNode poolItemExpression = generator.TypeExpression(genericType!, true); |
| | | 152 | | |
| | 0 | 153 | | parametersBuilder.Add(generator.ParameterDeclaration("action", poolActionExpression)); |
| | 0 | 154 | | parametersBuilder.Add(generator.ParameterDeclaration("item", poolItemExpression)); |
| | 0 | 155 | | break; |
| | | 156 | | case ScriptableType.List: |
| | | 157 | | case ScriptableType.Dictionary: |
| | 0 | 158 | | SyntaxNode collectionArgsExpression = generator.TypeExpression(semanticModel.Compilation.GetCollectionCh |
| | | 159 | | |
| | 0 | 160 | | parametersBuilder.Add(generator.ParameterDeclaration("args", collectionArgsExpression)); |
| | 0 | 161 | | break; |
| | | 162 | | default: |
| | 0 | 163 | | throw new NotSupportedException("Unsupported scriptable type: " + scriptableType); |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | SyntaxNode newMethod = generator.MethodDeclaration( |
| | 0 | 167 | | callbackName, |
| | 0 | 168 | | parametersBuilder.ToImmutable(), |
| | 0 | 169 | | accessibility: Accessibility.Private, |
| | 0 | 170 | | modifiers: DeclarationModifiers.Partial); |
| | | 171 | | |
| | 0 | 172 | | ThrowStatementSyntax throwStatement = SyntaxFactory.ThrowStatement( |
| | 0 | 173 | | SyntaxFactory.ObjectCreationExpression( |
| | 0 | 174 | | SyntaxFactory.ParseTypeName("System.NotImplementedException"), |
| | 0 | 175 | | SyntaxFactory.ArgumentList(), |
| | 0 | 176 | | null)); |
| | | 177 | | |
| | 0 | 178 | | newMethod = generator.WithStatements(newMethod, [throwStatement]); |
| | | 179 | | |
| | 0 | 180 | | return newMethod; |
| | 0 | 181 | | } |
| | | 182 | | } |