Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register command to show references #686

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 65 additions & 0 deletions src/showReferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as vscode from 'vscode';
import {
ClientCapabilities,
ServerCapabilities,
StaticFeature,
ReferencesRequest,
ReferenceContext,
BaseLanguageClient
} from 'vscode-languageclient';

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: BaseLanguageClient) {
}

public fillClientCapabilities(capabilities: ClientCapabilities): void {
if (!capabilities['experimental']) {
capabilities['experimental'] = {};
}
capabilities['experimental']['showReferencesCommandId'] = CLIENT_CMD_ID;
}

public initialize(capabilities: ServerCapabilities): void {
if ( !capabilities.experimental?.referenceCountCodeLens ) {
return
}

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

const doc = vscode.window.activeTextEditor.document;

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

const provider: vscode.ReferenceProvider = client.getFeature(ReferencesRequest.method).getProvider(doc);
const tokenSource: vscode.CancellationTokenSource = 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(): void {
this.registeredCommands.forEach(function(cmd, index, commands) {
cmd.dispose();
commands.splice(index, 1);
})
}
}