code formatting - Roslyn - replace node and fix the whitespaces -


in program use roslyn , need replace node new node. example, if have code like

public void foo() {    for(var = 0; < 5; i++)       console.writeline(""); } 

and want insert brackes for statement, get

public void foo() {    for(var = 0; < 5; i++) {       console.writeline(""); } } 

i tried use normalizewhitespace, if use on statement, get

public void foo() { for(var = 0; < 5; i++) {    console.writeline(""); } } 

however, i'd have statement formatted correctly. hints how it?

edit: solved using:

var blocksyntax = syntaxfactory.block(             syntaxfactory.token(syntaxkind.openbracetoken).withleadingtrivia(forstatementsyntax.getleadingtrivia()).withtrailingtrivia(forstatementsyntax.gettrailingtrivia()),             syntaxnodes,             syntaxfactory.token(syntaxkind.closebracetoken).withleadingtrivia(forstatementsyntax.getleadingtrivia()).withtrailingtrivia(forstatementsyntax.gettrailingtrivia()) ); 

however, answer sam correct.

you need use .withadditionalannotations(formatter.annotation), on specific element want format. here's example nullparametercheckrefactoring project.

ifstatementsyntax nullcheckifstatement = syntaxfactory.ifstatement(     syntaxfactory.token(syntaxkind.ifkeyword),     syntaxfactory.token(syntaxkind.openparentoken),     binaryexpression,      syntaxfactory.token(syntaxkind.closeparentoken),      syntaxblock, null).withadditionalannotations(formatter.annotation, simplifier.annotation); 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -