Eclipse自定义重命名参与者重命名文件,其引用和文件夹

时间:2014-04-30 07:49:17

标签: eclipse eclipse-plugin

我想覆盖Navigator弹出菜单 - >使用自定义RenameParticipant重命名功能。要求是 1。要重命名所选文件, 2.在所有文件中搜索并替换其引用(不带扩展名) 3.重命名具有相同名称的文件夹(不带扩展名)。

E.g。假设选择重命名的文件是" mystuff.flow :,那么就存在一个名字和#34; mystuff"的文件夹。重命名参与者也应重命名此文件夹。

我能够通过以下代码实现前两个要求,但坚持使用第三个要求。

的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>

自定义重命名参与者

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;
}

感谢您的帮助。 谢谢。

1 个答案:

答案 0 :(得分:1)

您应该能够为org.eclipse.ltk.core.refactoring.resource.RenameResourceChange结果MoveResourceChange添加IFolder(或CompositeChange}的实例。