如何防止 Visual Studio 代码将链式函数推到新行?

How to prevent Visual Studio Code from pushing chained functions on to new line?

如何防止 Visual Studio 代码将链式 JavaScript 函数推到新行,如下面的 GIF 所示:

如您所见,它使代码极难查看并将简单的 if 签入扩展到多行。我正在使用名为 Prettier - Code Formatter 的扩展,我查看了首选项并发现了以下内容:

// Format a file on save. A formatter must be available, the file must not be 
//auto-saved, and editor must not be shutting down.
"editor.formatOnSave": false

这会在您点击保存时完全停止格式化。我查看了其他选项,但找不到此设置。

上面的过程有名字吗?

我怎样才能保留 formatOnSave 但不让它按原样格式化?

因为它在他们的 GitHub as a feature, it makes sense there wouldn't be an option to change it from the configuration 上。如果您添加 // prettier-ignore

这似乎不可配置(不确定是否可以),这是 Github post 的直接副本 issue:

The suggested behavior of this feature has been:

  • Wrap after hitting the line length limit
  • Wrap after 3 chained methods
  • Wrap after a configurable number of chained methods
  • Wrap when one of two conditions is met:
    • The line length limit is reached
    • The user opts into it by manually inserting a newline, like object literals

The initial implementation was (1).

The current implementation is (2).

(3) is unlikely to happen since prettier tries to avoid configuration.

The consensus is against (4) due to wanting to minimize user input influence in prettier's output.

I think the current implementation (2) makes sense in the majority of cases, but I find myself wanting (4) often enough that I no longer use prettier for JS.

基于此,目前没有办法修改此行为,也没有任何计划(截至撰写本文时)。

绝对不是我想要的答案,但这是给出的答案。

VSCode 实际上并没有使用 prettier,它可能作为您的扩展之一漏掉了,或者作为 eslint 或提示设置启用。如果我发生这种情况。在终端试试这个:"code --disable-extensions".

don/t 使用 prettier,尝试美化 + ESlint

这是认真的回答,因为我尝试解决了很长时间。但确实无法修复。