Skip to content

Visual Studio Code

常见配置

默认以新标签页打开文件

"workbench.editor.enablePreview": false,

https://github.com/microsoft/vscode/issues/209752

长行按显示器可视区域自动换行

  "diffEditor.wordWrap": "on",
  "editor.wordWrap": "on",

前端支持

内置默认 TypeScript/JavaScript/JSON/CSS/HTML/React 格式化刷 esbenp.prettier-vscode

Python support

代码格式化包 pip3 install autopep8

保存文件时自动格式化代码,并禁用自动对 import 移动和排序,因为需要修改 sys.path 后再 import 情况会导致无法 import 。

json
"[python]": {
  "editor.formatOnType": true
},
"python.formatting.autopep8Args": ["--ignore", "E402", "--max_line_length", "150"],

调试 node/TypeScript 项目

场景:调试 rollup/vite 自定义插件。

以 Windows 为例,定位到 vite 命令行工具安装路径,在终端输入

bat
where vite

# 结果可能是多个
%USERPROFILE%\AppData\Roaming\npm\vite
%USERPROFILE%\AppData\Roaming\npm\vite.cmd

%USERPROFILE% 为你当前登录系统用户数据主路径

最终 vite 路径示例 %USERPROFILE%\AppData\Roaming\npm\node_modules\vite\bin\vite.js

在终端输入

bat
cd c:\path\to\项目根路径\
node --inspect-brk %USERPROFILE%\AppData\Roaming\npm\node_modules\vite\bin\vite.js build --mode development --watch

vscode 打开对应项目源码,设置断点后在命令面板输入 Debug: attach to node process

注入到对应进程即可开始断点调试。

综合以上 run - add Configuration...

json
{
  "configurations": [
    {
      "name": "Launch Program",
      "program": "${env:USERPROFILE}/AppData/Roaming/npm/node_modules/vite/bin/vite.js",
      "args": ["build", "--mode", "development", "--watch"],
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "type": "node"
    }
  ]
}

Debugging Next.js

https://nextjs.org/docs/app/building-your-application/configuring/debugging

Go in Visual Studio Code

Add support for Windows 7

  • stick to to Go v1.20.14
  • downgrade dependency packages to compatible with Go v1.20.14

custom settings.json

json
{
  "workbench.editor.enablePreview": false,
  "go.toolsManagement.checkForUpdates": "off",
  "terminal.integrated.defaultProfile.windows": "Command Prompt",
  "window.zoomLevel": 1,
  "explorer.confirmDelete": false
}

Custom Go version

json
"go.alternateTools": {
  "go": "/snap/go/current/bin/go"
}

https://github.com/golang/vscode-go/issues/1411

Install analysis tools by manual

For Go 1.20.14 & Windows 7

shell
#!/bin/bash

# dlv
go install github.com/go-delve/delve/cmd/dlv@v1.21.2

# gopls
go install golang.org/x/tools/gopls@v0.15.3

# goimports
go install golang.org/x/tools/cmd/goimports@v0.9.3

# staticcheck
go install honnef.co/go/tools/cmd/staticcheck@2023.1.3

# impl
go install github.com/josharian/impl@latest

# gotests
go install github.com/cweill/gotests/gotests@latest

# goplay
go install github.com/haya14busa/goplay/cmd/goplay@latest

# gomodifytags
go install github.com/fatih/gomodifytags@latest

Custom .vscode/launch.json, disable dlv outdated warning

json
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Package",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "dlvFlags": ["--check-go-version=false"],
      "program": "${fileDirname}"
    }
  ]
}

See also

VSCode 调试

VSCode 调试模式时,设置 TCP bind 0.0.0.0 会不生效,实际是 127.0.0.1 ;

VSCode 设置环境变量用于调试

json
"go.testEnvVars": {
  "envKey": "envValue"
}

常用插件

Databases

Released under the CC-BY-NC-4.0