private static DeclarationExpressionSyntax GetDeclarationExpression(
            SourceText sourceText, IdentifierNameSyntax identifier,
            TypeSyntax newType, VariableDeclaratorSyntax declaratorOpt)
        {
            newType = newType.WithoutTrivia().WithAdditionalAnnotations(Formatter.Annotation);
            var designation = SyntaxFactory.SingleVariableDesignation(identifier.Identifier);

            if (declaratorOpt != null)
            {
                // We're removing a single declarator.  Copy any comments it has to the out-var.
                //
                // Note: this is tricky due to comment ownership.  We want the comments that logically
                // belong to the declarator, even if our syntax model attaches them to other tokens.
                var precedingTrivia = declaratorOpt.GetAllPrecedingTriviaToPreviousToken(
                    sourceText, includePreviousTokenTrailingTriviaOnlyIfOnSameLine: true);
                if (precedingTrivia.Any(t => t.IsSingleOrMultiLineComment()))
                {
                    designation = designation.WithPrependedLeadingTrivia(MassageTrivia(precedingTrivia));
                }

                if (declaratorOpt.GetTrailingTrivia().Any(t => t.IsSingleOrMultiLineComment()))
                {
                    designation = designation.WithAppendedTrailingTrivia(MassageTrivia(declaratorOpt.GetTrailingTrivia()));
                }
            }

            return(SyntaxFactory.DeclarationExpression(newType, designation));
        }
        private static DeclarationExpressionSyntax GetDeclarationExpression(
            SourceText sourceText, IdentifierNameSyntax identifier,
            TypeSyntax newType, VariableDeclaratorSyntax declaratorOpt)
        {
            newType = newType.WithoutTrivia().WithAdditionalAnnotations(Formatter.Annotation);
            var designation = SyntaxFactory.SingleVariableDesignation(identifier.Identifier);

            if (declaratorOpt != null)
            {
                // We're removing a single declarator.  Copy any comments it has to the out-var.
                //
                // Note: this is tricky due to comment ownership.  We want hte comments that logically
                // belong to the declarator, even if our syntax model attaches them to other tokens.
                var precedingTrivia = declaratorOpt.GetAllPrecedingTriviaToPreviousToken(
                    sourceText, includePreviousTokenTrailingTriviaOnlyIfOnSameLine: true);
                if (precedingTrivia.Any(t => t.IsSingleOrMultiLineComment()))
                {
                    designation = designation.WithPrependedLeadingTrivia(MassageTrivia(precedingTrivia));
                }

                if (declaratorOpt.GetTrailingTrivia().Any(t => t.IsSingleOrMultiLineComment()))
                {
                    designation = designation.WithAppendedTrailingTrivia(MassageTrivia(declaratorOpt.GetTrailingTrivia()));
                }
            }

            return SyntaxFactory.DeclarationExpression(newType, designation);
        }
Beispiel #3
0
        public static async Task <Document> RefactorAsync(
            Document document,
            LocalDeclarationStatementSyntax localDeclaration,
            VariableDeclaratorSyntax declarator,
            ITypeSymbol typeSymbol,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            VariableDeclaratorSyntax newDeclarator = GetNewDeclarator(
                declarator,
                localDeclaration.Declaration.Type.WithoutTrivia(),
                typeSymbol);

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

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

            return(await document.ReplaceNodeAsync(localDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Beispiel #4
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)));
                }
            }
        }
Beispiel #5
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 Method TraverseVarDeclarators(VariableDeclaratorSyntax vds)
        {
            Method retMethod = new Method();
            Variables retVar = new Variables();

            if (vds.HasLeadingTrivia)
            {
                SetOuterComments(retVar, vds.GetLeadingTrivia().ToFullString());
            }

            if (vds.HasTrailingTrivia)
            {
                SetInnerComments(retVar, vds.GetTrailingTrivia().ToFullString());
            }

            retVar.Name = vds.Identifier.ValueText;
            Model.Type retType = new Model.Type();
            retType.IsKnownType = SyntaxFacts.IsKeywordKind(vds.CSharpKind());
            retType.IsNotUserDefined = SyntaxFacts.IsKeywordKind(vds.CSharpKind());
            retVar.Type = retType;
            retMethod.AccessedVariables.Add(retVar);
            var valueClauses = from aValueClase in vds.ChildNodes().OfType<EqualsValueClauseSyntax>() select aValueClase;
            foreach (EqualsValueClauseSyntax evcs in valueClauses)
            {
                retMethod.AccessedVariables.AddRange(TraverseEqualsClause(evcs).AccessedVariables);
            }
            //TODO
            //Don't know if I need more stuff here
            //or even if i can fetch it from vds
            return retMethod;
        }