You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整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格式化设置的步骤

  1. 打开IDE设置:Windows/Linux按File → Settings,Mac按IntelliJ IDEA → Settings
  2. 导航到Editor → Code Style → Kotlin,切换到Wrapping and Braces标签页
  3. 找到Function literals区域:
    • 取消勾选*Align when multiline*选项
    • 确认*Indent inside lambda body*的数值和普通代码缩进一致(比如默认4)
  4. 额外检查Call arguments区域:
    • 取消勾选*Align when multiline*选项,避免函数参数内的lambda被强制对齐到括号位置
  5. 点击Apply保存设置,重新格式化代码即可恢复正常缩进

内容的提问来源于stack exchange,提问作者HugoRune

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 08:33:18