简体   繁体   English

使用 formatOnSave 时如何使 VSCode 尊重 pyproject.toml 配置中的黑色排除文件

[英]How to make VSCode honor black excluded files in pyproject.toml configuration when using formatOnSave

I have the following pyproject.toml for configuring black:我有以下pyproject.toml用于配置黑色:

[tool.black]
exclude = 'foo.py'

If I run black.如果我跑black. from the project's root folder that only contains foo.py , I get No Python files are present to be formatted. Nothing to do � 从项目的仅包含foo.py的根文件夹中,我得到No Python files are present to be formatted. Nothing to do � No Python files are present to be formatted. Nothing to do � as expected. No Python files are present to be formatted. Nothing to do � 正如预期的那样。

However, when I save foo.py from within VS Code (I have black configured as the formatter and enabled Format On Save), the file is still formatted by black.但是,当我从 VS Code 中保存foo.py时(我已将黑色配置为格式化程序并启用了保存时格式化),该文件仍为黑色格式化。

Interestingly, VS Code seems to honor other configurations, eg line-length .有趣的是,VS Code 似乎支持其他配置,例如line-length

Is there a way to make VSCode honor the exclude configuration?有没有办法让 VSCode 尊重exclude配置?

If you list a file explicitly, this takes precedence over the any excluded files.如果您明确列出一个文件,这将优先于任何排除的文件。 This seems to be what black's developers consider the expected behaviour, see for example this issue .这似乎是黑色的开发人员认为的预期行为,例如看到这个问题 Also, this won't be fixed within vscode, see this issue .此外,这不会在 vscode 中修复,请参阅此问题

This is also a problem when using pre-commit .这也是使用pre-commit时的问题。

Since I didn't want to maintain yet another list (I do this for pre-commit already) of files for which I do not want to use FormatOnSave I ended up using a different approach.因为我不想维护另一个我不想使用FormatOnSave的文件列表(我已经这样做了),所以我最终使用了不同的方法。 Since all of the files that I don't want to auto-format on save reside in a specific folder within my project, I ended up using the workaround described in this answer and deactivate FormatOnSave in the sub-folder settings.由于我不想在保存时自动格式化的所有文件都位于我项目中的特定文件夹中,因此我最终使用了此答案中描述的解决方法并在子文件夹设置中停用FormatOnSave

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM