Add action

This commit is contained in:
Nora
2023-01-04 11:48:18 +00:00
parent 8346eda8b8
commit b5f865432e
5 changed files with 115 additions and 2 deletions

View File

@@ -66,6 +66,7 @@
"onCommand:codeQLDatabasesExperimental.setSelectedItemContextMenu", "onCommand:codeQLDatabasesExperimental.setSelectedItemContextMenu",
"onCommand:codeQLDatabasesExperimental.renameItemContextMenu", "onCommand:codeQLDatabasesExperimental.renameItemContextMenu",
"onCommand:codeQLDatabasesExperimental.openOnGitHubContextMenu", "onCommand:codeQLDatabasesExperimental.openOnGitHubContextMenu",
"onCommand:codeQLDatabasesExperimental.removeItemContextMenu",
"onCommand:codeQL.quickQuery", "onCommand:codeQL.quickQuery",
"onCommand:codeQL.restartQueryServer", "onCommand:codeQL.restartQueryServer",
"onWebviewPanel:resultsView", "onWebviewPanel:resultsView",
@@ -392,6 +393,10 @@
"command": "codeQLDatabasesExperimental.openOnGitHubContextMenu", "command": "codeQLDatabasesExperimental.openOnGitHubContextMenu",
"title": "Open on GitHub" "title": "Open on GitHub"
}, },
{
"command": "codeQLDatabasesExperimental.removeItemContextMenu",
"title": "Remove"
},
{ {
"command": "codeQLDatabases.chooseDatabaseFolder", "command": "codeQLDatabases.chooseDatabaseFolder",
"title": "Choose Database from Folder", "title": "Choose Database from Folder",
@@ -791,6 +796,10 @@
} }
], ],
"view/item/context": [ "view/item/context": [
{
"command": "codeQLDatabasesExperimental.removeItemContextMenu",
"when": "view == codeQLDatabasesExperimental && viewItem =~ /canBeRemoved/"
},
{ {
"command": "codeQLDatabasesExperimental.setSelectedItemContextMenu", "command": "codeQLDatabasesExperimental.setSelectedItemContextMenu",
"when": "view == codeQLDatabasesExperimental && viewItem =~ /canBeSelected/" "when": "view == codeQLDatabasesExperimental && viewItem =~ /canBeSelected/"
@@ -1043,6 +1052,10 @@
"command": "codeQLDatabasesExperimental.openOnGitHubContextMenu", "command": "codeQLDatabasesExperimental.openOnGitHubContextMenu",
"when": "false" "when": "false"
}, },
{
"command": "codeQLDatabasesExperimental.removeItemContextMenu",
"when": "false"
},
{ {
"command": "codeQLDatabases.setCurrentDatabase", "command": "codeQLDatabases.setCurrentDatabase",
"when": "false" "when": "false"

View File

@@ -22,7 +22,10 @@ import {
LocalDatabaseDbItem, LocalDatabaseDbItem,
LocalListDbItem, LocalListDbItem,
RemoteUserDefinedListDbItem, RemoteUserDefinedListDbItem,
DbItem,
DbItemKind,
} from "../db-item"; } from "../db-item";
import { mapDbItemToSelectedDbItem } from "../db-item-selection";
export class DbConfigStore extends DisposableObject { export class DbConfigStore extends DisposableObject {
public readonly onDidChangeConfig: AppEvent<void>; public readonly onDidChangeConfig: AppEvent<void>;
@@ -88,6 +91,84 @@ export class DbConfigStore extends DisposableObject {
await this.writeConfig(config); await this.writeConfig(config);
} }
public async removeDbItem(dbItem: DbItem): Promise<void> {
if (!this.config) {
throw Error("Cannot remove item if config is not loaded");
}
const config: DbConfig = cloneDbConfig(this.config);
const selectedItem: SelectedDbItem | undefined = config.selected;
// Remove item from databases
switch (dbItem.kind) {
case DbItemKind.LocalList:
config.databases.local.lists = config.databases.local.lists.filter(
(list) => list.name !== dbItem.listName,
);
break;
case DbItemKind.RemoteUserDefinedList:
config.databases.remote.repositoryLists =
config.databases.remote.repositoryLists.filter(
(list) => list.name !== dbItem.listName,
);
break;
case DbItemKind.LocalDatabase:
if (dbItem.parentListName) {
const parent = config.databases.local.lists.find(
(list) => list.name === dbItem.parentListName,
);
if (!parent) {
throw Error(`Cannot find parent list '${dbItem.parentListName}'`);
} else {
parent.databases = parent.databases.filter(
(db) => db.name !== dbItem.databaseName,
);
}
}
config.databases.local.databases =
config.databases.local.databases.filter(
(db) => db.name !== dbItem.databaseName,
);
break;
case DbItemKind.RemoteRepo:
if (dbItem.parentListName) {
const parent = config.databases.remote.repositoryLists.find(
(list) => list.name === dbItem.parentListName,
);
if (!parent) {
throw Error(`Cannot find parent list '${dbItem.parentListName}'`);
} else {
parent.repositories = parent.repositories.filter(
(repo) => repo !== dbItem.repoFullName,
);
}
}
config.databases.remote.repositories =
config.databases.remote.repositories.filter(
(repo) => repo !== dbItem.repoFullName,
);
break;
case DbItemKind.RemoteOwner:
config.databases.remote.owners = config.databases.remote.owners.filter(
(owner) => owner !== dbItem.ownerName,
);
break;
default:
throw Error(`Type '${dbItem.kind}' cannot be removed`);
}
// Remove item from selected
const mappedItem = mapDbItemToSelectedDbItem(dbItem);
if (
selectedItem &&
JSON.stringify(mappedItem) === JSON.stringify(selectedItem)
) {
config.selected = undefined;
}
await this.writeConfig(config);
}
public async addRemoteRepo( public async addRemoteRepo(
repoNwo: string, repoNwo: string,
parentList?: string, parentList?: string,

View File

@@ -80,15 +80,15 @@ export function mapDbItemToSelectedDbItem(
case DbItemKind.LocalDatabase: case DbItemKind.LocalDatabase:
return { return {
kind: SelectedDbItemKind.LocalDatabase, kind: SelectedDbItemKind.LocalDatabase,
listName: dbItem?.parentListName,
databaseName: dbItem.databaseName, databaseName: dbItem.databaseName,
listName: dbItem?.parentListName,
}; };
case DbItemKind.RemoteRepo: case DbItemKind.RemoteRepo:
return { return {
kind: SelectedDbItemKind.RemoteRepository, kind: SelectedDbItemKind.RemoteRepository,
listName: dbItem?.parentListName,
repositoryName: dbItem.repoFullName, repositoryName: dbItem.repoFullName,
listName: dbItem?.parentListName,
}; };
} }
} }

View File

@@ -75,6 +75,10 @@ export class DbManager {
} }
} }
public async removeDbItem(dbItem: DbItem): Promise<void> {
await this.dbConfigStore.removeDbItem(dbItem);
}
public async updateDbItemExpandedState( public async updateDbItemExpandedState(
dbItem: DbItem, dbItem: DbItem,
itemExpanded: boolean, itemExpanded: boolean,

View File

@@ -107,6 +107,12 @@ export class DbPanel extends DisposableObject {
(treeViewItem: DbTreeViewItem) => this.renameItem(treeViewItem), (treeViewItem: DbTreeViewItem) => this.renameItem(treeViewItem),
), ),
); );
this.push(
commandRunner(
"codeQLDatabasesExperimental.removeItemContextMenu",
(treeViewItem: DbTreeViewItem) => this.removeItem(treeViewItem),
),
);
} }
private async openConfigFile(): Promise<void> { private async openConfigFile(): Promise<void> {
@@ -363,6 +369,15 @@ export class DbPanel extends DisposableObject {
await this.dbManager.renameList(dbItem, newName); await this.dbManager.renameList(dbItem, newName);
} }
private async removeItem(treeViewItem: DbTreeViewItem): Promise<void> {
if (treeViewItem.dbItem === undefined) {
throw new Error(
"Not a removable database item. Please select a valid item.",
);
}
await this.dbManager.removeDbItem(treeViewItem.dbItem);
}
private async onDidCollapseElement( private async onDidCollapseElement(
event: TreeViewExpansionEvent<DbTreeViewItem>, event: TreeViewExpansionEvent<DbTreeViewItem>,
): Promise<void> { ): Promise<void> {