Python

安装black

  1. 打开vscode,新建一个.py文件,按下快捷键Shift+Alt+F,右下角会弹出如下窗口,选择Use black

image.png

如果没有弹出窗口,应该是该项目已经设置了默认的格式化库。只需要打开项目目录下.vscode/settings.json 这个文件,把里面的 "python.formatting.provider": "***"删除,然后重复刚刚的步骤即可。

  1. 打开.vscode/setting.json文件,添加如下配置。
    1. {
    2. "python.formatting.provider": "black",
    3. "python.formatting.blackArgs": [
    4. "--line-length", "280", // 单行最大长度
    5. // "--skip-string-normalization", // 默认所有字符串使用双引号,取消该注释则忽略该问题
    6. ],
    7. }

    以上设置只对指定项目生效,如果想对所有python项目都应用该配置,在vscode的全局设置添加上面的配置项即可。

保存自动整理import

  1. 添加vscode配置

    1. "[python]": {
    2. // 在保存 python 文件的时候,用isort进行import 排序
    3. "editor.codeActionsOnSave": {
    4. "source.organizeImports": true
    5. }
    6. },
    7. // isort排序时,代码宽度是自行指定的,其设置值如下
    8. "python.sortImports.args": [
    9. "-l",
    10. "280"
    11. ]
  2. 找个python文件,直接保存检查头部import部分是否发生变化,如果没有变化,可能需要全局安装isort

    1. sudo pip3 install isort

    Vue

    安装VSCode插件

  3. 安装VeturESLint插件

image.png

  1. 添加setting.json配置信息
    1. {
    2. // 每次保存的时候将代码按eslint格式进行修复
    3. "[vue]": {
    4. "editor.codeActionsOnSave": {
    5. "source.fixAll.eslint": true
    6. }
    7. },
    8. "[html]": {
    9. "editor.codeActionsOnSave": {
    10. "source.fixAll.eslint": true
    11. }
    12. },
    13. "[css]": {
    14. "editor.codeActionsOnSave": {
    15. "source.fixAll.eslint": true
    16. }
    17. },
    18. "[less]": {
    19. "editor.codeActionsOnSave": {
    20. "source.fixAll.eslint": true
    21. }
    22. },
    23. "[javascript]": {
    24. "editor.codeActionsOnSave": {
    25. "source.fixAll.eslint": true
    26. }
    27. }
    28. }

    完整的setting.json设置

    1. {
    2. /***************************************************
    3. ****************** 编辑器 设置相关 *****************
    4. ****************************************************/
    5. // 关闭右侧迷你地图滚动
    6. "editor.minimap.enabled": false,
    7. // 当按tab键是,用空格代替
    8. // PS: 开启这个可以充分避免因为tab键和space键混用而导致的代码一团糟
    9. // 特别是在编写python代码的时候 XD
    10. "editor.insertSpaces": true,
    11. // 按住 Ctrl 键并滚动鼠标滚轮时对编辑器字体大小进行缩放
    12. // 如在meeting适,share屏幕下VS Code字体太小,就能进行调节
    13. "editor.mouseWheelZoom": true,
    14. // 显示控制字符
    15. // 这个也非常有用,有时候一些看不到的特殊字符可能会让你debug半天
    16. "editor.renderControlCharacters": true,
    17. // 显示空白字符
    18. // 我使用的是 boundary 模式,这样可以比较清晰地看到每行开头和结尾的空格字符
    19. "editor.renderWhitespace": "boundary",
    20. // 文件的EOL,统一成 "\n"
    21. // see https://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types
    22. // 避免不同平台之间出现诸如"^M"等问题
    23. "files.eol": "\n",
    24. // vscode默认启用了根据文件类型自动设置tabsize的选项
    25. "editor.detectIndentation": false,
    26. // 重新设定tabsize
    27. "editor.tabSize": 2,
    28. /***************************************************
    29. ****************** Python 设置相关 *****************
    30. ****************************************************/
    31. "python.formatting.provider": "black",
    32. "python.formatting.blackArgs": [
    33. "--line-length",
    34. "280", // 单行最大长度
    35. // "--skip-string-normalization", // 默认所有字符串使用双引号,取消该注释则忽略该问题
    36. ],
    37. "[python]": {
    38. // 在保存时对代码进行格式化
    39. "editor.formatOnSave": true,
    40. // 在保存 python 文件的时候,用isort进行import 排序
    41. "editor.codeActionsOnSave": {
    42. "source.organizeImports": true
    43. }
    44. },
    45. // isort排序时,代码宽度是自行指定的,其设置值如下
    46. "python.sortImports.args": [
    47. "-l",
    48. "280"
    49. ],
    50. /***************************************************
    51. ********************* 前端 设置相关 *****************
    52. ****************************************************/
    53. // 每次保存的时候将代码按eslint格式进行修复
    54. "[vue]": {
    55. "editor.codeActionsOnSave": {
    56. "source.fixAll.eslint": true
    57. }
    58. },
    59. "[html]": {
    60. "editor.codeActionsOnSave": {
    61. "source.fixAll.eslint": true
    62. }
    63. },
    64. "[css]": {
    65. "editor.codeActionsOnSave": {
    66. "source.fixAll.eslint": true
    67. }
    68. },
    69. "[less]": {
    70. "editor.codeActionsOnSave": {
    71. "source.fixAll.eslint": true
    72. }
    73. },
    74. "[javascript]": {
    75. "editor.codeActionsOnSave": {
    76. "source.fixAll.eslint": true
    77. }
    78. }
    79. }