Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: kiota workspace tab correction #5897

Merged
merged 9 commits into from
Dec 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class RegenerateButtonCommand extends Command {
await regenerateService.regenerateTeamsApp(workspaceJson, clientOrPluginKey);
}
}
await vscode.commands.executeCommand('kiota.workspace.refresh');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ export class RegenerateCommand extends Command {
await regenerateService.regenerateTeamsApp(workspaceJson, clientOrPluginKey);
}
}
await vscode.commands.executeCommand('kiota.workspace.refresh');
}
}
2 changes: 1 addition & 1 deletion vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
29 changes: 11 additions & 18 deletions vscode/microsoft-kiota/src/providers/workspaceTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceT
private workspaceContentService: WorkspaceContentService,
private sharedService: SharedService
) {
void this.loadContent();
}

async refreshView(): Promise<void> {
Expand All @@ -47,11 +46,15 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceT
}

if (!element) {
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 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')
];
}

Expand All @@ -64,9 +67,6 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceT
if (Object.keys(this.workspaceContent.plugins).length > 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;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceT
case 'root':
element.command = {
command: 'kiota.workspace.openWorkspaceFile',
title: vscode.l10n.t("Open File"),
title: vscode.l10n.t('Open File'),
arguments: [vscode.Uri.file(getWorkspaceJsonPath())]
};
element.contextValue = 'folder';
Expand Down Expand Up @@ -139,12 +139,5 @@ export async function loadTreeView(context: vscode.ExtensionContext, treeDataPro
await vscode.commands.executeCommand('kiota.editPaths', label, properties, category);
})
);
context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument(async (event: vscode.TextDocumentChangeEvent) => {
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');
};
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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;
});

Expand Down
Loading