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
Post a Comment