IntelliJ IDEA—Java代码中SQL的语法突出显示

时间:2021-08-24 20:23:21

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:

我正在使用intellij13作为一个IDE,用于一个通过Spring的JDBC模板与DB进行通信的项目。当我在Java中有如下代码片段时:

getJdbcTemplate().queryForObject("SELECT CONVERT(VARCHAR(255), NEWID())", String.class);

where getJdbcTemplate() returns initialized JdbcTemplate object, the IDE has proper syntax highlighting for the SQL statement (you can see it on the snippet bellow):

getJdbcTemplate()返回初始化的JdbcTemplate对象,IDE对SQL语句有适当的语法突出显示(可以在snippet bellow上看到):


.code {font-family: Monospace}
.db-stmt {background: #EDFCED}
.db-keyword {color: #000080; font-weight: bold;}
.db-column {color: #7D0C8D; font-weight: bold;}
.db-number {color: #0000FF;}
.java-class {color: #39008E; font-weight: bold;}
<span class="code">getJdbcTemplate().queryForObject("<span class="db-stmt"><span class="db-keyword">SELECT CONVERT</span>(<span class="db-keyword">VARCHAR</span>(<span class="db-number">255</span>), NEWID())</span>", String.<span class="java-class">class</span>);</span>


However, when I use another method, created by me:

然而,当我使用另一种由我创造的方法时:

protected String queryForString(String sql, Object... args) {
    try {
        return getJdbcTemplate().queryForObject(sql, String.class, args);
    }
    catch (RuntimeException e) {
        return null;
    }
}

for the same query:

同样的查询:

queryForString("SELECT CONVERT(VARCHAR(255), NEWID())");

there is no syntax highlighting of the SQL statement. Do you know how to enable syntax highlighting of SQL staments for Java stings that are not arguments of methonds of JdbcTemplate or java.sql interfaces?

没有SQL语句的语法高亮显示。您知道如何为Java stings启用SQL staments的语法高亮显示吗?这些stings不是JdbcTemplate或Java的methonds的参数。sql接口?

1 个解决方案

#1


20  

You can do this via 1) a setting 2) an annotation, and/or 3) a comment. For any of these, you must have the (bundled) IntelliLang Plugin enabled.

您可以通过1)设置2)注释和/或3)注释来实现这一点。对于其中任何一个,都必须启用(绑定)IntelliLang插件。

Setting

设置

You can add a setting to say that a particular method's parameter is of a certain 'language' (SQL for example.)

您可以添加一个设置来说明特定方法的参数是某种“语言”(例如SQL)。

  1. Open Setting (Ctrl+Alt+S / ,) > Editor > Language Injections
  2. 打开设置(Ctrl + Alt + S /⌘)> >编辑语言注射
  3. On the right, click the add button and select Java Parameter
  4. 在右边,单击add按钮并选择Java参数
  5. In the Language Injection Settings dialog, select SQL (or the desired language) in the ID field
    • You can also specify a specific SQL dialect such as MySQL, Oracle, etc.
    • 您还可以指定特定的SQL方言,如MySQL、Oracle等。
  6. 在语言注入设置对话框中,在ID字段中选择SQL(或所需的语言),还可以指定特定的SQL方言,如MySQL、Oracle等。
  7. In the "Class Methods" field, enter the method you want to identify as taking an SQL parameter. You can open a class browser/search window via the ellipsis button IntelliJ IDEA—Java代码中SQL的语法突出显示
    • You can select classes from your code, or from libraries
    • 您可以从代码或库中选择类
  8. 在“类方法”字段中,输入要识别为SQL参数的方法。可以通过省略号按钮打开类浏览器/搜索窗口,可以从代码或库中选择类
  9. If your method takes multiple parameters, you can select the parameter in the dialog: IntelliJ IDEA—Java代码中SQL的语法突出显示
  10. 如果您的方法采用多个参数,您可以在对话框中选择参数:

Now IntelliJ IDEA will automatically apply language injection when you use that method: IntelliJ IDEA—Java代码中SQL的语法突出显示

现在IntelliJ IDEA会自动应用语言注入当你使用这个方法的时候:

Annotations

注释

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: g:"com.intellij" AND a:"annotations" (Note: The latest jar in maven central is from version 12. It has everything you need. A non related annotation was added in v14. I'll upload the jars from v13 and v14 to maven central today (as I've been the one to do such in the past and have been meaning to update them). But it takes a few days to process through the system.)

可以将方法参数(或变量或字段)注释为特定语言类型。默认情况下,IntelliJ IDEA会使用org.intellij.lang.annotations.Language annotation,位于annotation中。jar包括IntelliJ IDEA。它位于 /redist/annotation。jar或maven中环:g:“com.intellij”和“注释”(注:maven中心的最新jar来自版本12。它有你需要的一切。在v14中添加了一个不相关的注释。我今天将把v13和v14中的jar上传到maven central(因为我以前就是这样做的,而且一直打算更新它们)。但这需要几天的时间才能通过这个系统。

Add the annotations JAR to your class path. Then annotate your method's Parameter as being SQL. Note that code completion will work when you type the language type:

将注释JAR添加到类路径。然后将方法的参数注释为SQL。请注意,当您输入语言类型时,代码完成将工作:

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: IntelliJ IDEA—Java代码中SQL的语法突出显示

我喜欢这个注释,因为即使是非intellij IDEA的用户,它也让他们知道一个字符串应该是有效的SQL、XML、CSS或其他东西。您还可以对变量或字段进行注释:

If desired, you can change the annotation to use in Setting (Ctrl+Alt+S / ,) > Editor > Language Injections > Advanced

如果需要,您可以更改注释用于设置(Ctrl + Alt + S /⌘,)注射>编辑>语言>先进

Comment

评论

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: IntelliJ IDEA—Java代码中SQL的语法突出显示

作为注释的替代方法,您可以使用注释。我不记得是什么时候加的。所以它可能不在v13中。就我所知,通过注释进行语言注入仅适用于变量和字段。它对参数不起作用。例如:

P.S. See the help topics Help > Manuals > General Guidelines > Using Language Injections (available online here) and Help > Manuals > Language and Frameworks - Specific Guidelines > IntelliLang (available online here) for more information.

请参见帮助主题帮助>手册>使用语言注入(可在此在线提供)和帮助>手册>语言和框架-具体的指南> IntelliLang(可在此在线)获取更多信息。

#1


20  

You can do this via 1) a setting 2) an annotation, and/or 3) a comment. For any of these, you must have the (bundled) IntelliLang Plugin enabled.

您可以通过1)设置2)注释和/或3)注释来实现这一点。对于其中任何一个,都必须启用(绑定)IntelliLang插件。

Setting

设置

You can add a setting to say that a particular method's parameter is of a certain 'language' (SQL for example.)

您可以添加一个设置来说明特定方法的参数是某种“语言”(例如SQL)。

  1. Open Setting (Ctrl+Alt+S / ,) > Editor > Language Injections
  2. 打开设置(Ctrl + Alt + S /⌘)> >编辑语言注射
  3. On the right, click the add button and select Java Parameter
  4. 在右边,单击add按钮并选择Java参数
  5. In the Language Injection Settings dialog, select SQL (or the desired language) in the ID field
    • You can also specify a specific SQL dialect such as MySQL, Oracle, etc.
    • 您还可以指定特定的SQL方言,如MySQL、Oracle等。
  6. 在语言注入设置对话框中,在ID字段中选择SQL(或所需的语言),还可以指定特定的SQL方言,如MySQL、Oracle等。
  7. In the "Class Methods" field, enter the method you want to identify as taking an SQL parameter. You can open a class browser/search window via the ellipsis button IntelliJ IDEA—Java代码中SQL的语法突出显示
    • You can select classes from your code, or from libraries
    • 您可以从代码或库中选择类
  8. 在“类方法”字段中,输入要识别为SQL参数的方法。可以通过省略号按钮打开类浏览器/搜索窗口,可以从代码或库中选择类
  9. If your method takes multiple parameters, you can select the parameter in the dialog: IntelliJ IDEA—Java代码中SQL的语法突出显示
  10. 如果您的方法采用多个参数,您可以在对话框中选择参数:

Now IntelliJ IDEA will automatically apply language injection when you use that method: IntelliJ IDEA—Java代码中SQL的语法突出显示

现在IntelliJ IDEA会自动应用语言注入当你使用这个方法的时候:

Annotations

注释

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: g:"com.intellij" AND a:"annotations" (Note: The latest jar in maven central is from version 12. It has everything you need. A non related annotation was added in v14. I'll upload the jars from v13 and v14 to maven central today (as I've been the one to do such in the past and have been meaning to update them). But it takes a few days to process through the system.)

可以将方法参数(或变量或字段)注释为特定语言类型。默认情况下,IntelliJ IDEA会使用org.intellij.lang.annotations.Language annotation,位于annotation中。jar包括IntelliJ IDEA。它位于 /redist/annotation。jar或maven中环:g:“com.intellij”和“注释”(注:maven中心的最新jar来自版本12。它有你需要的一切。在v14中添加了一个不相关的注释。我今天将把v13和v14中的jar上传到maven central(因为我以前就是这样做的,而且一直打算更新它们)。但这需要几天的时间才能通过这个系统。

Add the annotations JAR to your class path. Then annotate your method's Parameter as being SQL. Note that code completion will work when you type the language type:

将注释JAR添加到类路径。然后将方法的参数注释为SQL。请注意,当您输入语言类型时,代码完成将工作:

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: IntelliJ IDEA—Java代码中SQL的语法突出显示

我喜欢这个注释,因为即使是非intellij IDEA的用户,它也让他们知道一个字符串应该是有效的SQL、XML、CSS或其他东西。您还可以对变量或字段进行注释:

If desired, you can change the annotation to use in Setting (Ctrl+Alt+S / ,) > Editor > Language Injections > Advanced

如果需要,您可以更改注释用于设置(Ctrl + Alt + S /⌘,)注射>编辑>语言>先进

Comment

评论

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: IntelliJ IDEA—Java代码中SQL的语法突出显示

作为注释的替代方法,您可以使用注释。我不记得是什么时候加的。所以它可能不在v13中。就我所知,通过注释进行语言注入仅适用于变量和字段。它对参数不起作用。例如:

P.S. See the help topics Help > Manuals > General Guidelines > Using Language Injections (available online here) and Help > Manuals > Language and Frameworks - Specific Guidelines > IntelliLang (available online here) for more information.

请参见帮助主题帮助>手册>使用语言注入(可在此在线提供)和帮助>手册>语言和框架-具体的指南> IntelliLang(可在此在线)获取更多信息。