When you write Java code, repetitive operations are often involved, so it's good to think of a plug-in like this. If it's a scenario everyone will encounter, the IDE may have already provided it, or someone else may have written it.If this is unique to your coding environment, you will have to write your own tools.So here's how to write IDEA plug-ins to make your programming environment stronger and better to force.
development environment
Developing IDEA plug-ins depends on the following:
- IntelliJ IDEA Community Edition
- IntelliJ IDEA Community Edition Source
- Plugin DevKit Plugin
- IntelliJ Platform SDK
Install IntelliJ IDEA Community Edition
You may have installed the Ultimate version, but you still need to install the community version of IDEA.Core code cannot be debugged while debugging because the commercial version is closed source.
Download IntelliJ IDEA Community Edition Source
The community version of the 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
Reference for methods to run IDEA from source code: Check Out And Build Community Edition
Add IDEA jdk
Although the reason is unknown, according to Check Out And Build Community Edition , we need to create an IDEA jdk to run the plug-in:
Unless you use the official JDK on your Mac, you need to manually add/lib/tools.jar to the classpath.
Configure IntelliJ Platform SDK
Open File | Project Structure to create a new IntelliJ Platform SDK:
The Java SDK chooses the IDEA jdk we just created:
Then we can add the downloaded IDEA Community Edition source code to the source path so that when debugging, we can debug IDEA's own code:
First Plugin
Let's write the simplest plug-in to learn the full steps of writing a plug-in.
New Project
Select IntellJ Platform Plugin and the Project SDK specifies the newly created plugin sdk:
New plug-in project:
There are two directories, src and resources, under the plug-in root directory.src is the plug-in code directory, resource is the plug-in resource directory, where META-INF/plugin.xml is the plug-in's description file, just like the web.xml of a Java web project.
The default content of plugin.xml is as follows:
<idea-plugin> <id>com.your.company.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>
Create a new Action
The most common way a plug-in extends IDEA is to add menu items to a menu bar or toolbar, where users can trigger plug-in functionality by clicking on menu items.IDEA provides the AnAction class, which has a virtual method, actionPerformed, which is called each time a menu is clicked.
There are two steps to creating a new custom Action:
- Inherit the AnAction class to implement 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 register through plugin.xml, you can ignore this constructor public TextBoxes() { // Set menu item name super("Text _Boxes"); // You can also set menu item names, descriptions, icons // 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()); } }
Then we register the 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" text="Text _Boxes" description="A test menu item" /> </group> </actions>
Here we've created a new menu group where the underscore of the text string indicates that the letter is used as a shortcut.This menu displays the following effects:
In addition to manually creating a new Action, IDEA also provides a quick way to create a new Action by clicking New in the code directory to see the Action:
You can fill in this panel with the Action information you want to create, IDEA will help you create a new class, and plugin.xml will help you register:
Run Plugins
Running a plug-in is as easy as running normal Java code. Clicking the Run or Debug button launches a new IDEA instance where the plug-in is valid.
Click Text Boxes to see the effect of the plug-in.
Reference material
This independent blog address: Write IntelliJ IDEA Plugin from scratch | Muquoia Blog