Skip to content

Commit ba4fa5f

Browse files
authored
Remove command prefix (#1075)
This removes generating a command prefix when registering commands for the language client connecting to terraform-ls. Previously the extension would create a LanguageClient/terraform-ls pair per 'folder' in a workspace. This means there was a terraform-ls process per folder to keep track of. The hashicorp/terraform-ls#502 PR enabled a single terraform-ls process to handle multi-folder workspaces, which meant we could refactor the extension in #845 to use a single LanguageClient instance. A leftover from the original design was a prefix for commands registered between terraform-ls and the client. This ensured unique command IDs when multiple clients were present, but is no longer necessary with a single language client.
1 parent fad292a commit ba4fa5f

6 files changed

+50
-79
lines changed

package-lock.json

-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@
418418
},
419419
"dependencies": {
420420
"@vscode/extension-telemetry": "^0.4.9",
421-
"short-unique-id": "^3.2.3",
422421
"vscode-languageclient": "^7.0.0",
423422
"vscode-uri": "^3.0.2",
424423
"which": "^2.0.2"

src/clientHandler.ts

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ShortUniqueId from 'short-unique-id';
21
import * as vscode from 'vscode';
32
import TelemetryReporter from '@vscode/extension-telemetry';
43
import {
@@ -16,18 +15,12 @@ import { PartialManifest, CustomSemanticTokens } from './features/semanticTokens
1615
import { ShowReferencesFeature } from './features/showReferences';
1716
import { TelemetryFeature } from './features/telemetry';
1817

19-
export interface TerraformLanguageClient {
20-
commandPrefix: string;
21-
client: LanguageClient;
22-
}
23-
2418
/**
2519
* ClientHandler maintains lifecycles of language clients
2620
* based on the server's capabilities
2721
*/
2822
export class ClientHandler {
29-
private shortUid: ShortUniqueId = new ShortUniqueId();
30-
private tfClient: TerraformLanguageClient | undefined;
23+
private client: LanguageClient | undefined;
3124
private commands: string[] = [];
3225

3326
public extSemanticTokenTypes: string[] = [];
@@ -47,14 +40,14 @@ export class ClientHandler {
4740
public async startClient(): Promise<vscode.Disposable> {
4841
console.log('Starting client');
4942

50-
this.tfClient = await this.createTerraformClient();
51-
const disposable = this.tfClient.client.start();
43+
this.client = await this.createTerraformClient();
44+
const disposable = this.client.start();
5245

53-
await this.tfClient.client.onReady();
46+
await this.client.onReady();
5447

5548
this.reporter.sendTelemetryEvent('startClient');
5649

57-
const initializeResult = this.tfClient.client.initializeResult;
50+
const initializeResult = this.client.initializeResult;
5851
if (initializeResult !== undefined) {
5952
const multiFoldersSupported = initializeResult.capabilities.workspace?.workspaceFolders?.supported;
6053
console.log(`Multi-folder support: ${multiFoldersSupported}`);
@@ -65,10 +58,8 @@ export class ClientHandler {
6558
return disposable;
6659
}
6760

68-
private async createTerraformClient(): Promise<TerraformLanguageClient> {
69-
const commandPrefix = this.shortUid.seq();
70-
71-
const initializationOptions = this.getInitializationOptions(commandPrefix);
61+
private async createTerraformClient(): Promise<LanguageClient> {
62+
const initializationOptions = this.getInitializationOptions();
7263

7364
const serverOptions = await this.getServerOptions();
7465

@@ -109,7 +100,7 @@ export class ClientHandler {
109100
}
110101
});
111102

112-
return { commandPrefix, client };
103+
return client;
113104
}
114105

115106
private async getServerOptions(): Promise<ServerOptions> {
@@ -128,7 +119,7 @@ export class ClientHandler {
128119
return serverOptions;
129120
}
130121

131-
private getInitializationOptions(commandPrefix: string) {
122+
private getInitializationOptions() {
132123
const rootModulePaths = config('terraform-ls').get<string[]>('rootModules', []);
133124
const terraformExecPath = config('terraform-ls').get<string>('terraformExecPath', '');
134125
const terraformExecTimeout = config('terraform-ls').get<string>('terraformExecTimeout', '');
@@ -146,7 +137,6 @@ export class ClientHandler {
146137

147138
const experimentalFeatures = config('terraform-ls').get('experimentalFeatures');
148139
const initializationOptions = {
149-
commandPrefix,
150140
experimentalFeatures,
151141
ignoreSingleFileWarning,
152142
...(terraformExecPath.length > 0 && { terraformExecPath }),
@@ -160,16 +150,16 @@ export class ClientHandler {
160150
}
161151

162152
public async stopClient(): Promise<void> {
163-
if (this.tfClient?.client === undefined) {
153+
if (this.client === undefined) {
164154
return;
165155
}
166156

167-
await this.tfClient.client.stop();
157+
await this.client.stop();
168158
console.log('Client stopped');
169159
}
170160

171-
public getClient(): TerraformLanguageClient | undefined {
172-
return this.tfClient;
161+
public getClient(): LanguageClient | undefined {
162+
return this.client;
173163
}
174164

175165
public clientSupportsCommand(cmdName: string): boolean {

src/extension.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import TelemetryReporter from '@vscode/extension-telemetry';
33
import { ExecuteCommandParams, ExecuteCommandRequest } from 'vscode-languageclient';
44
import { LanguageClient } from 'vscode-languageclient/node';
55
import { Utils } from 'vscode-uri';
6-
import { ClientHandler, TerraformLanguageClient } from './clientHandler';
6+
import { ClientHandler } from './clientHandler';
77
import { GenerateBugReportCommand } from './commands/generateBugReport';
88
import { ModuleCallsDataProvider } from './providers/moduleCalls';
99
import { ModuleProvidersDataProvider } from './providers/moduleProviders';
@@ -99,10 +99,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
9999
if (selected && client) {
100100
const moduleUri = selected[0];
101101
const requestParams: ExecuteCommandParams = {
102-
command: `${client.commandPrefix}.terraform-ls.terraform.init`,
102+
command: `terraform-ls.terraform.init`,
103103
arguments: [`uri=${moduleUri}`],
104104
};
105-
await execWorkspaceCommand(client.client, requestParams);
105+
await execWorkspaceCommand(client, requestParams);
106106
}
107107
}),
108108
vscode.commands.registerCommand('terraform.initCurrent', async () => {
@@ -162,7 +162,7 @@ export async function updateTerraformStatusBar(documentUri: vscode.Uri): Promise
162162
return;
163163
}
164164

165-
const initSupported = clientHandler.clientSupportsCommand(`${client.commandPrefix}.terraform-ls.terraform.init`);
165+
const initSupported = clientHandler.clientSupportsCommand(`terraform-ls.terraform.init`);
166166
if (!initSupported) {
167167
return;
168168
}
@@ -236,12 +236,12 @@ interface ModuleCallersResponse {
236236
}
237237

238238
// eslint-disable-next-line @typescript-eslint/no-explicit-any
239-
async function modulesCallersCommand(languageClient: TerraformLanguageClient, moduleUri: string): Promise<any> {
239+
async function modulesCallersCommand(languageClient: LanguageClient, moduleUri: string): Promise<any> {
240240
const requestParams: ExecuteCommandParams = {
241-
command: `${languageClient.commandPrefix}.terraform-ls.module.callers`,
241+
command: `terraform-ls.module.callers`,
242242
arguments: [`uri=${moduleUri}`],
243243
};
244-
return execWorkspaceCommand(languageClient.client, requestParams);
244+
return execWorkspaceCommand(languageClient, requestParams);
245245
}
246246

247247
export async function moduleCallers(moduleUri: string): Promise<ModuleCallersResponse> {
@@ -286,10 +286,10 @@ async function terraformCommand(command: string, languageServerExec = true): Pro
286286

287287
if (languageServerExec && languageClient) {
288288
const requestParams: ExecuteCommandParams = {
289-
command: `${languageClient.commandPrefix}.terraform-ls.terraform.${command}`,
289+
command: `terraform-ls.terraform.${command}`,
290290
arguments: [`uri=${selectedModule}`],
291291
};
292-
return execWorkspaceCommand(languageClient.client, requestParams);
292+
return execWorkspaceCommand(languageClient, requestParams);
293293
} else {
294294
const terminalName = `Terraform ${selectedModule}`;
295295
const moduleURI = vscode.Uri.parse(selectedModule);

src/providers/moduleCalls.ts

+23-25
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider<ModuleCa
7979

8080
constructor(ctx: vscode.ExtensionContext, public handler: ClientHandler) {
8181
this.svg = ctx.asAbsolutePath(path.join('assets', 'icons', 'terraform.svg'));
82+
8283
ctx.subscriptions.push(
8384
vscode.commands.registerCommand('terraform.modules.refreshList', () => this.refresh()),
8485
vscode.commands.registerCommand('terraform.modules.openDocumentation', (module: ModuleCallItem) => {
@@ -134,34 +135,31 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider<ModuleCa
134135
if (handler === undefined) {
135136
return [];
136137
}
138+
await handler.onReady();
137139

138-
return await handler.client.onReady().then(async () => {
139-
const moduleCallsSupported = this.handler.clientSupportsCommand(
140-
`${handler.commandPrefix}.terraform-ls.module.calls`,
141-
);
142-
if (!moduleCallsSupported) {
143-
return Promise.resolve([]);
144-
}
145-
146-
const params: ExecuteCommandParams = {
147-
command: `${handler.commandPrefix}.terraform-ls.module.calls`,
148-
arguments: [`uri=${documentURI}`],
149-
};
150-
151-
const response = await handler.client.sendRequest<ExecuteCommandParams, ModuleCallsResponse, void>(
152-
ExecuteCommandRequest.type,
153-
params,
154-
);
155-
if (response === null) {
156-
return Promise.resolve([]);
157-
}
140+
const commandSupported = this.handler.clientSupportsCommand(`terraform-ls.module.calls`);
141+
if (!commandSupported) {
142+
return Promise.resolve([]);
143+
}
158144

159-
const list = response.module_calls.map((m) =>
160-
this.toModuleCall(m.name, m.source_addr, m.version, m.source_type, m.docs_link, this.svg, m.dependent_modules),
161-
);
145+
const params: ExecuteCommandParams = {
146+
command: `terraform-ls.module.calls`,
147+
arguments: [`uri=${documentURI}`],
148+
};
149+
150+
const response = await handler.sendRequest<ExecuteCommandParams, ModuleCallsResponse, void>(
151+
ExecuteCommandRequest.type,
152+
params,
153+
);
154+
if (response === null) {
155+
return Promise.resolve([]);
156+
}
157+
158+
const list = response.module_calls.map((m) =>
159+
this.toModuleCall(m.name, m.source_addr, m.version, m.source_type, m.docs_link, this.svg, m.dependent_modules),
160+
);
162161

163-
return list;
164-
});
162+
return list;
165163
}
166164

167165
toModuleCall(

src/providers/moduleProviders.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,19 @@ export class ModuleProvidersDataProvider implements vscode.TreeDataProvider<Modu
100100
if (handler === undefined) {
101101
return [];
102102
}
103-
await handler.client.onReady();
103+
await handler.onReady();
104104

105-
const moduleCallsSupported = this.handler.clientSupportsCommand(
106-
`${handler.commandPrefix}.terraform-ls.module.providers`,
107-
);
108-
if (!moduleCallsSupported) {
105+
const commandSupported = this.handler.clientSupportsCommand(`terraform-ls.module.providers`);
106+
if (!commandSupported) {
109107
return [];
110108
}
111109

112110
const params: ExecuteCommandParams = {
113-
command: `${handler.commandPrefix}.terraform-ls.module.providers`,
111+
command: `terraform-ls.module.providers`,
114112
arguments: [`uri=${documentURI}`],
115113
};
116114

117-
const response = await handler.client.sendRequest<ExecuteCommandParams, ModuleProvidersResponse, void>(
115+
const response = await handler.sendRequest<ExecuteCommandParams, ModuleProvidersResponse, void>(
118116
ExecuteCommandRequest.type,
119117
params,
120118
);

0 commit comments

Comments
 (0)