Skip to content

Commit

Permalink
Register command to show references
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jul 21, 2021
1 parent 8b38ec4 commit 389b383
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/clientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
sortedWorkspaceFolders
} from './vscodeUtils';
import TelemetryReporter from 'vscode-extension-telemetry';
import { ShowReferencesFeature } from './showReferences';
import { ServerPath, CUSTOM_BIN_PATH_OPTION_NAME } from './serverPath';

export interface TerraformLanguageClient {
Expand Down Expand Up @@ -177,6 +178,8 @@ export class ClientHandler {
clientOptions
);

client.registerFeature(new ShowReferencesFeature(client));

client.onDidChangeState((event) => {
if (event.newState === State.Stopped) {
clients.delete(location);
Expand Down
68 changes: 68 additions & 0 deletions src/showReferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as vscode from 'vscode';
import {
ClientCapabilities,
DocumentSelector,
ServerCapabilities,
StaticFeature,
ReferencesRequest,
ReferenceContext,
BaseLanguageClient} from 'vscode-languageclient';

function ensure<T, K extends keyof T>(target: T, key: K): T[K] {
if (target[key] === void 0) {
target[key] = Object.create(null) as any;
}
return target[key];
}

type Position = {
line: number;
character: number;
}

type RefContext = {
includeDeclaration: boolean;
}

const showReferencesCommandId = 'client.showReferences'

export class ShowReferencesFeature implements StaticFeature {
private registeredCommands: vscode.Disposable[] = [];

constructor(private _client: BaseLanguageClient) {
}

public fillClientCapabilities(capabilities: ClientCapabilities): void {
ensure(capabilities, 'experimental')['showReferencesCommandId'] = showReferencesCommandId;
};

public initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector | undefined): void {
if ( !capabilities.experimental?.referenceCountCodeLens ) {
return
}

let showRefs = vscode.commands.registerCommand(showReferencesCommandId, async (pos: Position, refCtx: RefContext) => {
let client = this._client;

const doc = vscode.window.activeTextEditor.document;

let position = new vscode.Position(pos.line, pos.character);
let context: ReferenceContext = {includeDeclaration: refCtx.includeDeclaration}

let provider: vscode.ReferenceProvider = client.getFeature(ReferencesRequest.method).getProvider(doc);
let tokenSource: vscode.CancellationTokenSource = new vscode.CancellationTokenSource();

let locations = await provider.provideReferences(doc, position, context, tokenSource.token);

await vscode.commands.executeCommand('editor.action.showReferences', doc.uri, position, locations);
})
this.registeredCommands.push(showRefs);
};

public dispose(): void {
this.registeredCommands.forEach(function(cmd, index, commands) {
cmd.dispose();
commands.splice(index, 1);
})
};
}

0 comments on commit 389b383

Please sign in to comment.