简体   繁体   English

如何防止vscode中的python扩展自动插入除空格外的新字符?

[英]How to prevent python extension in vscode to automatically insert new characters except space?

Only python(v2020.6.91350) and material icon theme extensions are enabled, this is my code before saving:仅启用了python(v2020.6.91350)material icon theme扩展,这是我保存前的代码:

flipkart_modified_url = flipkart_base_url + urllib.parse.urlencode({"q": search_item})

after saving the file my line changes to:保存文件后,我的行更改为:

flipkart_modified_url = flipkart_base_url + \
urllib.parse.urlencode({"q": search_item})

why is the \ automatically inserted into the code?为什么\会自动插入到代码中? and i dont understand how does the code still work with the wrong syntax, I dont want to disable the extension, is there any way to actually prevent this from happening而且我不明白代码如何仍然使用错误的语法,我不想禁用扩展,有什么方法可以真正防止这种情况发生

It seems that the theme changes your settings to automatic formatting when saving.似乎主题在保存时会将您的设置更改为自动格式化。 I recommend you to download other themes like VSCode Icons .我建议您下载其他主题,例如VSCode Icons If you insist using this theme, follow steps below to turn off the automatic formatting (Risk: you will have to manually format your codes by Alt+Shift+F ):如果您坚持使用此主题,请按照以下步骤关闭自动格式化(风险:您必须通过Alt+Shift+F手动格式化您的代码):

  1. Check your defalut settings(Ctrl+Shif+P, Preferences: Open Default Settings (Json))检查您的默认设置(Ctrl+Shif+P,首选项:打开默认设置(Json))
  2. Search(Ctrl+F) editor.formatOnSave to see if it is "editor.formatOnType": true .搜索(Ctrl+F) editor.formatOnSave看是否为"editor.formatOnType": true if it is, follow steps bellow:如果是,请按照以下步骤操作:
  • Ctrl+Shif+P, Preferences: Open User Settings (Json) Ctrl+Shif+P,首选项:打开用户设置(Json)

  • Add this line:添加这一行:

    "editor.formatOnSave": false “editor.formatOnSave”:假

    Notice : don't forget to add ' , ' at the end of the previous line.注意:不要忘记在上一行的末尾添加“ , ”。 Edit settings like this像这样编辑设置

  1. Ctrl+S to save the json file and check if the problem is solved Ctrl+S保存json文件,查看问题是否解决

'\' -> This called Explicit line joining : Two or more physical lines may be joined into logical lines using backslash characters (\). '\' -> 这称为显式行连接:两个或多个物理行可以使用反斜杠字符 (\) 连接成逻辑行。

It was applied by the formatting you provided.它是由您提供的格式应用的。 If you did not set it, it defaults to 'autopep8', you can find this setting in 'defaultSettings.json', it was set by 'Python' extension.如果你没有设置它,它默认为'autopep8',你可以在'defaultSettings.json'中找到这个设置,它是由'Python'扩展设置的。 You can look in the Output panel -> Python channel to see how the 'Python' extension format your code.您可以在 Output 面板 -> Python 频道中查看“Python”扩展如何格式化您的代码。 And of course, you need to install 'autopep8' package.当然,您需要安装“autopep8”package。

If you want to change the behavior of autopep8, not to truncate the code to lines, you can add this to settings.json file: "python.formatting.autopep8Args": [ "--max-line-length", "120", ], And you can refer to autopep8 docs for more details.如果要更改 autopep8 的行为,而不是将代码截断为行,可以将其添加到 settings.json 文件:“python.formatting.autopep8Args”:[“--max-line-length”,“120” , ], 您可以参考autopep8 文档了解更多详细信息。

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

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