Skip to content

Instantly share code, notes, and snippets.

@ghsatpute
Created April 24, 2020 17:04
Show Gist options
  • Save ghsatpute/965f3a5d68527a8ed6ea5bb3527a5c82 to your computer and use it in GitHub Desktop.
Save ghsatpute/965f3a5d68527a8ed6ea5bb3527a5c82 to your computer and use it in GitHub Desktop.
IntelliJ Plugin: Exact file type matcher

While writing IntelliJ Idea Plugin how to map the exact file name.

For example, you want to write a plugin for files exactly named abc.json, or in more practical scenarios Jenkinsfile

  1. Write FileMatcherType class
 class MyFileType extends LanguageFileTyp {
    public static final MyFileType INSTANCE = new MyFileType();

    private MyFileType() {
        super(MyLanguage.INSTANCE);
    }

    @NotNull
    @Override
    public String getName() {
        return "My File";
    }

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

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

    @Nullable
    @Override
    public Icon getIcon() {
        return MyLanguageIcons.FILE;
    }
}

Now, in your plugin.xml, add following lines

    <extensions defaultExtensionNs="com.intellij">
        <fileType implementationClass="MyFileType" fileNames="abc.json" name="My File"
                  fieldName="INSTANCE" language="My Language"/>
    </extensions>

Now, here it's bit odd that in getDefaultExtension we write json and in plugin.xml we write fileNames="abc.json". But this works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment