如何调整IntelliJ/Android Studio中Kotlin构造参数代码缩进?
Kotest FreeSpec代码格式化缩进异常的解决方法
问题现象
格式化前的FreeSpec代码:
class MyTests : FreeSpec({ "String.length" - { "should return the length of the string" { "sammy".length shouldBe 5 "".length shouldBe 0 } } })
使用Ctrl+Alt+L格式化后,代码被添加了大量不必要的缩进:
class MyTests : FreeSpec({ "String.length" - { "should return the length of the string" { "sammy".length shouldBe 5 "".length shouldBe 0 } } })
调整IDE格式化设置的步骤
- 打开IDE设置:Windows/Linux按
File → Settings,Mac按IntelliJ IDEA → Settings - 导航到
Editor → Code Style → Kotlin,切换到Wrapping and Braces标签页 - 找到Function literals区域:
- 取消勾选
*Align when multiline*选项 - 确认
*Indent inside lambda body*的数值和普通代码缩进一致(比如默认4)
- 取消勾选
- 额外检查Call arguments区域:
- 取消勾选
*Align when multiline*选项,避免函数参数内的lambda被强制对齐到括号位置
- 取消勾选
- 点击
Apply保存设置,重新格式化代码即可恢复正常缩进
内容的提问来源于stack exchange,提问作者HugoRune
相关产品推荐
相关产品推荐

