How do you determine which formatter is being used for VS Code?

如果安装了多个扩展,如何确定文档上正在运行哪个格式化程序?

例如,我有两个 HTML 扩展,可以格式化 HTML,但我不确定哪一个是真正的格式化,或者是否有多个。我甚至不确定哪些扩展可能会对格式有所贡献。最近 HTML 和 CSS 中的自动格式化已经改变了它们的格式化方式,我并不关心,我想知道哪个扩展正在这样做,所以我可以改变配置或禁用扩展。目前我有一些类似80-90的扩展,所以一个接一个地完成是一个非常及时的过程,如果有一种程序化的方法来确定这一点,我希望远离这个过程。似乎扩展必须在格式化服务中注册,这样它们才能进行自动格式化,但我不确定是否有方法来调试、挂钩或查看这些内容。

112098 次浏览

从1.33版本(2019年3月) 开始,尝试格式化一个在弹出窗口中注册了多个格式化程序的文件,如下所示:

Note that the notification is "silent" if formatting happened implicitly via "format on save" or "format on paste", meaning that you need to click the bell in the lower right for it to show up:

然后,Configure...菜单列出当前语言可用的所有格式化程序。可以选择其中一个作为 Format DocumentFormat Selection的默认格式化程序:

例如,在这里选择“ Prettier”会导致将其添加到全球 settings.json:

"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}

还有两个用于使用特定格式化程序格式化文件的新命令: Format Document With...Format Selection With...。这对于使用未设置为默认格式化程序的格式化程序格式化特定文件非常有用。前者也可从上下文菜单中找到:

In addition to the answer given by Gama11 你可以在给定的路径下到达 settings.json

C:\Users\<username>\AppData\Roaming\Code\User\settings.json

我使用 "prettier"格式化程序为我的 html文件, 也可以找到配置后用于其他扩展的格式化程序。

Formatter configured for HTML files in settings.json

Look at https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

ctrl-shift-p > search "open json"

Add

{
"editor.defaultFormatter": null,
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
}

罗福尔answer中提到的默认级别格式化程序将很快从 第126187期得到更好的管理

为了各地开发人员的理智,应该摒弃和废除这种做法。

几天前,我冒险离开了 JS/TS/CSS/HTML/JSON 的路径,并认为我尝试了一些新的东西,重新脚本的 VScode。

我安装了插件 https://marketplace.visualstudio.com/items?itemName=chenglou92.rescript-vscode

但是格式没有用。
在 Python 格式化程序插件中也尝试了同样的方法,也是同样的问题。

After a little investigation I found that I had this configured at the top level of my settings.json. It also has a nice GUI pulldown.

"editor.defaultFormatter": "esbenp.prettier-vscode",

这显然是覆盖了 ReScript 和 python 插件。
它可能已经覆盖了所有的插件,即使是那些更漂亮的语言甚至没有注册使用,这是一件恶魔般的事情。

在谷歌了一下之后,我发现我可以添加这个并修复我的问题

"[rescript]": {
"editor.defaultFormatter": "chenglou92.rescript-vscode"
}

或者我可以只是设置 defaultFormatter回到 null,让插件接管。

但是我想知道有多少用户遇到过同样的问题?有多少插件需要处理这个问题?为什么我认为在顶层添加 defaultFormatter 是一个好主意呢?

Well, with VSCode 1.61 (Sept. 2021), this will now show a modal dialog.
Configure”操作将使您为该特定语言配置默认格式化程序,而不是为所有语言配置默认格式化程序。
请参见提交 afc8ab1(参见 http://github.com/microsoft/vscode/commit/afc8ab16ef9c4d52259ca57e8b99cc3327387341)

https://user-images.githubusercontent.com/1794099/131115167-eede31ff-487b-45e2-a9f9-76b7fcf4e39c.png