When you’re writing Java code and it often involves repetitive operations, you might want to have such a plug-in. If it’s a common scenario, the IDE might already provide it, or someone might have written one.
If this operation is specific to your coding environment, you may have to write your own tools. So here to learn how to write IDEA plug-in, let their programming environment more powerful, better install force.
The development environment
Developing the IDEA plug-in has the following dependencies:
-
IntelliJ IDEA Community Edition
-
IntelliJ IDEA Community Edition source code
-
The Plugin DevKit plug-in
-
IntelliJ Platform SDK
Install IntelliJ IDEA Community Edition
You may already have Ultimate installed, but you still need to install the community version of IDEA. Because the commercial version is closed source, the core code cannot be debugged at debugging time.
Download the IntelliJ IDEA Community Edition source code
The community edition installation package does not contain the source code, so we need to manually clone a copy from Github:
git clone --depth 1 git://git.jetbrains.org/idea/community.git idea
Copy the code
Check Out And Build Community Edition Check Out And Build Community Edition
www.jetbrains.org/intellij/sd…
Add the IDEA the JDK
We don’t know why, but according to Check Out And Build Community Edition:
www.jetbrains.org/intellij/sd…
We need to create an IDEA JDK to run the plugin:
Unless you’re using the official JDK on your Mac, you’ll need to manually add /lib/tools.jar to your classpath. Follow wechat public number: Java technology stack, in the background reply: IDEA, you can get the latest N IDEA tutorials I organized, are dry goods.
Configure the IntelliJ Platform SDK
Open the File | Project Structure to create a new IntelliJ Platform SDK:
The Java SDK selects the IDEA JDK we just created:
Then we can add the downloaded SOURCE code of IDEA community edition to the source path, so that when debugging, we can debug IDEA itself:
The first plug-in
Let’s write a simple plug-in to learn the complete steps of writing a plug-in.
New construction
Select the IntellJ Platform Plugin, and then the Project SDK specifies the newly created Plugin SDK:
New plug-in project:
There are two directories under the plug-in root: SRC and Resources. SRC is the plug-in code directory, resource is the plug-in resource directory, where meta-INF /plugin.xml is the plug-in description file, just like web.xml for Java Web projects.
The default plugin.xml content is as follows:
< the idea - the plugin > < id > com.your.com pany. Unique. Plugin. Id < / id > < name > plugin display name here < / name > < version > 1.0 < / version > <vendor email="[email protected]" url="http://www.yourcompany.com">YourCompany</vendor> <description><! \[CDATA\[ Enter short description for your plugin here.<br> <em>most HTML tags may be used</em> \]\]></description> <change-notes><! \[CDATA\[ Add change notes here.<br> <em>most HTML tags may be used</em> \]\]> </change-notes> <! \-\- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting\_started/build\_number_ranges.html for Description --> <idea-version since-build="145.0"/> <! \-\- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting\_started/plugin\_compatibility.html on how to target different products --> <! \-\- uncomment to enable plugin in all products <depends>com.intellij.modules.lang</depends> --> <extensions defaultExtensionNs="com.intellij"> <! \-\- Add your extensions here --> </extensions> <actions> <! \-\- Add your actions here --> </actions> </idea-plugin>Copy the code
Create a new Action
Plug-ins The most common way to extend IDEA is to add a menu item to the menu bar or toolbar. Users can click the menu item to trigger the plug-in function. IDEA provides the AnAction class, which has a virtual method, actionPerformed, that is called every time the menu is clicked.
There are two steps to creating a custom Action:
-
Inherit the AnAction class and implement the plug-in logic in the actionPerformed method
-
There are two ways to register an action: through code and through plugin.xml
Let’s start with a simple Action class:
Public class TextBoxes extends AnAction {// If registered through Java code, this constructor is called and the string passed to the parent class is used as the name of the menu item // If you registered through plugin.xml, You can omit the constructor public TextBoxes() {// set the menu item name super("Text _Boxes"); // super("Text _Boxes","Item description", iconloader.geticon ("/Mypackage/icon.png")); } public void actionPerformed(AnActionEvent event) { Project project = event.getData(PlatformDataKeys.PROJECT); String txt= Messages.showInputDialog(project, "What is your name?" , "Input your name", Messages.getQuestionIcon()); Messages.showMessageDialog(project, "Hello, " \+ txt + "! \\n I am glad to see you.", "Information", Messages.getInformationIcon()); }}Copy the code
Then we register this Action in plugin.xml:
<actions>
<group id="MyPlugin.SampleMenu" text="_Sample Menu" description="Sample menu">
<add-to-group group-id="MainMenu" anchor="last" />
<action id="Myplugin.Textboxes"class="Mypackage.TextBoxes" text="Text _Boxes" description="A test menu item" />
</group>
</actions>
Copy the code
Here we create a new menu group where the underscore of the text string indicates the letter as a shortcut key. This menu looks like this:
In addition to manually creating an Action, IDEA also provides a quick way to create an Action. Click New in the code directory to see the Action:
In this panel, you can fill in the information about the Action you want to create. IDEA will help you create the Action, and you can register it in plugin.xml:
Run the plugin
Running the plug-in is particularly easy. Just like running normal Java code, clicking the Run or Debug button launches a new IDEA instance in which the plug-in is in effect.
Click on the Text Boxes to see what the plugin looks like.
Source: Musugi’s blog imushan.com/
Read more on my blog:
1.Java JVM, Collections, Multithreading, new features series tutorials
2.Spring MVC, Spring Boot, Spring Cloud series tutorials
3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial
4.Java, backend, architecture, Alibaba and other big factory latest interview questions
Life is good. See you tomorrow