Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 109153

Summary: [api] No public API to rename elements in one file without saving
Product: [Eclipse Project] JDT Reporter: Srimanth <sgunturi>
Component: UIAssignee: 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 CLA 2005-09-09 10:51:51 EDT
We would like to rename a variable/method/field/etc. in just one file without a
full-fledged refactoring taking place. Refactoring/reconciles are expensive for
us would like to just change the text in the file. 

Currently the org.eclipse.jdt.ui.refactoring.RenameSupport.create(...) APIs do
full-fledged refactoring which involves possibly changing other files, saving
the current file if changed, reconciles, workspace builds etc. Since we would
like to do something primitive like 'Ctrl + 1' > Rename, we dont want other
expensive operations. Also renames should be done on the working copy and it
should not be saved as the user might want to revert back to original source.
This support should allow for renaming of inner local variables.
Comment 1 Jerome Lanneluc CLA 2005-09-14 04:32:26 EDT
Moving to JDT UI for comment
Comment 2 Dirk Baeumer CLA 2005-09-14 09:39:51 EDT
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. 
Comment 3 Srimanth CLA 2005-09-14 09:56:06 EDT
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.
Comment 4 Dirk Baeumer CLA 2005-09-15 08:09:11 EDT
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.
    }
  }
});
Comment 5 Srimanth CLA 2005-09-16 12:24:59 EDT
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.
Comment 6 Martin Aeschlimann CLA 2005-09-17 08:33:46 EDT
Is it ok to close this bug?
Comment 7 Srimanth CLA 2005-09-19 11:49:37 EDT
Closing the bug. If there is more demand for this api from others it can be
reopened.
Comment 8 Srimanth CLA 2005-12-16 13:32:56 EST
Verified in VE-SDK-I20051216 GTK.