c# - How do I create a new root by adding and removing nodes retrieved from the old root? -
i creating code fix changes this:
if(obj myclass) { var castedobj = obj myclass; }
into this:
var castedobj = obj myclass; if(castedobj != null) { }
this means have 3 things:
- change condition in
if
statement. - move casting right above
if
statement. - remove statement in body.
so far, attempts have stranded me @ getting @ 2 of these things work.
i believe problem occurs because have 2 syntax nodes on same level. such, making change 1 of them invalidates location of other one. or that. long story short: either manage copy variable assignment outside if
statement, or manage change condition + remove variable assignment. never 3.
how solve this?
for measure, here code changes condition , removes assignment:
var newifstatement = ifstatement.removenode( variabledeclaration, syntaxremoveoptions.keepexteriortrivia); newifstatement = newifstatement.replacenode(newifstatement.condition, newcondition); var ifparent = ifstatement.parent; var newparent = ifparent.replacenode(ifstatement, newifstatement); newparent = newparent.insertnodesbefore( newifstatement, new[] { variabledeclaration }) .withadditionalannotations(formatter.annotation); var newroot = root.replacenode(ifparent, newparent);
have looked @ documenteditor
class ? useful when dealing modifying syntax, when changes applied tree might cause invalidation problems. operations pretty same ones have defined, use documenteditor
methods instead , see if helps. can't verify if solves problem atm, think solved similar problem me once in past. i'll test out later if can.
something it:
var editor = await documenteditor.createasync(document); editor.removenode(variabledeclaration); editor.replacenode(ifstatement.condition, newcondition); editor.insertbefore(ifstatement, new[] { variabledeclaration.withadditionalannotations(formatter.annotation) }); var newdocument = editor.getchangeddocument();
Comments
Post a Comment