📜 ⬆️ ⬇️

Creating a plugin for Intellij IDEA. Notes and small tips

Half a year ago or so, I caught fire with the ideas to write my own plugin for Intellij IDEA. According to the idea, he had to count how much time the developer spent on the project (total, per day, per session) and display the result on the diagram. No magic, but such a great function would help me calculate the time of work.



Scrolling through the catalog of existing plug-ins, I still found one like that. But he didn’t draw any diagrams, he opened up through two drop-down lists - which is terrible, how inconvenient, - and it turned out to be rather scanty.


')
In short, I became even more entrenched in the belief that my plugin would be useful, and set to work. At once I want to say that Intellij IDEA plug-in development materials are not only small, but there are almost none.

But something still managed to dig.

useful links



JetBrains Documentation

Plugin Developer Forum for Intellij Platform

Short and clear about where to get the source code IDE

Very detailed cycle of articles on Habré from @Lucyfer

Java API Examples


Small tips


I decided not to talk about such things as setting up the environment for plug-in development, the configuration file, etc., since all few articles in runet are dedicated to this. During the creation of the plug-in, I stumbled many times over the non-obvious (to me, in any case) features of Intellij IDEA. If I had known about them before, things would have gone much faster. Therefore, I will share some points that can save you a lot of time.

Add your CustomStatusBarWidget


For convenience, I decided to supplement the Status Bar with a small element that would display how much time was spent during the current session in the environment, while not distracting the developer from the editor. The StatusBar interface contains an overloaded addWidget () method that accepts the CustomStatusBarWidget parameter:

public interface StatusBar extends StatusBarInfo, Disposable { ... void addWidget(@NotNull StatusBarWidget var1); void addWidget(@NotNull StatusBarWidget var1, @NotNull String var2); void addWidget(@NotNull StatusBarWidget var1, @NotNull Disposable var2); void addWidget(@NotNull StatusBarWidget var1, @NotNull String var2, @NotNull Disposable var3); ... } 

You can get the StatusBar of the project itself as follows:

 statusBar = WindowManager.getInstance().getStatusBar(currentProject); 

The main problem for me was the implementation of CustomStatusBarWidget, as well as its location in relation to other widgets (I did not know their names, so I could not find them in the source code of the environment).

Salvation came from here . That is, the widget can be added relative to others as follows:

 statusBar.addWidget(myWidget1,"before " + IdeMessagePanel.FATAL_ERROR); statusBar.addWidget(myWidget2, "after Encoding"); statusBar.addWidget(myWidget3,"after InsertOverwrite"); statusBar.addWidget(myWidget4,"after Position"); 

Implement CustomStatusBarWidget:

  class Widget implements CustomStatusBarWidget{ private JLabel myLabel = new JLabel("00:00:00"); @Override public JComponent getComponent() { return myLabel; } @NotNull @Override public String ID() { return null; } @Nullable @Override public WidgetPresentation getPresentation(@NotNull PlatformType platformType) { return null; } @Override public void install(@NotNull StatusBar statusBar) { } @Override public void dispose() { } } 

Add our widget on StatusBar, add JLabel with an icon ...



Getting ToolBar'a action from Java code


You can read about the action system itself in Intellij IDEA plugins here .
Getting a component using ActionManager.

  private JComponent createActionToolBar(AnAction ...actions){ DefaultActionGroup actionGroup = new DefaultActionGroup(); for(AnAction anAction : actions){ actionGroup.add(anAction); } ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("Tempore.MainPanel", actionGroup, false); return toolbar.getComponent(); } 

The address of the project where the plugin works


The project path can be obtained using an instance of the Project class. For example, the path to the .idea folder of the project:

 String path = currentProject.getProjectFile().getParent().getPath(); 

Getting components from the actionPerformed () method


From the AnActionEvent parameter you can access the components:

 Project currentProject = DataKeys.PROJECT.getData(actionEvent.getDataContext()); VirtualFile currentFile = DataKeys.VIRTUAL_FILE.getData(actionEvent.getDataContext()); Editor editor = DataKeys.EDITOR.getData(actionEvent.getDataContext()); StatusBar statusBar = WindowManager.getInstance().getStatusBar(DataKeys.PROJECT.getData(actionEvent.getDataContext())); . 

Pop up messages


A simple message with information or an error message can be added to the StatusBar as follows:

  JBPopupFactory.getInstance() .createHtmlTextBalloonBuilder("You have been working for two hours! Recommend to have a break ", MessageType.INFO, null) .setFadeoutTime(7500) .createBalloon() .show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.atRight); 

It looks like this:


Compatibility with Intellij Platform Environments


Since counting the time spent on a project is not only useful in Intellij IDEA, I decided to make the plugin compatible with other Intellij Platform environments. To do this, add a tag in the plugin.xml file:

 <idea-plugin version="0.5.5b"> ... <depends>com.intellij.modules.lang</depends> ... </idea-plugin> 

Now, besides Intellij IDEA, the plugin is also suitable for RubyMine, WebStorm, PhpStorm, PyCharm and AppCode.

Results


For about a month, I managed to create a plugin that monitors how much time you spend on your projects, advises taking a break if you write non-stop code for several hours and draws a chart of the hours spent for each day.
It also stops if the activity in the environment has stopped for more than 5 minutes and starts when the user starts working again.



If you wish, the plugin itself can be downloaded here .

I hope someone this article will help to deal with some aspects of the development and get rid of sleepless nights reading the API.

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


All Articles