Elixir 的混合格式忽略行长选项

s3c*_*ur3 4 code-formatting elixir

我的.formatter.exs项目根目录中有一个 Elixir 项目 文件,如下所示:

[
  line_length: 120,
  inputs: ["mix.exs", "config/*.exs"],
  subdirectories: ["apps/*"]
]
Run Code Online (Sandbox Code Playgroud)

但据我所知,该line_length参数被忽略了。

给定以下代码,最长的行(长度为 102 个字符)总是被分成两行(when子句换行):

defmodule SomeModule do
  def lookup_data(parameter1, parameter2) when is_integer(parameter1) and is_integer(parameter2) do
    :not_implemented
  end
end
Run Code Online (Sandbox Code Playgroud)

相比之下,如果我将模块复制并粘贴到在线 Elixir 格式化程序中并将行长度选项设置为 120,则不会对文本进行任何更改(如预期)。

我一定是搞砸了.formatter.exs,但对于我的生活,我不知道是怎么回事。

s3c*_*ur3 6

mix format docs 中,它指出如果您的顶级.formatter.exs列出子目录(我的有"apps/*"),则顶级格式化程序中的规则将不会在这些子目录中使用。

解决方案是使用 line length 参数编辑apps/[my_app]/.formatter.exs(之前已自动生成mix new):

[
  line_length: 120
]
Run Code Online (Sandbox Code Playgroud)