VS Code Extension
Overview
Capabilities:
look - theming
custom components & views - extending workbench
webview
language extensions
debugger extension
API Reference:
Anatomy
Manifest:
package.json (https://code.visualstudio.com/api/references/extension-manifest )
most important fields:
name and publisher: VS Code uses <publisher>.<name> as a unique ID for the extension. For example, the Hello World sample has the ID vscode-samples.helloworld-sample. VS Code uses the ID to uniquely identify your extension
main: The extension entry point.
activationEvents and contributes: Activation Events and Contribution Points.
engines.vscode: This specifies the minimum version of VS Code API that the extension depends on.
Extension Entry:
provides two functions:
activate
deactivate
VS Code UI
https://code.visualstudio.com/api/references/extension-guidelines
Activation Event
JSON declarations:
where: "activationEvents" of package.json
Example:
"activationEvents": [
"onCommand:helloworld.helloWorld" // a command in contributions.commands
],
Contribution Point
JSON declarations:
where: "contributes" field in package.json
Commands:
shows:
Command Palette (show category, no icon, no disabled command)
editor context menu (show disabled items, no category)
emit activationEvent "onCommand: ${command}"
Consists of: id, title, icon, category, enablement (when clause https://code.visualstudio.com/api/references/when-clause-contexts )
Configuration & configurationDefaults:
expose configuration keys -
able to set in user settings / workspace settings
use settings UI or JSON
configurationDefaults - contributes default editor configurations to language
Custom Editors
Menus
Views
VS Code API
API Patterns:
Cancellation tokens - for operations on volatile state that may change before finish, hence can check and get noticed when cancellation occurs
Disposables - (like Java try-with-resources syntax)
Events - onVerbNoun pattern methods
workspace.findFiles:
glob:
when using absolute glob (string), return Url results from all workspace folders
use workspace.getWorkspaceFolder(url) to obtain the workspace folder {uri, name,index}
How to: command
define command:
in "contributes" "commands"
give it at least ID and title
register activation on command
in "activationEvents"
"onCommand:" + ID
in main, provide activate function
export function activate(context: vscode.ExtensionContext) {
use API vscode.commands.registerCommand to register handler, return a "disposable"
add the disposable to "context.subscriptions" so when extension is deactivated, disposable can be disposed