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

Consistent delete behavior from command palette #826

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/lib/deleteAppMaps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import deleteAppMap from './deleteAppMap';
import closeEditorByUri from './closeEditorByUri';
import { promisify } from 'util';
import { glob } from 'glob';
import AppMapCollection from '../services/appmapCollection';
Expand All @@ -14,7 +15,13 @@ export default async function deleteAppMaps(
vscode.Uri.file(path)
);

await Promise.all(appmapFiles.map((uri) => deleteAppMap(uri, appMapCollection)));
await Promise.all(
appmapFiles.map((uri) => {
deleteAppMap(uri, appMapCollection);
closeEditorByUri(uri);
})
);
appMapCollection.clear();

return appmapFiles.length;
}
2 changes: 2 additions & 0 deletions src/services/appmapCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export default interface AppMapCollection {
allAppMapsForWorkspaceFolder(workspaceFolder: WorkspaceFolder): AppMapLoader[];

has(uri: vscode.Uri): boolean;

clear(): void;
}
6 changes: 6 additions & 0 deletions src/services/appmapCollectionFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ export default class AppMapCollectionFile implements AppMapCollection, AppMapsSe
public has(uri: vscode.Uri): boolean {
return this.loaders.has(uri.fsPath);
}

public clear(): void {
console.debug('Clearing AppMap collection from tree', this.loaders.size);
this.loaders.clear();
this.emitUpdated(undefined);
}
}
17 changes: 15 additions & 2 deletions src/tree/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,21 @@ export default class ContextMenu {
);
context.subscriptions.push(
vscode.commands.registerCommand('appmap.context.deleteAppMap', async (item: AppMapLoader) => {
await deleteAppMap(item.descriptor.resourceUri, appmaps);
await closeEditorByUri(item.descriptor.resourceUri);
let uri: vscode.Uri;
if (!item) {
const { activeTab } = vscode.window.tabGroups.activeTabGroup;
if (!activeTab) {
vscode.window.showErrorMessage('No active editor.');
return;
}

uri = (activeTab.input as vscode.TabInputCustom).uri;
} else {
uri = item.descriptor.resourceUri;
}

await deleteAppMap(uri, appmaps);
await closeEditorByUri(uri);
})
);
context.subscriptions.push(
Expand Down
40 changes: 40 additions & 0 deletions test/integration/command/deleteAppMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as vscode from 'vscode';
import { assert } from 'chai';
import { join } from 'path';
import * as sinon from 'sinon';
import { initializeWorkspace, ProjectA, waitForExtension } from '../util';

describe('deleteAppMap test', function () {
let sandbox: sinon.SinonSandbox;

const appmapFilePath = join(
ProjectA,
'tmp/appmap/minitest',
'Microposts_controller_can_get_microposts_as_JSON.appmap.json'
);

beforeEach(() => (sandbox = sinon.createSandbox()));
beforeEach(initializeWorkspace);
beforeEach(async () => await waitForExtension());

afterEach(initializeWorkspace);
afterEach(() => sandbox.restore());

it('deletes appmap with matching URI', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockUri: vscode.Uri = { path: appmapFilePath } as any;
await vscode.workspace.openTextDocument(mockUri);
await vscode.window.showTextDocument(vscode.Uri.file(mockUri.path));
await vscode.commands.executeCommand('appmap.context.deleteAppMap');

const tabs = vscode.window.tabGroups.all.map((tg) => tg.tabs).flat();
const index = tabs.findIndex(
(tab) =>
(tab.input instanceof vscode.TabInputCustom ||
tab.input instanceof vscode.TabInputText ||
tab.input instanceof vscode.TabInputNotebook) &&
tab.input.uri.path === mockUri.path
);
assert.isTrue(index === -1);
});
});
7 changes: 4 additions & 3 deletions test/integration/lib/closeEditorByUri.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ describe('closeEditorByUri Tests', function () {
const tabs = vscode.window.tabGroups.all.map((tg) => tg.tabs).flat();
const index = tabs.findIndex(
(tab) =>
tab.input instanceof vscode.TabInputCustom ||
tab.input instanceof vscode.TabInputText ||
(tab.input instanceof vscode.TabInputNotebook && tab.input.uri.path === mockUri.path)
(tab.input instanceof vscode.TabInputCustom ||
tab.input instanceof vscode.TabInputText ||
tab.input instanceof vscode.TabInputNotebook) &&
tab.input.uri.path === mockUri.path
);

assert.isTrue(index === -1);
Expand Down