embed a custom live template in a plugin

Hallo,
in my custom language plugin I want to provide live templates. I tried it the following way: I created my template via the settings-editor. The created xml looks like this:

<?xml version="1.0" encoding="UTF-8"?> <templateSet group="user">     <template name="create property" value="$PROPERTY_TYPE$ $PROPERTY_NAME$ {&#10;    $END$&#10;}" description="creates the stub for a new property" toReformat="false" toShortenFQNames="true">         <variable name="PROPERTY_TYPE" expression="enum(&quot;BOOLEAN NOT_COMPARABLE&quot;,&quot;BOOLEAN AVAILABILITY&quot;,&quot;INTEGER MORE_IS_BETTER&quot;, &quot;INTEGER LESS_IS_BETTER&quot;,&quot;DOUBLE MORE_IS_BETTER&quot;, &quot;DOUBLE LESS_IS_BETTER&quot;,&quot;STRING NOT_COMPARABLE&quot;,&quot;STRING_ENUM AVIALABILITY&quot;, &quot;STRING_ENUM RICHNESS&quot;)" defaultValue="&quot;STRINGENUM NOT_COMPARABLE&quot;" alwaysStopAt="true" />         <variable name="PROPERTY_NAME" expression="" defaultValue="&quot;PROPERTY_NAME&quot;" alwaysStopAt="true" />         <context>             <option name="sol" value="true" />         </context>     </template> </templateSet>


I stored the generated xml in my plugin project under %project%/resources/liveTemplate/user.xml

Then I implemented a DefaultLiveTemplateProvider:

public class SOLTemplateProvider implements DefaultLiveTemplatesProvider{     @Override     public String[] getDefaultLiveTemplateFiles() {         return new String[] {"resources/liveTemplates/user"};     }     @Nullable     @Override     public String[] getHiddenLiveTemplateFiles() {         return new String[] {};     } }

I used this extension point to register the provider:
<defaultLiveTemplatesProvider implementation="com.sol.SOLTemplateProvider"/>

In my plugin project I then created the template context with this implementation:

public class SOLTemplateContextProvider extends TemplateContextType{     protected SOLTemplateContextProvider() {         super("SOLTemplates", "sol");     }     @Override     public boolean isInContext(@NotNull PsiFile file, int offset) {         return true;     } }


And I used this extension point to register my TemplateContext:

<liveTemplateContext implementation="com.sol.SOLTemplateContextProvider"/>

I hoped that's all I needed to embed the live template in my plugin. However when I started the plugin the template can not be used and is not listed in the settings editor.
Have I forgot something or is this the wrong way to accomplish this?

Thanks for any help, Sebastian  

9 comments
Comment actions Permalink

I assume "resources" is a source/resource folder root, so its output is copied to plugin output. Please try removing "/resources/" prefix in your SOLTemplateProvider.

See XmlDefaultLiveTemplatesProvider/XmlContextType as reference.

0
Comment actions Permalink

Hallo Yann,
thank you for the quick answer, it works perfect. I thought I tried all path-possibilities

0
Comment actions Permalink

Hi Yann,

Is there any way to embedd predefined project templates(.zip file) into a plugin so that when the plugin is installed those project templates would display in the "New Project Wizard"

0
Comment actions Permalink

Yes, see com.intellij.platform.ProjectTemplateEP, you can find example in JavaFX plugin

0
Comment actions Permalink

Thank you very much for the quick reply. I'll look inside the JavaFX plugin

0
Comment actions Permalink

I've copied my project template zip file as resources/projectTemplates/Java/MyProjectTemplate.zip and the added      
<projectTemplate projectType="Java" templatePath="resources/projectTemplates/Java/MyProjectTemplate.zip"></projectTemplate> line into extension section in plugin.xml. But it is shown in red and there is a message saying "Element projectTemplates is not allowed here". What did I do wrong?


0
Comment actions Permalink

What version of IntelliJ IDEA are you using?
Please post your full plugin.xml.

0
Comment actions Permalink

Nevermind. I've able to solve that problem by using Idea13 as SDK. Thanks for the help :)

0
Comment actions Permalink

Hi Yann,

Sorry to bother you again. As it turns out by placing my .zip files of projectTemplates inside resources/projectTemplates/Java  Idea will automaticall pick them up and shows in 'new project wizard'. I don't have to specify anything in plugin.xml. But the problem is there are 3 .zip files and the name of those projectTemplates are repeated 3 times (altogether 9 times)  in 'new project wizard. if I put 4 .zip files in that folder, their name will be repeated 4 times (altoger 16 times). Is this a bug in Idea? If not how can I overcome it?

0

Please sign in to leave a comment.