From 6fb8c3fd63e55a1df3644965087800d2957a333c Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 12 Nov 2021 17:32:19 +0100 Subject: [PATCH] run new module.providers command to fetch panel content --- src/extension.ts | 4 +- src/providers/providerProvider.ts | 116 +++++++++++++++++++++++++----- 2 files changed, 102 insertions(+), 18 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 68137e0b9..d380101bd 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,7 +6,7 @@ import { Utils } from 'vscode-uri'; import { ClientHandler, TerraformLanguageClient } from './clientHandler'; import { defaultVersionString, isValidVersionString, LanguageServerInstaller } from './languageServerInstaller'; import { ModuleProvider } from './providers/moduleProvider'; -import { ProviderProvider } from './providers/providerProvider'; +import { ModuleProviderProvider } from './providers/providerProvider'; import { ServerPath } from './serverPath'; import { SingleInstanceTimeout } from './utils'; import { config, getActiveTextEditor } from './vscodeUtils'; @@ -111,7 +111,7 @@ export async function activate(context: vscode.ExtensionContext): Promise { if (event.affectsConfiguration('terraform') || event.affectsConfiguration('terraform-ls')) { const reloadMsg = 'Reload VSCode window to apply language server changes'; diff --git a/src/providers/providerProvider.ts b/src/providers/providerProvider.ts index fbdf775ff..fc733833b 100644 --- a/src/providers/providerProvider.ts +++ b/src/providers/providerProvider.ts @@ -1,30 +1,114 @@ import * as vscode from 'vscode'; +import { Utils } from 'vscode-uri'; +import { ExecuteCommandParams, ExecuteCommandRequest } from 'vscode-languageclient'; + import { ClientHandler } from '../clientHandler'; +import { getActiveTextEditor } from '../vscodeUtils'; -class Provider extends vscode.TreeItem {} +interface ModuleProvidersResponse { + v: number; + provider_requirements: { + [provider: string]: { + display_name: string; + version_constraint: string; + }; + }; + installed_providers: { + [provider: string]: string; + }; +} -export class ProviderProvider implements vscode.TreeDataProvider { - onDidChangeTreeData?: vscode.Event; +class ModuleProvider extends vscode.TreeItem { + constructor( + public fullName: string, + public displayName: string, + public requiredVersion: string, + public installedVersion: string | undefined, + ) { + super(`${displayName} ${requiredVersion}`, vscode.TreeItemCollapsibleState.None); - constructor(ctx: vscode.ExtensionContext, public handler: ClientHandler) {} + this.description = installedVersion ?? 'n.a.'; + this.iconPath = new vscode.ThemeIcon('package'); + this.tooltip = fullName; + } +} + +export class ModuleProviderProvider implements vscode.TreeDataProvider { + private readonly didChangeTreeData = new vscode.EventEmitter(); + public readonly onDidChangeTreeData = this.didChangeTreeData.event; + + constructor(ctx: vscode.ExtensionContext, private handler: ClientHandler) { + ctx.subscriptions.push( + vscode.window.onDidChangeActiveTextEditor(async (event: vscode.TextEditor) => { + if (event && getActiveTextEditor()) { + this.refresh(); + } + }), + ); + } - getTreeItem(element: Provider): vscode.TreeItem | Thenable { - throw new Error('Method not implemented.'); + refresh(): void { + this.didChangeTreeData.fire(); } - getChildren(element?: Provider): vscode.ProviderResult { - throw new Error('Method not implemented.'); + getTreeItem(element: ModuleProvider): vscode.TreeItem | Thenable { + return element; } - getParent?(element: Provider): vscode.ProviderResult { - throw new Error('Method not implemented.'); + getChildren(element?: ModuleProvider): vscode.ProviderResult { + if (element) { + return []; + } else { + return this.getProvider(); + } } - resolveTreeItem?( - item: vscode.TreeItem, - element: Provider, - token: vscode.CancellationToken, - ): vscode.ProviderResult { - throw new Error('Method not implemented.'); + async getProvider(): Promise { + const activeEditor = getActiveTextEditor(); + + const document = activeEditor?.document; + if (document === undefined) { + return []; + } + + const editor = document.uri; + const documentURI = Utils.dirname(editor); + const handler = this.handler.getClient(); + if (handler === undefined) { + return []; + } + await handler.client.onReady(); + + const moduleCallsSupported = this.handler.clientSupportsCommand( + `${handler.commandPrefix}.terraform-ls.module.providers`, + ); + if (!moduleCallsSupported) { + return []; + } + + const params: ExecuteCommandParams = { + command: `${handler.commandPrefix}.terraform-ls.module.providers`, + arguments: [`uri=${documentURI}`], + }; + + const response = await handler.client.sendRequest( + ExecuteCommandRequest.type, + params, + ); + if (response === null) { + return []; + } + + return Object.entries(response.provider_requirements) + .map( + ([provider, details]) => + new ModuleProvider( + provider, + details.display_name, + details.version_constraint, + response.installed_providers[provider], + ), + ) + .filter((m) => Boolean(m.requiredVersion)); } }