How to append action after rename file

I wanna rename match test file name after rename lib file, my question is:
how to invoke a input dialog and get its value?
what is the class name of rename action

7 comments
Comment actions Permalink

You should use com.intellij.refactoring.rename.naming.AutomaticRenamerFactory instead which will automatically provide those renaming targets
during the actual rename operation.

0
Comment actions Permalink

I'm aimed to refactory ruby test file after rename ruby lib file, This is my example:


# lib/ex.rb
# spec/lib/ex_spec.rb

I hope when i rename lib/ex.rb to lib/ex2.rb, the spec/lib/ex_spec.rb will auto become spec/lib/ex2_spec.rb

So this is my thinking:

If I can get the value what I type in rename dialog, I can handle this value in code
and then I find public RenameElementAction#getHandler, I hope find a way to custom the handler.

my question is:


How to get rename dialog value?
How to customize getHandler in sub class of RenameElementAction
I can undo refactoring with hitting ctrl+z, If I implement above, how to undo it?

And sorry, I'm not so understand How to use AutomaticRenamerFactory to implement them, can you explain it detail according my example.

0
Comment actions Permalink

Please try to understand existing code, e.g. com.intellij.lang.properties.refactoring.rename.ResourceBundleRenamerFactory
is a very basic sample on how to use it.

"undo" support will be available automatically, as well as "preview changes"

0
Comment actions Permalink

I still has question, following is my code, if i rename ex.rb, it will change ex_test.rb, but when i hit ctrl+z to undo refactoring, ex_test.rb doesn't recovery and is changed forever. My question is how to recovery ex_test.rb

// Renamer.groovy

 
package ro.tryApi.spec

import com.intellij.refactoring.rename.naming.AutomaticRenamer
import com.intellij.usageView.UsageInfo

class Renamer extends AutomaticRenamer {
    Renamer() {
        new File("/home/roroco/Dropbox/try/plugin/rb/spec/lib/ex_test.rb").write(new Date().toString())
    }

    @Override
    String getDialogTitle() {
        return 'try dialog title'
    
}

    @Override
    String getDialogDescription() {
        return "try dialog desc"
    
}

    @Override
    String entityName() {
        return 'try entity name'
    
}
}


// RenamerFactory

 
package ro.tryApi.spec

import com.intellij.psi.PsiElement
import com.intellij.refactoring.rename.naming.AutomaticRenamer
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
import com.intellij.usageView.UsageInfo

class RenamerFactory implements AutomaticRenamerFactory {

    @Override
    boolean
isApplicable(PsiElement psiElement) {
        return true
    
}

    @Override
    
String getOptionName() {
        return "try opt"
    
}

    @Override
    boolean isEnabled
() {
        return true
    
}

    @Override
    void setEnabled
(boolean b) {

    }

    AutomaticRenamer createRenamer(PsiElement e, String s, Collection<UsageInfo> c) {
        return new Renamer()
    }
}
0
Comment actions Permalink

What is this:  new File("/home/roroco/Dropbox/try/plugin/rb/spec/lib/ex_test.rb").write(new Date().toString())

IDE cannot know about such modifications and undo them. You must simply override #suggestNameForElement to provide new "automatic" filename and return false from allowChangeSuggestedName() to disallow manual editing.

0
Comment actions Permalink

Finally, I implement all feature what I need, but i find it will jump a new dialog, can you tell me how to use RenamePsiElementProcessor instead of AutomaticRenamerFactory

0
Comment actions Permalink

Finally, I find following code can fix my question:

// plugin.xml

 
<idea-plugin version="2">
    ...
    <extensions defaultExtensionNs="com.intellij">
        <renamePsiElementProcessor implementation="ro.tryApi.spec.RenameProcessor"></renamePsiElementProcessor>
    </extensions>
</idea-plugin>


// /home/roroco/Dropbox/try/idea-plugin/try/src/main/groovy/ro/tryApi/spec/RenameProcessor.groovy
// It's just a try code

 
package ro.tryApi.spec

import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.newvfs.impl.VirtualFileImpl
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.search.SearchScope
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import org.jetbrains.annotations.NotNull

/**
* Created by roroco on 5/28/15.
*/
class
RenameProcessor extends RenamePsiElementProcessor {
    @Override
    boolean
canProcessElement(@NotNull PsiElement psiElement) {
        psiElement.class.name.matches(/.*RFileImpl$/)
    }

    @Override
    void
prepareRenaming(PsiElement e, String s, Map<PsiElement, String> map, SearchScope searchScope) {
        VirtualFileImpl vf = LocalFileSystem.instance.findFileByPath("/home/roroco/Dropbox/try/plugin/rb/spec/lib/ex2_spec.rb")
        PsiFile f = e.manager.findFile(vf)
        map[f] = new Date().toString()
        super.prepareRenaming(f, s, map, searchScope)
    }
}
0

Please sign in to leave a comment.