Heyo! I was wondering if it was possible on an ano...
# getting-started
t
Heyo! I was wondering if it was possible on an anonymous function to make the return type be annotated with the @Language tag, I'm trying to do something like this with no luck so far.
Copy code
typealias Test = () -> @receiver:Language("SQL") String

fun example(l: Test) {
    println(l())
}
The syntax of
Copy code
example {
  "SELECT"
}
doesn't have the SQL highlighting.
a
Short answer: no. The best you can do is to add
/* language=SQL */
before the String. (And you can collapse the comment to make it more subtle)
👌 1
The
@Language
annotation is written in Java, and annotation-detector in IntelliJ util doesn't recognise the annotation when it's added to Kotlin-specific concepts. I think this is the issue relevant to your question https://youtrack.jetbrains.com/issue/KTIJ-5644/Support-language-injection-on-operator-functions However... KMP support is in progress https://github.com/JetBrains/java-annotations/pull/103, so, maybe one day!
tbh I don't think that
@receiver:Language("SQL")
would be right even if IntelliJ did fully support Kotlin 🤔 There's no receiver in
() -> String
. What you'd want is
typealias Tmp = () -> @Language("SQL") String
- but, even if IntelliJ did recognise it, it won't compile because there's an error: "This annotation is not applicable to target 'type usage'"
d
Maybe wrap it with a value class with it's string property annotated, would that work? That wouldn't waste an allocation either...
Copy code
import org.intellij.lang.annotations.Language

@JvmInline
value class Sql(@Language("SQL") val value: String)

fun example(l: () -> Sql) {
    val sql = l()
    println(sql.value)
}

example { 
    Sql("SELECT * FROM foo")
}
You can even override unaryPlus in a dsl scope in example to write
+"SELECT..."
I think
t
Interesting suggestions, @dave08 I was actually experimenting with having a sql function, in all reality what you need to do is just this.
Copy code
fun sql(@Language("SQL") sql: String) = sql
And you'll get the syntax highlighting. @Adam S Interesting, I didn't know
/* language=SQL */
was a thing but unfortunately I don't think that'd be very good DevEx, what I'm probably going to do is just use a global function like the one I just stated to do something like this,
Copy code
example {
  sql("""
    SELECT * FROM test;
  """.trim())
}
Thank you for the help though, this is a pretty obscure use-case 😅
Overriding the unary does sound kind of interesting though, may play with that but I don't think it'll work
d
It should work like the function, but I wouldn't do that at top level...
Just in a DSL
t
Ah I get what you mean
I'll play around with it later
d
Good luck!
t
🫡