Christian Dräger
03/15/2021, 3:07 PM@Language
annotation intellij is able to syntax hightlight / autocomplete strings of some languages.
For instance one could do something like
@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:
fun evalJs(source: () -> String): Value = JavascriptEngine.context.eval("js", source())
that could than be used like:
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:
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)
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
?