コード例 #1
0
        private static IEnumerable <SyntaxTrivia> GetLeadingTriviaFor(VariableDeclaratorSyntax variable)
        {
            var previousToken = variable.GetFirstToken().GetPreviousToken();

            return(previousToken.TrailingTrivia
                   .Concat(variable.GetLeadingTrivia()));
        }
コード例 #2
0
        private static async Task <Document> InitializeFromConstructor(Document document, FieldDeclarationSyntax fieldDeclaration, VariableDeclaratorSyntax fieldVariable, bool alwaysCreateNew, CancellationToken cancellationToken)
        {
            var classDeclaration = fieldDeclaration.Ancestors().OfType <ClassDeclarationSyntax>().Single();

            var parameterType   = fieldDeclaration.Declaration.Type.ToString();
            var identifierToken = fieldVariable.Identifier;
            var parameterName   = identifierToken.Text.TrimStart('_');

            // create the new parameter
            var parameter = Parameter(
                Identifier(parameterName))
                            .WithType(IdentifierName(parameterType)
                                      );

            // create the new assignment to add to the constructor body
            var assignment = ExpressionStatement(
                AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                     MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName(identifierToken)),
                                     IdentifierName(parameterName)));

            // get the syntax root
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            // get existing constructor if any
            var existingConstructors = GetConstructors(fieldVariable);

            // update the class by either updating the constructor, or adding one
            SyntaxNode updatedClassDecl = classDeclaration;

            if (!existingConstructors.Any() || alwaysCreateNew)
            {
                var constructor = ConstructorDeclaration(classDeclaration.Identifier.Text)
                                  .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                                  .WithParameterList(ParameterList(
                                                         SingletonSeparatedList(parameter)
                                                         ))
                                  .WithBody(Block(assignment))
                                  .WithLeadingTrivia(fieldVariable.GetLeadingTrivia().Insert(0, CarriageReturnLineFeed));

                var insertionPoint = (SyntaxNode)existingConstructors.LastOrDefault() ?? fieldDeclaration;

                updatedClassDecl = classDeclaration.InsertNodesAfter(insertionPoint, new[] { constructor });
            }
            else
            {
                updatedClassDecl = classDeclaration.ReplaceNodes(existingConstructors,
                                                                 (constructor, updatedConstructor) => UpdateConstructor(constructor, parameter, assignment));
            }

            // replace the root node with the updated class
            var newRoot = root.ReplaceNode(classDeclaration, updatedClassDecl);

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #3
0
        private SyntaxNode RemoveDeclaratorFromVariableList(VariableDeclaratorSyntax variableDeclarator, VariableDeclarationSyntax variableDeclaration)
        {
            Debug.Assert(variableDeclaration.Variables.Count > 1);
            Debug.Assert(variableDeclaration.Variables.Contains(variableDeclarator));

            var localDeclaration = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
            var scope            = GetScope(variableDeclarator);

            var newLocalDeclaration = variableDeclarator.GetLeadingTrivia().Any(t => t.IsDirective)
                ? localDeclaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepExteriorTrivia)
                : localDeclaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia);

            return(scope.ReplaceNode(
                       localDeclaration,
                       newLocalDeclaration.WithAdditionalAnnotations(Formatter.Annotation)));
        }
 private static IEnumerable<SyntaxTrivia> GetLeadingTriviaFor(VariableDeclaratorSyntax variable)
 {
     var previousToken = variable.GetFirstToken().GetPreviousToken();
     return previousToken.TrailingTrivia
         .Concat(variable.GetLeadingTrivia());
 }
コード例 #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));
        }
コード例 #6
0
        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;
        }