【问题标题】:My vs code will flicker when I save. I turned on black and flake8 and formatonSave. Why does it flicker? How to stop it?当我保存时,我的 vs 代码会闪烁。我打开了 black and flake8 和 formatonSave。为什么会闪烁?如何阻止它?
【发布时间】:2021-02-06 13:38:08
【问题描述】:

我的 Django 项目在 VSCODE 中有以下工作区设置。

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": "/Users/kim/.pyenv/versions/3.7.7/bin/python3.7",
        "files.exclude": {
            "**/.classpath": true,
            "**/.project": true,
            "**/.settings": true,
            "**/.factorypath": true
        },
        "editor.tabSize": 4,
        "[javascript]": {
            "editor.tabSize": 2
        },
        "[json]": {
            "editor.tabSize": 2
        },
        "[markdown]": {
            "editor.tabSize": 2
        },
        "javascript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
        "python.linting.flake8Enabled": true,
        "python.linting.pylintArgs": ["--enable=unused-import", "--enable=W0614"],
        "python.formatting.provider": "black",
        "[python]": {
            "editor.formatOnSave": true,
            "editor.codeActionsOnSave": {
                "source.organizeImports": true
            }
        },
        "workbench.iconTheme": "vscode-icons",
        "editor.formatOnSave": true
    }
}

当我按下 Cmd+S 保存文件时,您可以看到它是如何闪烁的。来自这个gif

为什么会出现这种闪烁?如果它发生一次,我可以理解。但它会来回闪烁。好像 vscode 在两种不同的格式之间保存时格式化,无法下定决心。

我该如何正确解决这个问题?

【问题讨论】:

  • 你的 VS Code 版本是多少?您是否检查过“输出”>“Python”选项卡中是否有任何“奇怪”的消息? AFAIK,只有黑色对 formatOnSave 有影响,而 flake8 结果应该只出现在“问题”选项卡中(因为它是一个 linter,而不是一个格式化程序)。
  • 解决了。您可以在此处查看 github 链接以了解原因和建议的解决方案 github.com/microsoft/vscode-python/issues/…

标签: python-3.x visual-studio-code vscode-settings


【解决方案1】:

好的,我意识到我找到了问题和解决方案。

基本上问题在于有两个格式化程序用于对导入进行排序。

我在这个 github 评论中使用了答案

https://github.com/microsoft/vscode-python/issues/6933#issuecomment-543059396

        "[python]": {
            "editor.formatOnSave": true,
            "editor.codeActionsOnSave": {
                "source.organizeImports.python": true
            }
        },
        "python.formatting.blackArgs": ["--line-length", "88"],
        "python.sortImports.args": [
            "--multi-line=3",
            "--trailing-comma",
            "--force-grid-wrap=0",
            "--use-parentheses",
            "--line-width=88",
        ],

请注意,我将organizeImports 更改为organizeImports.python。 我还添加了"python.formatting.blackArgs": ["--line-length", "88"]。这是为了与行长一致。

在另一个问题中针对同一问题提出了建议。我独自尝试过,但没有奏效。所以我将它与 sortImports args 结合起来

【讨论】:

  • 我很想尝试这个,因为我也更喜欢在导入时使用尾随逗号和括号。但是,我不能让它正常运行:我也使用黑色,但是对于我的生活,我不知道我必须处理什么扩展来处理排序导入,因为我目前在我的命令面板中有两个选项:“组织导入”和“Python重构:对导入进行排序”
  • 这是给我的问题吗?我不能直接帮助你。我只能说什么对我有用。如果您可以编写自己的问题供其他人帮助,也许会更好。
  • 其实我只是想知道你必须使用什么格式化程序/扩展来组织你的导入。
  • 黑色和片状8。它在问题的标题中。问题内容中的我的工作区设置。我的答案中对设置的更改。
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
相关资源
最近更新 更多