From 65746305d6cf4b44797cc184508049f2ed6846b6 Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Wed, 11 Dec 2024 18:45:43 +0300 Subject: [PATCH] Fix: kiota workspace tab correction (#5897) --- .../deleteWorkspaceItemCommand.ts | 11 ++++++- .../generate/generateClientCommand.ts | 1 + .../regenerate/regenerateButtonCommand.ts | 1 + .../commands/regenerate/regenerateCommand.ts | 1 + vscode/microsoft-kiota/src/extension.ts | 2 +- .../workspace/workspaceContentService.ts | 7 +---- .../src/providers/workspaceTreeProvider.ts | 29 +++++++------------ .../deleteWorkspaceItemCommand.test.ts | 6 +++- 8 files changed, 31 insertions(+), 27 deletions(-) diff --git a/vscode/microsoft-kiota/src/commands/deleteWorkspaceItem/deleteWorkspaceItemCommand.ts b/vscode/microsoft-kiota/src/commands/deleteWorkspaceItem/deleteWorkspaceItemCommand.ts index 8d84b15a3e..4eb9b79c05 100644 --- a/vscode/microsoft-kiota/src/commands/deleteWorkspaceItem/deleteWorkspaceItemCommand.ts +++ b/vscode/microsoft-kiota/src/commands/deleteWorkspaceItem/deleteWorkspaceItemCommand.ts @@ -3,6 +3,8 @@ import * as vscode from "vscode"; import { extensionId } from "../../constants"; import { getLogEntriesForLevel, KiotaLogEntry, LogLevel } from "../../kiotaInterop"; +import { OpenApiTreeProvider } from "../../providers/openApiTreeProvider"; +import { SharedService } from "../../providers/sharedService"; import { WorkspaceTreeItem } from "../../providers/workspaceTreeProvider"; import { isPluginType } from "../../util"; import { exportLogsAndShowErrors } from "../../utilities/logging"; @@ -12,7 +14,9 @@ import { removeClient, removePlugin } from "./removeItem"; export class DeleteWorkspaceItemCommand extends Command { constructor( private _context: vscode.ExtensionContext, - private _kiotaOutputChannel: vscode.LogOutputChannel + private _openApiTreeProvider: OpenApiTreeProvider, + private _kiotaOutputChannel: vscode.LogOutputChannel, + private sharedService: SharedService ) { super(); } @@ -33,9 +37,14 @@ export class DeleteWorkspaceItemCommand extends Command { ); if (response?.title === yesAnswer.title) { + if (this.sharedService.get('clientOrPluginKey') === workspaceTreeItem.label) { + this._openApiTreeProvider.closeDescription(); + } + const result = await this.deleteItem(type, workspaceTreeItem); if (result) { const isSuccess = result.some(k => k.message.includes('removed successfully')); + await vscode.commands.executeCommand('kiota.workspace.refresh'); if (isSuccess) { void vscode.window.showInformationMessage(vscode.l10n.t('{0} removed successfully.', workspaceTreeItem.label)); } else { diff --git a/vscode/microsoft-kiota/src/commands/generate/generateClientCommand.ts b/vscode/microsoft-kiota/src/commands/generate/generateClientCommand.ts index 172f138fa6..c7e21c47f0 100644 --- a/vscode/microsoft-kiota/src/commands/generate/generateClientCommand.ts +++ b/vscode/microsoft-kiota/src/commands/generate/generateClientCommand.ts @@ -172,6 +172,7 @@ export class GenerateClientCommand extends Command { } else { await displayGenerationResults(this._openApiTreeProvider, config); } + await vscode.commands.executeCommand('kiota.workspace.refresh'); } clearDeepLinkParams(); // Clear the state after successful generation diff --git a/vscode/microsoft-kiota/src/commands/regenerate/regenerateButtonCommand.ts b/vscode/microsoft-kiota/src/commands/regenerate/regenerateButtonCommand.ts index 44e07ad3ff..7319264b44 100644 --- a/vscode/microsoft-kiota/src/commands/regenerate/regenerateButtonCommand.ts +++ b/vscode/microsoft-kiota/src/commands/regenerate/regenerateButtonCommand.ts @@ -61,6 +61,7 @@ export class RegenerateButtonCommand extends Command { await regenerateService.regenerateTeamsApp(workspaceJson, clientOrPluginKey); } } + await vscode.commands.executeCommand('kiota.workspace.refresh'); } } \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/commands/regenerate/regenerateCommand.ts b/vscode/microsoft-kiota/src/commands/regenerate/regenerateCommand.ts index d9647fe9b6..c4c0d673fc 100644 --- a/vscode/microsoft-kiota/src/commands/regenerate/regenerateCommand.ts +++ b/vscode/microsoft-kiota/src/commands/regenerate/regenerateCommand.ts @@ -46,5 +46,6 @@ export class RegenerateCommand extends Command { await regenerateService.regenerateTeamsApp(workspaceJson, clientOrPluginKey); } } + await vscode.commands.executeCommand('kiota.workspace.refresh'); } } \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index a11d7471da..19e9828adc 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -82,7 +82,7 @@ export async function activate( const closeDescriptionCommand = new CloseDescriptionCommand(openApiTreeProvider); const statusCommand = new StatusCommand(); const selectLockCommand = new SelectLockCommand(openApiTreeProvider); - const deleteWorkspaceItemCommand = new DeleteWorkspaceItemCommand(context, kiotaOutputChannel); + const deleteWorkspaceItemCommand = new DeleteWorkspaceItemCommand(context, openApiTreeProvider, kiotaOutputChannel, sharedService); const updateClientsCommand = new UpdateClientsCommand(context, kiotaOutputChannel); await loadTreeView(context, workspaceTreeProvider); diff --git a/vscode/microsoft-kiota/src/modules/workspace/workspaceContentService.ts b/vscode/microsoft-kiota/src/modules/workspace/workspaceContentService.ts index a7997b2444..1ea61a6882 100644 --- a/vscode/microsoft-kiota/src/modules/workspace/workspaceContentService.ts +++ b/vscode/microsoft-kiota/src/modules/workspace/workspaceContentService.ts @@ -3,7 +3,6 @@ import * as path from 'path'; import * as fs from 'fs'; import { WorkspaceContent } from "."; -import { KIOTA_WORKSPACE_FILE } from "../../constants"; import { getWorkspaceJsonPath } from '../../util'; class WorkspaceContentService { @@ -15,11 +14,7 @@ class WorkspaceContentService { return; } try { - const workspaceJson = vscode.workspace.textDocuments.find(doc => doc.fileName.endsWith(KIOTA_WORKSPACE_FILE)); - if (!workspaceJson) { - throw new Error('Workspace file not found'); - } - const content = workspaceJson.getText(); + const content = await fs.promises.readFile(getWorkspaceJsonPath(), 'utf-8'); return JSON.parse(content); } catch (error) { console.error('Error loading workspace.json:', error); diff --git a/vscode/microsoft-kiota/src/providers/workspaceTreeProvider.ts b/vscode/microsoft-kiota/src/providers/workspaceTreeProvider.ts index a7ed3f9943..51563edfdf 100644 --- a/vscode/microsoft-kiota/src/providers/workspaceTreeProvider.ts +++ b/vscode/microsoft-kiota/src/providers/workspaceTreeProvider.ts @@ -29,7 +29,6 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider { @@ -47,11 +46,15 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider 0; - const hasPlugins = this.workspaceContent?.plugins && Object.keys(this.workspaceContent.plugins).length > 0; - const collapsibleState = (hasClients || hasPlugins) ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed; - return [ - new WorkspaceTreeItem(KIOTA_WORKSPACE_FILE, collapsibleState, 'root') + const hasClients = this.workspaceContent.clients && Object.keys(this.workspaceContent.clients).length > 0; + const hasPlugins = this.workspaceContent.plugins && Object.keys(this.workspaceContent.plugins).length > 0; + const hasWorkspaceContent = hasClients || hasPlugins; + return hasWorkspaceContent ? [ + new WorkspaceTreeItem(KIOTA_WORKSPACE_FILE, + vscode.TreeItemCollapsibleState.Expanded, 'root') + ] : [ + new WorkspaceTreeItem(vscode.l10n.t('No clients or plugins are available'), + vscode.TreeItemCollapsibleState.None, 'info') ]; } @@ -64,9 +67,6 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider 0) { children.push(new WorkspaceTreeItem(PLUGINS, vscode.TreeItemCollapsibleState.Expanded, 'category')); } - if (children.length === 0) { - children.push(new WorkspaceTreeItem(vscode.l10n.t("No clients or plugins are available"), vscode.TreeItemCollapsibleState.None, 'info')); - } return children; } @@ -101,7 +101,7 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider { - const document = event.document; - if (document.fileName.endsWith(KIOTA_WORKSPACE_FILE)) { - await vscode.commands.executeCommand('kiota.workspace.refresh'); - } - }) - ); + await vscode.commands.executeCommand('kiota.workspace.refresh'); }; \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/test/suite/commands/deleteWorkspaceItemCommand.test.ts b/vscode/microsoft-kiota/src/test/suite/commands/deleteWorkspaceItemCommand.test.ts index b431e29e36..9b6debfb39 100644 --- a/vscode/microsoft-kiota/src/test/suite/commands/deleteWorkspaceItemCommand.test.ts +++ b/vscode/microsoft-kiota/src/test/suite/commands/deleteWorkspaceItemCommand.test.ts @@ -3,6 +3,8 @@ import * as sinon from "sinon"; import * as vscode from 'vscode'; import { DeleteWorkspaceItemCommand } from '../../../commands/deleteWorkspaceItem/deleteWorkspaceItemCommand'; +import * as treeModule from "../../../providers/openApiTreeProvider"; +import * as sharedServiceModule from '../../../providers/sharedService'; import { WorkspaceTreeItem } from '../../../providers/workspaceTreeProvider'; suite('DeleteWorkspaceItemCommand Tests', () => { @@ -14,7 +16,9 @@ suite('DeleteWorkspaceItemCommand Tests', () => { setup(() => { context = { extension: { packageJSON: { telemetryInstrumentationKey: 'test-key' } } } as any; outputChannel = { appendLine: sinon.stub() } as any; - command = new DeleteWorkspaceItemCommand(context, outputChannel); + var treeProvider = sinon.createStubInstance(treeModule.OpenApiTreeProvider); + var stubbedSharedService = sinon.createStubInstance(sharedServiceModule.SharedService); + command = new DeleteWorkspaceItemCommand(context, treeProvider, outputChannel, stubbedSharedService,); workspaceTreeItem = { label: 'test-item', category: 'plugin' } as any; });