Jul 11, 2008

FreeMarker integration with Intellij IDEA

Some of you who uses FreeMarker as your favorite template engine and Intellij IDEA as your favorite IDE will understand my pain. At the moment there is no support of FreeMarker in IDEA, so developers try to find other ways while waiting for IDEA 8.0 roadmap will be finished.

One of the best approaches is to use live templates - very powerful feature of the IDEA. Originally I have found basic templates with full explanation here. Then I refactor all templates to work with square brackets and add some new for Spring integration:

<template name="fm.spring.bind" value="[@spring.bind '$MODEL_KEY$' /]"
description="Sping bind function for the freemarker template"
toReformat="false" toShortenFQNames="true">
<variable name="MODEL_KEY" expression=""
defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="false" />
<option name="JAVA_COMMENT" value="false" />
<option name="JAVA_STRING" value="false" />
<option name="XML" value="false" />
<option name="HTML" value="true" />
<option name="JSP" value="false" />
<option name="COMPLETION" value="false" />
<option name="OTHER" value="false" />
</context>
</template>

<template name="fm.spring.expr" value="${spring.status.expression}"
description="Spring binded name for the freemarker template"
toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="false" />
<option name="JAVA_COMMENT" value="false" />
<option name="JAVA_STRING" value="false" />
<option name="XML" value="false" />
<option name="HTML" value="true" />
<option name="JSP" value="false" />
<option name="COMPLETION" value="false" />
<option name="OTHER" value="false" />
</context>
</template>

<template name="fm.spring.url"
value="[@spring.url relativeUrl=$RELATIVE_URL$ /]"
shortcut="SPACE"
description="Insert URL to another page of the application"
toReformat="false" toShortenFQNames="true">
<variable name="RELATIVE_URL" expression=""
defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="false" />
<option name="JAVA_COMMENT" value="false" />
<option name="JAVA_STRING" value="false" />
<option name="XML" value="false" />
<option name="HTML" value="true" />
<option name="JSP" value="false" />
<option name="COMPLETION" value="false" />
<option name="OTHER" value="true" />
</context>
</template>

<template name="fm.spring.val" value="${spring.status.value}"
description="Spring binded value for the freemarker template"
toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="false" />
<option name="JAVA_COMMENT" value="false" />
<option name="JAVA_STRING" value="false" />
<option name="XML" value="false" />
<option name="HTML" value="true" />
<option name="JSP" value="false" />
<option name="COMPLETION" value="false" />
<option name="OTHER" value="false" />
</context>
</template>

These templates help me every day and save time to manual typing. May be it will be starting point of using live templates for some of us. Develop with pleasure!

No comments: