I am using IntelliJ 13 as an IDE for a project where communication with DB is done via Spring\'s JDBC Template. When I have code fragments in Java like the following one:
<You can do this via any combination of:
For any of these, you must have the (bundled) IntelliLang
Plugin enabled.
You can add a setting to say that a particular method's parameter is of a certain 'language' (SQL for example.)
Java Parameter
SQL
(or the desired language) in the ID field
MySQL
, Oracle
, etc.SQL
parameter. You can open a class browser/search window via the ellipsis button
Now IntelliJ IDEA will automatically apply language injection when you use that method:
You can annotate a method parameter (or variable or field) as being of a particular language type. By default, IntelliJ IDEA will use the org.intellij.lang.annotations.Language
annotation that is in the annotations.jar
included with IntelliJ IDEA. It is located at <installDirectory>/redist/annotations.jar
or in maven central, org.jetbrains:annotations
Note: There are also some unofficial uploads of the annotations jar with different group IDs including (but not limited to) "com.intellij". This is because years ago, JetBrains was not uploading them to maven central. So some community members did, but under various group IDs. For the past several years, JetBrains has been uploading them with the (official) group ID of "org.jetbrains". This is what other artifacts use in their POMs (such as the Kotlin POMs), and so should be used.
Add the annotations JAR to your class path. (In most cases, the JAR only needs to be on the classpath for development, and is not needed at runtime. Thus you can use a maven <scope>
of "provided".) Then annotate your method's Parameter as being SQL. Note that code completion will work when you type the language type:
public void checkDatabase(String foo, @Language("SQL")String sql, String bar)
I like the annotation since even for non-IntelliJ IDEA users, it lets them know that a String should be valid SQL, XML, CSS, or whatever. You can also annotate a variable, or field:
If desired, you can change the annotation to use in Setting (Ctrl+Alt+S / ⌘,) > Editor > Language Injections > Advanced
As an alternative to annotations, you can use a comment. (I can't remember when this was added. So it may not be in v13). To the best of my knowledge though, language injection via comments only works for variables and fields. It does not work for parameters. For example:
For more information, see the IntelliJ IDEA user guide