- 编辑器
- Visual Studio Code 基础
- 安装Visual Studio Code
- VS Code 扩展市场 VS Code Extension Marketplace
- 通过任务集成外部工具 Integrate with External Tools via Tasks
- 调试
- Why Visual Studio Code? - 为什么选用VSCode
- 版本控制(Version Control)
- Accessibility 易用性
- Editing Evolved - 与时俱进的编辑体验
- 定制化
- 定制化Visual Studio Code(Customize Visual Studio Code)
- 用户和工作区设置(User and Workspace Settings)
- VS Code 的快捷按键(Key Bindings for Visual Studio Code)
- 向 VSC 添加代码段(Adding Snippets to Visual Studio Code)
- Color Themes - 颜色主题
- Display Language - 语言区域
- 工具
- vsce - Publishing Tool Reference
- Yo Code - Extension Generator
- VS Code Extension Samples
- 技术支持
- Visual Studio Code FAQ
- Common Error Cases
- 如何升级到最新版 How to update to the latest release
- Requirements for Visual Studio Code
- 扩展
- Visual Studio Code 扩展 Extending Visual Studio Code
- 示例 - Hello World Example - Hello World
- 示例 - 单词数统计 Example - Word Count
- 示例 - 语言服务 Example - Language Server
- 示例 - 调试器 Example - Debuggers
- Running and Debugging Your Extension
- 安装扩展
- Extensibility Principles and Patterns
- Testing Your Extension
- Our Approach to Extensibility
- 扩展API
- 可扩展性参考
- 扩展清单文件 - package.json
- Contribution Points - package.json
- Activation Events - package.json
- vscode namespace API
- Complex Commands API
- Debugging API
- 语言
- 语言 Languages
- JavaScript
- 用C#进行工作 Working with C#
- C/C++ for VS Code (预览)(Preview)
- JSON
- VS Code对HTML的相关 HTML Programming in VS Code
- VS Code中的PHP编程 PHP Programming in VS Code
- VS Code 对 Python 的支持
- Markdown and VS Code - Markdown与VS Code
- Editing TypeScript
- CSS, Sass and Less
- 使用Docker工作
- 运行时
- Node.js Applications with VS Code
- ASP.NET Core with VS Code
- Unity Development with VS Code
- Office Add-ins with VS Code
- 开始(Getting Started)
Running and Debugging Your Extension
You can use VS Code to develop an extension for VS Code and VS Code provides several tools that simplify extension development:
- Yeoman generators to scaffold an extension
- IntelliSense, hover, and code navigation for the extension API
- Compiling TypeScript (when implementing an extension in TypeScript)
- Running and debugging an extension
- Publishing an extension
Creating an Extension
We suggest you start your extension by scaffolding out the basic files. You can use the yo code
Yeoman generator to do this and we cover the details in the Yo Code document. The generator will ensure everything is set up so you have a great development experience.
Running and Debugging your Extension
You can easily run your extension under the debugger by pressing F5
. This opens a new VS Code window with your extension loaded. Output from your extension shows up in the Debug Console
. You can set break points, step through your code, and inspect variables either in the Debug
view or the Debug Console
.
Let's peek at what is going on behind the scenes. If you are writing your extension in TypeScript then your code must first be compiled to JavaScript.
Compiling TypeScript
The TypeScript compilation is setup as follows in the generated extension:
- A
tsconfig.json
defines the compile options for the TypeScript compiler. Read more about it at the TypeScript wiki or in our TypeScript Language Section. - A TypeScript compiler with the proper version is included inside the node_modules folder.
- A
typings/vscode-typings.d.ts
: instructs the TypeScript compiler to include thevscode
API definition. - The API definition is included in
node_modules/vscode
.
The TypeScript compilation is triggered before running your extension. This is done with the preLaunchTask
attribute defined in the .vscode/launch.json
file which declares a task to be executed before starting the debugging session. The task is defined inside the .vscode/tasks.json
file.
Note: The TypeScript compiler is started in watch mode, so that it compiles the files as you make changes.
Launching your Extension
Your extension is launched in a new window with the title Extension Development Host
. This window runs VS Code or more precisely the Extension Host
with your extension under development.
You can accomplish the same from the command line using the extensionDevelopmentPath
option. This options tells VS Code in what other locations it should look for extensions, e.g.,
code --extensionDevelopmentPath=_my_extension_folder
.
Once the Extension Host is launched, VS Code attaches the debugger to it and starts the debug session.
This is what happens when pressing F5
:
.vscode/launch.json
instructs to first run a task namednpm
..vscode/tasks.json
defines the tasknpm
as a shell command tonpm run compile
.package.json
defines the scriptcompile
asnode ./node_modules/vscode/bin/compile -watch -p ./
- This eventually invokes the TypeScript compiler included in node_modules, which generates
out/src/extension.js
andout/src/extension.js.map
. - Once the TypeScript compilation task is finished, the
code --extensionDevelopmentPath=${workspaceRoot}
process is spawned. - The second instance of VS Code is launched in a special mode and it searches for an extension at
${workspaceRoot}
.
Changing and Reloading your Extension
Since the TypeScript compiler is run in watch mode, the TypeScript files are automatically compiled as you make changes. You can observe the compilation progress on the left side of the VS Code status bar. On the status bar you can also see the error and warning counts of a compilation. When the compilation is complete with no errors, you must reload the Extension Development Host
so that it picks up your changes. You have two options to do this:
- Click on the debug restart action to relaunch the Extension Development Host window.
- Press
kbstyle(Ctrl+R)
(Mac:kbstyle(Cmd+R)
) in the Extension Development Host window.
Next Steps
- Testing your Extension - Learn how to write unit and integration tests for your extension
- Publishing Tool - Publish your extension with the vsce command line tool.
- Extension Manifest file - VS Code extension manifest file reference
- Extension API - Learn about the VS Code extensibility APIs
Common Questions
Q: How can I use API from my extension that was introduced in a newer release of VS Code?
A: If your extension is using an API that was introduced in a newer release of VS Code, you have to declare this dependency in the engines
field of the package.json
file of the extension.
Here are the steps:
- Set the minimal version of VS Code that your extension requires in the
engine
field of thepackage.json
. - Make sure your devDependency for the
vscode
module is at least0.11.0
. - Add a
postinstall
script to yourpackage.json
like this:
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install"
}
- Type
npm install
from the root of your extension. - The
vscode
module will download the appropriate version ofvscode.d.ts
based on theengine
field you declared. - Go back to VS Code and see how the API for the specific version you chose appears in IntelliSense and validation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论