簡體   English   中英

Spring Java Format + IntelliJ + 鏈式方法調用包裝

[英]Spring Java Format + IntelliJ + chain method call wrapping

我正在使用 IntelliJ 的 Spring Java Format 插件來標准化我的代碼風格。 我也使用 Save Action Plugin 來格式化代碼。 我正在嘗試找到哪個規則使鏈方法調用被加入。

例如,如果我像這樣格式化我的代碼:

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

保存后,格式如下:

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

假設第三個方法調用轉置右邊距限制。

我想保留沒有自動加入的包裝版本。 有沒有人遇到同樣的問題?

編輯1:

我剛剛意識到編碼是由mvn spring-javaformat:apply連接在一起的。 所以問題與 IntelliJ Spring Java Format Plugin 無關。

我仍在嘗試確定 Checkstyle 規則在做什么。

您看到的是 Spring Java Format 的默認代碼樣式。 雖然它“通常不可配置”,但有一個轉義艙可以完全禁用格式化。 此處的文檔中提到了此功能: 禁用代碼塊的格式設置

對齊鏈式方法為此調用了一個理想的用例:

// @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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM