【问题标题】:Spring Java Format + IntelliJ + chain method call wrappingSpring Java Format + IntelliJ + 链式方法调用包装
【发布时间】:2019-11-26 18:26:48
【问题描述】:

我正在使用 IntelliJ 的 Spring Java Format 插件来标准化我的代码样式。我也在使用保存操作插件来格式化代码,只要我保存它。我正在尝试找出哪个规则使链方法调用被加入。

例如,如果我像这样格式化我的代码:

method()
    .call1()
    .call2()
    .call3()

保存时格式如下:

method().call1().call2()
    .call3()
------------------------^ right margin limit

假设第三个方法调用转置右边距限制。

我想保留不自动加入的包装版本。 有人遇到同样的问题吗?

编辑1:

我刚刚意识到编码是由mvn spring-javaformat:apply 连接在一起的。所以这个问题与 IntelliJ Spring Java Format Plugin 无关。

我仍在尝试确定 Checkstyle 规则在做什么。

【问题讨论】:

  • 禁用 Spring JavaFormat 插件会发生这种情况吗?链式方法调用的 IntelliJ IDEA 代码样式设置是什么?
  • 当 Spring Java Format 插件由 Save Action Plugin 触发时,它会保留我想要的代码,但是,当我 ctrl shif f(我使用 Eclipse 键盘映射)格式化我的代码或运行 Maven目标是格式化,它将所有内容连接在一起。
  • 是的,我已经在我的 IDEA 上做了同样的配置。
  • 您可能启用了多个格式化程序,很难说是哪一个触发了这种行为。尝试不使用第三方插件的干净 IDE 安装。

标签: java intellij-idea checkstyle


【解决方案1】:

您看到的是 Spring Java 格式的默认代码样式。尽管它“通常不可配置”,但有一个逃生舱可以完全禁用格式化。此处的文档中提到了此功能:Disabling formatting for blocks of code

对齐链式方法调用一个理想的用例:

// @formatter:off
method()
    .call1()
    .call2()
    .call3()
// @formatter:on

许多官方 Spring 项目也这样做。例如:

来源:spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-jersey/src/main/java/smoketest/secure/jersey/SecurityConfiguration.java

    @Bean
    SecurityFilterChain configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.authorizeRequests()
                .requestMatchers(EndpointRequest.to("health", "info")).permitAll()
                .requestMatchers(EndpointRequest.toAnyEndpoint().excluding(MappingsEndpoint.class)).hasRole("ACTUATOR")
                .antMatchers("/**").hasRole("USER")
                .and()
            .httpBasic();
        return http.build();
        // @formatter:on
    }

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 2015-10-04
    • 1970-01-01
    • 2017-02-06
    • 2011-09-10
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多