英文:
Eclipse: how to prevent formatting multi-line concatenated strings?
问题
Sure, here's the translated content:
我在使用 Eclipse 时遇到了一个问题,我在编写 JPQL 查询,但是 Eclipse 一直将连接内容格式化为同一行。例如,这是我想要的代码:
@Query(
"SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson(@Param("personId") Long personId);
然而,Eclipse 一直将查询格式化为以下样式:
@Query(
"SELECT p "
+ "FROM Person p " + "LEFT JOIN FETCH p.Address a " + "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
有人知道如何阻止这种情况吗?我正在使用 Java 11,因此无法使用文本块功能。
英文:
I'm having this issue with Eclipse where I'm writing a JPQL query and eclipse keeps formatting the concatenations onto the same line. For example, this is what I want:
@Query(
"SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson(@Param("personId") Long personId);
However, Eclipse keeps changing the query to this:
@Query(
"SELECT p "
+ "FROM Person p " + "LEFT JOIN FETCH p.Address a " + "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
Does anyone know how to prevent this? I'm using Java 11 so text blocks aren't an option here
答案1
得分: 2
这方面有一个未解决的问题:https://bugs.eclipse.org/bugs/show_bug.cgi?id=425473
其中一个评论是:
> 您可以禁用连接已格式化的行(代码样式 > 格式化程序 > 编辑 > 行包装
选项卡)
英文:
There is an open bug about this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=425473
One comment on this is
> You can disable joining already formatted lines (Code Style > Formatter > Edit > Line Wrapping tab
)
答案2
得分: 1
public class QueryFormat {
// 在行尾添加填充
// 优点:无需编辑格式配置文件
// 缺点:降低可读性,需要手动操作。
@Query("SELECT p " /* */
+ "FROM Person p " /* */
+ "LEFT JOIN FETCH p.Address a " /* */
+ "WHERE p.id = :personId " /* */
+ "ORDER BY p.id DESC")
public List<Person> findPerson1(@Param("personId") Long personId) {
return Collections.emptyList();
}
// 需要启用"Off/On Tags" -> 勾选"Enable Off/On Tags"
// 优点:不影响现有格式
// 缺点:需要自行格式化空格、缩进
// @formatter:off
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
// @formatter:on
public List<Person> findPerson2(@Param("personId") Long personId) {
return Collections.emptyList();
}
// 代码风格 > 格式化程序 > 编辑 > 换行选项卡 > 勾选"Never Join already
// wrapped line (suggested by Christian Baumann)"
// 优点:似乎是三者中最好的选择,保留格式化,同时不会合并行
// 缺点:可能会更改现有格式
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson3(@Param("personId") Long personId) {
return Collections.emptyList();
}
}
英文:
You can try one of the following.
public class QueryFormat {
// Add padding in line end
// Good: No need to edit formatting profile
// Bad: Decrease readability and need to do manual work.
@Query("SELECT p " /* */
+ "FROM Person p " /* */
+ "LEFT JOIN FETCH p.Address a " /* */
+ "WHERE p.id = :personId " /* */
+ "ORDER BY p.id DESC")
public List<Person> findPerson1(@Param("personId") Long personId) {
return Collections.emptyList();
}
// Need to enable "Off/On Tags" -> check "Enable Off/On Tags"
// Good: Will not affect existing formatting
// Bad: Need to format space, indentation yourself
// @formatter:off
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
// @formatter:on
public List<Person> findPerson2(@Param("personId") Long personId) {
return Collections.emptyList();
}
// Code Style > Formatter > Edit > Line Wrapping tab > Check "Never Join already
// wrapped line (suggested by Christian Baumann)"
// Good: Seems to be best out of three, preserve formatting while line will not be
// joined
// Bad: May change existing formatting
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson3(@Param("personId") Long personId) {
return Collections.emptyList();
}
}
答案3
得分: 0
只需:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论