diff --git a/src/extension.ts b/src/extension.ts index 810010728a..06ce3d7c94 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,3 +1,4 @@ +import * as tfStatus from './status/terraform'; import * as terraform from './terraform'; import * as vscode from 'vscode'; import TelemetryReporter from '@vscode/extension-telemetry'; @@ -50,6 +51,39 @@ export async function activate(context: vscode.ExtensionContext): Promise // Subscriptions context.subscriptions.push( new GenerateBugReportCommand(context), + vscode.commands.registerCommand('terraform.restartLanguageServer', async () => { + vscode.commands.executeCommand('workbench.action.reloadWindow'); + }), + vscode.commands.registerCommand('terraform.showLanguageServerLogs', async () => { + outputChannel.show(); + }), + vscode.commands.registerCommand('terraform.languageServer.commands', async () => { + await vscode.window + .showQuickPick([ + { + label: 'Restart Language Server', + command: 'terraform.restartLanguageServer', + }, + { + label: 'Show Language Server Logs', + command: 'terraform.showLanguageServerLogs', + }, + { + label: 'Enable Language Server', + command: 'terraform.enableLanguageServer', + }, + { + label: 'Disable Language Server', + command: 'terraform.disableLanguageServer', + }, + ]) + .then((option) => { + if (!option || !option.command || option.command.length === 0) { + return; + } + vscode.commands.executeCommand(option.command); + }); + }), vscode.commands.registerCommand('terraform.enableLanguageServer', async () => { if (config('terraform').get('languageServer.enable') === true) { return startLanguageServer(context); @@ -206,6 +240,16 @@ export async function activate(context: vscode.ExtensionContext): Promise ); await startLanguageServer(context); + + /* + In the future, we can hook this to onDidChange or a similar handler, but currently + we only detect Terraform versions at start inside terraform-ls, so it is sufficient to ask once here + */ + const workspaces = vscode.workspace.workspaceFolders; + if (workspaces !== undefined) { + const response = await terraform.terraformVersion(workspaces[0].uri.toString(), client, reporter); + tfStatus.setTerraformVersion(response.discovered_version); + } } export async function deactivate(): Promise { diff --git a/src/status/terraform.ts b/src/status/terraform.ts new file mode 100644 index 0000000000..8a3dad4164 --- /dev/null +++ b/src/status/terraform.ts @@ -0,0 +1,27 @@ +import * as vscode from 'vscode'; + +const terraformStatus = vscode.languages.createLanguageStatusItem('terraform.status', [ + { language: 'terraform' }, + { language: 'terraform-vars' }, +]); +terraformStatus.name = 'Terraform'; +terraformStatus.detail = 'Terraform'; +terraformStatus.command = { + command: 'terraform.commands', + title: 'Terraform Commands', + tooltip: 'foo', +}; + +export function setTerraformState( + detail: string, + busy: boolean, + severity: vscode.LanguageStatusSeverity = vscode.LanguageStatusSeverity.Information, +) { + terraformStatus.busy = busy; + terraformStatus.detail = detail; + terraformStatus.severity = severity; +} + +export function setTerraformVersion(version: string) { + terraformStatus.text = version; +} diff --git a/src/terraform.ts b/src/terraform.ts index 6f1697a974..db060f5a16 100644 --- a/src/terraform.ts +++ b/src/terraform.ts @@ -1,6 +1,12 @@ +import * as tfStatus from './status/terraform'; import TelemetryReporter from '@vscode/extension-telemetry'; import * as vscode from 'vscode'; -import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClient } from 'vscode-languageclient/node'; +import { + ExecuteCommandParams, + ExecuteCommandRequest, + LanguageClient, + WorkDoneProgress, +} from 'vscode-languageclient/node'; import { Utils } from 'vscode-uri'; import { getActiveTextEditor } from './utils/vscode'; import { clientSupportsCommand } from './utils/clientHelpers'; @@ -10,6 +16,12 @@ export interface ModuleCaller { uri: string; } +export interface TerraformInfoResponse { + v: number; + required_version: string; + discovered_version: string; + discovered_path: string; +} export interface ModuleCallersResponse { v: number; callers: ModuleCaller[]; @@ -44,6 +56,18 @@ interface ModuleProvidersResponse { } /* eslint-enable @typescript-eslint/naming-convention */ +export async function terraformVersion( + moduleUri: string, + client: LanguageClient, + reporter: TelemetryReporter, +): Promise { + const command = 'terraform-ls.module.terraform'; + + const response = await execWorkspaceLSCommand(command, moduleUri, client, reporter); + + return response; +} + export async function moduleCallers( moduleUri: string, client: LanguageClient,