Build a Custom IntelliJ Code Inspection Plugin | by Ruben Quadros | May, 2022

Create a plugin that inspects Kotlin information courses

Supply: https://www.behance.net/binalogue

Introduction

IntelliJ Platform absolutely helps plugins they usually have offered APIs to create your individual customized plugins. In keeping with their docs: Merchandise based mostly on the IntelliJ Platform will be modified and adjusted for customized functions by including plugins.

By the tip of this text, you’ll have a plugin that inspects Kotlin information courses and warns you in case you are lacking the SerializedName annotation. All this occurs in real-time! Test it out:

Information class inspector

Inspiration

I work as an Android dev and in case you are accustomed to Android growth, all the discharge apps would have some type of code obfuscation. More often than not you utilize Gson to parse JSON response or perhaps you’re utilizing it to transform JSON into an object or vice-versa.

When you miss annotating your information class params with SerializedName annotation then Gson wouldn’t have the ability to parse the response into your object due to code obfuscation and the parsing will fail. This has occurred to our workforce a couple of occasions (as soon as by me as effectively) and I needed to do one thing about it.

If you’re questioning, sure lint checks are one other method however I needed to have one thing which updates the person in real-time! I did attempt making a custom android lint rule however it didn’t work as anticipated.

Let’s code!

Challenge setup

Create a brand new IntelliJ Platform Plugin challenge from the IntelliJ IDEA challenge wizard. You’ll have the next construction:

Supply: Plugin development doc

A very powerful file right here is plugin.xml. This file is used to configure the plugin.

Firstly, let’s go forward and add the required dependencies. For the code inspection plugin we are going to solely require the Kotlin dependency. We must add the dependency in our gradle file in addition to our plugin configuration file plugin.xml.

Add Kotlin dependency to construct.gradle file
Add Kotlin dependency to plugin.xml

Now that we have now setup our challenge, let’s get into the fascinating a part of code inspection!

Declaring the inspector

There will be 2 forms of inspectors as follows:

  • Native inspector — The com.intellij.localInspection extension level is used for inspections that function on one file at a time, and in addition function “on-the-fly” because the person edits the file.
  • World inspector — The com.intellij.globalInspection extension level is used for inspections that function throughout a number of recordsdata, and the related repair would possibly, for instance, refactor code between recordsdata.

In our case we’d like the localInspection as every information class must inspected individually and in addition when it’s being edited.

Let’s add this extension to our plugin configuration file.

Add localInspection in plugin.xml

Now our plugin.xml file seems as follows:

plugin.xml

Let’s attempt to perceive the keys in localInspection.

groupPath — Father or mother group names used to show in IDE settings UI.
In our case: Settings/Editor/Kotlin

groupBundle — Get show messages from this bundle. You possibly can specify your individual bundle for displaying customized messages.

groupKey — Group identify used to show in IDE settings UI.
In our case: Settings/Editor/Kotlin/Possible bugs

implementationClass — Corresponding inspection implementation class.

Remainder of the keys are self explanatory.

Creating the inspector

Now that we have now declared the code inspector within the plugin configuration file, let’s go forward and implement the identical.

The implementation is as follows:

SerializedNameInspector.kt
Extensions.kt

Let’s attempt to perceive what’s occurring within the implementation.

For inspecting a Kotlin class, the implementation class ought to prolong AbstractKotlinInspection. This class presents strategies to examine courses, fields and strategies.

Subsequent, we are able to override the buildVisitor methodology to find out whether or not the weather of the file are of curiosity to inspection. Right here, we examine whether or not the information class params are annotated with the required SerializedName annotation. If not, we register it as an issue so the IDE can spotlight it to the person.

Fairly easy, don’t you agree?

Creating the editor notification

To make it simpler for the person so as to add the annotation to your entire class, let’s go forward and supply a notification with related actions.

Editor notification

Principally, if there’s a lacking SerializedName annotation, together with highlighting the param for which it’s lacking we additionally present a notification.

This notification has 2 actions:

  • Add annotation — This provides all of the lacking annotations for your entire class
  • Ignore — This ignores the inspection for this explicit file

Firstly, we have now so as to add the notification extension to the plugin configuration file.

Add editorNotificationProvider in plugin.xml

The implementation is as follows:

SerializedNameNotification.kt

To show an editor notification, the implementation class ought to prolong EditorNotifications.Supplier<T>. We will use EditorNotificationPanel because it gives the required UI to show the message and the related actions.

The getKey() methodology returns the distinctive key for this notification. This key can be utilized to take away the notification.

First, we examine if any of the params are lacking the required annotation (identical as our inspector) and in that case, we create a notification. If the category params get up to date with the required annotation, we take away the notification whether it is being displayed.

The createPanel(..) methodology is used to create the notification which is to be displayed. Let’s see the implementation.

Create the notification

Right here, we offer the message to be displayed as effectively the the actions. Additionally, this notification needs to be proven provided that there are lacking annotations and the person has not ignored inspection for this file.

The Add annotation and Ignore actions are as follows:

Add lacking annotation
Ignore inspection for the file

Lastly, to take away the notification which is being displayed we are able to do the next:

Take away notification whether it is displayed

You’ll additionally wish to examine the interior courses. You possibly can simply get the interior courses of a PsiClass as follows:

Examine interior class

More Posts