Embedding custom languages in HTML script tags?

Hey all,

One of the most asked for features in the Handlebars/Mustache plugin is the ability to embed templates into HTML files, usually in script blocks.  This has turned out to be tough since the language we want to embed contains HTML, which confuses the standard language injection system.  Any insight/help on how to implement this would be much, much appreciated.

Extra context:

See the history of this issue on here (note in particular the description of why language injection cannot be used for this).

I noticed that an HtmlScriptContentProvider extension point was recently added, which certainly sounds like what I'm after... implementing it in the obvious way doesn't quite work though (none of the plugins features are enabled in the new node, and pressing "Enter" in the editor anywhere inside that node results in IDEA throwing an error).  Perhaps this is still being built out?

Hopefully that's a helpful amount of context, and not too wordy/confusing.  Thanks in advance for any help!  It would be absolutely fantastic to have this feature.

4 comments

Hi!

Yep. It's very a tricky question. :) Here is a patch with the feature for your language. I assume there are some problems with highlightning, formatting, resolving and so on. But with the patch mustache is parsed inside a script tag with proper type.

NB: To fix highlightning you can create a ProjectComponent and use com.intellij.ide.highlighter.HtmlFileHighlighter#registerEmbeddedTokenAttributes and com.intellij.ide.highlighter.XmlFileHighlighter#registerEmbeddedTokenAttributes to register your attrributes.



Attachment(s):
HbScriptContent.patch.zip
0

Wow, Fedor, this is awesome.  And tricky indeed :)

There's a pretty big piece missing though: the code is not being parsed as a template, so the Template Data Language here is effectively plain text.  This is a pretty huge loss in functionality since Handlebars and Mustache's only purpose is to template another language.

Is there some other trickiness we can employ to get a template data language in there?  (Defaulting to HTML would be fine; that's the common use case and we can figure out how to make it configurable later)

Apologies for being difficult!  I really appreciate you putting together this patch (and learned a lot from it!).  I will also keep trying to figure out how to enable this, but it seemed worth checking if you (or anyone else on this forum) had some ideas.

0

Ok. I'll take a look at the problem in a couple of days(have some other priority work). ;)

0

Totally understand competing priorities; thanks in advance for finding more time for this!

0

Please sign in to leave a comment.