getting started on first plugin Follow
I am about to begin work on my first intellij plugin for Java.
My plugin should be able to (1) replace all of the method function names, (2) replace all of the variable names, (3) remove final variable declarations, and substitute the final values throughout the code.
Effectively, I am building a code minimizer. Here is a bit of background about what I am going to try to do:
http://devnet.jetbrains.net/message/5475325#5475325
I am hoping to get some suggestions about:
1. how to write a plugin which changes an entire file in one go (most plugins I have used add additional UI support).
2. where the relevant parts of the API are for this project.
Similar example projects might be most helpful.
Thanks much, and excited to learn more about plugin development.
Please sign in to leave a comment.
As an intro to plugin development, I recommend watching this webinar recording:
http://blogs.jetbrains.com/idea/2012/12/webinar-recording-live-coding-a-plugin-from-scratch/
1. There is nothing special with changing the entire file. Get the PsiClass from data context as shown in the webinar and process all of its methods, fields and nested classes.
2. RefactoringFactory.createRename() will do the entire renaming operation for you automatically.
Thank you. This is pleasurable development, natch!
A few questions:
1. In this example, I want to catch possible duplicate inner class names. How would I catch them so that I can iterate until I get a unique name?
RefactoringFactory rf = RefactoringFactory.getInstance(psiJavaFile.getProject());
PsiClass[] classes = psiJavaFile.getClasses();
for (int i=0; i <classes.length; i++)
{ PsiClass iPsiClass = classes[i];
PsiClass[] innerClasses = iPsiClass.getInnerClasses();
for (int j=0; j < innerClasses.length; j++)
{ PsiClass jInnerPsiClass = innerClasses[j];
RenameRefactoring renamer = rf.createRename( jInnerPsiClass, "j"+0, true, true );
}
}
2.a. Can I call my plugin from an ANT task?
2.b. If not, how can I copy my source file and run the plugin on that duplicate file? (I would like to leave the original unscathed).
1. getInnerClasses() returns only the list of classes which are direct children of the class; the Java languages guarantees the names to be unique. You may want to look at JavaRecursiveElementVisitor for an easy way to iterate all classes.
2. You can run IntelliJ IDEA with your plugin from an Ant task, but it's kinda complicated. Copying files is easier: you can call the PsiFile.copy() method to create a copy of a file and then PsiDirectory.add() to save it to disk.
Is there a list of the ther GUI hooks I can use to run my plugin? I am currently using AnAction as you do in your screencast, but it is a little unwieldy for this plugin. As I wrote earlier, I was hoping for ANT integration, but maybe launching from a menu item might make more sense? Other suggestions?
A menu item is also represented as an AnAction, so I'm not sure what you mean by that.
There are plenty of places to hook into - if you describe what you want to accompilsh, we could tell how to implement it.
Currently, to run my plugin, I have to go to the Code menu and select the Generate Menu Item, and then select my plugin from a dialog floating in the middle of the application.
As my tool will only be used intermittently, and as part of my ANT build script, I would most prefer to have it integrated with ANT. But, as you've said, it is complicated, so let's pass on that for now.
So, for now, can I add it as a menuitem to one of the main menus?
Simply add your action to the action group corresponding to the menu you want to add it to, instead of the Generate action group.