|
|
|
|
|
# DO NOT TOUCH — Managed by doc writer |
|
|
ContentId: 8CEBCDF8-4F0A-4C81-A904-3DEA43480EA6 |
|
|
DateApproved: 3/7/2019 |
|
|
|
|
|
VSCodeCommitHash: 2380981c97a07816a3d1c10edf1fb678463dc215 |
|
|
VSCodeVersion: 1.32.0 |
|
|
|
|
|
# Summarize the whole topic in less than 300 characters for SEO purpose |
|
|
MetaDescription: Visual Studio Code extensions (plug-in) API Reference. |
|
|
|
|
|
|
|
|
# VS Code API |
|
|
|
|
|
**VS Code API** is a set of JavaScript APIs that you can invoke in your Visual Studio Code extension. This page lists all VS Code APIs available to extension authors. |
|
|
|
|
|
## API Patterns |
|
|
|
|
|
These are some of the common patterns we use in the VS Code API. |
|
|
|
|
|
### Promises |
|
|
|
|
|
The VS Code API represents asynchronous operations with [promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise). From extensions, __any__ type of promise can be returned, like ES6, WinJS, A+, etc. |
|
|
|
|
|
Being independent of a specific promise library is expressed in the API by the `Thenable`-type. `Thenable` represents the common denominator which is the [then](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) method. |
|
|
|
|
|
In most cases the use of promises is optional and when VS Code calls into an extension, it can handle the _result type_ as well as a `Thenable` of the _result type_. When the use of a promise is optional, the API indicates this by returning `or`-types. |
|
|
|
|
|
```typescript |
|
|
provideNumber(): number | Thenable<number> |
|
|
``` |
|
|
|
|
|
### Cancellation Tokens |
|
|
|
|
|
Often operations are started on volatile state which changes before operations can finish. For instance, computing IntelliSense starts and the user continues to type making the result of that operation obsolete. |
|
|
|
|
|
APIs that are exposed to such behavior will get passed a `CancellationToken` on which you can check for cancellation (`isCancellationRequested`) or get notified when cancellation occurs (`onCancellationRequested`). The cancellation token is usually the last parameter of a function call and optional. |
|
|
|
|
|
### Disposables |
|
|
|
|
|
The VS Code API uses the [dispose pattern](https://en.wikipedia.org/wiki/Dispose_pattern) for resources that are obtained from VS Code. This applies to event listening, commands, interacting with the UI, and various language contributions. |
|
|
|
|
|
For instance, the `setStatusBarMessage(value: string)` function returns a `Disposable` which upon calling `dispose` removes the message again. |
|
|
|
|
|
### Events |
|
|
|
|
|
Events in the VS Code API are exposed as functions which you call with a listener-function to subscribe. Calls to subscribe return a `Disposable` which removes the event listener upon dispose. |
|
|
|
|
|
```javascript |
|
|
var listener = function(event) { |
|
|
console.log("It happened", event); |
|
|
}; |
|
|
|
|
|
// start listening |
|
|
var subscription = fsWatcher.onDidDelete(listener); |
|
|
|
|
|
// do more stuff |
|
|
|
|
|
subscription.dispose(); // stop listening |
|
|
``` |
|
|
|
|
|
Names of events follow the `on[Will|Did]VerbNoun?` pattern. The name signals if the event is going to happen *(onWill)* or already happened *(onDid)*, what happened *(verb)*, and the context *(noun)* unless obvious from the context. |
|
|
|
|
|
An example from the VS Code API is `window.onDidChangeActiveTextEditor` which is an event fired when the active text editor *(noun)* has been (*onDid*) changed (*verb*). |
|
|
|
|
|
### Strict null |
|
|
|
|
|
The VS Code API uses the `undefined` and `null` TypeScript types where appropriate to support [strict null checking](https://github.com/Microsoft/TypeScript/pull/7140). |
|
|
|
|
|
## API Listing |
|
|
|
|
|
This listing is compiled from the [`vscode.d.ts`](https://github.com/Microsoft/vscode/blob/master/src/vs/vscode.d.ts) file from the VS Code repository. |
|
|
|
|
|
{$ Content $} |