开发者

Eclipse Plugin Development, associating different editors to same file extension

开发者 https://www.devze.com 2023-02-09 20:54 出处:网络
I am developing an eclipse plugin that associates a certain Editor to a specific file extension, say \".abc\".

I am developing an eclipse plugin that associates a certain Editor to a specific file extension, say ".abc".

The problem is I want to associate .abc files to that editor only for my own projects with my own nature on it. As it is now, it will always open .abc files with that editor no matter in which project.

How can I op开发者_运维技巧en my own editor for ".abc" files only if they are in projects with my own nature?


You need to define a content-type using the org.eclipse.core.contenttype extension point. Then you need to associate your editor with the particular content type (and not the file extension).

Next, you need to associate your project nature with the content type that you just defined.

You may also need to create a second content type that should be used for your files when outside of a project with the specific nature.

Here is an example that we used in Groovy-Eclipse so that *.groovy files would be opened with a groovy editor by default in groovy projects, but by a text editor outside of groovy projects:

 <extension point="org.eclipse.core.contenttype.contentTypes">
    <content-type
       base-type="org.eclipse.jdt.core.javaSource"
       file-extensions="groovy"
       id="groovySource"
       name="Groovy Source File (for Groovy projects)"
       priority="high"/>

    <content-type
       base-type="org.eclipse.core.runtime.text"
       file-extensions="groovy"
       id="groovyText"
       name="Groovy Text File (for non-Groovy projects)"
       priority="low"/>
</extension>

<extension
     id="groovyNature"
     name="Groovy Nature"
     point="org.eclipse.core.resources.natures">
  <runtime>
     <run class="org.codehaus.jdt.groovy.model.GroovyNature"/>
  </runtime>
  <requires-nature id="org.eclipse.jdt.core.javanature"/>
  <content-type
        id="org.eclipse.jdt.groovy.core.groovySource">
  </content-type>

Here, we define groovySource for groovy projects and groovyText for non-groovy projects. Notice also, that the priority of the content-types are different.

And then, elsewhere, we associate the GroovyEditor with the groovySource content-type.

0

精彩评论

暂无评论...
验证码 换一张
取 消