internal static AttributeSyntax GetGeneratedCodeAttributeSyntax()
 {
     return
         (SF.Attribute(typeof(GeneratedCodeAttribute).GetNameSyntax())
          .AddArgumentListArguments(
              SF.AttributeArgument(CodeGeneratorName.GetLiteralExpression()),
              SF.AttributeArgument(CodeGeneratorVersion.GetLiteralExpression())));
 }
        public static CompilationUnitSyntax AddGeneratedCodeAttribute(GeneratedSyntax generatedSyntax)
        {
            var codeGenTargetAttributes =
                SF.AttributeList()
                .AddAttributes(
                    generatedSyntax.SourceAssemblies.Select(
                        asm =>
                        SF.Attribute(typeof(OrleansCodeGenerationTargetAttribute).GetNameSyntax())
                        .AddArgumentListArguments(
                            SF.AttributeArgument(asm.GetName().FullName.GetLiteralExpression()))).ToArray())
                .WithTarget(SF.AttributeTargetSpecifier(SF.Token(SyntaxKind.AssemblyKeyword)));
            var generatedCodeAttribute =
                SF.AttributeList()
                .AddAttributes(
                    SF.Attribute(typeof(GeneratedCodeAttribute).GetNameSyntax())
                    .AddArgumentListArguments(
                        SF.AttributeArgument("Orleans-CodeGenerator".GetLiteralExpression()),
                        SF.AttributeArgument(RuntimeVersion.FileVersion.GetLiteralExpression())))
                .WithTarget(SF.AttributeTargetSpecifier(SF.Token(SyntaxKind.AssemblyKeyword)));

            return(generatedSyntax.Syntax.AddAttributeLists(generatedCodeAttribute, codeGenTargetAttributes));
        }
 public static AttributeSyntax Attribute(string attributeName)
 {
     return(SF.Attribute(SF.IdentifierName(attributeName)));
 }
 public static AttributeSyntax AttributeWithArgument(string attributeName, params string[] attributeArguments)
 {
     return(SF.Attribute(SF.IdentifierName(attributeName),
                         SF.AttributeArgumentList()
                         .WithArguments(SF.SeparatedList(attributeArguments.Select(arg => SF.AttributeArgument(SF.IdentifierName(arg)))))));
 }
示例#5
0
        private static async Task <Document> GenerateCodeFixAsync(Document document,
                                                                  ClassDeclarationSyntax node,
                                                                  CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            var symbol = semanticModel.GetDeclaredSymbol(node);

            if (symbol is null)
            {
                throw new NullReferenceException(nameof(symbol));
            }

            var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken);

            if (syntaxTree is null)
            {
                throw new NullReferenceException(nameof(syntaxTree));
            }

            var flags = GetMetadataFlags(symbol);
            List <AttributeSyntax> list = new(3);

            if ((flags & MetadataFlags.HasEditorGroup) == 0)
            {
                var args = SF.ParseAttributeArgumentList("(group_Unknown)");
                list.Add(SF.Attribute(SF.IdentifierName("EditorGroup"), args));
            }
            if ((flags & MetadataFlags.HasMetaImage) == 0)
            {
                var args = SF.ParseAttributeArgumentList("(tex_)");
                list.Add(SF.Attribute(SF.IdentifierName("MetaImage"), args));
            }
            if ((flags & MetadataFlags.HasMetaInfo) == 0)
            {
                string className = symbol.Name;
                var    args      = SF.ParseAttributeArgumentList($"({nameof(Lang)}.Default, \"{ParsePascalName(className)}\", \"todo\")");
                list.Add(SF.Attribute(SF.IdentifierName("MetaInfo"), args));
                var args2 = SF.ParseAttributeArgumentList($"({nameof(Lang)}.{nameof(Lang.schinese)}, \"\", \"\")");
                list.Add(SF.Attribute(SF.IdentifierName("MetaInfo"), args2));
            }
            if ((flags & MetadataFlags.HasMetaType) == 0)
            {
                MetaType metaType = 0; // 0 = MetaType.Undefined
                if (symbol.HasBaseType("AncientMysteries.Items.AMStaff"))
                {
                    metaType = MetaType.Magic;
                    goto mustBeIt;
                }
                if (symbol.HasBaseType("AncientMysteries.Items.AMMelee"))
                {
                    metaType = MetaType.Melee;
                    goto mustBeIt;
                }
                if (symbol.HasBaseType("AncientMysteries.Items.AMChestPlate",
                                       "AncientMysteries.Items.AMBoots",
                                       "AncientMysteries.Items.AMEquipment",
                                       "AncientMysteries.Items.AMHelmet"))
                {
                    metaType = MetaType.Equipment;
                    goto mustBeIt;
                }
                if (symbol.HasBaseType("AncientMysteries.Items.AMThrowable"))
                {
                    metaType = MetaType.Throwable;
                    goto mustBeIt;
                }
                if (symbol.HasBaseType("AncientMysteries.Items.AMDecoration"))
                {
                    metaType = MetaType.Decoration;
                    goto mustBeIt;
                }
                if (symbol.HasBaseType("AncientMysteries.Items.AMGun"))
                {
                    metaType = MetaType.Gun;
                    goto mustBeIt;
                }
mustBeIt:
                var args = SF.ParseAttributeArgumentList($"(MetaType.{metaType})");
                list.Add(SF.Attribute(SF.IdentifierName("MetaType"), args));
            }
            if (list.Count == 0)
            {
                return(document);
            }
            var updatedNode = node;

            foreach (var item in list)
            {
                updatedNode = updatedNode.AddAttributeLists(SF.AttributeList(SF.SingletonSeparatedList(item)));
            }

            var root = await syntaxTree.GetRootAsync(cancellationToken);

            var updatedSyntaxTree = root.ReplaceNode(node, updatedNode);

            return(document.WithSyntaxRoot(updatedSyntaxTree));
        }