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:

  1. vscode.languages.registerHoverProvider('javascript', {
  2. provideHover(document, position, token) {
  3. return {
  4. contents: ['Hover Content']
  5. };
  6. }
  7. });

As you see above, the vscode.languages.registerHoverProvider API provides an easy way to provide hover contents to JavaScript files. After this extension gets activated, whenever you hover over some JavaScript code, VS Code queries all HoverProvider for JavaScript and shows the result in a Hover widget. The Language Feature Listing and illustrated gif below provides an easy way for you to locate which VS Code API / LSP Method your extension needs.

An alternative approach is to implement a Language Server that speaks Language Server Protocol. The way it works is:

  • An extension provides a Language Client and a Language Server for JavaScript.
  • The Language Client is like any other VS Code extension, running in the Node.js Extension Host context. When it gets activated, it spawns the Language Server in another process and communicates with it through Language Server Protocol.
  • You hover over JavaScript code in VS Code
  • VS Code informs the Language Client of the hover
  • The Language Client queries the Language Server for a hover result and sends it back to VS Code
  • VS Code displays the hover result in a Hover widget

The process seems more complicated, but it provides two major benefits:

  • The Language Server can be written in any language
  • The Language Server can be reused to provide smart editing features for multiple editors

For a more in-depth guide, head over to the Language Server Extension Guide.


Language Features Listing

This listing includes the following items for each language feature:

  • An illustration of the language feature in VS Code
  • Related VS Code API
  • Related LSP methods
VS Code APILSP method
createDiagnosticCollectionPublishDiagnostics
registerCompletionItemProviderCompletion & Completion Resolve
registerHoverProviderHover
registerSignatureHelpProviderSignatureHelp
registerDefinitionProviderDefinition
registerTypeDefinitionProviderTypeDefinition
registerImplementationProviderImplementation
registerReferenceProviderReferences
registerDocumentHighlightProviderDocumentHighlight
registerDocumentSymbolProviderDocumentSymbol
registerCodeActionsProviderCodeAction
registerCodeLensProviderCodeLens & CodeLens Resolve
registerDocumentLinkProviderDocumentLink & DocumentLink Resolve
registerColorProviderDocumentColor & Color Presentation
registerDocumentFormattingEditProviderFormatting
registerDocumentRangeFormattingEditProviderRangeFormatting
registerOnTypeFormattingEditProviderOnTypeFormatting
registerRenameProviderRename & Prepare Rename
registerFoldingRangeProviderFoldingRange

Provide Diagnostics

Diagnostics are a way to indicate issues with the code.

Diagnostics indicating a misspelled method name

Language Server Protocol

Your language server sends the textDocument/publishDiagnostics message to the language client. The message carries an array of diagnostic items for a resource URI.

Note: The client does not ask the server for diagnostics. The server pushes the diagnostic information to the client.

Direct Implementation

  1. let diagnosticCollection: vscode.DiagnosticCollection;
  2. export function activate(ctx: vscode.ExtensionContext): void {
  3. ...
  4. ctx.subscriptions.push(getDisposable());
  5. diagnosticCollection = vscode.languages.createDiagnosticCollection('go');
  6. ctx.subscriptions.push(diagnosticCollection);
  7. ...
  8. }
  9. function onChange() {
  10. let uri = document.uri;
  11. check(uri.fsPath, goConfig).then(errors => {
  12. diagnosticCollection.clear();
  13. let diagnosticMap: Map<string, vscode.Diagnostic[]> = new Map();
  14. errors.forEach(error => {
  15. let canonicalFile = vscode.Uri.file(error.file).toString();
  16. let range = new vscode.Range(error.line-1, error.startColumn, error.line-1, error.endColumn);
  17. let diagnostics = diagnosticMap.get(canonicalFile);
  18. if (!diagnostics) { diagnostics = []; }
  19. diagnostics.push(new vscode.Diagnostic(range, error.msg, error.severity));
  20. diagnosticMap.set(canonicalFile, diagnostics);
  21. });
  22. diagnosticMap.forEach((diags, file) => {
  23. diagnosticCollection.set(vscode.Uri.parse(file), diags);
  24. });
  25. })
  26. }

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.

Code Completion prompting variable, method, and parameter names while writing code

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.

  1. {
  2. ...
  3. "capabilities" : {
  4. "completionProvider" : {
  5. "resolveProvider": "true",
  6. "triggerCharacters": [ '.' ]
  7. }
  8. ...
  9. }
  10. }

Direct Implementation

  1. class GoCompletionItemProvider implements vscode.CompletionItemProvider {
  2. public provideCompletionItems(
  3. document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken):
  4. Thenable<vscode.CompletionItem[]> {
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(getDisposable());
  11. ctx.subscriptions.push(
  12. vscode.languages.registerCompletionItemProvider(
  13. GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
  14. ...
  15. }

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 Hovers

Hovers show information about the symbol/object that’s below the mouse cursor. This is usually the type of the symbol and a description.

Showing details about a workspace and a method when hovering over them

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides hovers.

  1. {
  2. ...
  3. "capabilities" : {
  4. "hoverProvider" : "true",
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoHoverProvider implements HoverProvider {
  2. public provideHover(
  3. document: TextDocument, position: Position, token: CancellationToken):
  4. Thenable<Hover> {
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(
  11. vscode.languages.registerHoverProvider(
  12. GO_MODE, new GoHoverProvider()));
  13. ...
  14. }

Basic

Show type information and include documentation if available.

Advanced

Colorize method signatures in the same style you colorize the code.

Help With Function and Method Signatures

When the user enters a function or method, display information about the function/method that is being called.

Showing information about the getPackageInfo method including the necessary parameters

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides signature help.

  1. {
  2. ...
  3. "capabilities" : {
  4. "signatureHelpProvider" : {
  5. "triggerCharacters": [ '(' ]
  6. }
  7. ...
  8. }
  9. }

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

Direct Implementation

  1. class GoSignatureHelpProvider implements SignatureHelpProvider {
  2. public provideSignatureHelp(
  3. document: TextDocument, position: Position, token: CancellationToken):
  4. Promise<SignatureHelp> {
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(
  11. vscode.languages.registerSignatureHelpProvider(
  12. GO_MODE, new GoSignatureHelpProvider(), '(', ','));
  13. ...
  14. }

Basic

Ensure that the signature help contains the documentation of the parameters of the function or method.

Advanced

Nothing additional.

Show Definitions of a Symbol

Allow the user to see the definition of variables/functions/methods right where the variables/functions/methods are being used.

Right click a variable, function, or method and select "Go to Definition" to jump to the definition

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides goto-definition locations.

  1. {
  2. ...
  3. "capabilities" : {
  4. "definitionProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoDefinitionProvider implements vscode.DefinitionProvider {
  2. public provideDefinition(
  3. document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken):
  4. Thenable<vscode.Location> {
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(
  11. vscode.languages.registerDefinitionProvider(
  12. GO_MODE, new GoDefinitionProvider()));
  13. ...
  14. }

Basic

If a symbol is ambivalent, you can show multiple definitions.

Advanced

Nothing additional.

Find All References to a Symbol

Allow the user to see all the source code locations where a certain variable/function/method/symbol is being used.

Right clicking and selecting "Find All References" to highlight all the locations where that symbol is used

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides symbol reference locations.

  1. {
  2. ...
  3. "capabilities" : {
  4. "referencesProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoReferenceProvider implements vscode.ReferenceProvider {
  2. public provideReferences(
  3. document: vscode.TextDocument, position: vscode.Position,
  4. options: { includeDeclaration: boolean }, token: vscode.CancellationToken):
  5. Thenable<vscode.Location[]> {
  6. ...
  7. }
  8. }
  9. export function activate(ctx: vscode.ExtensionContext): void {
  10. ...
  11. ctx.subscriptions.push(
  12. vscode.languages.registerReferenceProvider(
  13. GO_MODE, new GoReferenceProvider()));
  14. ...
  15. }

Basic

Return the location (resource URI and range) for all references.

Advanced

Nothing additional.

Highlight All Occurrences of a Symbol in a Document

Allow the user to see all occurrences of a symbol in the open editor.

Select a symbol to highlight all occurences

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides symbol document locations.

  1. {
  2. ...
  3. "capabilities" : {
  4. "documentHighlightProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoDocumentHighlightProvider implements vscode.DocumentHighlightProvider {
  2. public provideDocumentHighlights(
  3. document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken):
  4. vscode.DocumentHighlight[] | Thenable<vscode.DocumentHighlight[]>;
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(
  11. vscode.languages.registerDocumentHighlightProvider(
  12. GO_MODE, new GoDocumentHighlightProvider()));
  13. ...
  14. }

Basic

You return the ranges in the editor’s document where the references are being found.

Advanced

Nothing additional.

Show all Symbol Definitions Within a Document

Allow the user to quickly navigate to any symbol definition in the open editor.

Navigate to a symbol definition in the open editor using @

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides symbol document locations.

  1. {
  2. ...
  3. "capabilities" : {
  4. "documentSymbolProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
  2. public provideDocumentSymbols(
  3. document: vscode.TextDocument, token: vscode.CancellationToken):
  4. Thenable<vscode.SymbolInformation[]> {
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(
  11. vscode.languages.registerDocumentSymbolProvider(
  12. GO_MODE, new GoDocumentSymbolProvider()));
  13. ...
  14. }

Basic

Return all symbols in the document. Define the kinds of symbols such as variables, functions, classes, methods, etc.

Advanced

Nothing additional.

Show all Symbol Definitions in Folder

Allow the user to quickly navigate to symbol definitions anywhere in the folder (workspace) opened in VS Code.

Navigate to symbol definitions in the workspace using #

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides global symbol locations.

  1. {
  2. ...
  3. "capabilities" : {
  4. "workspaceSymbolProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider {
  2. public provideWorkspaceSymbols(
  3. query: string, token: vscode.CancellationToken):
  4. Thenable<vscode.SymbolInformation[]> {
  5. ...
  6. }
  7. }
  8. export function activate(ctx: vscode.ExtensionContext): void {
  9. ...
  10. ctx.subscriptions.push(
  11. vscode.languages.registerWorkspaceSymbolProvider(
  12. new GoWorkspaceSymbolProvider()));
  13. ...
  14. }

Basic

Return all symbols define by the source code within the open folder. Define the kinds of symbols such as variables, functions, classes, methods, etc.

Advanced

Nothing additional.

Possible Actions on Errors or Warnings

Provide the user with possible corrective actions right next to an error or warning. If actions are available, a light bulb appears next to the error or warning. When the user clicks the light bulb, a list of available Code Actions is presented.

Selecting a light bulb to view a list of available Code Actions

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides Code Actions.

  1. {
  2. ...
  3. "capabilities" : {
  4. "codeActionProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoCodeActionProvider implements vscode.CodeActionProvider {
  2. public provideCodeActions(
  3. document: vscode.TextDocument, range: vscode.Range,
  4. context: vscode.CodeActionContext, token: vscode.CancellationToken):
  5. Thenable<vscode.Command[]> {
  6. ...
  7. }
  8. }
  9. export function activate(ctx: vscode.ExtensionContext): void {
  10. ...
  11. ctx.subscriptions.push(
  12. vscode.languages.registerCodeActionsProvider(
  13. GO_MODE, new GoCodeActionProvider()));
  14. ...
  15. }

Basic

Provide Code Actions for error/warning correcting actions.

Advanced

In addition, provide source code manipulation actions such as refactoring. For example, Extract Method.

CodeLens - Show Actionable Context Information Within Source Code

Provide the user with actionable, contextual information that is displayed interspersed with the source code.

CodeLens providing context

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides CodeLens results and whether it supports the codeLens\resolve method to bind the CodeLens to its command.

  1. {
  2. ...
  3. "capabilities" : {
  4. "codeLensProvider" : {
  5. "resolveProvider": "true"
  6. }
  7. ...
  8. }
  9. }

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

Direct Implementation

  1. class GoCodeLensProvider implements vscode.CodeLensProvider {
  2. public provideCodeLenses(document: TextDocument, token: CancellationToken):
  3. CodeLens[] | Thenable<CodeLens[]> {
  4. ...
  5. }
  6. public resolveCodeLens?(codeLens: CodeLens, token: CancellationToken):
  7. CodeLens | Thenable<CodeLens> {
  8. ...
  9. }
  10. }
  11. export function activate(ctx: vscode.ExtensionContext): void {
  12. ...
  13. ctx.subscriptions.push(
  14. vscode.languages.registerCodeLensProvider(
  15. GO_MODE, new GoCodeLensProvider()));
  16. ...
  17. }

Basic

Define the CodeLens results that are available for a document.

Advanced

Bind the CodeLens results to a command by responding to codeLens/resolve.

Show Color Decorators

Allow the user to preview and modify colors in the document.

Showing the color picker

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides color information.

  1. {
  2. ...
  3. "capabilities" : {
  4. "colorProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoColorProvider implements vscode.DocumentColorProvider {
  2. public provideDocumentColors(
  3. document: vscode.TextDocument, token: vscode.CancellationToken):
  4. Thenable<vscode.ColorInformation[]> {
  5. ...
  6. }
  7. public provideColorPresentations(
  8. color: Color, context: { document: TextDocument, range: Range }, token: vscode.CancellationToken):
  9. Thenable<vscode.ColorPresentation[]> {
  10. ...
  11. }
  12. }
  13. export function activate(ctx: vscode.ExtensionContext): void {
  14. ...
  15. ctx.subscriptions.push(
  16. vscode.languages.registerColorProvider(
  17. GO_MODE, new GoColorProvider()));
  18. ...
  19. }

Basic

Return all color references in the document. Provide color presentations for the color formats supported (for example rgb(…), hsl(…)).

Advanced

Nothing additional.

Format Source Code in an Editor

Provide the user with support for formatting whole documents.

Right click and select format code

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides document formatting.

  1. {
  2. ...
  3. "capabilities" : {
  4. "documentFormattingProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoDocumentFormatter implements vscode.DocumentFormattingEditProvider {
  2. public formatDocument(document: vscode.TextDocument):
  3. Thenable<vscode.TextEdit[]> {
  4. ...
  5. }
  6. }
  7. export function activate(ctx: vscode.ExtensionContext): void {
  8. ...
  9. ctx.subscriptions.push(
  10. vscode.languages.registerDocumentFormattingEditProvider(
  11. GO_MODE, new GoDocumentFormatter()));
  12. ...
  13. }

Basic

Don’t provide formatting support.

Advanced

You should always return the smallest possible text edits that result in the source code being formatted. This is crucial to ensure that markers such as diagnostic results are adjusted correctly and are not lost.

Format the Selected Lines in an Editor

Provide the user with support for formatting a selected range of lines in a document.

Select lines, right click, and select format code

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides formatting support for ranges of lines.

  1. {
  2. ...
  3. "capabilities" : {
  4. "documentRangeFormattingProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoDocumentRangeFormatter implements vscode.DocumentRangeFormattingEditProvider{
  2. public provideDocumentRangeFormattingEdits(
  3. document: vscode.TextDocument, range: vscode.Range,
  4. options: vscode.FormattingOptions, token: vscode.CancellationToken):
  5. Thenable<vscode.TextEdit[]>;
  6. ...
  7. }
  8. }
  9. export function activate(ctx: vscode.ExtensionContext): void {
  10. ...
  11. ctx.subscriptions.push(
  12. vscode.languages.registerDocumentRangeFormattingEditProvider(
  13. GO_MODE, new GoDocumentRangeFormatter()));
  14. ...
  15. }

Basic

Don’t provide formatting support.

Advanced

You should always return the smallest possible text edits that result in the source code being formatted. This is crucial to ensure that markers such as diagnostic results are adjusted corrected and are not lost.

Incrementally Format Code as the User Types

Provide the user with support for formatting text as they type.

Note: The user setting editor.formatOnType controls whether source code gets formatted or not as the user types.

Visual indicators for formatting as code is typed

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides formatting as the user types. It also needs to tell the client on which characters formatting should be triggered. moreTriggerCharacters is optional.

  1. {
  2. ...
  3. "capabilities" : {
  4. "documentOnTypeFormattingProvider" : {
  5. "firstTriggerCharacter": "}",
  6. "moreTriggerCharacter": [";", ","]
  7. }
  8. ...
  9. }
  10. }

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

Direct Implementation

  1. class GoOnTypingFormatter implements vscode.OnTypeFormattingEditProvider{
  2. public provideOnTypeFormattingEdits(
  3. document: vscode.TextDocument, position: vscode.Position,
  4. ch: string, options: vscode.FormattingOptions, token: vscode.CancellationToken):
  5. Thenable<vscode.TextEdit[]>;
  6. ...
  7. }
  8. }
  9. export function activate(ctx: vscode.ExtensionContext): void {
  10. ...
  11. ctx.subscriptions.push(
  12. vscode.languages.registerOnTypeFormattingEditProvider(
  13. GO_MODE, new GoOnTypingFormatter()));
  14. ...
  15. }

Basic

Don’t provide formatting support.

Advanced

You should always return the smallest possible text edits that result in the source code being formatted. This is crucial to ensure that markers such as diagnostic results are adjusted corrected and are not lost.

Rename Symbols

Allow the user to rename a symbol and update all references to the symbol.

Rename a symbol and update all references to the new name

Language Server Protocol

In the response to the initialize method, your language server needs to announce that it provides for renaming.

  1. {
  2. ...
  3. "capabilities" : {
  4. "renameProvider" : "true"
  5. ...
  6. }
  7. }

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

Direct Implementation

  1. class GoRenameProvider implements vscode.RenameProvider {
  2. public provideRenameEdits(
  3. document: vscode.TextDocument, position: vscode.Position,
  4. newName: string, token: vscode.CancellationToken):
  5. Thenable<vscode.WorkspaceEdit> {
  6. ...
  7. }
  8. }
  9. export function activate(ctx: vscode.ExtensionContext): void {
  10. ...
  11. ctx.subscriptions.push(
  12. vscode.languages.registerRenameProvider(
  13. GO_MODE, new GoRenameProvider()));
  14. ...
  15. }

Basic

Don’t provide rename support.

Advanced

Return the list of all workspace edits that need to be performed, for example all edits across all files that contain references to the symbol.