Spring Java格式+ IntelliJ +链方法调用包装

时间:2019-07-17 20:59:51

标签: java intellij-idea checkstyle

我正在使用IntelliJ的Spring Java Format插件来标准化我的代码样式。我还可以在每次保存代码时使用“保存操作插件”来格式化代码。我正在尝试查找哪个规则使链方法调用得以加入。

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

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

保存后,其格式如下:

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

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

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

编辑1:

我刚刚意识到编码是由mvn spring-javaformat:apply连接在一起的。因此,该问题与IntelliJ Spring Java格式插件无关。

我仍在尝试确定Checkstyle规则正在执行的操作。

1 个答案:

答案 0 :(得分:0)

您看到的是 Spring Java Format 的默认代码样式。虽然它“通常不可配置”,但有一个逃生舱口可以完全禁用格式化。此处的文档中提到了此功能: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
    }