Skip to content

Commit

Permalink
Fix: Display "override" message only when changes have been made (#5048)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElinorW authored Aug 1, 2024
1 parent 72e3f8b commit 48f3d9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
9 changes: 5 additions & 4 deletions vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export async function activate(
`${treeViewId}.searchOrOpenApiDescription`,
async () => {
const yesAnswer = vscode.l10n.t("Yes, override it");
if (!openApiTreeProvider.isEmpty() && openApiTreeProvider.hasSelectionChanged()) {
if (!openApiTreeProvider.isEmpty() && openApiTreeProvider.hasChanges()) {
const response = await vscode.window.showWarningMessage(
vscode.l10n.t(
"Before adding a new API description, consider that your changes and current selection will be lost."),
Expand Down Expand Up @@ -249,6 +249,7 @@ export async function activate(
clientOrPluginObject = clientObject;
workspaceGenerationType = generationType;
await loadEditPaths(clientOrPluginKey, clientObject, openApiTreeProvider);
openApiTreeProvider.resetInitialState();
await updateTreeViewIcons(treeViewId, false, true);
}),
registerCommandWithTelemetry(reporter,`${treeViewId}.regenerateButton`, async () => {
Expand Down Expand Up @@ -441,10 +442,10 @@ export async function activate(
async function displayGenerationResults(context: vscode.ExtensionContext, openApiTreeProvider: OpenApiTreeProvider, config: any, outputPath: string) {
const clientNameOrPluginName = config.clientClassName || config.pluginName;
openApiTreeProvider.refreshView();
openApiTreeProvider.setSelectionChanged(false);
const workspaceJsonPath = getWorkspaceJsonPath();
await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, clientNameOrPluginName );
await vscode.commands.executeCommand('kiota.workspace.refresh');
openApiTreeProvider.resetInitialState();
await updateTreeViewIcons(treeViewId, false, true);
}
async function regenerateClient(clientKey: string, clientObject:any, settings: ExtensionSettings, selectedPaths?: string[]): Promise<void> {
Expand Down Expand Up @@ -480,7 +481,7 @@ export async function activate(
return result;
});
void vscode.window.showInformationMessage(`Client ${clientKey} re-generated successfully.`);
openApiTreeProvider.setSelectionChanged(false);
openApiTreeProvider.resetInitialState();
}
async function regeneratePlugin(clientKey: string, clientObject:any, settings: ExtensionSettings, selectedPaths?: string[]) {
const pluginTypes = Array.isArray(clientObject.types) ? parsePluginType(clientObject.types) : [KiotaPluginType.ApiPlugin];
Expand Down Expand Up @@ -514,7 +515,7 @@ export async function activate(
return result;
});
void vscode.window.showInformationMessage(`Plugin ${clientKey} re-generated successfully.`);
openApiTreeProvider.setSelectionChanged(false);
openApiTreeProvider.resetInitialState();
}

// create a new status bar item that we can now manage
Expand Down
29 changes: 20 additions & 9 deletions vscode/microsoft-kiota/src/openApiTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as os from 'os';
import * as path from 'path';
import * as vscode from 'vscode';
import * as rpc from 'vscode-jsonrpc/node';
import * as crypto from 'crypto';
import {
ClientObjectProperties,
ClientOrPluginProperties,
Expand All @@ -23,7 +24,7 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
private _onDidChangeTreeData: vscode.EventEmitter<OpenApiTreeNode | undefined | null | void> = new vscode.EventEmitter<OpenApiTreeNode | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<OpenApiTreeNode | undefined | null | void> = this._onDidChangeTreeData.event;
private apiTitle?: string;
private selectionChanged: boolean = false;
private initialStateHash: string = '';
constructor(
private readonly context: vscode.ExtensionContext,
private readonly settingsGetter: () => ExtensionSettings,
Expand Down Expand Up @@ -168,17 +169,12 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
public isEmpty(): boolean {
return this.rawRootNode === undefined;
}
public hasSelectionChanged(): boolean {
return this.selectionChanged;
}
public setSelectionChanged(state: boolean) {
this.selectionChanged = state;
}
public async setDescriptionUrl(descriptionUrl: string): Promise<void> {
this.closeDescription(false);
this._descriptionUrl = descriptionUrl;
const settings = this.settingsGetter();
await this.loadNodes(settings.clearCache);
this.initialStateHash = this.hashDescription(this.rawRootNode);
this.refreshView();
}
public get descriptionUrl(): string {
Expand All @@ -196,7 +192,6 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
}
private selectInternal(apiNode: KiotaOpenApiNode, selected: boolean, recursive: boolean) {
apiNode.selected = selected;
this.setSelectionChanged(true);
const isOperationNode = apiNode.isOperation ?? false;
if (recursive) {
apiNode.children.forEach(x => this.selectInternal(x, selected, recursive));
Expand All @@ -206,7 +201,6 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
const parent = this.findApiNode(getPathSegments(trimOperation(apiNode.path)), this.rawRootNode);
if (parent) {
parent.selected = selected;
this.setSelectionChanged(true);
}
}
}
Expand Down Expand Up @@ -238,6 +232,23 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
return undefined;
}

private hashDescription(description: KiotaOpenApiNode | undefined): string {
const json = JSON.stringify(description);
return crypto.createHash('sha256').update(json).digest('hex');
}

public hasChanges(): boolean {
if (!this.rawRootNode) {
return false;
}
const currentStateHash = this.hashDescription(this.rawRootNode);
return currentStateHash !== this.initialStateHash;
}

public resetInitialState(): void {
this.initialStateHash = this.hashDescription(this.rawRootNode);
}

refreshView(): void {
this._onDidChangeTreeData.fire();
}
Expand Down

0 comments on commit 48f3d9f

Please sign in to comment.