Question

I would like override the Navigator popup menu -> Rename functionality by using a custom RenameParticipant. The requirement is to 1 . To rename the file selected, 2. Search and replace its references in all files (without extension) 3. Rename a folder with same name (without extension).

E.g. Let say the file selected for rename is "mystuff.flow:, then there exists a folder by name "mystuff". The rename participant should rename this folder as well.

I'm able to achieve first two requirements with the following code but stuck with the third one.

plugin.xml

<extension
     point="org.eclipse.ltk.core.refactoring.renameParticipants">
  <renameParticipant
        class="com....flow.refactoring.MyflowRenameParticipant"
        id="com.....flow.refactoring.myflowRenameParticipant"
        name="MyflowRenameParticipant">
     <enablement>
         <and>
        <instanceof
              value="org.eclipse.core.resources.IFile">
        </instanceof>
        <test
              property="org.eclipse.core.resources.extension"
              value="flow">
        </test>
     </and>
     </enablement>
  </renameParticipant>

Custom Rename Participant

public class MyflowRenameParticipant extends RenameParticipant {

    @Override
    public Change createChange(IProgressMonitor pm) throws CoreException,
        OperationCanceledException {
    final HashMap<IFile, TextFileChange> changes= new HashMap<IFile, TextFileChange>();


    // Use the text search engine to find matches in files
    // limit to the current project
    IResource[] roots= { fFile.getProject() };  
    String[] fileNamePatterns= { "*" }; //$NON-NLS-1$ 
    FileTextSearchScope scope= FileTextSearchScope.newSearchScope(roots , fileNamePatterns, false);
    FileNamePatternSearchScope fscope= FileNamePatternSearchScope.newSearchScope("Folders Imp", roots , false);

    // only find the simple name of the file without extention
    Pattern pattern= Pattern.compile(getNameWithoutExt(fFile)); 
    TextSearchRequestor collector= new TextSearchRequestor() {
        public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess) throws CoreException {
            IFile file= matchAccess.getFile();
            TextFileChange change= (TextFileChange) changes.get(file);
            if (change == null) {
                // an other participant already modified that file?
                TextChange textChange= getTextChange(file); 
                if (textChange != null) {
                    // don't try to merge changes
                    return false; 
                }
                change= new TextFileChange(file.getName(), file);
                change.setEdit(new MultiTextEdit());
                changes.put(file, change);
            }
            ReplaceEdit edit= new ReplaceEdit(matchAccess.getMatchOffset(), matchAccess.getMatchLength(), newName);
            change.addEdit(edit);
            change.addTextEditGroup(new TextEditGroup("Updates text reference", edit)); //$NON-NLS-1$
            return true;
        }
    };
    TextSearchEngine.create().search(scope, collector, pattern, pm);

    if (changes.isEmpty())
        return null;

    CompositeChange result= new CompositeChange("Callflow  Updates"); //$NON-NLS-1$
    for (Iterator<TextFileChange> iter= changes.values().iterator(); iter.hasNext();) {
        result.add((Change) iter.next());
    }
          // Gets the folder to be renamed.
    IFolder folder = getMyflowFolder(fFile);

           // How to add this to the result? so that the same is available in the context for preview and subsequent rename operation.

    return result;
}

Appreciate your help. Thank you.

Was it helpful?

Solution

You should be able to add an instance of org.eclipse.ltk.core.refactoring.resource.RenameResourceChange (or maybe MoveResourceChange) for the IFolder to your CompositeChange result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top