Eclipse already has very impressive and useful what I call “source code modifiers” (please suggest a better name).
For example, it has “Quick Fix”, “Word Completion”, “Externalize Strings” and other functions that modify source code via menu (or key-combination).
Now, I am looking to add my own “source code modifier” function: I would like to:
- Highlight (select) an arbitrary string.
- Right-click on it
- Invoke a menu item that would “translate” that string to a different string, using a function that I wrote (preferably in Java). Similar to “Quick Fix” or “Replace With” currently on the default context menu.
Is this possible in Eclipse?
If so, what do I need to do to accomplish this?
The short answer:
The quick assist will have to modify the AST of the Java code. Essentially you will have to replace a
org.eclipse.jdt.core.dom.SimpleNamenode with one that you want.The long answer:
The
org.eclipse.jdt.ui.quickAssistProcessorsextension point enables you to contribute your own Java code quick assists.To create a new extension for the extension point you need to first provide the required extension in the plugin.xml. For example, JDT defines the following processor
(For a description of the individual attributes, please refer to the extension point documentation)
Then you need to create the class that implements the
org.eclipse.jdt.ui.text.java.IQuickAssistProcessorinterface, and modify the AST in this class. (This class is the same as the one you specified while declaring the extension)Supplying the right IJavaCompletionProposal
JDT provides the following default implementations for correction proposals that can be used to contribute quick fixes and quick assists.
If you use an ASTRewrite, you should create an ASTRewriteCorrectionProposal.
ASTView Plugin
This is something that will help you visualize the AST of a Java source file http://www.eclipse.org/jdt/ui/astview/index.php