← 返回首页
Programmatic Language Features | Visual Studio Code Extension API

Extension API

Topics Overview Your First Extension Extension Anatomy Wrapping Up Overview Common Capabilities Theming Extending Workbench Overview AI Extensibility Language Model Tool MCP Dev Guide Chat Participant Chat Tutorial Language Model Language Model Tutorial Language Model Chat Provider Prompt TSX Command Color Theme File Icon Theme Product Icon Theme Tree View Webview Notebook Custom Editors Virtual Documents Virtual Workspaces Web Extensions Workspace Trust Task Provider Source Control Debugger Extension Markdown Extension Test Extension Custom Data Extension Telemetry Overview Activity Bar Sidebars Panel Status Bar Views Editor Actions Quick Picks Command Palette Notifications Webviews Context Menus Walkthroughs Settings Overview Syntax Highlight Guide Semantic Highlight Guide Snippet Guide Language Configuration Guide Programmatic Language Features Language Server Extension Guide Embedded Languages Testing Extensions Publishing Extensions Bundling Extensions Continuous Integration Extension Host Remote Development and Codespaces Using Proposed API Migrate from TSLint to ESLint Python Extension Template VS Code API Contribution Points Activation Events Extension Manifest Built-In Commands When Clause Contexts Theme Color Product Icon Reference Document Selector

Programmatic Language Features

Programmatic Language Features is a set of smart-editing features powered by the vscode.languages.* API. There are two common ways to provide a dynamic language feature in Visual Studio Code. Let's take Hover as an example:

let diagnosticCollection: vscode.DiagnosticCollection; export function activate(ctx: vscode.ExtensionContext): void { ... ctx.subscriptions.push(getDisposable()); diagnosticCollection = vscode.languages.createDiagnosticCollection('go'); ctx.subscriptions.push(diagnosticCollection); ... } function onChange() { let uri = document.uri; check(uri.fsPath, goConfig).then(errors => { diagnosticCollection.clear(); let diagnosticMap: Map<string, vscode.Diagnostic[]> = new Map(); errors.forEach(error => { let canonicalFile = vscode.Uri.file(error.file).toString(); let range = new vscode.Range(error.line-1, error.startColumn, error.line-1, error.endColumn); let diagnostics = diagnosticMap.get(canonicalFile); if (!diagnostics) { diagnostics = []; } diagnostics.push(new vscode.Diagnostic(range, error.msg, error.severity)); diagnosticMap.set(canonicalFile, diagnostics); }); diagnosticMap.forEach((diags, file) => { diagnosticCollection.set(vscode.Uri.parse(file), diags); }); }) }

Basic

Report diagnostics for open editors. Minimally, this needs to happen on every save. Better, diagnostics should be computed based on the un-saved contents of the editor.

Advanced

Report diagnostics not only for the open editors but for all resources in the open folder, no matter whether they have ever been opened in an editor or not.

Show Code Completion Proposals

Code completions provide context sensitive suggestions to the user.

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides completions and whether or not it supports the completionItem\resolve method to provide additional information for the computed completion items.

class GoCompletionItemProvider implements vscode.CompletionItemProvider { public provideCompletionItems( document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> { ... } } export function activate(ctx: vscode.ExtensionContext): void { ... ctx.subscriptions.push(getDisposable()); ctx.subscriptions.push( vscode.languages.registerCompletionItemProvider( GO_MODE, new GoCompletionItemProvider(), '.', '\"')); ... }

Basic

You don't support resolve providers.

Advanced

You support resolve providers that compute additional information for completion proposal the user selects. This information is displayed along-side the selected item.

Show Inline Completions

Inline completions present multi-token suggestions directly in the editor (ghost text).

Direct Implementation

{ ... "capabilities" : { "hoverProvider" : "true", ... } }

In addition, your language server needs to respond to the textDocument/hover request.

Direct Implementation

{ ... "capabilities" : { "signatureHelpProvider" : { "triggerCharacters": [ '(' ] } ... } }

In addition, your language server needs to respond to the textDocument/signatureHelp request.

Direct Implementation

{ ... "capabilities" : { "definitionProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/definition request.

Direct Implementation

{ ... "capabilities" : { "referencesProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/references request.

Direct Implementation

{ ... "capabilities" : { "documentHighlightProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/documentHighlight request.

Direct Implementation

{ ... "capabilities" : { "documentSymbolProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/documentSymbol request.

Direct Implementation

{ ... "capabilities" : { "workspaceSymbolProvider" : "true" ... } }

In addition, your language server needs to respond to the workspace/symbol request.

Direct Implementation

{ ... "capabilities" : { "codeActionProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/codeAction request.

Direct Implementation

{ ... "capabilities" : { "codeLensProvider" : { "resolveProvider": "true" } ... } }

In addition, your language server needs to respond to the textDocument/codeLens request.

Direct Implementation

{ ... "capabilities" : { "colorProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/documentColor and textDocument/colorPresentation requests.

Direct Implementation

{ ... "capabilities" : { "documentFormattingProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/formatting request.

Direct Implementation

{ ... "capabilities" : { "documentRangeFormattingProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/rangeFormatting request.

Direct Implementation

{ ... "capabilities" : { "documentOnTypeFormattingProvider" : { "firstTriggerCharacter": "}", "moreTriggerCharacter": [";", ","] } ... } }

In addition, your language server needs to respond to the textDocument/onTypeFormatting request.

Direct Implementation

{ ... "capabilities" : { "renameProvider" : "true" ... } }

In addition, your language server needs to respond to the textDocument/rename request.

Direct Implementation