如何配置IntelliJ IDEA Java代码格式化规则使其不自动对齐链式方法调用
你期望保留的链式调用格式如下:
public void createEntity(String jsonString) { response = getBaseRequestWithCurrentCookie() .given(). body(jsonString). contentType(ContentType.JSON) .when(). post() .then(). log().ifError(). extract().response(); }
IDEA 默认格式化后会变成同层级对齐的样式:
public void createEntity(String jsonString) { response = getBaseRequestWithCurrentCookie() .given(). body(jsonString). contentType(ContentType.JSON) .when(). post() .then(). log().ifError(). extract().response(); }
配置步骤
- 打开IDEA设置界面:
- Windows/Linux 系统:按快捷键
Ctrl + Alt + S,或点击菜单栏File > Settings - macOS 系统:按快捷键
⌘ + ,,或点击菜单栏IntelliJ IDEA > Preferences
- Windows/Linux 系统:按快捷键
- 在左侧菜单依次进入
Editor > Code Style > Java,切换到*Wrapping and Braces(换行和大括号)*标签页 - 找到*Chained method calls(链式方法调用)*配置项:
- 取消勾选右侧的
Align when multiline(多行时对齐)选项 - 该配置项的缩进策略选择
Use normal indent(使用普通缩进),也可以根据你需要的缩进层级自定义缩进值
- 取消勾选右侧的
- 在当前页面找到*Keep when reformatting(格式化时保留)*区域,勾选
Line breaks(手动换行)和Indentation of chained calls(链式调用缩进)两个选项 - 点击
Apply(应用)后再点击OK(确定)保存配置
后续再执行代码格式化操作时,就不会自动对齐链式方法调用,保留你预设的缩进格式。
内容的提问来源于stack exchange,提问作者Fiz
相关产品推荐
相关产品推荐

