c# - How to remove a list of SyntaxNode from the Document using Roslyn code fix provider APIs? -
i'm having custom generated variable declaration using syntaxfactory.variabledeclaration
, list of syntaxnode
's collected according conditions.
i did following:
modify node
var newroot = root.replacenode(expression, newvariabledeclaration)
this modified node newvariabledeclaration
.
in loop remove nodes corresponding ones nowadays in list
foreach (var listobject in listobjects) { newroot = newroot.removenode(listobject, syntaxremoveoptions.keepnotrivia); }
this doesn't alter newroot
, listobject
required changed remains same.
if utilize root.removenode(listobject, syntaxremoveoptions.keepnotrivia)
instead will, obviously, maintain on replacing previous changes.
so here newvariabledeclaration
only node changed in whole document, because newroot
syntaxnodes
have changes of syntaxnode
obtained root
.
please right me if i'm doing wrong.
edit: looked csharpsyntaxrewriter
appears analyzing single node everytime it's visiting node , can modify single node @ time. in scenario, i'll have visit particular node, create changes it, , remove other nodes respect changes made visited node.
the problem approach whenever alter tree (using replacenode()
or removenode()
), means nodes alter well. why calls removenode()
after replacenode()
don't anything.
one way prepare utilize tracknodes()
, can find nodes in modified tree correspond nodes in original tree.
uisng this, method replaces sequence of nodes single node this:
public static t replacenodes<t>( t root, ireadonlylist<syntaxnode> oldnodes, syntaxnode newnode) t : syntaxnode { if (oldnodes.count == 0) throw new argumentexception(); var newroot = root.tracknodes(oldnodes); var first = newroot.getcurrentnode(oldnodes[0]); newroot = newroot.replacenode(first, newnode); var toremove = oldnodes.skip(1).select(newroot.getcurrentnode); newroot = newroot.removenodes(toremove, syntaxremoveoptions.keepnotrivia); homecoming newroot; }
c# visual-studio-2013 roslyn vsix
No comments:
Post a Comment