Skip to content

Commit f1e44ef

Browse files
author
Dave Bartolomeo
authored
Merge pull request #165 from jcreedcmu/remove-items
Allow removing items from query history
2 parents a5da556 + 0e53afc commit f1e44ef

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

extensions/ql-vscode/package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@
161161
},
162162
{
163163
"command": "codeQLQueryHistory.openQuery",
164-
"title": "CodeQL: Open Query"
164+
"title": "Open Query"
165+
},
166+
{
167+
"command": "codeQLQueryHistory.removeHistoryItem",
168+
"title": "Remove History Item"
165169
},
166170
{
167171
"command": "codeQLQueryHistory.itemClicked",
@@ -196,6 +200,11 @@
196200
"command": "codeQLQueryHistory.openQuery",
197201
"group": "9_qlCommands",
198202
"when": "view == codeQLQueryHistory"
203+
},
204+
{
205+
"command": "codeQLQueryHistory.removeHistoryItem",
206+
"group": "9_qlCommands",
207+
"when": "view == codeQLQueryHistory"
199208
}
200209
],
201210
"explorer/context": [
@@ -235,6 +244,10 @@
235244
"command": "codeQLQueryHistory.openQuery",
236245
"when": "false"
237246
},
247+
{
248+
"command": "codeQLQueryHistory.removeHistoryItem",
249+
"when": "false"
250+
},
238251
{
239252
"command": "codeQLQueryHistory.itemClicked",
240253
"when": "false"

extensions/ql-vscode/src/query-history.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ class HistoryTreeDataProvider implements vscode.TreeDataProvider<QueryHistoryIte
115115
setCurrentItem(item: QueryHistoryItem) {
116116
this.current = item;
117117
}
118+
119+
remove(item: QueryHistoryItem) {
120+
if (this.current === item)
121+
this.current = undefined;
122+
const index = this.history.findIndex(i => i === item);
123+
if (index >= 0) {
124+
this.history.splice(index, 1);
125+
if (this.current === undefined && this.history.length > 0) {
126+
// Try to keep a current item, near the deleted item if there
127+
// are any available.
128+
this.current = this.history[Math.min(index, this.history.length - 1)];
129+
}
130+
this._onDidChangeTreeData.fire();
131+
}
132+
}
118133
}
119134

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

148+
async invokeCallbackOn(queryHistoryItem: QueryHistoryItem) {
149+
if (this.selectedCallback !== undefined) {
150+
const sc = this.selectedCallback;
151+
await sc(queryHistoryItem);
152+
}
153+
}
154+
133155
async handleOpenQuery(queryHistoryItem: QueryHistoryItem) {
134156
const textDocument = await vscode.workspace.openTextDocument(vscode.Uri.file(queryHistoryItem.info.query.program.queryPath));
135157
await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
136158
}
137159

160+
async handleRemoveHistoryItem(queryHistoryItem: QueryHistoryItem) {
161+
this.treeDataProvider.remove(queryHistoryItem);
162+
const current = this.treeDataProvider.getCurrent();
163+
if (current !== undefined) {
164+
this.treeView.reveal(current);
165+
await this.invokeCallbackOn(current);
166+
}
167+
}
168+
138169
async handleItemClicked(queryHistoryItem: QueryHistoryItem) {
139170
this.treeDataProvider.setCurrentItem(queryHistoryItem);
140171

@@ -150,10 +181,7 @@ export class QueryHistoryManager {
150181
}
151182
else {
152183
// show results on single click
153-
if (this.selectedCallback !== undefined) {
154-
const sc = this.selectedCallback;
155-
await sc(queryHistoryItem);
156-
}
184+
await this.invokeCallbackOn(queryHistoryItem);
157185
}
158186
}
159187

@@ -170,6 +198,7 @@ export class QueryHistoryManager {
170198
}
171199
});
172200
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.openQuery', this.handleOpenQuery));
201+
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.removeHistoryItem', this.handleRemoveHistoryItem.bind(this)));
173202
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.itemClicked', async (item) => {
174203
return this.handleItemClicked(item);
175204
}));

0 commit comments

Comments
 (0)