Skip to content

Commit

Permalink
Improve moduleProviders and moduleCalls welcome views (#1676)
Browse files Browse the repository at this point in the history
This commit improves the welcome views for moduleProviders and moduleCalls by adding context variables to the views. This allows us to show different messages depending on the state of the open file inside the tree views.
  • Loading branch information
jpogran authored Feb 8, 2024
1 parent 54b6fae commit bbd86a9
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 30 deletions.
46 changes: 44 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,53 @@
"viewsWelcome": [
{
"view": "terraform.providers",
"contents": "The active editor cannot provide information about installed providers. [Learn more about providers](https://www.terraform.io/docs/language/providers/index.html). You may need to update your language server version."
"contents": "This view requires the language server to be enabled:\n[Open Settings](command:terraform.openSettingsJson)",
"when": "config.terraform.languageServer.enable === false"
},
{
"view": "terraform.providers",
"contents": "There are no open Terraform files. Please open a Terraform configuration file to see installed providers.",
"when": "config.terraform.languageServer.enable && terraform.providers.documentOpened === false"
},
{
"view": "terraform.providers",
"contents": "The active document is not a Terraform file. Please open a Terraform configuration file to see installed providers.",
"when": "config.terraform.languageServer.enable && terraform.providers.documentIsTerraform === false"
},
{
"view": "terraform.providers",
"contents": "There are no installed providers found for the current open file.\n[Learn more about providers](https://www.terraform.io/docs/language/providers/index.html)",
"when": "config.terraform.languageServer.enable && terraform.providers.noProviders === true"
},
{
"view": "terraform.providers",
"contents": "The active editor cannot provide information about installed providers.\n[Learn more about providers](https://www.terraform.io/docs/language/providers/index.html)",
"when": "config.terraform.languageServer.enable && terraform.providers.noResponse === true"
},
{
"view": "terraform.modules",
"contents": "This view requires the language server to be enabled:\n[Open Settings](command:terraform.openSettingsJson)",
"when": "config.terraform.languageServer.enable === false"
},
{
"view": "terraform.modules",
"contents": "There are no open Terraform files. Please open a Terraform configuration file to see installed modules.",
"when": "config.terraform.languageServer.enable && terraform.modules.documentOpened === false"
},
{
"view": "terraform.modules",
"contents": "The active document is not a Terraform file. Please open a Terraform configuration file to see installed modules.",
"when": "config.terraform.languageServer.enable && terraform.modules.documentIsTerraform === false"
},
{
"view": "terraform.modules",
"contents": "There are no installed modules found for the current open file.\n[Learn more about modules](https://www.terraform.io/docs/language/modules/develop/index.html)",
"when": "config.terraform.languageServer.enable && terraform.modules.noModules === true"
},
{
"view": "terraform.modules",
"contents": "The active editor cannot provide information about installed modules. [Learn more about modules](https://www.terraform.io/docs/language/modules/develop/index.html) You may need to run 'terraform get' or update your language server version."
"contents": "The active editor cannot provide information about installed modules.\n[Learn more about modules](https://www.terraform.io/docs/language/modules/develop/index.html)",
"when": "config.terraform.languageServer.enable && terraform.modules.noResponse === true"
},
{
"view": "terraform.cloud.workspaces",
Expand Down
14 changes: 14 additions & 0 deletions src/commands/terraformls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ export class TerraformLSCommands implements vscode.Disposable {

await config('terraform').update('languageServer.enable', false, scope);
}),
vscode.commands.registerCommand('terraform.openSettingsJson', async () => {
// this opens the default settings window (either UI or json)
const s = await vscode.workspace.getConfiguration('workbench').get('settings.editor');
if (s === 'json') {
return await vscode.commands.executeCommand('workbench.action.openSettingsJson', {
revealSetting: { key: 'terraform.languageServer.enable', edit: true },
});
} else {
return await vscode.commands.executeCommand('workbench.action.openSettings', {
focusSearch: true,
query: '@ext:hashicorp.terraform',
});
}
}),
];
}

Expand Down
3 changes: 3 additions & 0 deletions src/features/moduleCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ export class ModuleCallsFeature implements StaticFeature {

if (!capabilities.experimental?.refreshModuleCalls) {
console.log('Server does not support client.refreshModuleCalls');
await vscode.commands.executeCommand('setContext', 'terraform.modules.supported', false);
return;
}

await vscode.commands.executeCommand('setContext', 'terraform.modules.supported', true);

const d = this.client.onRequest(CLIENT_MODULE_CALLS_CMD_ID, () => {
this.view?.refresh();
});
Expand Down
3 changes: 3 additions & 0 deletions src/features/moduleProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ export class ModuleProvidersFeature implements StaticFeature {

if (!capabilities.experimental?.refreshModuleProviders) {
console.log("Server doesn't support client.refreshModuleProviders");
await vscode.commands.executeCommand('setContext', 'terraform.providers.supported', false);
return;
}

await vscode.commands.executeCommand('setContext', 'terraform.providers.supported', true);

const d = this.client.onRequest(CLIENT_MODULE_PROVIDERS_CMD_ID, () => {
this.view?.refresh();
});
Expand Down
48 changes: 35 additions & 13 deletions src/providers/moduleCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,10 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider<ModuleCa
vscode.env.openExternal(vscode.Uri.parse(module.docsLink));
}
}),
vscode.window.onDidChangeActiveTextEditor(async (event: vscode.TextEditor | undefined) => {
const activeEditor = getActiveTextEditor();

if (!isTerraformFile(activeEditor?.document)) {
return;
}

if (event && activeEditor) {
this.refresh();
}
vscode.window.onDidChangeActiveTextEditor(async () => {
// most of the time this is called when the user switches tabs or closes the file
// we already check for state inside the getModule function, so we can just call refresh here
this.refresh();
}),
);
}
Expand All @@ -109,26 +103,48 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider<ModuleCa
async getModules(): Promise<ModuleCallItem[]> {
const activeEditor = getActiveTextEditor();

await vscode.commands.executeCommand('setContext', 'terraform.modules.documentOpened', true);
await vscode.commands.executeCommand('setContext', 'terraform.modules.documentIsTerraform', true);
await vscode.commands.executeCommand('setContext', 'terraform.modules.lspConnected', true);
await vscode.commands.executeCommand('setContext', 'terraform.modules.noResponse', false);
await vscode.commands.executeCommand('setContext', 'terraform.modules.noModules', false);

if (activeEditor?.document === undefined) {
// there is no open document
await vscode.commands.executeCommand('setContext', 'terraform.modules.documentOpened', false);
return [];
}

if (!isTerraformFile(activeEditor.document)) {
// the open file is not a terraform file
await vscode.commands.executeCommand('setContext', 'terraform.modules.documentIsTerraform', false);
return [];
}

const editor = activeEditor.document.uri;
const documentURI = Utils.dirname(editor);
if (this.client === undefined) {
// connection to terraform-ls failed
await vscode.commands.executeCommand('setContext', 'terraform.modules.lspConnected', false);
return [];
}

const editor = activeEditor.document.uri;
const documentURI = Utils.dirname(editor);

let response: terraform.ModuleCallsResponse;
try {
const response = await terraform.moduleCalls(documentURI.toString(), this.client, this.reporter);
response = await terraform.moduleCalls(documentURI.toString(), this.client, this.reporter);
if (response === null) {
// no response from terraform-ls
await vscode.commands.executeCommand('setContext', 'terraform.modules.noResponse', true);
return [];
}
} catch {
// error from terraform-ls
await vscode.commands.executeCommand('setContext', 'terraform.modules.noResponse', true);
return [];
}

try {
const list = response.module_calls.map((m) => {
return this.toModuleCall(
m.name,
Expand All @@ -141,8 +157,14 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider<ModuleCa
);
});

if (list.length === 0) {
await vscode.commands.executeCommand('setContext', 'terraform.modules.noModules', true);
}

return list;
} catch {
// error mapping response
await vscode.commands.executeCommand('setContext', 'terraform.modules.noResponse', true);
return [];
}
}
Expand Down
52 changes: 38 additions & 14 deletions src/providers/moduleProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,10 @@ export class ModuleProvidersDataProvider implements vscode.TreeDataProvider<Modu
constructor(ctx: vscode.ExtensionContext, private client: LanguageClient, private reporter: TelemetryReporter) {
ctx.subscriptions.push(
vscode.commands.registerCommand('terraform.providers.refreshList', () => this.refresh()),
vscode.window.onDidChangeActiveTextEditor(async (event: vscode.TextEditor | undefined) => {
const activeEditor = getActiveTextEditor();

if (!isTerraformFile(activeEditor?.document)) {
return;
}

if (event && activeEditor) {
this.refresh();
}
vscode.window.onDidChangeActiveTextEditor(async () => {
// most of the time this is called when the user switches tabs or closes the file
// we already check for state inside the getprovider function, so we can just call refresh here
this.refresh();
}),
vscode.commands.registerCommand('terraform.providers.openDocumentation', (module: ModuleProviderItem) => {
if (module.docsLink) {
Expand Down Expand Up @@ -75,27 +69,49 @@ export class ModuleProvidersDataProvider implements vscode.TreeDataProvider<Modu
async getProvider(): Promise<ModuleProviderItem[]> {
const activeEditor = getActiveTextEditor();

await vscode.commands.executeCommand('setContext', 'terraform.providers.documentOpened', true);
await vscode.commands.executeCommand('setContext', 'terraform.providers.documentIsTerraform', true);
await vscode.commands.executeCommand('setContext', 'terraform.providers.lspConnected', true);
await vscode.commands.executeCommand('setContext', 'terraform.providers.noResponse', false);
await vscode.commands.executeCommand('setContext', 'terraform.providers.noProviders', false);

if (activeEditor?.document === undefined) {
// there is no open document
await vscode.commands.executeCommand('setContext', 'terraform.providers.documentOpened', false);
return [];
}

if (!isTerraformFile(activeEditor.document)) {
// the open file is not a terraform file
await vscode.commands.executeCommand('setContext', 'terraform.providers.documentIsTerraform', false);
return [];
}

const editor = activeEditor.document.uri;
const documentURI = Utils.dirname(editor);
if (this.client === undefined) {
// connection to terraform-ls failed
await vscode.commands.executeCommand('setContext', 'terraform.providers.lspConnected', false);
return [];
}

const editor = activeEditor.document.uri;
const documentURI = Utils.dirname(editor);

let response: terraform.ModuleProvidersResponse;
try {
const response = await terraform.moduleProviders(documentURI.toString(), this.client, this.reporter);
response = await terraform.moduleProviders(documentURI.toString(), this.client, this.reporter);
if (response === null) {
// no response from terraform-ls
await vscode.commands.executeCommand('setContext', 'terraform.providers.noResponse', true);
return [];
}
} catch {
// error from terraform-ls
await vscode.commands.executeCommand('setContext', 'terraform.providers.noResponse', true);
return [];
}

return Object.entries(response.provider_requirements).map(
try {
const list = Object.entries(response.provider_requirements).map(
([provider, details]) =>
new ModuleProviderItem(
provider,
Expand All @@ -105,7 +121,15 @@ export class ModuleProvidersDataProvider implements vscode.TreeDataProvider<Modu
details.docs_link,
),
);

if (list.length === 0) {
await vscode.commands.executeCommand('setContext', 'terraform.providers.noProviders', true);
}

return list;
} catch {
// error mapping response
await vscode.commands.executeCommand('setContext', 'terraform.providers.noResponse', true);
return [];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ModuleCallsResponse {
module_calls: ModuleCall[];
}

interface ModuleProvidersResponse {
export interface ModuleProvidersResponse {
v: number;
provider_requirements: {
[provider: string]: {
Expand Down

0 comments on commit bbd86a9

Please sign in to comment.