Code formatting for template languages
Hi,
Anybody knows how to do code formatting for template languages. Any example?
I found the class TemplateLanguageFormattingModelBuilder. But no idea of how to use it.
I tried to register the extended class under the extension lang.formatter. But It didn't work.
Thanks..!!
Please sign in to leave a comment.
Hey Amila,
For quick-win formatting, you can use com.intellij.psi.templateLanguages.SimpleTemplateLanguageFormattingModelBuilder. This will use the formatter of the language you are templating, which is most of the battle. See the Handlebars plugin for an example (https://github.com/dmarcotte/idea-handlebars/blob/master/META-INF/plugin.xml, https://github.com/dmarcotte/idea-handlebars/blob/master/src/com/dmarcotte/handlebars/format/HbFormattingModelBuilder.java). I believe that as long as your FileViewProvider implements TemplateLanguageFileViewProvider it should just work (ex: https://github.com/dmarcotte/idea-handlebars/blob/master/src/com/dmarcotte/handlebars/file/HbFileViewProvider.java).
As for TemplateLanguageFormattingModelBuilder, I am currently trying to improve the Handlebars formatting to also format the Handlebars syntax (not just the file it is templating). TemplateLanguageFormattingModelBuilder seems like the way to do this, but it's proving a bit tough to reverse engineer, and I'm also not seeing any examples of its usage out there. Anyone have any experience making a full template language formatter?
Hi,
Thank you very much for your help Marcotte. It was really helpfull.
My language structure was like this.
html
<%
javascript
%>
html
I got the code formatting for html part using SimpleTemplateLanguageFormattingModelBuilder. But could not get the code formatting for
javascript content. Javascript is parsed using IlazyParseableElementType. Any example will be much helpfull.
Thanks ..!!
Glad to help!
And yup, if I get any traction on a proper template formatter, I'll definitely update this thread.
Hi Marcotte,
Your kind concern is really appreciated.
Thanks ..!!
(edited to reflect that this is no longer a work in progress)
Update: I've got a working TemplateLanguageFormattingModelBuilder implemented for the IDEA-Handlebars plugin. You can check out the work in progress here finished formatter: https://github.com/dmarcotte/idea-handlebars/pull/27/files. I'll update again when it's polished, but I thought I'd post sooner than later (done!)
Some notes:
Hope that helps.
Almost forgot to note the most important tip:
The diff at https://github.com/dmarcotte/idea-handlebars/pull/27/files now represents a fully functioning example of a template formatter. Hopefully it's a help for anyone trying to implement their own.
I know this is obsolete, but still someone might find it useful. You can check out my plugin hosted on github, where exactly this problem is solved. There are lots of formatter tests in /testData, which could make you quickly decide whether go with it or not. Me and my colleagues have been using this for some time now, and it really does work like charm. This template language basically consists of html, javascript delimited by <% %> markers, custom xml tags and some directives. So this plugin can be used as a guide to create formatter for html + custom stuff, html + javascript or html + javascript + custom stuff. All the code you're interested in is in /src/ool/idea/plugin/editor/format. The code is quite complex, so I'll try to describe just the key posts, which may not be clear from reading it:
I've heen trying some experiments with TemplateLanguageFormattingModelBuilder, as Daniel suggests, but I've ran into some problems and limitations. I could only create blocks, which cover ast node, and I could never create a synthetic block. Just feel free to use the code, for html + js you'd just get rid of custom xml / directives related code and it should work well.