Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IntelliJ Language Injection within custom tag in a JSP

My company uses custom tags in our JSPs to wrap JavaScript. I cannot get IntelliJ to treat the content of these tags as JavaScript. Here is a simple example of what our tag looks like.

<ui:script>
  //Include javascript here...
  alert('Any code in here is treated as JavaScript');
</ui:script>

Any suggestions? I've tried using Language Injections, but I cannot find the right settings.

I just noticed that the problem is more linked to using JSP-specific language within the <ui:script> tag. A nastier example (notice the ${selectedReportID} tag that's breaking everything):

<ui:script>
    new Kamino.DependencyLoader({
        source: [
            '/static/js/modules/folders/Report.js'
        ],

        onSuccess: function () {
            new Kamino.Report({
                id: '${selectedReportID}',
                element: 'content-reporting-report-list'
            });
        }
    }).load();
</ui:script>
like image 844
Chris Esplin Avatar asked Jul 18 '12 15:07

Chris Esplin


People also ask

Which key is used to add language injection in IntelliJ?

You can configure language injection rules on the Editor | Language Injections page of the IDE settings Ctrl+Alt+S .

How do I change the language on IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Editor | Natural Languages. in the list of languages and select your language.

What languages does IntelliJ support?

Though designed primarily for Java development, IntelliJ IDEA understands many other programming languages, including Groovy, Kotlin, Scala, JavaScript, TypeScript, and SQL, and it provides smart coding assistance for each of them.


1 Answers

This is what worked for me in IntelliJ IDEA 12.

Here is the JSP snippet with custom tag (aui:script) that renders some javascript code:

before

As you can see, it is plain text, i.e. alt+enter gives no suggestions etc.

Here is the XML Tag Injection I added in Language Injection settings:

setting

Be sure to choose correct namespace. After this, the same code looks like this (expect some minor delay after opening a file):

after

It is colored differently and, as you can see, the code between aui:script tags is aware of javascript context, suggestions are available etc.

Please try if this works for you, I haven't use this as much as I would like :)

like image 76
igr Avatar answered Sep 22 '22 21:09

igr