コード例 #1
0
        private static Task <Document> RenameTupleElementAsync(
            Document document,
            MethodDeclarationSyntax methodDeclaration,
            TupleElementSyntax tupleElement,
            IFieldSymbol fieldSymbol,
            string newName,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            MethodDeclarationSyntax newNode = methodDeclaration;

            var returnType = (TupleTypeSyntax)methodDeclaration.ReturnType;

            SyntaxToken newIdentifier = SyntaxFactory.Identifier(newName).WithTriviaFrom(tupleElement.Identifier);

            SeparatedSyntaxList <TupleElementSyntax> newElements = returnType.Elements.Replace(tupleElement, tupleElement.WithIdentifier(newIdentifier));

            newNode = methodDeclaration.WithReturnType(returnType.WithElements(newElements));

            var rewriter = new RenameRewriter(fieldSymbol, newName, semanticModel, cancellationToken);

            if (methodDeclaration.Body is BlockSyntax body)
            {
                newNode = newNode.WithBody((BlockSyntax)rewriter.VisitBlock(body));
            }
            else if (methodDeclaration.ExpressionBody is ArrowExpressionClauseSyntax expressionBody)
            {
                newNode = newNode.WithExpressionBody((ArrowExpressionClauseSyntax)rewriter.VisitArrowExpressionClause(expressionBody));
            }

            return(document.ReplaceNodeAsync(methodDeclaration, newNode, cancellationToken));
        }
コード例 #2
0
        private static Task <Document> RenameTupleElementAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            TupleElementSyntax tupleElement,
            IFieldSymbol fieldSymbol,
            string newName,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            PropertyDeclarationSyntax newNode = propertyDeclaration;

            var type = (TupleTypeSyntax)propertyDeclaration.Type;

            SyntaxToken newIdentifier = SyntaxFactory.Identifier(newName).WithTriviaFrom(tupleElement.Identifier);

            SeparatedSyntaxList <TupleElementSyntax> newElements = type.Elements.Replace(tupleElement, tupleElement.WithIdentifier(newIdentifier));

            newNode = propertyDeclaration.WithType(type.WithElements(newElements));

            var rewriter = new RenameRewriter(fieldSymbol, newName, semanticModel, cancellationToken);

            var newAccessorList = (AccessorListSyntax)rewriter.VisitAccessorList(propertyDeclaration.AccessorList);

            newNode = newNode.WithAccessorList(newAccessorList);

            return(document.ReplaceNodeAsync(propertyDeclaration, newNode, cancellationToken));
        }
コード例 #3
0
 public SyntaxNode AnnotateAndRename(RenameRewriterParameters parameters)
 {
     var renameAnnotationRewriter = new RenameRewriter(parameters);
     return renameAnnotationRewriter.Visit(parameters.SyntaxRoot);
 }