Skip to content

Commit

Permalink
run new module.providers command to fetch panel content
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Nov 15, 2021
1 parent 233bef1 commit 6fb8c3f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -111,7 +111,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<Terraf
await terraformCommand('validate', true);
}),
vscode.window.registerTreeDataProvider('terraform.modules', new ModuleProvider(context, clientHandler)),
vscode.window.registerTreeDataProvider('terraform.providers', new ProviderProvider(context, clientHandler)),
vscode.window.registerTreeDataProvider('terraform.providers', new ModuleProviderProvider(context, clientHandler)),
vscode.workspace.onDidChangeConfiguration(async (event: vscode.ConfigurationChangeEvent) => {
if (event.affectsConfiguration('terraform') || event.affectsConfiguration('terraform-ls')) {
const reloadMsg = 'Reload VSCode window to apply language server changes';
Expand Down
116 changes: 100 additions & 16 deletions src/providers/providerProvider.ts
Original file line number Diff line number Diff line change
@@ -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<Provider> {
onDidChangeTreeData?: vscode.Event<void | Provider>;
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<ModuleProvider> {
private readonly didChangeTreeData = new vscode.EventEmitter<void | ModuleProvider>();
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<vscode.TreeItem> {
throw new Error('Method not implemented.');
refresh(): void {
this.didChangeTreeData.fire();
}

getChildren(element?: Provider): vscode.ProviderResult<Provider[]> {
throw new Error('Method not implemented.');
getTreeItem(element: ModuleProvider): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element;
}

getParent?(element: Provider): vscode.ProviderResult<Provider> {
throw new Error('Method not implemented.');
getChildren(element?: ModuleProvider): vscode.ProviderResult<ModuleProvider[]> {
if (element) {
return [];
} else {
return this.getProvider();
}
}

resolveTreeItem?(
item: vscode.TreeItem,
element: Provider,
token: vscode.CancellationToken,
): vscode.ProviderResult<vscode.TreeItem> {
throw new Error('Method not implemented.');
async getProvider(): Promise<ModuleProvider[]> {
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<ExecuteCommandParams, ModuleProvidersResponse, void>(
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));
}
}

0 comments on commit 6fb8c3f

Please sign in to comment.