示例#1
0
        public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
        {
            if (node.Identifier.ToString() == renameFrom.ToString())
                node = node.WithIdentifier(renameTo);

            return base.VisitVariableDeclarator(node);
        }
示例#2
0
        public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
        {
            if (node.Identifier.ToString() == _renameFrom.ToString())
            {
                node = node.WithIdentifier(renameTo);
            }

            return(base.VisitVariableDeclarator(node));
        }
示例#3
0
            public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
            {
                var symbol = model.GetDeclaredSymbol(node);

                if (symbol != null)
                {
                    string newName = rename(symbol);
                    if (newName != null)
                    {
                        return(node.WithIdentifier(node.Identifier.CopyAnnotationsTo(SyntaxFactory.Identifier(newName))));
                    }
                }
                return(base.VisitVariableDeclarator(node));
            }
示例#4
0
        private static List <BaseFieldDeclarationSyntax> DeclarationSplitter(
            Document document,
            VariableDeclarationSyntax declaration,
            Func <VariableDeclarationSyntax, BaseFieldDeclarationSyntax> declarationFactory,
            SyntaxTriviaList declarationTrailingTrivia)
        {
            SeparatedSyntaxList <VariableDeclaratorSyntax> variables = declaration.Variables;
            VariableDeclaratorSyntax   first    = variables.First();
            BaseFieldDeclarationSyntax previous = null;
            var newFieldDeclarations            = new List <BaseFieldDeclarationSyntax>(variables.Count);

            foreach (SyntaxNodeOrToken nodeOrToken in variables.GetWithSeparators())
            {
                if (previous == null)
                {
                    VariableDeclaratorSyntax variable = (VariableDeclaratorSyntax)nodeOrToken.AsNode();
                    variable = variable.WithIdentifier(variable.Identifier.WithoutLeadingWhitespace());
                    var variableDeclarator = SyntaxFactory.SingletonSeparatedList(variable);
                    previous = declarationFactory(declaration.WithVariables(variableDeclarator));

                    if (variable != first)
                    {
                        var triviaList = previous.GetLeadingTrivia().WithoutDirectiveTrivia();
                        previous = previous.WithLeadingTrivia(triviaList);
                    }
                }
                else
                {
                    SyntaxToken      commaToken     = nodeOrToken.AsToken();
                    SyntaxTriviaList trailingTrivia = commaToken.TrailingTrivia;
                    if (trailingTrivia.Any())
                    {
                        if (!trailingTrivia.Last().IsKind(SyntaxKind.EndOfLineTrivia))
                        {
                            trailingTrivia = trailingTrivia.WithoutTrailingWhitespace().Add(FormattingHelper.GetNewLineTrivia(document));
                        }
                    }
                    else
                    {
                        trailingTrivia = SyntaxTriviaList.Create(FormattingHelper.GetNewLineTrivia(document));
                    }

                    newFieldDeclarations.Add(previous.WithTrailingTrivia(trailingTrivia));
                    previous = null;
                }
            }

            newFieldDeclarations.Add(previous.WithTrailingTrivia(declarationTrailingTrivia));
            return(newFieldDeclarations);
        }
示例#5
0
        private static RenameResult RenameSymbol(Document document, SyntaxNode root, SyntaxNode startNode, VariableDeclaratorSyntax declarationNode, string newName)
        {
            var identifierToken = declarationNode.Identifier;

            var methodAnnotation = new SyntaxAnnotation(SELECTED_METHOD_ANNOTATION);
            var changeDic        = new Dictionary <SyntaxNode, SyntaxNode>();

            changeDic.Add(startNode, startNode.WithAdditionalAnnotations(methodAnnotation));
            changeDic.Add(declarationNode, declarationNode.WithIdentifier(identifierToken.WithAdditionalAnnotations(RenameAnnotation.Create())));

            var annotatedRoot = root.ReplaceNodes(changeDic.Keys, (x, y) => changeDic[x]);

            var newSolution = RenameSymbol(document, annotatedRoot, identifierToken, methodAnnotation, newName).Result;

            return(GetNewStartNode(newSolution, document, methodAnnotation, startNode));
        }
        public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
        {
            var id = node.Identifier.ValueText;

            if (node.Ancestors().Any(anc => anc is FieldDeclarationSyntax))
            {
                return(base.VisitVariableDeclarator(node));
            }

            if (!identities.ContainsKey(id))
            {
                identities[id] = 1;
                return(base.VisitVariableDeclarator(node));
            }

            ++identities[id];
            return(base.VisitVariableDeclarator(node.WithIdentifier(Identifier(id))));
        }
示例#7
0
        public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
        {
            String name        = node.Identifier.ValueText;
            String replaceName = "";

            if (SharedContainer.Instance.nameMap.ContainsKey(name))
            {
                replaceName = SharedContainer.Instance.nameMap[name];
            }
            else
            {
                replaceName = SharedContainer.Instance.RandomString(SharedContainer.Instance.nameMap.Count() + 1);
                SharedContainer.Instance.nameMap[name] = replaceName;
            }

            VariableDeclaratorSyntax newSyntax = node.WithIdentifier(Identifier(replaceName)).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());

            return(node.ReplaceNode(node, newSyntax));
        }
        private static VariableDeclaratorSyntax GetNewDeclarator(
            VariableDeclaratorSyntax declarator,
            TypeSyntax type,
            ITypeSymbol typeSymbol)
        {
            ExpressionSyntax value = SyntaxUtility.CreateDefaultValue(typeSymbol, type);

            EqualsValueClauseSyntax @default = EqualsValueClause(value);

            if (declarator.Initializer == null || declarator.Initializer.IsMissing)
            {
                return(declarator
                       .WithIdentifier(declarator.Identifier.WithoutTrailingTrivia())
                       .WithInitializer(@default.WithTrailingTrivia(declarator.Identifier.TrailingTrivia)));
            }
            else
            {
                return(declarator
                       .WithInitializer(@default.WithTriviaFrom(declarator.Initializer.EqualsToken)));
            }
        }
示例#9
0
        private static VariableDeclaratorSyntax GetNewDeclarator(
            VariableDeclaratorSyntax declarator,
            TypeSyntax type,
            ITypeSymbol typeSymbol)
        {
            ExpressionSyntax value = typeSymbol.GetDefaultValueSyntax(type);

            EqualsValueClauseSyntax initializer    = declarator.Initializer;
            EqualsValueClauseSyntax newInitializer = EqualsValueClause(value);

            if (initializer == null || initializer.IsMissing)
            {
                return(declarator
                       .WithIdentifier(declarator.Identifier.WithoutTrailingTrivia())
                       .WithInitializer(newInitializer.WithTrailingTrivia(declarator.Identifier.TrailingTrivia)));
            }
            else
            {
                return(declarator
                       .WithInitializer(newInitializer.WithTriviaFrom(initializer.EqualsToken)));
            }
        }
示例#10
0
        public static Task <Document> RefactorAsync(
            Document document,
            LocalDeclarationStatementSyntax localDeclaration,
            VariableDeclaratorSyntax declarator,
            ITypeSymbol typeSymbol,
            CancellationToken cancellationToken = default)
        {
            VariableDeclaratorSyntax newDeclarator = GetNewDeclarator(localDeclaration.Declaration.Type.WithoutTrivia());

            LocalDeclarationStatementSyntax newNode = localDeclaration.ReplaceNode(declarator, newDeclarator);

            if (localDeclaration.SemicolonToken.IsMissing &&
                localDeclaration.Declaration.Variables.Last().Equals(declarator))
            {
                newNode = newNode.WithSemicolonToken(SemicolonToken().WithTrailingTrivia(newDeclarator.GetTrailingTrivia()));
            }

            return(document.ReplaceNodeAsync(localDeclaration, newNode, cancellationToken));

            VariableDeclaratorSyntax GetNewDeclarator(TypeSyntax type)
            {
                ExpressionSyntax value = typeSymbol.GetDefaultValueSyntax(document.GetDefaultSyntaxOptions(), type);

                EqualsValueClauseSyntax initializer    = declarator.Initializer;
                EqualsValueClauseSyntax newInitializer = EqualsValueClause(value);

                if (initializer?.IsMissing != false)
                {
                    return(declarator
                           .WithIdentifier(declarator.Identifier.WithoutTrailingTrivia())
                           .WithInitializer(newInitializer.WithTrailingTrivia(declarator.Identifier.TrailingTrivia)));
                }
                else
                {
                    return(declarator
                           .WithInitializer(newInitializer.WithTriviaFrom(initializer.EqualsToken)));
                }
            }
        }
示例#11
0
            public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
            {
                VariableDeclaratorSyntax processedNode = (VariableDeclaratorSyntax)base.VisitVariableDeclarator(node);
                
                ISymbol declaredSymbol = this.model.GetDeclaredSymbol(node, this.cancellationToken);
                if (this.renamedSymbol.Equals(declaredSymbol))
                {
                    // Update renamed variable declarator.
                    // Considers:
                    // int a = 1; -> int newVariableName = 1;

                    SyntaxToken identifier = node.Identifier;

                    SyntaxToken newIdentifier = Syntax.Identifier(identifier.LeadingTrivia, this.newName, identifier.TrailingTrivia);

                    VariableDeclaratorSyntax newDeclarator = processedNode.WithIdentifier(newIdentifier);

                    return newDeclarator;
                }

                return processedNode;
            }
示例#12
0
        private static List <BaseFieldDeclarationSyntax> DeclarationSplitter(
            Document document,
            VariableDeclarationSyntax declaration,
            Func <VariableDeclarationSyntax, BaseFieldDeclarationSyntax> declarationFactory,
            SyntaxTriviaList declarationTrailingTrivia)
        {
            SeparatedSyntaxList <VariableDeclaratorSyntax> variables = declaration.Variables;
            VariableDeclaratorSyntax   first    = variables.First();
            BaseFieldDeclarationSyntax previous = null;
            var newFieldDeclarations            = new List <BaseFieldDeclarationSyntax>(variables.Count);

            foreach (SyntaxNodeOrToken nodeOrToken in variables.GetWithSeparators())
            {
                if (previous == null)
                {
                    VariableDeclaratorSyntax variable = (VariableDeclaratorSyntax)nodeOrToken.AsNode();
                    variable = variable.WithIdentifier(variable.Identifier.WithoutLeadingWhitespace());
                    var variableDeclarator = SyntaxFactory.SingletonSeparatedList(variable);
                    previous = declarationFactory(declaration.WithVariables(variableDeclarator));

                    if (variable != first)
                    {
                        var triviaList = previous.GetLeadingTrivia().WithoutDirectiveTrivia();

                        // Remove all leading blank lines
                        var nonBlankLinetriviaIndex = TriviaHelper.IndexOfFirstNonBlankLineTrivia(triviaList);
                        if (nonBlankLinetriviaIndex > 0)
                        {
                            triviaList = triviaList.RemoveRange(0, nonBlankLinetriviaIndex);
                        }

                        // Add a blank line if the first line contains a comment.
                        var nonWhitespaceTriviaIndex = TriviaHelper.IndexOfFirstNonWhitespaceTrivia(triviaList, false);
                        if (nonWhitespaceTriviaIndex >= 0)
                        {
                            switch (triviaList[nonWhitespaceTriviaIndex].Kind())
                            {
                            case SyntaxKind.SingleLineCommentTrivia:
                            case SyntaxKind.MultiLineCommentTrivia:
                                triviaList = triviaList.Insert(0, SyntaxFactory.CarriageReturnLineFeed);
                                break;
                            }
                        }

                        previous = previous.WithLeadingTrivia(triviaList);
                    }
                }
                else
                {
                    SyntaxToken      commaToken     = nodeOrToken.AsToken();
                    SyntaxTriviaList trailingTrivia = commaToken.TrailingTrivia;
                    if (trailingTrivia.Any())
                    {
                        if (!trailingTrivia.Last().IsKind(SyntaxKind.EndOfLineTrivia))
                        {
                            trailingTrivia = trailingTrivia.WithoutTrailingWhitespace().Add(FormattingHelper.GetNewLineTrivia(document));
                        }
                    }
                    else
                    {
                        trailingTrivia = SyntaxTriviaList.Create(FormattingHelper.GetNewLineTrivia(document));
                    }

                    newFieldDeclarations.Add(previous.WithTrailingTrivia(trailingTrivia));
                    previous = null;
                }
            }

            newFieldDeclarations.Add(previous.WithTrailingTrivia(declarationTrailingTrivia));
            return(newFieldDeclarations);
        }
 public override SyntaxNode?VisitVariableDeclarator(VariableDeclaratorSyntax node)
 {
     AddMappedIdentifier(node.Identifier);
     return(base.VisitVariableDeclarator(node.WithIdentifier(MappedIdentifier(node.Identifier))));
 }