Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop plug-ins based on IntelliJ IDEA/Android Studio

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve plug-in development based on IntelliJ IDEA/Android Studio". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to develop plug-ins based on IntelliJ IDEA/Android Studio".

Catalogue

The basic process of plug-in development

1. Environment configuration

1.1 install PDK

1.2 configure plug-ins to develop SDK

two。 New plug-in project

3. Action

4. Configuration description

5. Debugging, packaging

Business practice

Plan 1: self-built Diff tool

Option 2: use JGit

Plan 3: use memory Git

The basic process of plug-in development 1. Environment configuration 1.1 install PDK

Just as Java development requires installation of Java DevKit,IDEA plug-ins, development also requires installation of Plugin DevKit. The role of PDK is to provide built-in IDEA support and related library functions for plug-ins.

Open Intellij IDEA-- > Preferences-- > Plugins. If it is not installed, you can search in marketplace and install it.

1.2 configure plug-ins to develop SDK

Configure SDK to develop plug-ins on the IntelliJ platform, that is, IntelliJ Platform Plugin SDK, which runs on top of JDK, similar to the Android SDK needed to develop Android applications.

Switch to File-> Project Structure, select SDKs under Platform Settings in the left sidebar, click the + button, first select Add JDK, specify the path of JDK, and then select Add IntelliJ Platform Plugin SDK, and specify the JDK added above as the JDK required by the plug-in. It is important to note that starting with IDEA 2020.3, the Java1.8 version can no longer be used. Because the IDEA 2020.3 version is built on Java11, you need to choose the Java11 version if you want to develop plug-ins for IDEA 2020.3 and later.

two。 New plug-in project

File-- > New-- > Project, select Gradle in the pop-up window, then select Java (which indicates that we developed in the Java language) and Intellij Platform Plugin, click Next, then set the name and location of the project, and click Finish to complete the creation.

3. Action

Plug-ins that we customize in IntelliJ can be added to menu items (such as right-click menus) or placed in the toolbar. When the user clicks, an action event is triggered, and IntelliJ calls back the actionPerformed function of the AnAction subclass. So we just need to rewrite the actionPerformed function. We can think of Action as the trigger entry for the plug-in. We can right-click New-- > Plugin DevKit-- > Action to create a new action, which is a subclass of AnAction.

In the next pop-up window, we can create an Action.

Action ID: the unique identity of this action

Class name of the Class Name:action

The name of the Name:action

Description: description of action

Groups: this tag specifies which menu our custom plug-in should be placed under. There are many menus in the IntelliJ IDEA menu bar, such as File, Edit, View, Navigate, Code, etc. , Help, etc. Their ID is usually menu name + Menu. For example, we want to put our custom plug-in in the Help menu as a sub-option of the Help menu. Then specify HelpMenu in Groups. The Anchor attribute on the left describes the location, and there are four main options: first, last, before, and after. Their meanings are as follows:

First: put it at the front

Last: at the end

Before: before the ID specified by the relative-to-action attribute

After: put after the ID specified by the relative-to-action attribute

Keyboard Shortcuts: you can specify a shortcut key for this action

Public class TestAction extends AnAction {@ Override public void actionPerformed (AnActionEvent e) {NotificationGroup notificationGroup = new NotificationGroup ("testid", NotificationDisplayType.BALLOON, false) / * desc: this is an IDEA notification, which will be notified to the floating window in the lower right corner of idea * content: notification content * type: notification type, warning,info,error * / Notification notification = notificationGroup.createNotification ("test notification", MessageType.INFO); Notifications.Bus.notify (notification);}}

After the creation, we can also see the action information we wrote earlier in src/resources/META-INF/plugin.xml. If you want to modify it, you can modify it directly in this configuration file.

4. Configuration description

Src/resources/META-INF/plugin.xml is the configuration file of the entire plug-in, which defines the plug-in name, description information, supported IDEA version number, author information, action related information, and so on.

Plugin.test PluginTest author_name v1.0] > 5. Debugging, packaging

Debug

When the configuration is complete, there is a collection of Intellij in the Gradle column on the right side of the IDEA. Click on the runIde inside to open a sandboxie that runs the IDEA instance containing the plug-in. You can also right-select debug mode to run.

Packing

Click the buildPlugin above to generate the plug-in zip package under the build/distributions/ directory, which is the final product we need. Set Preferences-> Plugins in IDEA, click the setting button next to installed, select Install Plugin from Disk, and then select the zip to install into IDEA.

Components of the plug-in

GUI

ToolWindow

The function of tool window (ToolWindow) is mainly to display information. At the same time, users can also call tools directly in toolwindow, such as default terminal under IDE, Git and so on. As a large part of the IDE sidebar, the interaction between toolwindow and users is very important in the entire ui.

The implementation of toolwindow is mainly divided into two steps: the first step is to create a class to implement the ToolWindowFactory interface, to write the required toolWindowFactory instance, and the second step is to register the ToolWindow in plugin.xml.

When the user clicks the tool window button, the method createToolWindowContent () of the factory class is called and the UI of the tool window is initialized. This process ensures that unused tool windows do not incur any overhead in startup time or memory usage: if the user does not interact with the plug-in's tool window, no plug-in code is loaded or executed.

Public class ToolFactoryCompute implements ToolWindowFactory {private ToolWindow myToolWindow; private JPanel myPanel; private JTextArea textContent; private JScrollPane myScrollPane; / * @ param project Project * @ param toolWindow window * / @ Override public void createToolWindowContent (@ NotNull Project project, @ NotNull ToolWindow toolWindow) {myToolWindow = toolWindow / / add the display panel to the display area ContentFactory contentFactory = ContentFactory.SERVICE.getInstance (); Content content = contentFactory.createContent (mPanel, "Control", false); toolWindow.getContentManager () .addContent (content);}}

Register toolwindow with plugin.xml.

Dialog

The session box (Dialog) can interact with the user to get the user-defined input, or it can be used as a pop-up window to tell the user the information. The implementation of the session box needs to define a subclass of the DialogWrapper abstract class that inherits IDEA. This subclass is the custom session box implementation, and all style definitions and function triggers are placed in this subclass, such as the following implementation:

Public class FormTestDialog extends DialogWrapper {private String projectName / / if you need to get the project name, put it in as a property of this class / / DialogWrapper does not have a default no-parameter constructor, so you need to rewrite the constructor, which provides many overloaded constructors. / / the constructor that passes project type parameters is used here. Through the Project object, you can obtain some properties of the project opened in the current IDEA, such as the project name. Public FormTestDialog (@ Nullable Project project) {super (project) such as project path SetTitle ("form test"); / / set the dialog title this.projectName = project.getName ();} / / override the following method to return a custom swing style that will be displayed at the top of the dialog box @ Override protected JComponent createNorthPanel () {return null } / / override the following method to return a custom swing style, which will be displayed at the bottom of the dialog box @ Override protected JComponent createSouthPanel () {return null;} / / overriding the following method, returning a custom swing style that will be displayed in the center of the dialog box @ Override protected JComponent createCenterPanel () {return null }} Business practice

Get file differences

Plan 1: self-built Diff tool

In order to obtain the file differences between the code directory and the compiled directory, it is necessary to use the Diff tool, which involves a lot of custom rules, such as whether the difference files should be ignored. The advantage is that the rules for identifying differences can be completely customized and flexible. The disadvantage is that it takes a long time, after all, to write a Diff system. Time is tight, so this plan is pass.

Option 2: use JGit

JGit is a set of Git tools written by Java. All instructions of Git can be called through Java code, which can perfectly solve the need to obtain file differences. However, after the actual test, it is found that when calling the git.status.call () method, because it needs to initialize Git, including the establishment of diff,filetree and other operations, for a large warehouse, a run will take more than ten seconds, which is unacceptable, so give up.

Git git = Git.open (new File ("~ / source-code.temp-1/git")); Status status = git.status (). Call (); / / the returned values are the relative workspace path, not the absolute path status.getAdded () .forEach (it-> System.out.println ("Add File:" + it)) / / git add command will see a change status.getRemoved () .forEach (it-> System.out.println ("Remove File:" + it)); / / git rm command will see a change, the list of files deleted from the staging area status.getModified () .forEach (it-> System.out.println ("Modified File:" + it)) / / modified file list status.getUntracked () .forEach (it-> System.out.println ("Untracked File:" + it)); / / New workspace file list status.getConflicting () .forEach (it-> System.out.println ("Conflicting File:" + it)); / / conflicting file list status.getMissing () .forEach (it-> System.out.println ("Missing File:" + it)) / / File list deleted in the workspace solution 3: make use of memory Git

After solution 2, we found that git meets our requirements, but because JGit needs to be initialized, it takes a long time. But when we run IDEA, using git status on the terminal is very fast, which is millisecond, so we can use the git in memory to execute the git status command directly and match the file differences in the returned results.

Millisecond correspondence can be achieved by letting Java execute the git command.

Java executes the shell command and returns the execution result

/ * execute the shellCommand command to get the returned result of the command. In the returned result, put the qualified filename into the file collection * * @ param cmd shell command * @ return command output * / public static String executeCommand (String [] cmd) throws IOException {String resultStr = ""; / / use runtime to execute the shell command Process ps = Runtime.getRuntime () .exec (cmd) / / get the normal flow and abnormal flow try of the process object (BufferedReader brInfo = new BufferedReader (new InputStreamReader (ps.getInputStream (); BufferedReader brError = new BufferedReader (new InputStreamReader (ps.getErrorStream () {StringBuilder stringBuffer = new StringBuilder (); String line / / read the output result and read if (brInfo.readLine ()! = null) {while ((line = brInfo.readLine ())! = null) {stringBuffer.append (line) .append ("\ n") per line; / / handle file differences filterFiles (line) }} else {/ / if the normal output stream is null, then get the exception flow while ((line = brError.readLine ())! = null) {stringBuffer.append (line) .append ("\ n") }} / / wait for the execution of the shell command to complete ps.waitFor (); resultStr = stringBuffer.toString ();} catch (Exception e) {e.printStackTrace ();} / / the returned result of the shell command return resultStr } / / Test public static void main (String [] args) {String cmd = "git status" in the main function; String resultStr = executeCommand (new String [] {"/ bin/sh", "- c", cmd}); System.out.println (resultStr) } Thank you for reading, the above is the content of "how to achieve IntelliJ IDEA/Android Studio-based plug-in development". After the study of this article, I believe you have a deeper understanding of how to achieve IntelliJ IDEA/Android Studio-based plug-in development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report