Developing an Intellij IDEA Plugin for a Custom Language — Tutorial 2-Creating a Gradle project for plugin and initial steps in developing the actual plugin

Raveen Savinda Rathnayake
5 min readNov 5, 2017

--

Go to Tutorial 1

In this article i will be considering on creating a Gradle project for plugin and implementing the file type recognition. Let’s consider Sample is the name of our custom language and the file extension for the language is .sample. I used gradle in the project because it is really easy to configure and do the necessary changes later in the project.

1. Creating the Gradle project

Click Create New Project or File -> New -> Project and select Gradle.

Step 1

Provide a GroupId and ArtifactId.

Step 2

Provide the Gradle home. If you haven’t installed gradle follow steps given here.

Step 3

Provide the project name and project location.

Step 4

After above steps you will get the following project structure.

Step 5

Then click File ->Project Structure and select Project tab and provide the Project SDK we created in Tutorial 01.

Step 6

2. Adding File Type Recognition feature

2.1 Update the build.gradle file

To run the plugin project we need to configure ‘run configuration’. So we need to add intellij gradle plugin to the file which helps to build and run the plugin inside a intellij IDEA instance. After updating build.gradle it should look like this.

buildscript {
repositories {
maven { url 'http://dl.bintray.com/jetbrains/intellij-plugin-service' }
}

}

plugins {
id "org.jetbrains.intellij" version "0.2.17"
}

repositories {
mavenCentral()
}

apply plugin: 'java'
sourceCompatibility = 1.8
group 'org.myorg'
version '1.0-SNAPSHOT'

apply plugin: 'org.jetbrains.intellij'
intellij {
//For a full list of IntelliJ IDEA releases, please see https://www.jetbrains.com/intellij-repository/releases.
version 'IC-2017.2.5'
pluginName 'Sample-Plugin'
updateSinceUntilBuild false
plugins 'coverage' //Bundled plugin dependencies
}

2.2 Define the language — SimpleLanguage.java

In here we define our language which is ‘Simple’. To do this we need to extend the Language class which is the base class for all programming language support implementations. Specific language implementations should inherit from this class.

package org.myorg;

import com.intellij.lang.Language;

public class SimpleLanguage extends Language {
public static final SimpleLanguage INSTANCE = new SimpleLanguage();

private SimpleLanguage() {
super("Simple");
}
}

2.3 Define an icon — SimpleIcons.java

In here we define a default icon for our language. For this i got a free icon from here. You should place this icon in the SimplePlugin/src/main/resources/icons path. Then intellij IDEA will automatically detect it as a resource. (refer to the screenshot)

package org.myorg;

import com.intellij.openapi.util.IconLoader;

import javax.swing.*;

public class SimpleIcons {
public static final Icon FILE = IconLoader.getIcon("/icons/simple.png");
}
location of Simple.png

2.4 Define a file type — SimpleFileType.java

In here we define the file type and other related details to the plugin.

package org.myorg;

import com.intellij.openapi.fileTypes.LanguageFileType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.Icon;

public class SimpleFileType extends LanguageFileType {

public static final SimpleFileType INSTANCE = new SimpleFileType();

private SimpleFileType() {
super(SimpleLanguage.INSTANCE);
}

@NotNull
@Override
public String getName() {
return "Simple file";
}

@NotNull
@Override
public String getDescription() {
return "Simple language file";
}

@NotNull
@Override
public String getDefaultExtension() {
return "simple";
}

@Nullable
@Override
public Icon getIcon() {
return SimpleIcons.ICON;
}
}

2.5 Define a file type factory— SimpleFileTypeFactory.java

This is used when a creating an instance of a file type associated to the Simple language.

package org.myorg;

import com.intellij.openapi.fileTypes.FileTypeConsumer;
import com.intellij.openapi.fileTypes.FileTypeFactory;
import org.jetbrains.annotations.NotNull;

public class SimpleFileTypeFactory extends FileTypeFactory {

@Override
public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) {
fileTypeConsumer.consume(SimpleFileType.INSTANCE, "simple");
}
}

2.6 Define the plugin.xml file

We need to define a file named plugin.xml in SimplePlugin/src/main/resources/META-INF/plugin.xml .(refer screenshot)

<idea-plugin>
<id>org.myorg</id>
<name>Simple</name>
<version>1.0</version>
<vendor email="sample@sample.com" url="sample.com">Sample</vendor>

<description><![CDATA[
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
]]></description>

<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
]]>
</change-notes>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="162"/>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>

<actions>
<!-- Add your actions here -->
</actions>

</idea-plugin>
location of plugin.xml

2.7 Update the plugin.xml with file type factory extension

We need to add the file type factory implementation location to the plugin.xml file. For this we declare the implementation inside <extensions></extension>

<extensions defaultExtensionNs="com.intellij">
<fileTypeFactory implementation="org.myorg.SimpleFileTypeFactory"/>
</extensions>

2.8 Build and run the plugin project

Click the runIde task in the gradle window and run the plugin. It will start a intellij IDEA instance with the simple plugin installed.

runIde task in gradle window
Starting the plugin

To test the file type association we need to create a project. Since we haven’t created a specific project type for our simple language yet, for the moment let’s create a java project.

Create a new project
Creating the new project

Then create a file with the extension .simple .

Creating a file with .sample extension

If you have followed all the above steps correctly, then you will see the icon you have provided earlier for the Simple language will be shown before the name of the file you created.

Hurrah! You have done the tutorial successfully. The source code foe this can be found in this github link.

My next tutorial will be on Implementing Syntax Highlighting for a custom language.

Stay tuned :)

Go to Tutorial 1

--

--

Raveen Savinda Rathnayake

Associate Technical Lead @WSO2 | Graduate of University of Westminster | www.raveen.me