Eclipse-Plugin using CDT: Associate file extension with custom CDT Language

女生的网名这么多〃 提交于 2019-12-13 03:48:54

问题


I can configure my plugin so that files with certain extensions are opened in the CDT CEditor.

<editor
      class="org.eclipse.cdt.internal.ui.editor.CEditor"
      contributorClass="org.eclipse.cdt.internal.ui.editor.CEditorActionContributor"
      default="true"
      filenames="*.ext, *.c"
      icon="icons/small.png"
      id="de.blub.ide.exteditor"
      name="EXT Editor">
</editor>

This works, somehow, but I want to extend the CEditor to have more features and change some existing functionality. For example, I am not content with the outline view. According to this old post from 2009, the way to do that is to define my own language with the extension point "org.eclipse.cdt.ui.language".

This is how I did it:

 <extension
     point="org.eclipse.cdt.core.language"
     id="de.blub.ide.mylanguage"
     name="My Test Languager">
     <language
         class="de.blub.base.utils.MyTestLanguage"
         id="de.blub.plugin.mytestlanguage"
         name="My Test Language">
         <contentType id="org.eclipse.cdt.core.cSource"/>
         <contentType id="org.eclipse.cdt.core.cHeader"/>
     </language>
 </extension>

I wrote a class that extends AbstractCLikeLanguage:

package de.blub.base.utils;

import org.eclipse.cdt.core.dom.parser.AbstractCLikeLanguage;
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICLanguageKeywords;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;

public class MyTestLanguage extends AbstractCLikeLanguage {

    private static final String id = "de.blub.plugin.mytestlanguage";

    @Override
    public String getId() {
        return id;
    }

    @Override
    public int getLinkageID() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    protected ISourceCodeParser createParser(IScanner scanner, ParserMode parserMode, IParserLogService logService,
            IIndex index) {
        return null;
    }

    @Override
    protected ParserLanguage getParserLanguage() {
        // TODO Auto-generated method stub
        return ParserLanguage.C;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public Object getAdapter(Class adapter) {
        if (ICLanguageKeywords.class.equals(adapter)) 
            return this;
        return super.getAdapter(adapter);
    }

    @Override
    public String[] getKeywords() {
        return super.getKeywords();
    }

}

How can I associate a certain file extension with this language? Unfortunately I found very few examples for the use of the extension point org.eclipse.cdt.core.language. At the moment I am not even sure if this is what I want. But the description seems promissing:

This extension point is used to declare a language or language variant. Languages define how the C model of a file is created (e.g. to populate the outline view).

EDIT:

I have added a contentType:

<extension
     point="org.eclipse.core.contenttype.contentTypes">
  <content-type
        file-extensions="ext"
        id="de.blub.contenttype.ext"
        name="EXT"
        priority="high">
  </content-type>

This contentType can be added in the language section.

<language 
    ...
    <contentType
        id="de.verified.rtt.contenttype.rts">
    </contentType>
</language>

Under Window->Preferences->General->Content Types I can link my new content type with my new editor. I can also add a new file association there. But I don't want to add a file association manually! It has to be possible to do this in the plugin.xml, somehow.

I also tried to add an editorContentTypeBinding like this:

 <editorContentTypeBinding
     contentTypeId="de.blub.contenttype.ext"
     editorId="de.blub.ide.exteditor">
  </editorContentTypeBinding>

With this entry, under Window->Preferences->General->Content Types I can see the file association. It's locked, so I can't edit it. Sounds promising, but unfortunately it has no effect. Maybe the correct Editor is used, but not the correct language.

来源:https://stackoverflow.com/questions/55575534/eclipse-plugin-using-cdt-associate-file-extension-with-custom-cdt-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!