Programmatic Language Features | Visual Studio Code Extension API
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. 🎬 Watch The Story of VS Code! VS Code is now on Instagram, follow @vscode.ig for updates, tips, and more. Rewatch GitHub Copilot Day. Programmatic Language Features View as Markdown Ask in VS Code Ask in VS Code Insiders 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: vscode . languages . registerHoverProvider ( 'javascript' , { provideHover ( document , position , token ) { return { contents: [ 'Hover Content' ] }; } }); 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 API LSP method createDiagnosticCollection Document Diagnostics and Publish Diagnostics registerCompletionItemProvider Completion & Completion Resolve registerInlineCompletionItemProvider registerHoverProvider Hover registerSignatureHelpProvider SignatureHelp registerDefinitionProvider Definition registerTypeDefinitionProvider TypeDefinition registerImplementationProvider Implementation registerReferenceProvider References registerDocumentHighlightProvider DocumentHighlight registerDocumentSymbolProvider DocumentSymbol registerCodeActionsProvider CodeAction registerCodeLensProvider CodeLens & CodeLens Resolve registerDocumentLinkProvider DocumentLink & DocumentLink Resolve registerColorProvider DocumentColor & Color Presentation registerDocumentFormattingEditProvider Formatting registerDocumentRangeFormattingEditProvider RangeFormatting registerOnTypeFormattingEditProvider OnTypeFormatting registerRenameProvider Rename & Prepare Rename registerSelectionRangeProvider Selection Range registerFoldingRangeProvider FoldingRange Provide Diagnostics Diagnostics are a way to indicate issues with the code.
Language Server Protocol Language servers can provide diagnostics with the pull or push model. With pull diagnostics, the server announces a diagnostic provider and responds to textDocument/diagnostic requests from the client. With push diagnostics, the server sends textDocument/publishDiagnostics notifications to the client. Direct Implementation 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. { ... "capabilities" : { "completionProvider" : { "resolveProvider" : "true" , "triggerCharacters" : [ '.' ] } ... } } Direct Implementation 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 vscode . languages . registerInlineCompletionItemProvider ({ language: 'javascript' }, { provideInlineCompletionItems ( document , position , context , token ) { const result : vscode .
InlineCompletionList = { items: [], commands: [], }; ... return result ; } }); You can explore a complete example in the inline completions sample extension . Basic Return inline completions only for a well-known list of patterns based on the current line content, for specific languages. Advanced Return inline completions based on content in the whole document or workspace and more complex patterns. 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. Language Server Protocol In the response to the initialize method, your language server needs to announce that it provides hovers. { ... "capabilities" : { "hoverProvider" : "true" , ... } } In addition, your language server needs to respond to the textDocument/hover request. Direct Implementation class GoHoverProvider implements HoverProvider { public provideHover ( document : TextDocument , position : Position , token : CancellationToken ): Thenable < Hover > { ... } } export function activate ( ctx : vscode .
ExtensionContext ): void { ... ctx . subscriptions . pu
The rest of this page is on the live site.