diff --git a/package.json b/package.json index 8149268988..32659b4a92 100644 --- a/package.json +++ b/package.json @@ -758,11 +758,43 @@ "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": "There are no open Terraform files. Please open a Terraform configuration file to see installed providers.", + "when": "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": "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/modules/develop/index.html)", + "when": "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": "terraform.providers.noResponse === true" + }, + { + "view": "terraform.modules", + "contents": "There are no open Terraform files. Please open a Terraform configuration file to see installed modules.", + "when": "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": "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": "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": "terraform.modules.noResponse === true" }, { "view": "terraform.cloud.workspaces", diff --git a/src/features/moduleCalls.ts b/src/features/moduleCalls.ts index 5412037269..ba6c946970 100644 --- a/src/features/moduleCalls.ts +++ b/src/features/moduleCalls.ts @@ -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(); }); diff --git a/src/features/moduleProviders.ts b/src/features/moduleProviders.ts index ac1dce16fd..bdec3fb12a 100644 --- a/src/features/moduleProviders.ts +++ b/src/features/moduleProviders.ts @@ -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(); }); diff --git a/src/providers/moduleCalls.ts b/src/providers/moduleCalls.ts index 07d6d84298..9b67cd1e84 100644 --- a/src/providers/moduleCalls.ts +++ b/src/providers/moduleCalls.ts @@ -76,15 +76,9 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider { - const activeEditor = getActiveTextEditor(); - - if (!isTerraformFile(activeEditor?.document)) { - return; - } - - if (event && activeEditor) { - this.refresh(); - } + // 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(); }), ); } @@ -109,26 +103,48 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider { 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, @@ -141,8 +157,14 @@ export class ModuleCallsDataProvider implements vscode.TreeDataProvider this.refresh()), vscode.window.onDidChangeActiveTextEditor(async (event: vscode.TextEditor | undefined) => { - const activeEditor = getActiveTextEditor(); - - if (!isTerraformFile(activeEditor?.document)) { - return; - } - - if (event && activeEditor) { - this.refresh(); - } + // 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) { @@ -75,27 +69,49 @@ export class ModuleProvidersDataProvider implements vscode.TreeDataProvider { 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, @@ -105,7 +121,15 @@ export class ModuleProvidersDataProvider implements vscode.TreeDataProvider