import * as vscode from 'vscode' ; import { BaseLanguageClient , ClientCapabilities , ReferenceContext , ReferencesRequest , ServerCapabilities , StaticFeature , } from 'vscode-languageclient' ; import { ExperimentalClientCapabilities } from './types' ; type Position = { line : number ; character : number ; } ; type RefContext = { includeDeclaration : boolean ; } ; const CLIENT_CMD_ID = 'client.showReferences' ; const VSCODE_SHOW_REFERENCES = 'editor.action.showReferences' ; export class ShowReferencesFeature implements StaticFeature { private registeredCommands : vscode . Disposable [ ] = [ ] ; constructor ( private _client ) { } public fillClientCapabilities ( capabilities ) { if ( ! capabilities [ 'experimental' ] ) { capabilities [ 'experimental' ] = { } ; } capabilities [ 'experimental' ] [ 'showReferencesCommandId' ] = CLIENT_CMD_ID ; } public initialize ( capabilities ) { if ( ! capabilities . experimental ?. referenceCountCodeLens ) { return ; } const showRefs = vscode . commands . registerCommand ( CLIENT_CMD_ID , async ( pos , refCtx ) => { const client = this . _client ; const doc = vscode . window ?. activeTextEditor ?. document ; if ( ! doc ) { return ; } const position = new vscode . Position ( pos . line , pos . character ) ; const context = { includeDeclaration : refCtx . includeDeclaration } ; const provider = client . getFeature ( ReferencesRequest . method ) . getProvider ( doc ) ; if ( ! provider ) { return ; } const tokenSource = new vscode . CancellationTokenSource ( ) ; const locations = await provider . provideReferences ( doc , position , context , tokenSource . token ) ; await vscode . commands . executeCommand ( VSCODE_SHOW_REFERENCES , doc . uri , position , locations ) ; } ) ; this . registeredCommands . push ( showRefs ) ; } public dispose ( ) { this . registeredCommands . forEach ( function ( cmd , index , commands ) { cmd . dispose ( ) ; commands . splice ( index , 1 ) ; } ) ; } }