/// <summary>
 /// Find LongName argument from the OptionAttribute.
 /// </summary>
 /// <param name="attributeSyntax"></param>
 /// <returns>
 /// name: the LongName value.
 /// location: the syntax tree location of the LongName argument value.
 /// </returns>
 private (string? name, Location? location) AnalyzeOptionAttributeArguments(AttributeSyntax attributeSyntax)
 {
     var argumentList = attributeSyntax.ChildNodes().OfType<AttributeArgumentListSyntax>().FirstOrDefault();
     if (argumentList != null)
     {
         var attributeArguments = argumentList.ChildNodes().OfType<AttributeArgumentSyntax>();
         foreach (var attributeArgument in attributeArguments)
         {
             var expressionSyntax = attributeArgument.Expression;
             var expression = expressionSyntax.ToString();
             var nameEqualsExists = attributeArgument.ChildNodes().OfType<NameEqualsSyntax>().Any();
             var longNameEqualsExists = attributeArgument.ChildNodes().OfType<NameEqualsSyntax>().Any(x => x.Name.ToString() == "LongName");
             var mayBeLongName = !nameEqualsExists || longNameEqualsExists;
             if (expression != null
                 && expression.StartsWith("\"", StringComparison.OrdinalIgnoreCase)
                 && expression.EndsWith("\"", StringComparison.OrdinalIgnoreCase)
                 && mayBeLongName)
             {
                 var value = expression.Substring(1, expression.Length - 2);
                 if (value.Length >= 2)
                 {
                     var isPascalCase = NamingHelper.CheckIsPascalCase(value);
                     if (!isPascalCase)
                     {
                         return (value, expressionSyntax.GetLocation());
                     }
                 }
                 break;
             }
         }
     }
     return (null, null);
 }
            private string GetAttributeName(AttributeSyntax attribute)
            {
                foreach (SyntaxNode childNode in attribute.ChildNodes())
                {
                    if (_cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }

                    switch (childNode)
                    {
                    case IdentifierNameSyntax attributeName:
                    {
                        return($"[{attributeName.Identifier.ValueText}]");
                    }

                    case QualifiedNameSyntax qualifiedName:
                    {
                        string identifierText = _tagger.Snapshot.GetText(qualifiedName.Span);
                        return(!identifierText.IsNullOrWhiteSpace()
                                    ? $"[{identifierText}]"
                                    : null);
                    }
                    }
                }

                return(null);
            }
Esempio n. 3
0
 public override void VisitAttribute(AttributeSyntax node)
 {
     CheckDllImport(node);
     foreach (var n in node.ChildNodes())
     {
         Visit(n);
     }
 }
        private static bool IsSpecifiedAttribute(AttributeSyntax attribute, string attributeName)
        {
            if (attribute != null && attribute.ChildNodes().First().ToString() == attributeName)
            {
                return(true);
            }

            return(false);
        }
        public override SyntaxNode?VisitAttribute(AttributeSyntax node)
        {
            if (node.Parent is AttributeListSyntax attributeListSyntax)
            {
                if (attributeListSyntax.Parent is ClassDeclarationSyntax ||
                    attributeListSyntax.Parent is StructDeclarationSyntax ||
                    attributeListSyntax.Parent is DelegateDeclarationSyntax)
                {
                    string?attributeName = null;
                    if (node.Name is IdentifierNameSyntax identifierName)
                    {
                        attributeName = identifierName.ToString();
                    }
                    else if (node.Name is QualifiedNameSyntax qualifiedName)
                    {
                        attributeName = qualifiedName.ChildNodes().OfType <IdentifierNameSyntax>().LastOrDefault()?.ToString();
                    }

                    if (attributeName != null && _attributeNames.Contains(attributeName))
                    {
                        ShouldGenerateGenericCode = true;

                        var argumentList = node.ChildNodes().OfType <AttributeArgumentListSyntax>().FirstOrDefault();
                        if (argumentList is null)
                        {
                            GenerateFromCount = _defaultGenerateFromCount;
                            GenerateToCount   = _defaultGenerateToCount;
                        }
                        else
                        {
                            var arguments   = argumentList.ChildNodes().OfType <AttributeArgumentSyntax>().SelectMany(x => x.ChildNodes());
                            var assignIndex = 0;
                            foreach (var argument in arguments)
                            {
                                if (int.TryParse(argument.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
                                {
                                    if (assignIndex == 0)
                                    {
                                        GenerateFromCount = value;
                                    }
                                    else
                                    {
                                        GenerateToCount = value;
                                    }
                                    assignIndex++;
                                }
                            }
                        }
                    }
                }
            }

            return(base.VisitAttribute(node));
        }