如何确定VS Code使用的格式化程序?

136

如果您安装了多个扩展,如何确定哪个格式化程序正在运行您的文档?例如,我有一些可能格式化HTML的HTML扩展,但我不确定实际上是哪一个在进行格式化,或者是否有多个扩展在进行格式化。老实说,我甚至不确定哪些扩展可能会对格式化产生贡献。最近,HTML和CSS中的自动格式化已经改变了它们的格式化方式,这些方式我并不喜欢,我想知道是哪个扩展正在进行这样的操作,以便我可以更改配置或禁用该扩展。目前我大约有80-90个扩展,所以逐个检查是一个耗时的过程,如果有编程方式可以确定这一点,那将是很好的。似乎扩展必须向格式化服务注册,以便它们可以进行自动格式化,但我不确定是否有一种调试、挂钩或查看这些服务的方法。


忘记在这里放了,但我已经打开了一个问题:https://github.com/Microsoft/vscode/issues/62809 现在有点晚了,因为Gama11的答案显示它即将推出! :) - CTS_AE
4个回答

143

从 1.33 版本(2019 年三月)开始,尝试格式化多种格式器已注册的文件会导致弹出此类提示:

请注意,如果通过“保存时格式化”或“粘贴时格式化”隐式发生格式化,则通知是“静默”的,这意味着您需要单击右下角的铃铛才能显示它:

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

例如,在此处选择 "Prettier" 将导致其添加到全局 settings.json

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

还有两个新的命令可用于使用特定格式化程序格式化文件,分别是使用...格式化文档使用...格式化所选内容。这对于使用未设置为默认格式化程序的格式化程序格式化特定文件非常有用。前者也可以从上下文菜单中访问:


1
能否一次性定义多种语言,例如:[javascript, json]: {} - Naxos84
2
目前还没有。https://github.com/microsoft/vscode/issues/51935 - Gama11
2
格式化文档命令的一些有用功能没有在上面提到,包括:1)它显示了可用格式化程序的菜单,并在当前打开文件类型的默认值旁边标记(default);2)它包括配置默认格式化程序...选项,以更改默认设置。 - James Murty
我的VSCode在保存时突然会添加一个空白行到文件末尾。我已经禁用了所有添加新行的选项。我甚至卸载了“Prettier”扩展,但是当我按下“ctrl+s”时,它仍然会在文件末尾添加一个新的空白行。为什么会这样? - CatarinaRuna
最近,如果VSCode检测到多个格式化程序,它会询问您要使用哪一个,这很好。根据@VonC的回答,似乎现在有更多即将到来的变化:https://github.com/microsoft/vscode/issues/126187。 - CTS_AE
@Naxos84,是的,使用语法 "[javascript][json]": {}。请参见 https://github.com/microsoft/vscode/issues/51935。 - Merchako

15
除了Gama11给出的答案之外,你可以前往以下路径下的settings.json
C:\Users\<username>\AppData\Roaming\Code\User\settings.json

我正在使用 "prettier" 格式化程序来格式化我的 html 文件,如果配置了,也可以在其他扩展中找到该格式化程序的使用。

已在settings.json中为HTML文件配置格式化程序


这只告诉你是否已更改它,仅当您在用户设置中设置它时才会告知。它也可能来自工作区设置,或被扩展覆盖。 - Eric Haynes

7

3
这根本没有回答问题... - Eric Haynes
1
你说得对。 - rofrol
不是 OP 所询问的内容。 - Tomer Gal

6

rofrol答案中提到的默认级别格式化程序将很快得到更好的管理,该信息来自于问题126187

It should be deprecated and banished for the sanity of developers everywhere.

Few days ago I ventured off the path of JS/TS/CSS/HTML/JSON and thought I try VScode with something new, ReScript.

I installed the plugin https://marketplace.visualstudio.com/items?itemName=chenglou92.rescript-vscode

But the formatting didn’t work.
Tried the same with a python formatter plugin, same problem.

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",

And this was apparently overriding the ReScript and python plugins.
It might have been overriding ALL plugins, even for languages that prettier is not even registered to use, which is a diabolical thing to do.

After some googling I found that I could add this and fix my problem

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

Or I could just set defaultFormatter back to null and let the plugins take over.

But I was wondering how many users have had this same problem? And how many plugins have to deal with this? And why did I think adding a defaultFormatter to the top level was a good idea?

好的,使用VSCode 1.61(2021年9月)将会显示一个模态对话框。
"配置"操作将帮助您为该特定语言配置默认格式化程序,而不是所有语言。
请参见提交 afc8ab1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接