From 72e3f8b9b1f5737513b655ccf4bfaa5faa6c0406 Mon Sep 17 00:00:00 2001 From: Elinor Date: Mon, 29 Jul 2024 19:55:31 +0300 Subject: [PATCH] Fix: Workspace file display (#5029) --- vscode/microsoft-kiota/package.json | 4 ++ vscode/microsoft-kiota/package.nls.json | 3 +- vscode/microsoft-kiota/src/extension.ts | 1 + vscode/microsoft-kiota/src/util.ts | 1 + .../src/workspaceTreeProvider.ts | 67 +++++++++++-------- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/vscode/microsoft-kiota/package.json b/vscode/microsoft-kiota/package.json index 41cbfdcbb6..c5b6cc0865 100644 --- a/vscode/microsoft-kiota/package.json +++ b/vscode/microsoft-kiota/package.json @@ -409,6 +409,10 @@ { "command": "kiota.regenerate", "title": "%kiota.openApiExplorer.regenerateButton.title%" + }, + { + "command": "kiota.workspace.refresh", + "title": "%kiota.openApiExplorer.refresh.title%" } ], "languages": [ diff --git a/vscode/microsoft-kiota/package.nls.json b/vscode/microsoft-kiota/package.nls.json index eb1facdf59..00e54d3cb5 100644 --- a/vscode/microsoft-kiota/package.nls.json +++ b/vscode/microsoft-kiota/package.nls.json @@ -29,5 +29,6 @@ "kiota.openApiExplorer.openFile.title": "Open file", "kiota.workspace.name": "My Workspace", "kiota.openApiExplorer.regenerateButton.title": "Re-generate", - "kiota.openApiExplorer.editPaths.title": "Edit paths" + "kiota.openApiExplorer.editPaths.title": "Edit paths", + "kiota.openApiExplorer.refresh.title": "Refresh" } diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index cc06ad07f8..cfc16c15b5 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -444,6 +444,7 @@ export async function activate( openApiTreeProvider.setSelectionChanged(false); const workspaceJsonPath = getWorkspaceJsonPath(); await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, clientNameOrPluginName ); + await vscode.commands.executeCommand('kiota.workspace.refresh'); await updateTreeViewIcons(treeViewId, false, true); } async function regenerateClient(clientKey: string, clientObject:any, settings: ExtensionSettings, selectedPaths?: string[]): Promise { diff --git a/vscode/microsoft-kiota/src/util.ts b/vscode/microsoft-kiota/src/util.ts index a45854b3f2..087dc03fde 100644 --- a/vscode/microsoft-kiota/src/util.ts +++ b/vscode/microsoft-kiota/src/util.ts @@ -42,6 +42,7 @@ export function getWorkspaceJsonDirectory(clientNameOrPluginName?: string): stri return workspaceFolder; } +//used to store output in the App Package directory in the case where the workspace is a Teams Toolkit Project export function findAppPackageDirectory(directory: string): string | null { if (!fs.existsSync(directory)) { return null; diff --git a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts index a82efd3c27..e97a32d47b 100644 --- a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts +++ b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts @@ -6,42 +6,54 @@ import { getWorkspaceJsonPath } from './util'; export class WorkspaceTreeProvider implements vscode.TreeDataProvider { - constructor(public isWSPresent: boolean) { - } - async getChildren(element?: vscode.TreeItem): Promise { - if (!this.isWSPresent) { - return []; - } - if (!element) { - return [new vscode.TreeItem(KIOTA_WORKSPACE_FILE, vscode.TreeItemCollapsibleState.None)]; - } + public isWSPresent: boolean; + private _onDidChangeTreeData: vscode.EventEmitter = new vscode.EventEmitter(); + readonly onDidChangeTreeData: vscode.Event = this._onDidChangeTreeData.event; + constructor( isWSPresent: boolean) { + this.isWSPresent = isWSPresent; + } + + async refreshView(): Promise { + this._onDidChangeTreeData.fire(); +} + + async getChildren(element?: vscode.TreeItem): Promise { + if (!this.isWSPresent) { return []; } - - getTreeItem(element: vscode.TreeItem): vscode.TreeItem { - if (element) { - element.command = { - command: 'kiota.workspace.openWorkspaceFile', - title: vscode.l10n.t("Open File"), - arguments: [vscode.Uri.file(getWorkspaceJsonPath())] - }; - element.contextValue = 'file'; - } - return element; + if (!element) { + return [new vscode.TreeItem(KIOTA_WORKSPACE_FILE, vscode.TreeItemCollapsibleState.None)]; } + return []; + } + + getTreeItem(element: vscode.TreeItem): vscode.TreeItem { + if (element) { + element.command = { + command: 'kiota.workspace.openWorkspaceFile', + title: vscode.l10n.t("Open File"), + arguments: [vscode.Uri.file(getWorkspaceJsonPath())] + }; + element.contextValue = 'file'; + } + return element; + } } async function openResource(resource: vscode.Uri): Promise { await vscode.window.showTextDocument(resource); } async function isKiotaWorkspaceFilePresent(): Promise { - const workspaceFileDir = path.resolve(getWorkspaceJsonPath()); - try { - await fs.promises.access(workspaceFileDir); - } catch (error) { - return false; - } - return true; + if(!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0){ + return false; + } + const workspaceFileDir = path.resolve(getWorkspaceJsonPath()); + try { + await fs.promises.access(workspaceFileDir); + } catch (error) { + return false; + } + return true; } export async function loadTreeView(context: vscode.ExtensionContext): Promise { @@ -54,5 +66,6 @@ export async function loadTreeView(context: vscode.ExtensionContext): Promise { treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); + await treeDataProvider.refreshView(); })); }