//Actual code to update tree node private async Task <Document> ReplaceNameAsync(Document doc, IdentifierNameSyntax usingNode, CancellationToken c) { //TODO GO through this code and remove unecessary bits //grab the Windows token to replace var winToken = usingNode.DescendantTokens().Single(n => n.Text == "Windows"); //grab leading trivia for first token var winLeadTrivia = winToken.LeadingTrivia; //grab traling trivia var winTrailTrivia = winToken.TrailingTrivia; //create a Microsoft token with old trivia var micToken = SyntaxFactory.Identifier(winLeadTrivia, SyntaxKind.IdentifierToken, "Microsoft", "Microsoft", winTrailTrivia); //Replace node with new var newNode = usingNode.ReplaceToken(winToken, micToken); // add annotations to format the new token d //TODO: TEST IF NECESSARY! - Sometimes removes tabs etc, consider removing entirely var formattedNode = newNode.WithAdditionalAnnotations(Formatter.Annotation); // replace the old win token with new Microsoft var oldRoot = await doc.GetSyntaxRootAsync(c); Debug.WriteLine(usingNode.ToFullString()); Debug.WriteLine(newNode.ToFullString()); //Try Not using formattedNode var newRoot = oldRoot.ReplaceNode(usingNode, newNode); // Return a new doc wit htransfromed tree return(doc.WithSyntaxRoot(newRoot)); }