|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CodeActions; |
| 3 | +using Microsoft.CodeAnalysis.CodeRefactorings; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 | +using Microsoft.CodeAnalysis.Editing; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 10 | + |
| 11 | +namespace GalaxyCheck.Xunit.CodeAnalysis.CodeRefactoringProviders |
| 12 | +{ |
| 13 | + [ExportCodeRefactoringProvider(LanguageNames.CSharp)] |
| 14 | + public class ConfigureGenWithMember : CodeRefactoringProvider |
| 15 | + { |
| 16 | + public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) |
| 17 | + { |
| 18 | + var document = context.Document; |
| 19 | + |
| 20 | + var root = await document.GetSyntaxRootAsync(context.CancellationToken); |
| 21 | + if (root is null) return; |
| 22 | + |
| 23 | + var semanticModel = await document.GetSemanticModelAsync(context.CancellationToken); |
| 24 | + if (semanticModel is null) return; |
| 25 | + |
| 26 | + var propertyAttributeType = semanticModel.Compilation.TryGetPropertyAttributeType(); |
| 27 | + if (propertyAttributeType is null) return; |
| 28 | + |
| 29 | + var activeNode = root.FindNode(context.Span); |
| 30 | + |
| 31 | + var activeMethodDeclarationSyntax = activeNode.FirstAncestorOrSelf<MethodDeclarationSyntax>(); |
| 32 | + if (activeMethodDeclarationSyntax is null) return; |
| 33 | + |
| 34 | + var propertyAttribute = activeMethodDeclarationSyntax |
| 35 | + .AttributeLists |
| 36 | + .SelectMany(l => l.Attributes) |
| 37 | + .FirstOrDefault(a => |
| 38 | + { |
| 39 | + var attributeSymbol = semanticModel.GetSymbolInfo(a); |
| 40 | + return SymbolEqualityComparer.Default.Equals(attributeSymbol.Symbol?.ContainingType, propertyAttributeType); |
| 41 | + }); |
| 42 | + if (propertyAttribute is null) return; |
| 43 | + |
| 44 | + var activeParameterSyntax = activeNode.FirstAncestorOrSelf<ParameterSyntax>(); |
| 45 | + if (activeParameterSyntax is not null) |
| 46 | + { |
| 47 | + context.RegisterRefactoring(InsertAndReferenceMemberGen( |
| 48 | + semanticModel, |
| 49 | + document, |
| 50 | + activeMethodDeclarationSyntax, |
| 51 | + activeParameterSyntax)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static CodeAction InsertAndReferenceMemberGen( |
| 56 | + SemanticModel semanticModel, |
| 57 | + Document document, |
| 58 | + MethodDeclarationSyntax methodDeclarationSyntax, |
| 59 | + ParameterSyntax parameterSyntax) |
| 60 | + { |
| 61 | + var compilation = semanticModel.Compilation; |
| 62 | + var title = "Configure generation of parameter with MemberGenAttribute"; |
| 63 | + |
| 64 | + var genGenericType = compilation.GetTypeByMetadataName("GalaxyCheck.IGen`1"); |
| 65 | + var parameterType = semanticModel.GetDeclaredSymbol(parameterSyntax)!.Type; |
| 66 | + var genType = genGenericType!.Construct(parameterType); |
| 67 | + |
| 68 | + var genFactoryType = compilation.GetTypeByMetadataName("GalaxyCheck.Gen")!; |
| 69 | + |
| 70 | + return CodeAction.Create(title, async cancellationToken => |
| 71 | + { |
| 72 | + var editor = await DocumentEditor.CreateAsync(document, cancellationToken); |
| 73 | + var syntaxGenerator = SyntaxGenerator.GetGenerator(document); |
| 74 | + |
| 75 | + var memberName = $"{methodDeclarationSyntax.Identifier.Text}_{parameterSyntax.Identifier.Text}"; |
| 76 | + |
| 77 | + var propertySyntax = CreateProperty(syntaxGenerator, memberName, genType, parameterType); |
| 78 | + var attributeSyntax = CreateMemberGenAttribute(syntaxGenerator, memberName); |
| 79 | + |
| 80 | + editor.InsertBefore(methodDeclarationSyntax, propertySyntax); |
| 81 | + editor.ReplaceNode(parameterSyntax, parameterSyntax.AddAttributeLists(attributeSyntax)); |
| 82 | + return editor.GetChangedDocument(); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private static PropertyDeclarationSyntax CreateProperty(SyntaxGenerator syntaxGenerator, string name, ITypeSymbol genType, ITypeSymbol parameterType) |
| 87 | + { |
| 88 | + var propertyType = syntaxGenerator.TypeExpression(genType); |
| 89 | + |
| 90 | + var propertyExpression = parameterType.Name switch |
| 91 | + { |
| 92 | + "Boolean" => SimpleBuiltInGenInvocation("Boolean"), |
| 93 | + "Byte" => SimpleBuiltInGenInvocation("Byte"), |
| 94 | + "Int16" => SimpleBuiltInGenInvocation("Int16"), |
| 95 | + "Int32" => SimpleBuiltInGenInvocation("Int32"), |
| 96 | + "Int64" => SimpleBuiltInGenInvocation("Int64"), |
| 97 | + "Char" => SimpleBuiltInGenInvocation("Char"), |
| 98 | + "String" => SimpleBuiltInGenInvocation("String"), |
| 99 | + "DateTime" => SimpleBuiltInGenInvocation("DateTime"), |
| 100 | + "Guid" => SimpleBuiltInGenInvocation("Guid"), |
| 101 | + _ => InvocationExpression( |
| 102 | + MemberAccessExpression( |
| 103 | + SyntaxKind.SimpleMemberAccessExpression, |
| 104 | + IdentifierName("Gen"), |
| 105 | + GenericName( |
| 106 | + Identifier("Create")) |
| 107 | + .WithTypeArgumentList( |
| 108 | + TypeArgumentList( |
| 109 | + SingletonSeparatedList<TypeSyntax>(IdentifierName(parameterType.Name)))))) |
| 110 | + }; |
| 111 | + |
| 112 | + var propertyValue = ArrowExpressionClause(propertyExpression); |
| 113 | + |
| 114 | + return ((PropertyDeclarationSyntax)syntaxGenerator.PropertyDeclaration( |
| 115 | + name, |
| 116 | + propertyType, |
| 117 | + Accessibility.Private, |
| 118 | + DeclarationModifiers.Static | DeclarationModifiers.ReadOnly, |
| 119 | + getAccessorStatements: null, |
| 120 | + setAccessorStatements: null)) |
| 121 | + .WithAccessorList(null) |
| 122 | + .WithExpressionBody(propertyValue) |
| 123 | + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); |
| 124 | + } |
| 125 | + |
| 126 | + private static InvocationExpressionSyntax SimpleBuiltInGenInvocation(string methodName) |
| 127 | + { |
| 128 | + return InvocationExpression( |
| 129 | + MemberAccessExpression( |
| 130 | + SyntaxKind.SimpleMemberAccessExpression, |
| 131 | + IdentifierName("Gen"), |
| 132 | + IdentifierName(methodName))); |
| 133 | + } |
| 134 | + |
| 135 | + private static AttributeListSyntax CreateMemberGenAttribute(SyntaxGenerator syntaxGenerator, string memberName) => (AttributeListSyntax)syntaxGenerator.Attribute( |
| 136 | + "MemberGen", |
| 137 | + syntaxGenerator.NameOfExpression(syntaxGenerator.IdentifierName(memberName))); |
| 138 | + } |
| 139 | +} |
0 commit comments