Exemple #1
0
        private static IEnumerable <FieldDeclarationSyntax> SplitFieldDeclaration(FieldDeclarationSyntax declaration)
        {
            SeparatedSyntaxList <VariableDeclaratorSyntax> variables = declaration.Declaration.Variables;

            FieldDeclarationSyntax declaration2 = declaration.WithoutTrivia();

            for (int i = 0; i < variables.Count; i++)
            {
                FieldDeclarationSyntax newDeclaration = FieldDeclaration(
                    declaration2.AttributeLists,
                    declaration2.Modifiers,
                    VariableDeclaration(
                        declaration2.Declaration.Type,
                        SingletonSeparatedList(variables[i])));

                if (i == 0)
                {
                    newDeclaration = newDeclaration.WithLeadingTrivia(declaration.GetLeadingTrivia());
                }

                if (i == variables.Count - 1)
                {
                    newDeclaration = newDeclaration.WithTrailingTrivia(declaration.GetTrailingTrivia());
                }

                yield return(newDeclaration.WithFormatterAnnotation());
            }
        }
Exemple #2
0
 public SimpleProperty(FieldDeclarationSyntax d)
 {
     Type           = d.Declaration.Type;
     Name           = d.Declaration.Variables[0].Identifier.Text;
     LeadingTrivia  = d.GetLeadingTrivia();
     TrailingTrivia = d.GetTrailingTrivia();
 }
Exemple #3
0
 public Field(FieldDeclarationSyntax d)
 {
     Type           = d.Declaration.Type;
     Name           = d.Declaration.Variables[0].Identifier.Text;
     LeadingTrivia  = d.GetLeadingTrivia();
     TrailingTrivia = d.GetTrailingTrivia();
     _bits          = FindBitFieldAttribute(d);
 }
 public SimpleProperty(FieldDeclarationSyntax d, CodeGenerationOptions options)
 {
     Type           = d.Declaration.Type;
     Name           = d.Declaration.Variables[0].Identifier.Text;
     LeadingTrivia  = d.GetLeadingTrivia();
     TrailingTrivia = d.GetTrailingTrivia();
     Options        = options;
 }
Exemple #5
0
        private static SyntaxNode MakeSingleFieldReadonly(SyntaxNode root, FieldDeclarationSyntax fieldDeclaration)
        {
            var newFieldDeclaration = fieldDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))
                                      .WithTrailingTrivia(fieldDeclaration.GetTrailingTrivia())
                                      .WithLeadingTrivia(fieldDeclaration.GetLeadingTrivia())
                                      .WithAdditionalAnnotations(Formatter.Annotation);
            var newRoot = root.ReplaceNode(fieldDeclaration, newFieldDeclaration);

            return(newRoot);
        }
        public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax field)
        {
            if (!field.Modifiers.Any(SyntaxKind.PrivateKeyword))
            {
                return(field);
            }

            if (field.Modifiers.Any(SyntaxKind.ConstKeyword))
            {
                return(field);
            }

            if (field.Modifiers.Any(SyntaxKind.StaticKeyword))
            {
                return(field);
            }


            //var variables = new List<VariableDeclaratorSyntax>();
            var variables = new List <VariableDeclaratorSyntax>();

            foreach (var variable in field.Declaration.Variables)
            {
                var newVariable = variable.WithIdentifier(SyntaxFactory.Identifier(GetChangedName(variable.Identifier.ValueText))
                                                          .WithLeadingTrivia(variable.Identifier.LeadingTrivia)
                                                          .WithTrailingTrivia(variable.Identifier.TrailingTrivia))
                                  .WithLeadingTrivia(variable.GetLeadingTrivia())
                                  .WithTrailingTrivia(variable.GetTrailingTrivia());

                variables.Add(newVariable);
            }

            field = field
                    .WithDeclaration(SyntaxFactory.VariableDeclaration(field.Declaration.Type, SyntaxFactory.SeparatedList(variables)))
                    .WithLeadingTrivia(field.GetLeadingTrivia())
                    .WithTrailingTrivia(field.GetTrailingTrivia());

            return(field);
        }
        private async Task <Solution> ChangeToReadOnlySpan(Document document, FieldDeclarationSyntax fieldDeclaration, CancellationToken cancellationToken)
        {
            var originalSolution = document.Project.Solution;
            var separatedTypes   = new SeparatedSyntaxList <TypeSyntax>().Add((fieldDeclaration.Declaration.Type as ArrayTypeSyntax).ElementType);
            var typeListSyntax   = SyntaxFactory.TypeArgumentList(separatedTypes);
            var readonlySpanType = SyntaxFactory.GenericName(SyntaxFactory.Identifier("ReadOnlySpan"), typeListSyntax);

            var equalsValueClauseSyntax = fieldDeclaration.Declaration.Variables.First().Initializer;
            var arrowExpressionSyntax   = SyntaxFactory.ArrowExpressionClause(equalsValueClauseSyntax.Value);
            var propertyDeclaration     = SyntaxFactory
                                          .PropertyDeclaration
                                              (fieldDeclaration.AttributeLists,
                                              new SyntaxTokenList(fieldDeclaration.Modifiers.Where(x => !x.IsKind(SyntaxKind.ReadOnlyKeyword))),
                                              readonlySpanType,
                                              null,
                                              fieldDeclaration.Declaration.Variables.First().Identifier,
                                              null,
                                              arrowExpressionSyntax,
                                              null);

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var newRoot = root.ReplaceNode(fieldDeclaration, propertyDeclaration.WithLeadingTrivia(fieldDeclaration.GetLeadingTrivia()).WithSemicolonToken(fieldDeclaration.SemicolonToken).WithTrailingTrivia(fieldDeclaration.GetTrailingTrivia()));

            return(originalSolution.WithDocumentSyntaxRoot(document.Id, newRoot));
        }
        public override Microsoft.CodeAnalysis.SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            FieldDeclarationSyntax newNode = null;
            var parent = node.Parent;

            // Verify we have the correct context for the Field Declaration

            switch (_declarationLocation)
            {
            case SyntaxNode.FieldDeclarationLocation.Class:
                if (parent.Kind() != SyntaxKind.ClassBlock)
                {
                    return(node);
                }
                break;

            case SyntaxNode.FieldDeclarationLocation.Module:
                if (parent.Kind() != SyntaxKind.ModuleBlock)
                {
                    return(node);
                }
                break;

            case SyntaxNode.FieldDeclarationLocation.Structure:
                if (parent.Kind() != SyntaxKind.StructureBlock)
                {
                    return(node);
                }
                break;
            }

            if (_targetPatternRegEx.Match(node.Declarators.First().Names.First().Identifier.ToString()).Success)
            {
                try
                {
                    // Verify this expression is on line by itself.  May not need to do this.
                    // See if can have multiple FieldDeclarations on same line.

                    if (VNCCA.Helpers.VB.IsOnLineByItself(node))
                    {
                        // HACK(crhodes)
                        // Figure out how to get Helpers to work here.
                        Messages.AppendLine(String.Format("Removing {0} {1}",
                                                          VNCCA.Helpers.VB.GetContainingContext(node, _configurationOptions),
                                                          node.ToString()));

                        if (_commentOutOnly)
                        {
                            List <SyntaxTrivia> newTrivia    = new List <SyntaxTrivia>();
                            string existingLeadingTrivia     = node.GetLeadingTrivia().ToString();
                            string existingLeadingTriviaFull = node.GetLeadingTrivia().ToFullString();

                            string existingTrailingTrivia     = node.GetTrailingTrivia().ToString();
                            string existingTrailingTriviaFull = node.GetTrailingTrivia().ToFullString();

                            string startOfLineWhiteSpace = existingLeadingTrivia.Replace(System.Environment.NewLine, "");

                            newTrivia.Add(SyntaxFactory.CommentTrivia(existingLeadingTriviaFull));
                            newTrivia.Add(SyntaxFactory.CommentTrivia(VNCCA.Helpers.VB.MultiLineComment(_comment, startOfLineWhiteSpace)));
                            newTrivia.Add(SyntaxFactory.CommentTrivia("' "));

                            newNode = node.WithLeadingTrivia(newTrivia);
                        }
                        else
                        {
                            // This removes the node

                            newNode = null;
                        }
                    }
                    else
                    {
                        Messages.AppendLine(String.Format("node: >{0}< >{1}< Is NOT OnLineByItself", node.ToString(), node.ToFullString()));
                        newNode = node;
                    }
                }
                catch (Exception ex)
                {
                    Messages.AppendLine(string.Format("Ex:{0} InnerEx:{1}", ex.ToString(), ex.InnerException.ToString()));
                }
            }
            else
            {
                newNode = node;
            }

            return(newNode);
        }