|
|
|
Re: xText rename refactor [message #1065022 is a reply to message #1052294] |
Sun, 23 June 2013 14:09 |
Michael Ernst Messages: 49 Registered: July 2010 |
Member |
|
|
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
Kind regards
Michael
[Updated on: Sun, 23 June 2013 14:12] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03333 seconds