public RoslynClassBuilder AddProperty(string name, string typeName, object initialValue, AttributeListSyntax attributes)
        {
            var propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(typeName), name)
                                      .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))

                                      .AddAccessorListAccessors(
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));


            if (attributes?.ChildNodes()?.Count() > 0)
            {
                propertyDeclaration = propertyDeclaration.AddAttributeLists(attributes);
            }

            Properties.Add(propertyDeclaration);
            return(this);
        }
            private void AddOutliningTagToAttribute(AttributeListSyntax attributeListNode)
            {
                if (AcuminatorVSPackage.Instance?.UseBqlOutlining != true || attributeListNode.Attributes.Count > 1)
                {
                    return;
                }

                AttributeSyntax attribute = attributeListNode.ChildNodes()
                                            .OfType <AttributeSyntax>()
                                            .FirstOrDefault();

                if (attribute?.ArgumentList == null || attribute.ArgumentList.Arguments.Count == 0 || _cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                string collapsedText = GetAttributeName(attribute);
                ITagSpan <IOutliningRegionTag> tag = attributeListNode.Span.ToOutliningTagSpan(_tagger.Snapshot, collapsedText);

                _tagger.OutliningsTagsCache.AddTag(tag);
            }
        public RoslynClassBuilder AddMethod(string name, string returns, string code, AttributeListSyntax attributes)
        {
            var methodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(returns), name)
                                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

            var MethodStatements = new List <StatementSyntax>();

            if (string.IsNullOrWhiteSpace(code) == false)
            {
                MethodStatements.Add(SyntaxFactory.ParseStatement(code));
            }

            methodDeclaration = methodDeclaration.WithBody(SyntaxFactory.Block(
                                                               MethodStatements
                                                               ));

            if (attributes?.ChildNodes()?.Count() > 0)
            {
                methodDeclaration = methodDeclaration.AddAttributeLists(attributes);
            }

            Methods.Add(methodDeclaration);
            return(this);
        }
示例#4
0
 public override SyntaxNode VisitAttributeList(AttributeListSyntax node)
 {
     //TODO:暂全部移除Attribute
     base.VisitAttributeList(node);
     return(node.RemoveNodes(node.ChildNodes(), SyntaxRemoveOptions.KeepEndOfLine));
 }
示例#5
0
        public static CheckedException.Core.DiagnosticSeverity?GetAttributeParameterSeverity(SyntaxNodeAnalysisContext context, AttributeListSyntax attrib)
        {
            if (attrib.ChildNodes().Count() == 0)
            {
                return(null);
            }

            IdentifierNameSyntax attributeType = null;

            if (attrib.ChildNodes().First().ChildNodes().FirstOrDefault() is QualifiedNameSyntax)
            {
                attributeType = attrib.ChildNodes().First().ChildNodes().FirstOrDefault().DescendantNodes().OfType <IdentifierNameSyntax>().LastOrDefault();
            }
            else
            {
                attributeType = attrib.ChildNodes().First().DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();
            }

            if (attributeType == null)
            {
                return(null);
            }

            var attributeInfo = context.SemanticModel.GetTypeInfo(attributeType);

            if (attributeInfo.Type == null)
            {
                return(null);
            }

            if (attributeInfo.Type.ToString() != typeof(ThrowsExceptionAttribute).ToString())
            {
                return(null);
            }

            var severity = attrib.Attributes.First().ArgumentList.DescendantNodes().OfType <MemberAccessExpressionSyntax>().FirstOrDefault();

            if (severity == null)
            {
                return(Core.DiagnosticSeverity.Error);
            }

            var typeInfo = context.SemanticModel.GetTypeInfo(severity.Expression);

            if (typeInfo.Type == null || typeInfo.Type.ToString() != typeof(Core.DiagnosticSeverity).ToString())
            {
                return(null);
            }

            Core.DiagnosticSeverity?result = null;
            foreach (var sev in Enum.GetValues(typeof(Core.DiagnosticSeverity)))
            {
                if (((Core.DiagnosticSeverity)sev).ToString().Equals(severity.Name.Identifier.Text))
                {
                    result = (Core.DiagnosticSeverity)sev;
                    break;
                }
            }

            return(result);
        }
示例#6
0
        public static ITypeSymbol GetAttributeParameterType(SyntaxNodeAnalysisContext context, AttributeListSyntax attrib)
        {
            if (attrib.ChildNodes().Count() == 0)
            {
                return(null);
            }

            IdentifierNameSyntax attributeType = null;

            if (attrib.ChildNodes().First().ChildNodes().FirstOrDefault() is QualifiedNameSyntax)
            {
                attributeType = attrib.ChildNodes().First().ChildNodes().FirstOrDefault().DescendantNodes().OfType <IdentifierNameSyntax>().LastOrDefault();
            }
            else
            {
                attributeType = attrib.ChildNodes().First().DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();
            }

            if (attributeType == null)
            {
                return(null);
            }

            var attributeInfo = context.SemanticModel.GetTypeInfo(attributeType);

            if (attributeInfo.Type == null)
            {
                return(null);
            }

            if (attributeInfo.Type.ToString() != typeof(ThrowsExceptionAttribute).ToString())
            {
                return(null);
            }

            var typeOf = attrib.Attributes.First().ArgumentList.DescendantNodes().OfType <TypeOfExpressionSyntax>().FirstOrDefault();

            if (typeOf == null)
            {
                return(null);
            }

            IdentifierNameSyntax exceptionType = null;
            var qualifiedName = typeOf.DescendantNodes().OfType <QualifiedNameSyntax>().FirstOrDefault();

            if (qualifiedName == null)
            {
                exceptionType = typeOf.DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();
            }
            else
            {
                exceptionType = qualifiedName.DescendantNodes().OfType <IdentifierNameSyntax>().LastOrDefault();
            }

            if (exceptionType == null)
            {
                return(null);
            }

            var info = context.SemanticModel.GetTypeInfo(exceptionType);

            return(info.Type);
        }