Skip to content

Allow removing items from query history #165

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

Merged
merged 1 commit into from
Nov 15, 2019
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
15 changes: 14 additions & 1 deletion extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@
},
{
"command": "codeQLQueryHistory.openQuery",
"title": "CodeQL: Open Query"
"title": "Open Query"
},
{
"command": "codeQLQueryHistory.removeHistoryItem",
"title": "Remove History Item"
},
{
"command": "codeQLQueryHistory.itemClicked",
Expand Down Expand Up @@ -196,6 +200,11 @@
"command": "codeQLQueryHistory.openQuery",
"group": "9_qlCommands",
"when": "view == codeQLQueryHistory"
},
{
"command": "codeQLQueryHistory.removeHistoryItem",
"group": "9_qlCommands",
"when": "view == codeQLQueryHistory"
}
],
"explorer/context": [
Expand Down Expand Up @@ -235,6 +244,10 @@
"command": "codeQLQueryHistory.openQuery",
"when": "false"
},
{
"command": "codeQLQueryHistory.removeHistoryItem",
"when": "false"
},
{
"command": "codeQLQueryHistory.itemClicked",
"when": "false"
Expand Down
37 changes: 33 additions & 4 deletions extensions/ql-vscode/src/query-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ class HistoryTreeDataProvider implements vscode.TreeDataProvider<QueryHistoryIte
setCurrentItem(item: QueryHistoryItem) {
this.current = item;
}

remove(item: QueryHistoryItem) {
if (this.current === item)
this.current = undefined;
const index = this.history.findIndex(i => i === item);
if (index >= 0) {
this.history.splice(index, 1);
if (this.current === undefined && this.history.length > 0) {
// Try to keep a current item, near the deleted item if there
// are any available.
this.current = this.history[Math.min(index, this.history.length - 1)];
}
this._onDidChangeTreeData.fire();
}
}
}

/**
Expand All @@ -130,11 +145,27 @@ export class QueryHistoryManager {
selectedCallback: ((item: QueryHistoryItem) => void) | undefined;
lastItemClick: { time: Date, item: QueryHistoryItem } | undefined;

async invokeCallbackOn(queryHistoryItem: QueryHistoryItem) {
if (this.selectedCallback !== undefined) {
const sc = this.selectedCallback;
await sc(queryHistoryItem);
}
}

async handleOpenQuery(queryHistoryItem: QueryHistoryItem) {
const textDocument = await vscode.workspace.openTextDocument(vscode.Uri.file(queryHistoryItem.info.query.program.queryPath));
await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
}

async handleRemoveHistoryItem(queryHistoryItem: QueryHistoryItem) {
this.treeDataProvider.remove(queryHistoryItem);
const current = this.treeDataProvider.getCurrent();
if (current !== undefined) {
this.treeView.reveal(current);
await this.invokeCallbackOn(current);
}
}

async handleItemClicked(queryHistoryItem: QueryHistoryItem) {
this.treeDataProvider.setCurrentItem(queryHistoryItem);

Expand All @@ -150,10 +181,7 @@ export class QueryHistoryManager {
}
else {
// show results on single click
if (this.selectedCallback !== undefined) {
const sc = this.selectedCallback;
await sc(queryHistoryItem);
}
await this.invokeCallbackOn(queryHistoryItem);
}
}

Expand All @@ -170,6 +198,7 @@ export class QueryHistoryManager {
}
});
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.openQuery', this.handleOpenQuery));
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.removeHistoryItem', this.handleRemoveHistoryItem.bind(this)));
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.itemClicked', async (item) => {
return this.handleItemClicked(item);
}));
Expand Down