Thanks to the `@Language` annotation intellij is a...
# intellij
c
Thanks to the
@Language
annotation intellij is able to syntax hightlight / autocomplete strings of some languages. For instance one could do something like
Copy code
@Language("HTML")
val someHtmlString = "<html><head><title>hello<!title><!head><!html>"
and would get autocompletion and syntaxhighlighting which is a really nice feature in my opinion Currently i am wondering if i could make a “feature” for a method i have written to evaluate Javascript code via graalvm. for better understanding i have a lambda function like this:
Copy code
fun evalJs(source: () -> String): Value = JavascriptEngine.context.eval("js", source())
that could than be used like:
Copy code
data class MyExampleService(
    val name: String
) {
    fun toGreeting(name: String) = copy(name = "Hi $name!")
}

@Test
    internal fun `can mutate kotlin data class in JS and do further operations on the object`() {
        val result = with(member(MyExampleService(""))) {
            evalJs {
                """
                    const converted = $this.toGreeting('Christian');
                    converted.getName().toUpperCase() // call data class getter 'getName' and use JS 'toUpperCase' function
                """
            }.asString()
        }
        expectThat(result).isEqualTo("HI CHRISTIAN!")

    }
since i pass a string that represents javascript code to my
evalJs
i think it would be great if it could have the syntax highlighting as well. what i have tried naively was just adding the
@Language
annotation to my
evalJs
parameter like so:
Copy code
fun evalJs(@Language("JS") source: () -> String): Value = JavascriptEngine.context.eval("js", source())
since this solution was not working i changed my parameter from beeing a function to a string which did the trick successfully (means whenever i pass a string parameter to the
evalJs
function it will be treated like JS by intellij)
Copy code
fun evalJs(@Language("JS") source: String): Value = JavascriptEngine.context.eval("js", source)
now i am wondering if this is somehow possible in the lambda version of
evalJs
?