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

如何配置Scalafmt保留List定义中的空行?

解决Scalafmt保留List元素间单个空行的配置方案

要让Scalafmt 3.7.3保留List定义中元素间的单个空行,你需要在现有.scalafmt.conf配置中添加针对集合内空行的保留规则,修改后的完整配置如下:

version = "3.7.3"
runner.dialect = scala3
align.preset = most
newlines.source = keep
newlines.preserveEmptyLines.withinList = true

配置说明

  • newlines.source = keep:作为基础配置,尽可能保留源码中的换行逻辑,但针对集合元素这类特定结构,需要额外规则补充才能生效。
  • newlines.preserveEmptyLines.withinList = true:专门针对List内部元素间的空行设置保留规则,确保单个空行不会被格式化工具移除。

如果需要同时对Set、Map等其他集合类型生效,可以将withinList替换为更通用的withinCollection:

newlines.preserveEmptyLines.withinCollection = true

验证效果

修改配置后,你的示例代码格式化后会完整保留元素间的单个空行,与源码结构完全一致:

List(
  "some-random-string1"
    .trim
    .stripMargin,

  "some-random-string2"
    .intern
    .dropRight(3),

  "some-random-string3"
    .takeRight(4)
    .substring(10)
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 03:07:22