1

我们可以在 vs 代码中使用Ctrl++Shifti自动格式化代码。vscode 断行超过 80 个字符。改变linewidth不会改变它。我想把 120 放在我的 python 代码中。解决办法是什么?

我没有发现类似的问题挖掘以前的问题。

这是我的setting.json

{
    "workbench.panel.defaultLocation": "right",
    "workbench.startupEditor": "none",
    "workbench.sideBar.location": "right",
    "python.pythonPath": "/usr/bin/python3",
    "editor.minimap.enabled": false,
    "workbench.colorTheme": "Monokai",
    "C_Cpp.updateChannel": "Insiders",
    "update.showReleaseNotes": false,
    "update.mode": "manual",
    "workbench.editorAssociations": [
        {
            "viewType": "jupyter.notebook.ipynb",
            "filenamePattern": "*.ipynb"
        }
    ],
    "files.associations": {
        "*.rmd": "markdown"
    },
    "window.zoomLevel": 1,
    "prettier.printWidth": 120,
    "editor.wordWrap": "wordWrapColumn",
    "editor.wrappingIndent": "same",
    "editor.wordWrapColumn": 120

}

正如@Subrato 建议的那样,这对我有用:

"editor.defaultFormatter": "esbenp.prettier-vscode",
  "[python]": {
    "editor.defaultFormatter": null
  },
  "python.formatting.blackArgs": ["--line-length", "120"],
  "python.formatting.provider": "black",
4

1 回答 1

3

在 vs 代码中的 settings.json 文件中添加此设置。

"editor.wordWrap": "wordWrapColumn",
"editor.wrappingIndent": "same",
"editor.wordWrapColumn": 120

记住editor.wordWrapColumn: 120单独是行不通的还需要添加 editor.wordWrap: 'wordWrapColumn'

@Updated Prettier 不适用于 Python。autopep8格式化python文件需要格式。

用于pip install pep8将 pep8 安装到您的 vs 代码编辑器中

"python.formatting.provider": "autopep8",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
    "editor.defaultFormatter": "ms-python.python"
 }
//custom config for python 
"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"],
于 2021-06-05T11:57:51.177 回答