简体   繁体   中英

Spring Java Format + IntelliJ + chain method call wrapping

I'm using Spring Java Format plugin for IntelliJ to standardize my code style. I'm also using Save Action Plugin to format the code anytime I save it. I'm trying do find which rule makes chain method calls to be joined.

For instance, if I format my code like this:

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

When it is saved, it is formatted like this:

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

Assuming the third method call transpose the right margin limit.

I would like to keep the wrapped version without automatic join. Does anybody faced the same problem?

Edit1:

I've just realized that the coded is joined together by mvn spring-javaformat:apply . So the problem is not related to IntelliJ Spring Java Format Plugin.

I'm still trying to identify what Checkstyle rule is doing that.

What you're seeing is the default code style for Spring Java Format. And although it is "not generally configurable", there is an escape hatch to disable formatting entirely. This feature is mentioned in the docs here: Disabling formatting for blocks of code .

Aligning chained method calls an ideal use case for this:

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

Many of the official Spring projects do this as well. For example:

Source: 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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM