Tech
06/08/2024, 10:37 PMtypealias Test = () -> @receiver:Language("SQL") String
fun example(l: Test) {
println(l())
}
The syntax of
example {
"SELECT"
}
doesn't have the SQL highlighting.Adam S
06/09/2024, 7:56 AM/* language=SQL */
before the String. (And you can collapse the comment to make it more subtle)Adam S
06/09/2024, 7:57 AM@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!Adam S
06/09/2024, 8:04 AM@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'"dave08
06/09/2024, 9:39 AMdave08
06/09/2024, 10:03 AMimport 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 thinkTech
06/09/2024, 1:25 PMfun 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,
example {
sql("""
SELECT * FROM test;
""".trim())
}
Thank you for the help though, this is a pretty obscure use-case 😅Tech
06/09/2024, 1:26 PMdave08
06/09/2024, 1:27 PMdave08
06/09/2024, 1:27 PMTech
06/09/2024, 1:27 PMTech
06/09/2024, 1:28 PMdave08
06/09/2024, 1:28 PMTech
06/09/2024, 1:28 PM