⬆️ ⬇️

Creating a plugin for Intellij Platform (IntelliJ IDEA, RubyMine, WebStorm, PhpStorm, PyCharm and AppCode)

Hi, Habr!

In this topic, I will tell you how to create a simple plugin for IntelliJ IDEA and PyCharm. Since all IDEs contain the same interface for working with plug-ins, modifying it for other systems does not require significant efforts (in more detail ), unlike the process of direct development.



I warn you that if you decide to create your own plugin, then the expected result may not correspond much to what was intended or will take considerable time. If I knew what would happen and how much time I spent, I would hardly have taken it.



Plugins are written in Java, UI is created in SWING. Plugin development documentation consists of just a few pages and a handful of examples. On the Internet, this topic is not reflected at all. The only thing that slightly helps is the presence of source codes for some already created plugins .

')

Initially, I wanted to create a window in which you can quickly and conveniently select the description of the standard python library, but it turned out badly. You can add more, even search and highlight the documentation of python. There is no desire to finish, while it is easier to use the browser in the old manner.







Then let's start:

1) Preparing the environment



IDE plugins are written in the IDE. They will have to be installed and configured.



Installation Tools:


We will need:

- Java (developers recommend 1.6);

- IntelliJ IDEA Community Edition or Ultimate. (I used the first one, as it is free. You can download it here )



Tool setting:


You must configure the Java SDK and IntelliJ IDEA Plugin SDK:

- we start IntelliJ IDEA

- open the menu item File | Project Structure

- select the SDKs tab, click on the plus sign and select the path to Java



- select the Project tab

- click on new, then IntelliJ IDEA Plugin SDK and in the menu that opens - choose the path to IntelliJ IDEA







Now you can create plug-in projects.



2) Creating a plugin



The plugin is a regular java program that uses a special open interface to interact with the IDE (this is just what it is called open, it would be better to call closeapi). There are several main types of plug-ins: user action (AnAction), service (Service), extensions for other plug-ins (Extension) and just components. Read more in the documentation. And although I have not extended a single plug-in, I registered it in the configuration file.



Create a plugin project


File | new project: Enter the name, select the type of Plugin Module, select the SDK configured in step 1, create a project.









Further, depending on the plugin being developed, the actions will be individual. I will describe how to create your plugin. For a basis I took the example of a plug-in from the documentation - Tool Window. To get its source code and other examples, you need to download the IDE source code.



Editing plugin.xml


After creating the project, the plugin structure and the main configuration file appeared - plugin.xml.







In it, we will register our classes and configure them. More information about this file can be found in the documentation.

File ID:

<idea-plugin version="2"> <name>DocPy</name> <description>Python standard documentation</description> <version>1.0</version> <vendor>YourCompany</vendor> <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description --> <idea-version since-build="107.105"/> <application-components> <!-- Add your application components here --> </application-components> <project-components> <!-- Add your project components here --> </project-components> <actions> <!-- Add your actions here --> </actions> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> </idea-plugin> 




For my plugin, I added two lines:



in the idea-plugin section:

com.intellij.modules.lang - in order for this plugin to work in PyCharme, if you do not specify any dependencies, the plugin will work only in IntelliJ IDEA. In this section, you can also specify the necessary dependencies for the operation of the plugin.



in the extensions section:

- to start the class when starting IDE and its initial location: anchor - window position, factoryClass - our class



Eventually:

 <idea-plugin version="2"> <name>DocPy</name> <description>Python standard documentation</description> <version>1.0</version> <vendor>YourCompany</vendor> <idea-version since-build="107.105"/> <depends>com.intellij.modules.lang</depends> <application-components> </application-components> <project-components> </project-components> <actions> </actions> <extensions defaultExtensionNs="com.intellij"> <toolWindow id="PythonDoc" secondary="true" anchor="right" factoryClass="MainPanel" > </extensions> </idea-plugin> 


We create class and graphic interface


Since I have a simple graphical window, I create a class from the Gui Form template, which will allow me to use a convenient graphical editor to create an interface. I add the necessary elements:





This is the result of this class:

 public class MainPanel { private JPanel panel1; private JTabbedPane tabbedPane1; private JTextArea MainText; private JList FileList; private JPanel FilesTab; private JPanel TextTab; private JScrollPane TextPane; } 


Here we have a panel with two tabs, on the first list of files, on the second text from this file.

We extend this class with the ToolWindowFactory interface. With it, you can make my class part of the IDE and add standard controls (icons, the possibility of location in an arbitrary place, scaling, etc.)



How did I find out about this interface? - you ask.

I copied from the example what else is unknown and is not written anywhere, one of the ways is to look in the source code of other plug-ins.



We implement the required method of this interface - createToolWindowContent, which is called when creating our window. Interesting in it only the last three lines.



 @Override public void createToolWindowContent(Project project, ToolWindow toolWindow) { final String[] FilesMas = getFileList("python-3.2.3-docs-text\library\"); FileList.setListData(FilesMas); //    FileList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { MainText.setText(readFileToStr("python-3.2.3-docs-text\library\" + FilesMas[FileList.getSelectedIndex()])); //     MainText.setSelectionStart(0); MainText.setSelectionEnd(0); //    TextPane.getVerticalScrollBar().setValue(0); //    tabbedPane1.setSelectedIndex(1); } }); MainText.setText(readFileToStr("A:\downloads\python-3.2.3-docs-text\library\sqlite3.txt")); //   ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); // ()   GUI Content content = contentFactory.createContent(panel1, "", false); //  IDE toolWindow.getContentManager().addContent(content); } 




After adding two more functions (getting the list of files and reading from the file) the plugin is ready for the next stage.



3) Build the plugin



To use the plugin you need to collect it in a certain way:

Build | Prepare Plugin Module PyDoc for Deployment



We get in the project directory a jar file with the name of our project.



4) Install the plugin in the IDE



Through settings-> Plugins-> Download from disk (select the jar obtained in the previous step) or copy the jar file to the plugins folder.



findings



Unfortunately, the developers and the community of the popular IDE line write little about the process of creating plugins, which does not contribute to their emergence and development. In the end, this is similar to finding a way out of a room without a single light source. I hope my post will give some impetus in this direction.



Since almost everything had to be done blindly, maybe I made mistakes somewhere. I would be grateful if you correct in the comments or via PM.



Some more information is here .

The source code of my project .

Source: https://habr.com/ru/post/148996/



All Articles