在VSCode设置中格式化代码以进行换行。

5
我们可以使用 Ctrl+Shift+i 在vs code中进行自动格式化代码。 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",
1个回答

16
在 VS Code 的 settings.json 文件中添加此设置。
"editor.wordWrap": "wordWrapColumn",
"editor.wrappingIndent": "same",
"editor.wordWrapColumn": 120

记住 editor.wordWrapColumn: 120 单独使用不起作用,您还需要添加 editor.wordWrap: 'wordWrapColumn'

@更新 Prettier 不能与 Python 一起使用。autopep8 格式要求格式化 python 文件。

使用 pip install pep8 将 pep8 安装到您的 vs code 编辑器中。

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

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