コード例 #1
0
        public void Visit()
        {
            string sourceWith    = File.ReadAllText(m_targetWithAttribute);
            string sourceWithout = File.ReadAllText(m_targetWithoutAttribute);

            ITypeSymbol typeSymbol = m_compilation.GetTypeByMetadataName(typeof(TestAttribute).FullName);
            SyntaxNode  attribute  = m_generator.Attribute(m_generator.TypeExpression(typeSymbol));
            SyntaxNode  node       = SyntaxFactory.ParseSyntaxTree(sourceWithout).GetRoot();

            var rewriter = new CodeGenerateRewriterAddAttributeToNode(CodeAnalysisEditorUtility.Generator, attribute, syntaxNode => syntaxNode.Kind() == SyntaxKind.ClassDeclaration);
            var format   = new CodeGenerateRewriterFormatAttributeList();

            node = rewriter.Visit(node);
            node = format.Visit(node);

            string result = node.ToFullString();

            Assert.AreEqual(sourceWith, result);
        }
コード例 #2
0
        private static SyntaxNode GenerateResolverAsset(string resolverName, string namespaceRoot, Compilation compilation = null, SyntaxGenerator generator = null)
        {
            if (string.IsNullOrEmpty(resolverName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(resolverName));
            }
            if (string.IsNullOrEmpty(namespaceRoot))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(namespaceRoot));
            }
            if (compilation == null)
            {
                compilation = CodeAnalysisEditorUtility.ProjectCompilation;
            }
            if (generator == null)
            {
                generator = CodeAnalysisEditorUtility.Generator;
            }

            if (!compilation.TryConstructTypeSymbol(typeof(Utf8JsonResolverAsset), out ITypeSymbol baseTypeSymbol))
            {
                throw new ArgumentException("Can't construct asset base type from the specified compilation.");
            }

            if (!compilation.TryConstructTypeSymbol(typeof(IJsonFormatterResolver), out ITypeSymbol resolverTypeSymbol))
            {
                throw new ArgumentException("Can't construct resolver type from the specified compilation.");
            }

            if (!compilation.TryConstructTypeSymbol(typeof(CreateAssetMenuAttribute), out ITypeSymbol createAttributeTypeSymbol))
            {
                throw new ArgumentException("Can't construct create attribute type from the specified compilation.");
            }

            string namespaceName = $"{namespaceRoot}.Asset";
            string menuName      = $"UGF/Utf8Json/Generated/{namespaceRoot}.{resolverName}";
            string className     = $"{resolverName}Asset";

            SyntaxNode baseType     = generator.TypeExpression(baseTypeSymbol);
            SyntaxNode resolverType = generator.TypeExpression(resolverTypeSymbol);
            SyntaxNode attribute    = generator.Attribute(generator.TypeExpression(createAttributeTypeSymbol), new[]
            {
                generator.AssignmentStatement(generator.IdentifierName("menuName"), generator.LiteralExpression(menuName)),
                generator.AssignmentStatement(generator.IdentifierName("order"), generator.LiteralExpression(2000))
            });

            var addAttributeToNode = new CodeGenerateRewriterAddAttributeToNode(generator, attribute, node => node.IsKind(SyntaxKind.ClassDeclaration));

            SyntaxNode declaration = generator.NamespaceDeclaration(namespaceName, new[]
            {
                generator.ClassDeclaration(className, null, Accessibility.Public, DeclarationModifiers.None, baseType, null, new[]
                {
                    generator.MethodDeclaration("GetResolver", null, null, resolverType, Accessibility.Public, DeclarationModifiers.Override, new[]
                    {
                        generator.ReturnStatement(generator.IdentifierName($"{namespaceRoot}.Resolvers.{resolverName}.Instance"))
                    })
                })
            });

            declaration = addAttributeToNode.Visit(declaration);
            declaration = declaration.NormalizeWhitespace();
            declaration = declaration.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);

            return(declaration);
        }