Skip links

33 how-to questions if you develop a JetBrains extension

Are you about to start developing a JetBrains extension? Or maybe you’re currently working on that? As you may already know, once you develop a JetBrains extension, it’s available in the marketplace for all the IDEs maintained by the editor, such as JetBrains, PyCharm, Rider, WebStorm, and so on.

At Packmind, we’ve built a JetBrains extension that helps developers to share their best coding practices in their organization. We had to dive into the JetBrains SDK, and from that work, we wanted to build a ‘how-to’ list of use cases that might be helpful for you. We wish we had such one when we started working on that extension! Of course, feel free to ask any more questions! 🙂

NB: We also produced a similar post for VSCode if you consider a VSCode extension.

Contents

  • #1 How to access the visible lines in the current editor?
  • #2 How to get the selected text?
  • #3 How to add a marker to a line with a custom image?
  • #4 How to get the programming language of the current editor?
  • #5 How to add a menu on the right-click event?
  • #6 How to set a custom shortcut to perform an action?
  • #7 How to list the JetBrains extensions pane opened in the IDE?
  • #8 How to retrieve all the JetBrains extensions installed?
  • #9 How to underline a line of code?
  • #10 How to retrieve the file name of the current tab?
  • #11 How to listen when a tab has been closed?
  • #12 How to listen when a tab has been opened?
  • #13 How to listen when the current tab has changed?
  • #14 How to add and access a JetBrains extension setting?
  • #15 How to implement a Caret Listener?
  • #16 How to get the current line of the caret?
  • #17 How to listen when the current editor is saved?
  • #18 How to listen when a specific shortcut is called?
  • #19 How to change the logo of my JetBrains extension?
  • #20 How can I display a modal form after a command is sent from a right-click menu?
  • #21 How to prompt a warning notification?
  • #22 How to get the IDE name?
  • #23 How to get the current version of the IDE?
  • #24 How to get the current compilation issues in the opened file?
  • #25 How to get the content of the Output Tab?
  • #26 How to get the current editor theme?
  • #27 How to listen when the IDE is opened?
  • #28 How to listen when the IDE is closed?
  • #29 How to run an asynchronous task in background?
  • #30 How to get the current language of the IDE?
  • #31 How to listen when an application is run?
  • #32 How to listen when an Extract Method operation is called?
  • #33 How to know if the current editor has defined the “LF” or “CRLF” mode for line breaks?

33 How-To questions if you develop a JetBrains extension

#1 How to access the visible lines in the current editor?

What if you’d be interested in computing stuff only for the content in the visible screen?


					
				

#2 How to get the selected text?

Here’re two examples of how to get the selected text in the editor:


					
				

#3 How to add a marker to a line with a custom image?

You’ll need to extend the GutterIconRenderer class.


					
				

Override the equals method ensures that the marker won’t be duplicated when the line is repainted.

#4 How to get the programming language of the current editor?

Easy one when you have access to the current Document:


					
				

#5 How to add a menu on the right-click event?

To add a menu to the right-click event, you need to create a custom AnAction :


					
				

And register it as a popup menu action:


					
				

#6 How to set a custom shortcut to perform an action?

Following tip #5, declare first an AnAction class. Then we will use the Keymap class to add a shortcut to the action:


					
				

#7 How to list the JetBrains extensions pane opened in the IDE?

If your extension needs to know that information, here you are:


					
				

The isVisible method determines if the tool window is currently open.

#8 How to retrieve all the JetBrains extensions installed?

This information can be complementary to the previous tip #7:


					
				

#9 How to underline a line of code?

You can use the EditorGutterComponent class and the GutterIconRenderer class:


					
				

And this is the GutterIconRenderer implementation, where we took as an example the notification icon, but you’ll be welcome to add your icon:


					
				

#10 How to retrieve the file name of the current tab?

Here you are:


					
				

Note that the FileEditorManagerEx class provides additional functionality for managing editor tabs.

#11 How to listen when a tab has been closed?

You can implement a listener for when a tab is closed:


					
				

#12 How to listen when a tab has been opened?

In the same spirit as tip #11:


					
				

#13 How to listen when the current tab has changed?

Again, same approach:


					
				

#14 How to add and access a JetBrains extension setting?

The Settings class and the State class will help you. In our case in Promyze, we had to use the current user API Key:


					
				

To access the setting, you can use the following code:


					
				

#15 How to implement a Caret Listener?

This is useful if your extension needs to run an analysis when the caret moves. The CaretListener interface will be your alley:


					
				

The getNewPosition method of the CaretEvent class returns the new position of the caret.

#16 How to get the current line of the caret?

Here you are:


					
				

#17 How to listen when the current editor is saved?

You’ll need to add a listener:


					
				

#18 How to listen when a specific shortcut is called?

This is how you can listen to the keyboard shortcut CTRL + X.


					
				

#19 How to change the logo of my JetBrains extension?

Add your icon to the resources directory of the project (in one of the following formats: .png, .jpeg, or .gif). Next, in the plugin.xml file, you need to specify the path to the image file in the icon attribute of the idea-plugin tag like this:


					
				

#20 How can I display a modal form after a command is sent from a right-click menu?

Assuming you’ve implemented an action in the contextual menu, you can then display a modal form thanks to the**DialogWrapper** class:


					
				

#21 How to prompt a warning notification?

The Notification class will help you with that purpose:


					
				

On the Notification object, you can also set the setListener property to provide a listener that will be notified when the notification is clicked.

You can use as well NotificationType.INFO and NotificationType.ERROR for informative and error messages.

#22 How to get the IDE name?

As said earlier, this value can be “IntelliJ IDEA”, “PyCharm”, “WebStorm”, etc., depending on the product being used:


					
				

The full version string typically has the format “major.minor.build”.

#24 How to get the current compilation issues in the opened file?

In case your extension needs them:


					
				

#25 How to get the content of the Output Tab?

To retrieve the output content of the app, you can use this trick:


					
				

You can see the tab name is dynamic so you can retrieve the content for another tab.

#26 How to get the current editor theme?

It might impact the way you want to render your components:


					
				

#27 How to listen when the IDE is opened?

You must create a class that implements StartupActivity and add it as an extension in your plugin.xml file:


					
				

And in your plugin.xml file:


					
				

#28 How to listen when the IDE is closed?

Similar to tip #27:


					
				

And in your plugin.xml file:


					
				

#29 How to run an asynchronous task in background?

This is recommended to avoid blocking operations for the users. You can be sure they won’t hesitate to uninstall your extension if it’s too annoying 😉

You can create a class that extends Task.Backgroundable and override the run method to perform the task in the background. Here is an example:


					
				

To run it:


					
				

#30 How to get the current language of the IDE?

Here you are:


					
				

The language code of the locale by using the getLanguage() method:


					
				

#31 How to listen when an application is run?

The Run feature in your IDE:


					
				

#32 How to listen when an Extract Method operation is called?

You can also listen for specific code operations; here, we take the Extract Method operation:


					
				

#33 How to know if the current editor has defined the “LF” or “CRLF” mode for line breaks?

For the record, LF is more on Unix/Mac, while CRLF is used on Windows. Trust me, if you need to parse the source code, it can spare you some trouble 😉


					
				

Wrapping up

That’s all, folks! We hope it can help you start working on your JetBrains extension. In case you work with your team, it can make sense to define best practices when developing a JetBrains extension. Promyze can help you with that; feel free to try it.