|xText rename refactor [message #1052294]
||Mon, 29 April 2013 18:59
| April Dai
Registered: April 2013
wanted to know if xText rename refactor can be used without UI context. rename refactor fails if no editor is available from IRenameContext. |
All the refactor plugins are under org.eclipse.xtext.ui.refactoring, seems like there is no core plugin which does not require a UI context for rename refactor to work.
Has anyone encountered the same issue? Is there a work around?
|Re: xText rename refactor [message #1065022 is a reply to message #1052294]
||Sun, 23 June 2013 10:09
| Michael Ernst
Registered: July 2010
Found a solution which works for me, but I would prefer it, if I could realize this via a Framework implementation.|
My ugly but working solution to rename elements which have no references to other artifacts:
- load the Xtext Resource so it is available via the emf model element
- select the model element which should be renamed
- Use the method NodeModelUtils.findNodesForFeature to get the node which contains the name of the element which should be renamed and store it in a list of nodes.
- Iterate through the model and select all references to the model element which should be renamed and add their nodes (access them also by using the method NodeModelUtils.findNodesForFeature) to the list of nodes
- sort all nodes by their total offset
- use the root node of (node.getRootNode) to access the whole parsed text and replace the content like in the snippet below.
String rootNodeText = compositeNode.getRootNode().getText();
String newText = rootNodeText;
int currentAdaptedOffset = 0;
int adaptOffset = toName.length() - fromName.length();
for( INode node : nodes )
int totalOffset = node.getTotalOffset();
int position = currentAdaptedOffset + totalOffset;
newText = newText.substring( 0, position ) + toName + newText.substring( position + fromName.length() );
currentAdaptedOffset += adaptOffset;
- parse the updated text and check for syntax errors
[Updated on: Sun, 23 June 2013 10:12]
Report message to a moderator
Powered by FUDForum
. Page generated in 0.01676 seconds