Home > Development Tools > VSCode > body text

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

青灯夜游
Release: 2022-03-09 20:18:23
forward
5478 people have browsed it

This article will summarize and share with you some VSCode advanced debugging and usage skills for each scenario, so as to double the efficiency of your daily development work. I hope it will be helpful to everyone!

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

#VsCode has quickly become popular with its excellent features since its birth. Especially for front-end development partners, it has almost become an indispensable development tool. Therefore, mastering the respective usage skills and debugging skills of VsCode will double the efficiency of your daily development work. This article will use a large number of pictures and texts to introduce various techniques of VsCode in detail from the following aspects:

  • The first part mainly introduces the basic techniques of VsCode, such as commonly used shortcut keys, auxiliary rulers, etc. Those who are familiar with this part can skip it directly. [Recommended study: "vscode introductory tutorial"]
  • The second part mainly focuses on various breakpoints (such as log breakpoints, inline breakpoints, expression breakpoints, etc.), data Panels, etc.
  • The third part mainly explains the practical debugging of various projects, such as the practical debugging of Node programs, TS programs, Vue programs, Electron programs, Html, etc.
  • The last part will explain Other useful tips, such as code snippets, refactoring, Emmet, etc.

Basic Tips

Quick Start

After VsCode is installed, environment variables will be automatically written , enter code in the terminal to invoke the VsCode application.

Commonly used shortcut keys

  • ctrl pQuickly search for files and jump, add : to jump to the specified line

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • ctrl shift p Access all available commands based on your current context.

  • ctrl shift cOpen a terminal externally and navigate to the current project path

  • ctrl Press the left of key 1 Symbol Show hidden terminal panel

  • Ctrl B Switch sidebar

  • Ctrl \ Quickly split file editing

  • alt single left click Add multiple cursors

  • alt shift single Left click Add cursor to all positions in the same column

  • alt shift mouse selection Select the area with the same start and end

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • alt up key or down key Move the current line or selected area up/down one line

Vertical Ruler

Add the following configuration in the configuration file to increase the number of characters. Ruler auxiliary lines

"editor.rulers": [40, 80, 100]
Copy after login

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

Advanced skills

breakpoint Basic usage

The following takes quickly debugging a Node project in VsCode as an example to demonstrate the basic use of breakpoints. The following article will continue to complete various advanced breakpoints.

  • Create a basic node project for Nodejs
  • Open the debugging panel on the left, select the name of the node project you want to debug, and add debugging configuration

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Select the debugging project type as Node.js

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Open the generated .vscode/launch. json file, specify the program entry file

program field is used to specify your program entry file, ${workspaceFolder} represents the current project root path

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • To add a breakpoint in the program, just click on the sidebar on the left to add a breakpoint

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Press F5 to start debugging. If debugging is successful, there will be a floating window operation bar.

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

The operation button functions of the floating window are in sequence. For:

  • Continue (F5),
  • Debug next step (F10),
  • Single step jump Enter (F11),
  • Single step out (Shift F11),
  • Redebug (Ctrl Shift F5) ,
  • End debugging (Shift F5)

Log breakpoint

Log breakpoint is a variant of ordinary breakpoint. The difference is that will not interrupt debugging , but can log information to the console. Log breakpoints are particularly useful when debugging services that cannot be paused or stopped. Proceed as follows:

  • Steps to add a log breakpoint

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Enter the information to be logged and click Enter to complete the addition

You can use {} to use variables, for example Add a log breakpoint here, the value of b is ${b}

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • After the log breakpoint is added successfully, there will be a diamond icon

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Press F5 Run to view debugging results

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

Expression conditional breakpoint

Conditional breakpoint is when the expression result is true Breakpoints will be made, the steps are as follows:

  • Right-click on the left side of the code line, you can also add a breakpoint, select add conditional breakpoint here

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Fill in the expression and press Enter

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • The small icon added successfully is as follows

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • Press F5 to debug, the condition is established, so the breakpoint is hit

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

## Counting breakpoints

A breakpoint will only be executed if the line of code hits the specified number of times. The steps are as follows:

    Select the conditional breakpoint, switch to the number of hits option, fill in the number of hits

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    The successful completion is as follows As shown in the picture

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    Press
  • F5 to debug, as shown in the picture, it will interrupt when the index is 9

1[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

Inline breakpoints

Inline breakpoints are hit only when execution reaches the column associated with the inline breakpoint. This is particularly useful when debugging minified code that contains multiple statements on a single line. For example, for loops, short-circuit operators, etc. are particularly useful when one line of code contains multiple expressions. The steps are as follows:

    Press
  • Shift F9

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    at the specified location. After debugging, each time Code running to this inline will be interrupted

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

Additional knowledge point: Data panel introduction

    The data panel can view all variables

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    Right-click on the variable to set the variable value, copy the variable value and other operations

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    When focusing on the data panel, you can search and filter by typing values. Click the button shown below to control whether to filter.

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

Additional knowledge point: Introduction to the monitoring panel

You can add variables to the monitoring panel and observe in real time changes in variables.

    Add variables to the monitoring panel by right-clicking on the variable panel and selecting "Add to Monitoring"

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    You can also directly Select the Add button in the monitoring panel to add variables

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    After adding variables, you can monitor changes in variables in real time

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

补充知识点:调试服务器时打开一个URI

开发 Web 程序通常需要在 Web 浏览器中打开特定 URL,以便在调试器中访问服务器代码。VS Code 有一个内置功能“ serverReadyAction ”来自动化这个任务。

  • 一段简单的server代码
var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('Hello World!');
});

app.listen(3000, function() {
  console.log('Example app listening on port 3000!');
});
Copy after login
  • 配置launch.json,以支持打开URI
{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "program": "${workspaceFolder}/app.js",

  "serverReadyAction": {
    "pattern": "listening on port ([0-9]+)",
    "uriFormat": "http://localhost:%s",
    "action": "openExternally"
  }
}
Copy after login

pattern是设置匹配的程度端口号,端口号放在小括号内,即作为一个正则的捕获组使用。uriFormat映射为URI,其中%s使用pattern中的第一个捕获组替换。最后使用该URI作为外部程序打开的URI。

  • F5调试,会自动打开浏览器,且会在下图所示处中断,当继续执行后,浏览器才能看到输出了server的内容

2[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

终局:各场景调试实战

调试NodeJS项目

关于NodeJs项目的调试方法,已经在上述的断点的基本使用部分做了介绍,可以网上滚动翻阅。

调试Typescript项目

  • 调试TS项目前,先创建一个TS项目

    • 先初始化一个ts程序,生成默认的tsconfig.json文件
    # 终端运行
    tsc --init
    Copy after login
    • 打开tsconfig.json文件,开启sourceMap选项和指定编译后输出的路径

    VS Code 内置了对 Ts 调试的支持。为了支持调试 Ts 与正在执行的 Js 代码相结合,VS Code 依赖于调试器的source map在 Ts 源代码和正在运行的 Js 之间进行映射,所以需要需要开启sourceMap选项。

    {
        "sourceMap": true,
        "outDir": "./out"
    }
    Copy after login
    • 新建index.ts文件,写一个基本的ts代码
    const num: number = 123;
    console.log(num);
    
    function fn(arg: string): void {
      console.log('fn', arg);
    }
    
    fn("Hello");
    Copy after login
  • 手动编译调试TS

    在上述的ts基本项目中:

    • 终端执行ts的编译命令tsc

    [Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    • 此时可以看到生成了out文件夹,里面包含一个index.js和一个index.js.map文件

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    • 在index.ts中随意添加一个断点

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    • F5或者运行 -> 启动调试,此时可以看到可以正常debug调试

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 通过构建任务构建调试TS

    • Ctrl+Shift+B或选择终端 -> 运行生成任务,此时会弹出一个下拉菜单

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    • 选择tsc构建选项,此时可以看到自动生成了编译文件

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    注意,如果你使用的是其他终端(比如cmder)的话,有可能会生成不了,如下图所示,使用默认的powershell即可:

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    • 调试的话和上述步骤一样,在有了编译后的文件后,按F5即可
  • 监视改变并实时编译

    • Ctrl + Shift + B选择监视选项,可以实时监视文件内容发生变化,重新编译

    3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

    • 如下图所示,会实时编译

3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

补充知识点:tasks配置文件的创建方式

  • 方法1:点击终端 -> 配置任务 -> 选择任务可以生成对应的tasks.json配置

3[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 方法2:点击终端 -> 运行生成任务 -> 点击设置图标也可以生成对应的tasks.json配置

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

补充知识点:每次调试时重新编译

  • 按上述的操作已经生成了task.json配置文件
{
	"version": "2.0.0",
	"tasks": [
    {
      "type": "typescript",
      "tsconfig": "tsconfig.json",
      "problemMatcher": [
        "$tsc"
      ],
      "group": "build",
      "label": "tsc: 构建 - tsconfig.json"
    }
  ]
}
Copy after login
  • 点击运行 -> 添加配置 -> 选择nodejs

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 在生成的launch.json文件中,添加preLaunchTask字段,值是tasks.jsonlabel值,一定要相同,注意大小写。该字段的作用是在执行命令前先执行改task任务。

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

注意,如果编译后的js文件不在相应的位置,通过图中的outFiles字段可以指定ts编译后的js路径。

  • index.ts文件中按F5启动调试,可以看到调试前已经生成了编译文件,而后就可以正常调试了。

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

补充知识点:VsCode的TS版本说明

  • vscode本身内置了对ts的支持

  • vscode内置的ts版本(即工作区版本),仅仅用于IntelliSense(代码提示),工作区ts版本与用于编译的ts版本无任何关系。

修改工作区ts版本的方法:

  • 在状态栏选择typescript的图标,选择版本切换

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 选择你需要的版本即可

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

调试html项目

学会了上述ts的调试后,我们尝试调试html文件,并且html文件中引入ts文件:

  • 创建html,引入ts编译后的js文件
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h3>Hello</h3>
  <script src="./out/index.js"></script>
</body>
</html>
Copy after login
  • ts源文件如下:
const num: number =  1221;
console.log(num);

function fn(arg: string): void {
  console.log(&#39;fn&#39;, arg);
}

document.body.append(&#39;World&#39;)

fn("he");
Copy after login
  • 打debug

4[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • launch.json启动命令配置
{
  // 使用 IntelliSense 了解相关属性。 
  // 悬停以查看现有属性的描述。
  // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-chrome",
      "request": "launch",
      "name": "Launch Chrome",
      "url": "file:///E:/demo/vscode/debug/ts/index.html",
      "preLaunchTask": "tsc: 构建 - tsconfig.json",
      "webRoot": "${workspaceFolder}"
    }
  ]
}
Copy after login
  • 选择我们的启动命令

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • F5可以正常唤起chrome浏览器,并在vscode的ts源码处会有debug效果

调试Vue项目的两种方式

下面介绍两种调试vue2项目的3种方法,其他框架的调试也类似:

不使用vscode插件Debugger for chrome的方法

  • 初始化vue项目,配置vue.config.js,指定要生成sourceMaps资源
module.exports = {
  configureWebpack: {
    // 生成sourceMaps
    devtool: "source-map"
  }
};
Copy after login
Copy after login
  • 根目录下创建./vscode/launch.json文件 或者选择运行 -> 添加配置 -> Chrome

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "vuejs: chrome",
      "url": "http://localhost:8080",
      "webRoot": "${workspaceFolder}",
      "breakOnLoad": true,
      "pathMapping": {
        "/_karma_webpack_": "${workspaceFolder}"
      },
      "sourceMapPathOverrides": {
        "webpack:/*": "${webRoot}/*",
        "/./*": "${webRoot}/*",
        "/src/*": "${webRoot}/*",
        "/*": "*",
        "/./~/*": "${webRoot}/node_modules/*"
      },
      "preLaunchTask": "serve"
    }
  ]
}
Copy after login
  • 添加任务脚本
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "serve",
      "type": "npm",
      "script": "serve",
      "isBackground": true,
      "problemMatcher": [
        {
          "base": "$tsc-watch",
          "background": {
            "activeOnStart": true,
            "beginsPattern": "Starting development server",
            "endsPattern": "Compiled successfully"
          }
        }
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
Copy after login

该脚本的作用是运行npm run serve编译命令。

  • F5启动调试即可

注意:此方式的主要点在于launch.json配置文件中,通过preLaunchTask字段指定调试前先运行一个任务脚本,preLaunchTask的值对应tasks.json文件中的label值。

更多详细内容,大家可以点击这里的参考文档查阅。

https://github.com/microsoft/vscode-recipes/tree/main/vuejs-cli

借助vscode插件Debugger for Chrome在Chrome中调试

  • 第一步还是初始化vue项目,添加vue.config.js文件配置,指定要生成sourceMaps资源
module.exports = {
  configureWebpack: {
    // 生成sourceMaps
    devtool: "source-map"
  }
};
Copy after login
Copy after login
  • vscode中扩展中安装Debugger for Chrome插件,并确保没有禁用插件

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 手动启动项目的运行, 此方式不需要配置tasks.json任务
# 终端执行命令,启动项目
npm run serve
Copy after login
  • F5启动调试即可

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

更多详细内容,请点击这里的参考文档查阅。

https://vuejs.org/v2/cookbook/debugging-in-vscode.html

借助vscode插件Debugger for Firfox在Firefox中调试

  • Debugger for Chrome基本一样,区别在于安装Debugger for Firfox插件,并在launch.json配置中,增加调试Firefox的配置即可,配置如下
{
  "version": "0.2.0",
  "configurations": [
    // 省略Chrome的配置...
    // 下面添加的Firefox的配置
    {
      "type": "firefox",
      "request": "launch",
      "reAttach": true,
      "name": "vuejs: firefox",
      "url": "http://localhost:8080",
      "webRoot": "${workspaceFolder}/src",
      "pathMappings": [{ "url": "webpack:///src/", "path": "${webRoot}/" }]
    }
  ]
}
Copy after login
  • 调试时选择对应的调试命令即可

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

Firefox初始启动时不会触发调试,需要刷新一次

调试Electron项目

Electron很多人都使用过,主要用于开发跨平台的系统桌面应用。那么来看下vue-cli-electron-builder创建的Electron项目怎么调试。步骤如下:

  • 在初始化项目后,首先修改vue.config.js文件配置,增加sourceMaps配置:
module.exports = {
  configureWebpack: {
    devtool: &#39;source-map&#39;
  }
}
Copy after login
  • 创建调试配置.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Electron: Main",
      "type": "node",
      "request": "launch",
      "protocol": "inspector",
      "preLaunchTask": "bootstarp-service",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
      "windows": {
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
      },
      "args": ["--remote-debugging-port=9223", "./dist_electron"],
      "outFiles": ["${workspaceFolder}/dist_electron/**/*.js"]
    },
    {
      "name": "Electron: Renderer",
      "type": "chrome",
      "request": "attach",
      "port": 9223,
      "urlFilter": "http://localhost:*",
      "timeout": 0,
      "webRoot": "${workspaceFolder}/src",
      "sourceMapPathOverrides": {
        "webpack:///./src/*": "${webRoot}/*"
      }
    },
  ],
  "compounds": [
    {
      "name": "Electron: All",
      "configurations": ["Electron: Main", "Electron: Renderer"]
    }
  ]
}
Copy after login

此处配置了两个调试命令: Electron: Main用于调试主进程,Electron: Renderer用于调试渲染进程;compounds[].选项用于定义复合调试选项; configurations定义的复合命令是并行的; preLaunchTask用于配置命令执行前先执行的任务脚本,其值对应tasks.json中的label字段; preLaunchTask用在compounds时,用于定义configurations复合任务执行前先执行的脚本。

  • 创建任务脚本
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "bootstarp-service",
      "type": "process",
      "command": "./node_modules/.bin/vue-cli-service",
      "windows": {
        "command": "./node_modules/.bin/vue-cli-service.cmd",
        "options": {
          "env": {
            "VUE_APP_ENV": "dev",
            "VUE_APP_TYPE": "local"
          }
        }
      },
      "isBackground": true,
      "args": [
        "electron:serve", "--debug"
      ],
      "problemMatcher": {
        "owner": "custom",
        "pattern": {
          "regexp": ""
        },
        "background": {
          "beginsPattern": "Starting development server\\.\\.\\.",
          "endsPattern": "Not launching electron as debug argument was passed\\."
        }
      }
    }
  ]
}
Copy after login
  • 启动调试

在主进程相关代码上打上断点,然后启动调试主进程命令就可以调试主进程了

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

注意,这里的options参数是根据实际的情况,自定义添加我们运行项目时所需要的参数,比如我这里因为启动项目的npm命令是:

"serve-local:dev": "cross-env VUE_APP_TYPE=local VUE_APP_ENV=dev vue-cli-service electron:serve"
Copy after login
  • 主进程调试成功

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 开始调试渲染进程

切换到渲染进程的调试选项,在渲染进程的代码处打上断点,点击调试。注意,此时并不会有断点终端,需要ctrl+r手动刷新软件进程才会看到渲染进程的断点。

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 刷新渲染进程后的效果,如下图,已经进入了断点

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 另一种方式

同时开启渲染进程和主进程的调试,只需要切换到调试全部的选项即可。注意,此种方式因为compounds[].configurations配置是并行执行的,并不一定能保证渲染进程调试一定能附加到主进程调试成功(估计是时机问题),有些时候会调试渲染进程不成功。所以,可以采取上面的方式进行调试。

5[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

更多调试Electron的内容,可以点击参考文档查阅。
https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/recipes.html#debugging-with-vscode

补充:更进一步:
VS调试React app文档(https://medium.com/@auchenberg/live-edit-and-debug-your-react-apps-directly-from-vs-code-without-leaving-the-editor-3da489ed905f)
VS调试Next.js文档(https://github.com/microsoft/vscode-recipes/tree/main/Next-js)
更多...(https://code.visualstudio.com/docs/nodejs/debugging-recipes)
Copy after login

其他技巧

技巧一:代码片段(snippets)

从扩展商店中安装snippets

@category:"snippets"
Copy after login

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

创建全局代码片段

  • 选择文件 -> 首选项 -> 用户片段
  • 选择新建全局代码片段文件

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 添加代码片段文件的文件名称,会生成.code-snippets后缀的文件

  • 定义用户片段

{
  "自动补全console.log": {
    "scope": "javascript,typescript",
      "prefix": "log",
      "body": [
        "console.log(&#39;$1&#39;);",
        "$2"
      ],
    "description": "输出console.log(&#39;&#39;)"
  }
}
Copy after login
关键词类型说明
scopestring代码片段生效的作用域,可以是多个语言,比如javascript,typescript表示在js和ts生效,不加scope字段表示对所有文件类型生效
prefix`stringstring[]`
bodystring[]代码片段内容,数组的每一项会是一行
descriptionstringIntelliSense 显示的片段的可选描述
11 -n-定义光标的位置,光标根据数字大小按tab依次跳转;注意$0是特殊值,表示光标退出的位置,是最后的光标位置。
  • 在键盘输入log时效果如下

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 指定光标处的默认值并选中
"body": [
  "console.log(&#39;${1:abc}&#39;);"
],
Copy after login

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 指定光标处的默认值有多个,并提供下拉选择

用两个竖线包含多个选择值,|多个选择值直接用逗号隔开|

"body": [
  "console.log(&#39;${1:abc}&#39;);",
  "${2|aaa,bbb,ccc|}"
],
Copy after login

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

新建当前工作区的代码片段

只需要选择文件 -> 首选项 -> 用户片段 -> 新建xxx文件夹的代码片段, 新建后会在当前工作区生成.vscode/xxx.code-snippets文件

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

技巧二:Emmet

vscode内置了对Emmet的支持,无需额外扩展。例如html的Emmet演示如下:

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

技巧三:对光标处代码变量快速重命名

选中或者光标所处的位置,按F2可以对所有的变量重命名

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

技巧四:代码重构建议

  • 选中要重构的代码,点击出现的黄色小灯的图标

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 选中重构的类型

6[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 输入新的变量名

[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 还可以重构到函数

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • TS中还可以提取接口等等

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

补充:VsCode扩展插件开发

VsCode扩展插件可以做什么事情?

  • 定制主题、文件图标
  • 扩展工作台功能
  • 创建webView
  • 自定义新的语言提示
  • 支持调试特定的runtime

基于Yeoman快速开发VsCode插件,步骤如下:

  • 安装Yeoman和用于生成模板的插件VS Code Extension Generator(https://www.npmjs.com/package/generator-code)
# 终端运行,主要node版本需要12及以上,node10会安装报错
npm i -g yo generator-code
Copy after login
  • 运行yo code创建命令,选择要生成的项目模板。这里演示New extension

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 根据提示依次选择

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 生成的内容如下

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • F5生成编译项目,此时会自动打开一个新窗口
  • 在新窗口按Ctrl+Shfit+P,输入Hello World命令

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 此时会弹出一个弹窗的效果

7[Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency

  • 至此,一个最简单的插件就完成了

更多关于VSCode的相关知识,请访问:vscode教程!!

The above is the detailed content of [Summary of vomiting blood] Tips on debugging and using VSCode to double your development work efficiency. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template