【问题标题】:Eclipse custom Rename Participant to rename a file, its references and folderEclipse 自定义 Rename Participant 重命名文件、其引用和文件夹
【发布时间】:2014-04-30 17:09:56
【问题描述】:

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

例如假设选择重命名的文件是“mystuff.flow:”,则存在一个名为“mystuff”的文件夹。重命名参与者也应该重命名这个文件夹。

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

插件.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;
}

感谢您的帮助。 谢谢。

【问题讨论】:

    标签: eclipse eclipse-plugin


    【解决方案1】:

    您应该能够将IFolderorg.eclipse.ltk.core.refactoring.resource.RenameResourceChange(或者MoveResourceChange)实例添加到您的CompositeChange 结果中。

    【讨论】:

    • RenameResourceChange rrChange = new RenameResourceChange(folder.getProjectRelativePath() ,"subflow/"+newName);结果.add(rrChange);
    • 我尝试了您的建议,但出现致命错误“没有提供输入元素”。这段代码有什么问题吗? RenameResourceChange rrChange = new RenameResourceChange(folder.getProjectRelativePath() ,"subflow/"+newName);结果.add(rrChange);我试过 folder.getLocation() 等。
    • 谢谢格雷格。这解决了问题 - RenameResourceChange rrChange = new RenameResourceChange(folder.getFullPath() ,newName);
    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 2023-03-22
    • 2014-09-09
    • 2016-07-24
    • 2014-04-27
    • 2020-04-30
    • 2014-08-11
    • 1970-01-01
    相关资源
    最近更新 更多