IntelliJ Platform Plugin SDK Help

2. Language and File Type

The IntelliJ Platform determines file type by examining the name of a file. Each language has Language and LanguageFileType objects defining the language. Register the LanguageFileType with the IntelliJ Platform in the plugin configuration file.

Reference: Registering a File Type

Define the Language

The language implemented in this tutorial is named "Simple" - note the case of the name. The SimpleLanguage class is defined in the org.intellij.sdk.language package of the simple_language_plugin code sample:

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.intellij.sdk.language; import com.intellij.lang.Language; public class SimpleLanguage extends Language { public static final SimpleLanguage INSTANCE = new SimpleLanguage(); private SimpleLanguage() { super("Simple"); } }

Define an Icon

The icon for the Simple Language is defined by the SimpleIcons class. There is nothing uniquely Simple Language-specific about defining the icon itself. The definition follows a pattern similar to defining, e.g., SdkIcons.

// Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.intellij.sdk.language; import com.intellij.openapi.util.IconLoader; import javax.swing.*; public class SimpleIcons { public static final Icon FILE = IconLoader.getIcon("/icons/jar-gray.png", SimpleIcons.class); }

Define a FileType

The Simple Language file type is defined by subclassing LanguageFileType:

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.intellij.sdk.language; import com.intellij.openapi.fileTypes.LanguageFileType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; 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.FILE; } }

Register the FileType

Direct registration is possible - no FileTypeFactory is required.

Instead, the file type is registered via the com.intellij.fileType extension point in plugin.xml:

<extensions defaultExtensionNs="com.intellij"> <fileType name="Simple File" implementationClass="org.intellij.sdk.language.SimpleFileType" fieldName="INSTANCE" language="Simple" extensions="simple"/> </extensions>

Define a FileType Factory

First, define SimpleFileTypeFactory as a subclass of FileTypeFactory.

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.intellij.sdk.language; import com.intellij.openapi.fileTypes.FileTypeConsumer; import com.intellij.openapi.fileTypes.FileTypeFactory; import org.jetbrains.annotations.NotNull; /** * Note: This class is only used with the fileTypeFactory extension point * for versions of the IntelliJ Platform prior to v2019.2. */ @SuppressWarnings("deprecation") public class SimpleFileTypeFactory extends FileTypeFactory { @Override public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) { fileTypeConsumer.consume(SimpleFileType.INSTANCE); } }

Register the FileType Factory

The SimpleFileTypeFactory is registered using the com.intellij.openapi.fileTypes.FileTypeFactory extension point in plugin.xml.

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

Run the Project

Run the plugin by using the Gradle runIde task.

Create an empty file with the extension *.simple, and IntelliJ IDEA automatically associates it with our language. Note the appearance of the Simple Language file icon next to the test.simple file in the Project Tool Window, and the editor tab for the file.

File Type Factory
Last modified: 23 September 2021