Skip to content

The Consulo 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 Consulo by annotating the implementation class with @ExtensionImpl.

Reference: Registering a File Type

2.1. 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.consulo.sdk.language package of the simple_language_plugin code sample:

java
package org.consulo.sdk.language;

import consulo.language.Language;

public class SimpleLanguage extends Language {

  public static final SimpleLanguage INSTANCE = new SimpleLanguage();

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

}

2.2. 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.

java
package org.consulo.sdk.language;

import consulo.ui.image.Image;
import consulo.ui.image.ImageKey;

public class SimpleIcons {

  public static final Image FILE = ImageKey.of("SimplePlugin", "simple-file", 16, 16);

}

2.3. Define a FileType

The Simple Language file type is defined by subclassing LanguageFileType:

java
package org.consulo.sdk.language;

import consulo.language.file.LanguageFileType;
import consulo.localize.LocalizeValue;
import consulo.ui.image.Image;

import jakarta.annotation.Nonnull;

public final class SimpleFileType extends LanguageFileType {

  public static final SimpleFileType INSTANCE = new SimpleFileType();

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

  @Nonnull
  @Override
  public String getId() {
    return "Simple File";
  }

  @Nonnull
  @Override
  public LocalizeValue getDisplayName() {
    return LocalizeValue.localizeTODO("Simple language file");
  }

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

  @Nonnull
  @Override
  public Image getIcon() {
    return SimpleIcons.FILE;
  }

}

2.4. Register the FileType

The LanguageFileType base class is annotated with @ExtensionAPI. To register the file type with the Consulo, annotate the SimpleFileType implementation class with @ExtensionImpl.

2.5. Run the Project

Create an empty file with the extension *.simple, and Consulo 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