Home > Development Tools > VSCode > body text

Take you step by step to understand the warehouse configuration in vscode

青灯夜游
Release: 2022-03-25 20:48:57
forward
4260 people have browsed it

This article will take you through the warehouse configuration in vscode, I hope it will be helpful to you!

Take you step by step to understand the warehouse configuration in vscode

Welcome to the world of vscode. There are many development tools. Only by identifying your own needs can you find the right tool, instead of "having a hammer in your hand and looking at everything." Like a nail"; for VSCode, its positioning lies in the editor rather than the IDE. The IDE focuses on out-of-the-box use, so it is huge, such as Eclipse.

The editor focuses on rich support and freedom for languages ​​and workflows, and is therefore more lightweight. This means that it does not do too much for users in a certain language or aspect, but it also This means that TA has a high degree of freedom, such as plug-in mechanism, warehouse configuration mechanism, etc. This article focuses on sharing warehouse configuration, and the plug-in mechanism is a separate article.

Then let’s start, rush, rush!

Take you step by step to understand the warehouse configuration in vscode

Configuration Overview

VS Code is managed based on folders, but VS Code allows you to create several Folders or project-related configurations are saved in this folder for easy sharing within the team. This folder is .vscode.

This folder can contain the following files.

Take you step by step to understand the warehouse configuration in vscode

Configuration file (settings.json)

It will only take effect when the current folder is opened in VS Code. It is the same as what we said about modifying user settings.

Task settings (tasks.json)

About the configuration file of the VS Code task system

Debug settings (launch.json)

Used to explain how to debug the code in the current folder

vscode warehouse configuration configuration file (settings)

As an editor, you naturally need to consider personal preferences When developing with multiple people, the project style is unified, such as font size, line breaks, automatic formatting plug-in configuration, etc. The corresponding function in VSCode is setting.jsonConfiguration

Configuration method

Basic information

**User Settings **: User settings, which are the default configuration, will be associated with all projects, and have lower weight than workspace settings

Workspace Settings: Workspace settings, configured for the project, none by default, you can create it yourself under the project root path, project path /.vscode/settings.json

User setting entrance: Use the UI setting interface

Use Ctrl, (mac is cmd,) or click the gear icon in the lower left corner and select Settings . Then find settings.json

Take you step by step to understand the warehouse configuration in vscode

User settings entrance: use the command panel

Use Ctrl Shift P (cmd shift P for mac) or click the gear icon in the lower left corner to select the command panel. Then enter settings

  • Open User Settings will open the UI settings interface;
  • Open Settings (JSON) The user settings settings.json file will be opened;
Workspace settings entrance: .vscode folder

Open the folder or workspace in When ##, manually create the .vscode folder and create the settings.json file in it.

Take you step by step to understand the warehouse configuration in vscode

Workspace setting entrance: use the command panel
Use

Ctrl Shift P (mac is cmd shift P) Or click the gear icon in the lower left corner and select the command panel. Then enter settings

  • Open Workspace Settings will also open the UI settings interface;
  • Open Workspace Settings (JSON) The workspace settings settings.json file will open

Configuration content

You can click here to view all configuration items

Corresponding documents, here are common settings sharing and search related configuration ideas.

Common settings: Editor appearance
  • editor.lineNumbers: Whether to display line numbers on the left side of the editor, the default display setting is sufficient
  • editor.renderWhitespace: all: Render all whitespace characters (spaces, tabs, etc.) in dot form
  • editor.renderIndentGuides: Indent reference Line, the default setting is to connect the code block
  • editor.rulers: [120]: Vertical ruler, a vertical line will be drawn at the specified column number
  • editor.minimap.enabled: false: Whether to display the minimap on the right, I personally like to turn it off
  • editor.cursorBlinking/cursorStyle/cursorWidth: Cursor style
  • editor.renderLineHighlight: 'all': Set the current line highlight background, and the line number will also be highlighted
Common settings: writing experience

Customize whitespace and tab characters

{
	editor.detectIndentation: false, // 关闭 VS Code 的自动检测来控制制表符或者空格键的使用
	editor.tabSize: 1, // 制表符对应的空格符长度
	editor.insertSpaces: 1 // 空格符对应空白长度
}
Copy after login

Auto save

{
	editor.formatOnSave: true
}
Copy after login

Default type of new files

{
  files.defaultLanguage: 'markdown'
}
Copy after login
Search related configuration ideas

Nothing, remember the keywords, the editor is as follows; for others, emmm, see the corresponding document

  • editor cursor, is related to cursor rendering and Multi-cursor-related settings;
  • editor find, are settings related to search within the editor;
  • editor font, are settings related to fonts;
  • editor format, It is code formatting;
  • editor suggest is configuration related to automatic completion, suggestion window, etc.

Then search in the setting UI panel

Take you step by step to understand the warehouse configuration in vscode

vscode warehouse configuration tasks

The purpose of the task system is to unify various task scripts as much as possible, and then provide a simple but highly customized way to operate them

Configuring tasks

There are two sources of tasks: automatic detection of projects and custom tasks

Automatic detection of projects

VSCode will automatically read the project The configuration file is generated by the configuration file type task;

Assume that there is package.json under the project, and the content is

{
 "name": "sample",
 "scripts": {
  "test": ""
 }
}
Copy after login

When it is run, it will It was found that there are two tasks related to npm by default:

  • npm install
  • npm test

Take you step by step to understand the warehouse configuration in vscode

Customized task

First we search for "Configure Task" in the command panel and execute it.

Take you step by step to understand the warehouse configuration in vscode

We can see a drop-down box, which provides a number of different options.

Take you step by step to understand the warehouse configuration in vscode

There are two options for customizing tasks here

Generate according to the commandtask.json

If we choose the first one, which is npm: install, VS Code will immediately create a tasks.json file in the .vscode folder , its format is JSON, which is very readable and easy to modify.

{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
  {
   "type": "npm",
   "script": "install",
   "problemMatcher": []
  }
 ]
}
Copy after login

The value of the tasks attribute is an array, which is all the tasks we can use in the current folder. Next, we will explain the information of the task object in detail.

##type represents Which script tool do you want to usescriptWhich script command does the script tool executeproblemMatcherSet rules to automatically analyze task running results, as explained below

但是这种类型的任务,受限于 VS Code 或者插件所支持的脚本工具,缺乏一定的灵活性。

使用模板创建 tasks.json 文件

Take you step by step to understand the warehouse configuration in vscode

紧接着 VS Code 就问我们了,希望使用哪种模板。这里模板的多少,同样取决于你装了哪些插件。默认情况下,VS Code 为 MSBuild、Maven、.NET Core 提供了模板,而最后一个 Others,则是一个通用的模板,我们一起来看下它。

Take you step by step to understand the warehouse configuration in vscode

选择完 Others 之后,VS Code 在当前文件夹根目录下的 .vscode 文件夹中,创建了 tasks.json 文件。

Take you step by step to understand the warehouse configuration in vscode

{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
  {
   "label": "echo",
   "type": "shell",
   "command": "echo Hello",
    "group": "none",
   "presentation": {
    "reveal": "always",
    "panel": "new"
   },
   "options": {
    "cwd": "",
    "env": {},
    "shell": {
     "executable": "bash"
    }
   }
  }
 ]
}
Copy after login
AttributeMeaning
属性含义是否必填 | 默认值
label任务的名字,执行任务时选择的 keytrue
type这个类型可以有两种选择,一种是这个任务被当作进程来运行,另一种则是在 shell 中作为命令行来运行。
command代表着我们希望在 shell 中运行哪一个命令,可以联合 args 属性使用true
args数组,在运行指定 command 的时候,args 里的每个值都会被当作其参数传入,注意事项很多,见下文[]
group分组,我们可以通过这个属性指定这个任务被包含在哪一种分组当中。这涉及到运行时的类别:运行测试任务” (Run Test Task) 、“运行生成任务” (Run Build Task)
presentation用于控制任务运行的时候,是否要自动调出运行的界面
options用于控制任务执行时候的几个配置,比如控制任务脚本运行的文件夹地址 “cwd”,控制环境变量 “env”,或者控制任务脚本运行的时候使用哪个 shell 环境。
dependsOn实现多任务执行
path相对项目根路径的相对路径,运行脚本时会先切换到这下面
扩展:group 属性,运行任务的分组

task属性中,还存在分组属性group,这就需要先了解Run task了,我们在命令面板中输入Run Task,会出现如下内容

Take you step by step to understand the warehouse configuration in vscode

运行任务在上文已经讲解过了;关键是【运行开发任务】和【运行测试任务】;功能都是一样的,提供任务列表,供用户选择执行,唯一不同就是vscode加了一个分类,这样便于用户定义任务时进行区分,而这个分类就是通过group属性定义的;

group 属性值含义对应执行命令
build将这个任务划分在打包任务列表中Run Build Task
test将这个任务划分在测试任务列表中Run Test Task
none将这个任务划分在默认任务列表中Run Task

而一般我们的打包或者测试任务都是固定且唯一的,这就意味着我们可以省略掉【选择命令】这一步,一键运行。如何设置呢?

"group": {
    "isDefault": true,
    "kind": "test" // 这是 Run Test Task 的一键执行命令;如果设定 Run Build Task 则 kind 的值为 build
   }
Copy after login
扩展:执行命令时的参数

task 对象定义中有一个属性args,是一个数组,在运行指定 command 的时候,args 里的每个值都会被当作其参数传入,如

{
  "label": "echo",
  "type": "shell",
  "command": "echo 'Hello World'"
}
Copy after login

我们可以改写为

{
 "label": "echo",
 "type": "shell",
 "command": "echo",
 "args": [
  "hello world"
 ]
}
Copy after login

但对于命令而言,不同的执行 shell 对空白符、$、引号等等都可能有不同的理解,这就意味着需要对参数进行转义规则的设定,所以 args 数组也可以存储对象

"args": [
        {
            "value": "Hello World",
            "quoting": "escape"
        }
]
Copy after login
keyvalue
value参数内容
quoting决定了该如何处理这段字符串

For quoting, there are three values ​​​​

ValueMeaning
escapeDefault value, the task system will escape this string according to the requirements of the shell we use
strongIn bash, we will use single quotes to wrap this string
weakIn bash we will Use double quotes to wrap this string
举例而言

escape 下执行的脚本实际上是

echo Hello\ World
Copy after login

strong 下执行的脚本实际上是

echo 'Hello World'
Copy after login

weak 下执行的脚本实际上是

echo "Hello World"
Copy after login

上面我们是以 bash 作为 shell 进行分析的,那对于 cmd、powershell 等等呢?可以搜索 “quoting mechanism” 来查找,也可以查阅VS Code 关于 Task 参数转义部分的文档

扩展:多任务执行

实现同时运行多个任务,其实挺简单的,就是配置dependsOn属性,是个数组,存储着所有要执行的任务的label

举例,我希望执行runOrderFirst时,帮我同时启动微应用基座项目和其内部的 order 项目;

那我们可以配置如下 tasks.json,其中包含【启动基座】、【启动 order】的 task

{
            "label": "runMapp",
            "type": "npm",
            "script": "start:dev"
        },
        {
            "type": "npm",
            "script": "serve",
            "path": "apps/order/",
            "problemMatcher": [],
            "label": "runOrder",
            "detail": "启动 order"
        },
Copy after login

然后我们新增一个 task,用于聚合这两个,tasks.json内容变为如下

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "runMapp",
            "type": "npm",
            "script": "start:dev"
        },
        {
            "type": "npm",
            "script": "serve",
            "path": "apps/order/",
            "problemMatcher": [],
            "label": "runOrder",
            "detail": "启动 order"
        },
        {
            "label": "runOrderFirst",
            "dependsOn": [
             "runMapp",
             "runOrder"
            ]
        }
    ]
}
Copy after login

运行这个 task 即可,效果如下

Take you step by step to understand the warehouse configuration in vscode

这种启动项目的命令很常用,每次还得选下命令挺麻烦,我们可以利用分组的功能,将之设置为测试任务并默认,这样Run Test Task就可以直接执行了

 {
            "label": "runOrderFirst",
            "dependsOn": [
             "runMapp",
             "runOrder"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
               }
        }
Copy after login

效果如下

Take you step by step to understand the warehouse configuration in vscode

运行任务

在控制面板中使用命令Run Task;然后选择对应的命令即可;

比如选择“echo”这个任务(这个就是我们在 label 里写的名字),按下回车后,VS Code 会问我们 “选择根据何种错误和警告扫描任务输出”,现在就选择第一个选项 “继续而不扫描任务输出” 好了。

Take you step by step to understand the warehouse configuration in vscode

例子

唤起 Chrome 浏览器,我们先实现在 mac 中唤起,再考虑通用。

首先:定义 task
{
 "version": "2.0.0",
 "tasks": [
  {
   "label": "chrome",
   "type": "process",
   "command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  }
 ]
}
Copy after login
其次:运行 task

我们运行看看它的效果,在控制面板中使用命令Run Task,然后选择chrome

1Take you step by step to understand the warehouse configuration in vscode

最后:考虑平台不同

如果使用的系统是 Windows 或者 Linux,那么这个任务就没法使用了,因为 Chrome 的地址完全对不上号。

所以我们可以修改task.json,为系统定制命令。

{
 "version": "2.0.0",
 "tasks": [
  {
   "label": "chrome",
   "type": "process",
   "command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
   "windows": {
    "command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
      },
   "linux": {
          "command": "/usr/bin/google-chrome"
      }
  }
 ]
}
Copy after login

vscode 仓库配置之调试设置(launch)

调试,是最必不可缺的一环功能,对于大多数的 IDE 而言,因为使用对象确定而使用极其方便,比如 IDEA 之于 JAVA,甚至谷歌浏览器之于前端;而对于 vscode 这种区别于 IDE 的编辑器而言,这需要考虑更大的灵活性,这就需要配置文件实现了。

常规使用

考虑新手友好,vscode 会存在默认设置,即开箱即用的调试功能;以nodejs为例,分两步:设置断点,调试。

设置断点

有两个方案,可以在文件中输入关键词【debugger】;也可以在文件的左侧可以点上红点,效果一致;

调试

可以点击左侧的 debugger 按钮【一只甲壳虫图标】,也可以使用快捷键【cmd + shift + D】。然后选择要调试的程序类型,这时默认会对当前打开文件进行调试处理。

Take you step by step to understand the warehouse configuration in vscode

高阶使用:调试配置 launch.json

那如果需求不止单文件,而是对一个项目进行调试呢?或者对项目内的指定文件,这就需要launch.json文件了,同任务功能,这个文件也是在.vscode下。

如何创建

点击左侧的 debugger 按钮【一只甲壳虫图标】,或者使用快捷键【cmd + shift + D】唤起 debugger 面板后,存在创建入口点击,然后选中类型即会自动创建。

Take you step by step to understand the warehouse configuration in vscode

{
 // 使用 IntelliSense 了解相关属性。 
 // 悬停以查看现有属性的描述。
 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
 "version": "0.2.0",
 "configurations": [
  {
   "type": "node",
   "request": "launch",
   "name": "启动程序",
   "program": "${file}"
  }
 ]
}
Copy after login
属性 作用 备注
type 代表着调试器的类型
request 代表着该如何启动调试器 [ launch | attach]  [ 直接启动代码并且调试 | 调试这个已有的代码进程]
name 就是这个配置的名字
program 告诉 Node.js 调试器,我们想要调试哪个文件 这个值支持预定义参数,如file{file}、{workspaceFolder}
如何书写

对于书写launch.json文件,我们有两个工具可以使用

  • 借助 VS Code 的调试器插件提供的模板

在创建launch.json时,会要求选择类型,从而创建对应的模板

  • 自动补全功能

另一个能够帮助到我们的,就是在书写配置属性的时候使用自动补全功能。当我们在书写新的属性时,按下 Ctrl + Space,就能够唤出建议列表,建议列表里提供了当前调试配置里可以使用的所有属性,然后我们就可以按需选用了

1Take you step by step to understand the warehouse configuration in vscode

尾声

        到此,仓库配置相关的分享就结束啦;这节的信息很多,而且因为编辑器的默认设置肯定是符合大部分场景需求的,所以我们大多数人很有可能从未接触过这些概念,但努力就是为了成为更好的自己嘛。

        举个场景吧,不白撒鸡血,前些日子开一个项目,希望接入保存自动格式化,也就是接入eslint+prettier,多数人入职时候这些就已经在项目里配置好了,我也一样,所以一开始有点懵,但还是想尝试,最后发现其实就是配置.vscode文件中setting.json文件,有兴趣的同学可以参考文末【关于实现项目代码风格统一的参考贴】,人家写的很好,我就不重写一篇了。

Take you step by step to understand the warehouse configuration in vscode

相关资料

关于实现项目代码风格统一的参考贴

  • VSCode 合理配置 ESLint+Prettier:https://juejin.cn/post/6899323798676307976
  • eslint 官网相关规则:http://eslint.cn/docs/rules/space-before-function-paren
  • 关于 prettier 配置的一些问题:https://www.jianshu.com/p/99e0c58f3ebf
  • vscode 自动保存格式化 prettier 和 eslint 规范冲突问题解决:https://www.itcan.cn/2020/09/12/vscode-prettier-eslint-format/

在配置文件里可以使用的预定义参数

  • Visual Studio Code Variables Reference

    https://code.visualstudio.com/docs/editor/variables-reference

学习文档

  • 戳此查看 VS Code 官方博客

    https://code.visualstudio.com/blogs

  • 戳此查看 VS Code 更新日志

    https://code.visualstudio.com/updates

  • 戳此查看 Erich Gamma 在 Goto Conference 上对 VS Code 的介绍

    https://www.youtube.com/watch?v=uLrnQtAq5Ec

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

The above is the detailed content of Take you step by step to understand the warehouse configuration in vscode. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!