| Summary: | [api] No public API to rename elements in one file without saving | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Srimanth <sgunturi> |
| Component: | UI | Assignee: | JDT-UI-Inbox <jdt-ui-inbox> |
| Status: | CLOSED WORKSFORME | QA Contact: | |
| Severity: | enhancement | ||
| Priority: | P3 | CC: | gmendel, richkulp |
| Version: | 3.1 | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Windows XP | ||
| Whiteboard: | |||
|
Description
Srimanth
Moving to JDT UI for comment This would be a new rename support since the feature you are requesting isn't implemented as a refactoring. FYI: all the pieces to implement this are currently API, meaning using the AST to collect all references to the element to be renamed in the CU and the AST rewrite to do the actual change. It would be great for the time being if you could tell which API to use to collect all the AST occurences of a name, so that ASTRewrite could act on them. To collect all occurrences of the name to change the following code can be used:
IBinding binding= nameNodeToRename.resolveBinding();
rootNode.accept(new ASTVisitor() {
public boolean visit(SimpleName name) {
if (binding.isEqualTo(name.resolveBinding()) {
... change the name or record the name node.
}
}
});
Thanks,
Setting 'ASTParser.setResolveBindings(true)' creates an AST tree with bindings
on names used. Using ASTRewrite multiple changes can be made to the tree at
once. Pasting the rename method for convenience.
---------------------------------------
private void rename(
CompilationUnit cuNode,
SimpleName simpleName,
final String newName,
final ASTRewrite rewrite) {
final IBinding simpleNameBinding = simpleName.resolveBinding();
if(simpleNameBinding!=null){
cuNode.accept(new ASTVisitor(){
public boolean visit(SimpleName node) {
if(simpleNameBinding.isEqualTo(node.resolveBinding())){
rewrite.set(node, SimpleName.IDENTIFIER_PROPERTY, newName, null);
}
return super.visit(node);
}
});
}
}
---------------------------------------
This also fixes VE's bug 94009 of multiple undo for a single rename, as
ASTRewrite allows us to change multiple times on a single AST tree.
Is it ok to close this bug? Closing the bug. If there is more demand for this api from others it can be reopened. Verified in VE-SDK-I20051216 GTK. |