Skip to content

Writing an IntelliJ Plugin for Inserting Timestamps

Posted on:July 24, 2022

The Problem

Whilst writing a lot of tests involving time-based functions, I got tired of having to manually write out full timestamps, I was copying and pasting timestamps then changed 1 or 2 numbers to make them unique for the test case. This became a repetitive task that strained my fingers from hitting copy and paste too much.

All developers face this problem at some point, the best word for this is “toil” (something that my manager at Freetrade pointed out to me, coined from the Google SRE book). It’s a repetitive task that doesn’t require any new thought process but rather requires manual steps to be followed.

timestamp plugin

The Solution

There are a lot of very useful plugins which work across all the different IDEs that JetBrains has to offer, I was also using a very popular one for generating UUIDs by Léo Millon which gave me the inspiration to migrate another text-generation problem away.

The IntelliJ SDK has action methods which are extended for inserting text into the clipboard or into the text editor at the cursor. The rest of the plugin is just facilitating this text insertion by creating a new timestamp value with a configurable format.

The settings page allows the user to change the format of timestamps from one of the standard ISO or Java formats (ISO 8601, Instant, LocalDate, LocalTime, LocalDateTime, ZonedDateTime, RFC 1123) or epoch seconds/milliseconds.

IDEA Gradle Plugin

The gradle plugin com.jetbrains.intellij applies all the methods required to build an IntelliJ plugin. This is where you define the version and platform (IDEA, WebStorm, PyCharm, etc.) that the plugin will target.

The plugin also brings in the tasks that you will use to build and run the plugin in a test IDE environment, through the runIde task. Creating an extension class to AnAction will be picked up by the IDE and can any code that is inside the actionPerformed method will be run.

Within this action, we will replace any highlighted text or insert the timestamp where the caret is within the editor. This means we have to check for the highlighted text and work out the position that our text will be inserted and then update the caret to be placed at the end of the inserted text. The IntelliJ plugin gives us a Document class which provides methods for all these requirements.

Build Pipeline

The repository uses GitHub Actions to build the plugin and run the tests to verify that the build is clean. None of the deployment is handled within GitHub, it is only serving as a place to verify that the plugin is in a good state.

GitHub action report
GitHub Action to build and test the plugin

The workflow that I use is fairly standard across my projects, where I use the actions/checkout to pull down the code, actions/setup-java to setup the JDK (I’m currently building against version 11), gradle/wrapper-validation-action to setup the gradle wrapper in the repository, then gradle/gradle-build-action to run the build arguments. This constitutes building the code and running all the tests against it.

The IntelliJ plugin has a publishPlugin task which is used to sign and push the latest version to JetBrains.

Settings Menu

The settings menu is written using the old school Swing library, with a .form file for the layout and a backing class for the logic in SettingsForm. The components in the form are initialised in the createUIComponents method, which loads up the format ComboBox (dropdown), and loadSettings handles setting the format ComboBox to the value that’s currently configured in the settings. There’s a updatePreviewLabel method which provides the change to the preview label each time a new format is selected to show the user how the timestamp will be formatted.

settings menu
The settings page for the plugin

Conclusion

There is definitely a lot of value in having the right setup of plugins for your IDE. It reduces the amount of time it takes to write code. Building a plugin for the IDE therefore makes the developer experience richer.

JetBrains have great documentation on how to build a plugin for IntelliJ and I am excited to write a new plugin that will utilise the new Kotlin DSL UI.


Go to Source Code


Get Plugin from Jetbrains Marketplace: