Can I create a language injection for this pattern...
# intellij
d
Can I create a language injection for this pattern?
Copy code
Set mode to SQL
    If a val has the word "where" in it 
        And is in a kotlin class with "Repository" in its name
I see something that looks similar for javascript, which is this:
Copy code
+ jsLiteral().withStringValueIgnoringInterpolations(stringMatchesBrics(" *(SELECT|DELETE) .*FROM .*"))
but I don't know how to translate that to looking at a variable in Kotlin. The result is that I'd like to be able to get automated SQL language support for variables that look like SQL in a repository class. I know I can add
//language=sql
val someStatement="select * from foo where x=?"
...but with a dozen statements in a repository, all those language comments are pretty cumbersome. Thanks for any advice!
r
Might find some help here: https://www.jetbrains.com/help/idea/using-language-injections.html#configure-injection-rules Notably that you can make them per project and check them into source control.
d
I found something that'll work for my use case and turned out to be pretty simple. In my case, we have a JDBC wrapping library that takes an argument of the SQL you want to run, and the parameter's name is
sql
. So I created this SQL language injection rule:
Copy code
+ kotlinParameter().withName("sql")
I then set it to be project-scoped so that I could save its
.idea/IntelliLang.xml
file in source control with the project so other people didn't need to manually add it.