Search code examples
intellij-ideaintellij-plugin

Custom Intellij language built on the top of XML


First of all, what is my intention. I have a set of XML files in my project. Some of those are hierarchical. The top XML looks like this (very simple example)

<RootElement>
  <Element id=1>
  <Element id=2>
  <Element id=3>
<RootElement>

The element itself contains some other elements and attributes, but it is not relevant in this case.

Than there are many files with this structure:

<RootElement>
  <Element id=1 superId=3>
  <Element id=2 superId=3>
  <Element id=3 superId=2>
<RootElement>

Where the superID is index to root XML file. What I want to implement is a plugin which I would use for simple navigation between those two files.

My idea was to create a new language which will extend XML and add some extra functionality. Only files with specific names (2 or 3) will be part of this language.

I have created the new language:

public class MyLanguage extends XMLLanguage {

  public static final MyLanguage INSTANCE = new MyLanguage();

  protected MyLanguage() {
      super(XMLLanguage.INSTANCE,"MyLanguage", new String[]{"text/xml"});
  }
}

New file type:

public class MyLanguageFileType extends XmlLikeFileType {      
  public static   final MyLanguageFileType INSTANCE = new MyLanguageFileType();

protected MyLanguageFileType() {
  super(MyLanguage.INSTANCE);
}
@Override
public Icon getIcon() {
    return Icons.FILE;
}

And new factory:

public class MyLanguageFileFactory extends XmlFileTypeFactory {
  @Override
  public void createFileTypes(FileTypeConsumer fileTypeConsumer) {
    fileTypeConsumer.consume(MyLanguageFileType.INSTANCE,
            new   FileNameMatcherFactoryImpl().createMatcher("nameOfXML1.xml"),
            new FileNameMatcherFactoryImpl().createMatcher("nameOfXML2.xml")
    );
}

What is the first problem? I see files with the right icon, but when I create Anotator and register it to MyLanguage, it doesn't work. When I register the same Annotator to the XML file, annotations works well.

So the first question is, what have I done wrong?

Thanks all.

P.S.: Minimally FileTypeFactory works well because I can see files with specific names with icons I have set to it.


Solution

  • You don't need to implement your own language to support the navigation. All you need to do is to implement a PsiReferenceContributor that will inject references into the attributes of your XML files.