Eclipse:如何防止格式化多行连接的字符串?

huangapple go评论103阅读模式
英文:

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(
      &quot;SELECT p &quot;
          + &quot;FROM Person p &quot;
          + &quot;LEFT JOIN FETCH p.Address a &quot;
          + &quot;WHERE p.id = :personId &quot;
          + &quot;ORDER BY p.id DESC&quot;)
  public List&lt;Person&gt; findPerson(@Param(&quot;personId&quot;) Long personId);

However, Eclipse keeps changing the query to this:

@Query(
      &quot;SELECT p &quot;
          + &quot;FROM Person p &quot; + &quot;LEFT JOIN FETCH p.Address a &quot; + &quot;WHERE p.id = :personId &quot;
          + &quot;ORDER BY p.id DESC&quot;)

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 &gt; Formatter &gt; Edit &gt; 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(&quot;SELECT p &quot; /*                                        */
			+ &quot;FROM Person p &quot; /*                                        */
			+ &quot;LEFT JOIN FETCH p.Address a &quot; /*                                        */
			+ &quot;WHERE p.id = :personId &quot; /*                                        */
			+ &quot;ORDER BY p.id DESC&quot;)
	public List&lt;Person&gt; findPerson1(@Param(&quot;personId&quot;) Long personId) {
		return Collections.emptyList();
	}

	// Need to enable &quot;Off/On Tags&quot; -&gt; check &quot;Enable Off/On Tags&quot;
	// Good: Will not affect existing formatting
	// Bad: Need to format space, indentation yourself
	// @formatter:off
	@Query(&quot;SELECT p &quot; 
			+ &quot;FROM Person p &quot; 
			+ &quot;LEFT JOIN FETCH p.Address a &quot; 
			+ &quot;WHERE p.id = :personId &quot; 
			+ &quot;ORDER BY p.id DESC&quot;)
	// @formatter:on
	public List&lt;Person&gt; findPerson2(@Param(&quot;personId&quot;) Long personId) {
		return Collections.emptyList();
	}

	// Code Style &gt; Formatter &gt; Edit &gt; Line Wrapping tab &gt; Check &quot;Never Join already
	// wrapped line (suggested by Christian Baumann)&quot;
	// Good: Seems to be best out of three, preserve formatting while line will not be
	// joined
	// Bad: May change existing formatting
	@Query(&quot;SELECT p &quot;
			+ &quot;FROM Person p &quot;
			+ &quot;LEFT JOIN FETCH p.Address a &quot;
			+ &quot;WHERE p.id = :personId &quot;
			+ &quot;ORDER BY p.id DESC&quot;)
	public List&lt;Person&gt; findPerson3(@Param(&quot;personId&quot;) Long personId) {
		return Collections.emptyList();
	}
}

答案3

得分: 0

只需:

  1. 编辑一个非内置配置文件或添加一个新配置文件。
  2. 启用Off/On标签 Eclipse:如何防止格式化多行连接的字符串?
  3. 注意:冒号后面没有空格 @formatter:on/off
英文:

Just

  1. edit a non-built-in profile or add a new one.
  2. enable Off/On tags Eclipse:如何防止格式化多行连接的字符串?
  3. be aware: there is no space after the colon @formatter:on/off

huangapple
  • 本文由 发表于 2020年10月3日 20:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64184391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定