Skip to content

Commit 7c338d0

Browse files
committed
Add ability to star and unstar query history
Signed-off-by: worksofliam <[email protected]>
1 parent 949b797 commit 7c338d0

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@
310310
"id": "queryHistory",
311311
"name": "Statement History",
312312
"visibility": "visible",
313-
"when": "code-for-ibmi:connected == true && vscode-db2i:jobManager"
313+
"when": "code-for-ibmi:connected == true && vscode-db2i:jobManager.hasJob"
314314
},
315315
{
316316
"id": "jobManager",
@@ -587,6 +587,12 @@
587587
"category": "Db2 for i",
588588
"icon": "$(search)"
589589
},
590+
{
591+
"command": "vscode-db2i.queryHistory.toggleStar",
592+
"title": "Star statement",
593+
"category": "Db2 for i",
594+
"icon": "$(star)"
595+
},
590596
{
591597
"command": "vscode-db2i.openSqlDocument",
592598
"title": "Open SQL Document",
@@ -870,6 +876,10 @@
870876
"command": "vscode-db2i.queryHistory.find",
871877
"when": "never"
872878
},
879+
{
880+
"command": "vscode-db2i.queryHistory.toggleStar",
881+
"when": "never"
882+
},
873883
{
874884
"command": "vscode-db2i.jobManager.closeJob",
875885
"when": "never"
@@ -1159,6 +1169,11 @@
11591169
"when": "view == queryHistory && viewItem == query",
11601170
"group": "inline"
11611171
},
1172+
{
1173+
"command": "vscode-db2i.queryHistory.toggleStar",
1174+
"when": "view == queryHistory && viewItem == query",
1175+
"group": "inline"
1176+
},
11621177
{
11631178
"command": "vscode-db2i.jobManager.closeJob",
11641179
"when": "view == jobManager && viewItem == sqlJob",

src/Storage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const SERVERCOMPONENT_KEY = `serverVersion`
66
export interface QueryHistoryItem {
77
query: string;
88
unix: number;
9+
starred?: boolean;
910
}
1011

1112
export type QueryList = QueryHistoryItem[];

src/views/queryHistoryView/contributes.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"id": "queryHistory",
77
"name": "Statement History",
88
"visibility": "visible",
9-
"when": "code-for-ibmi:connected == true && vscode-db2i:jobManager"
9+
"when": "code-for-ibmi:connected == true && vscode-db2i:jobManager.hasJob"
1010
}
1111
]
1212
},
@@ -34,6 +34,12 @@
3434
"title": "Search query history",
3535
"category": "Db2 for i",
3636
"icon": "$(search)"
37+
},
38+
{
39+
"command": "vscode-db2i.queryHistory.toggleStar",
40+
"title": "Star statement",
41+
"category": "Db2 for i",
42+
"icon": "$(star)"
3743
}
3844
],
3945
"menus": {
@@ -45,6 +51,10 @@
4551
{
4652
"command": "vscode-db2i.queryHistory.find",
4753
"when": "never"
54+
},
55+
{
56+
"command": "vscode-db2i.queryHistory.toggleStar",
57+
"when": "never"
4858
}
4959
],
5060
"view/title": [
@@ -64,6 +74,11 @@
6474
"command": "vscode-db2i.queryHistory.remove",
6575
"when": "view == queryHistory && viewItem == query",
6676
"group": "inline"
77+
},
78+
{
79+
"command": "vscode-db2i.queryHistory.toggleStar",
80+
"when": "view == queryHistory && viewItem == query",
81+
"group": "inline"
6782
}
6883
]
6984
}

src/views/queryHistoryView/index.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { commands, EventEmitter, ExtensionContext, MarkdownString, ThemeIcon, TreeItem, TreeItemCollapsibleState, window, workspace, Event } from "vscode";
22
import { TreeDataProvider } from "vscode";
33
import { Config } from "../../config";
4+
import { QueryHistoryItem } from "../../Storage";
45

56
const openSqlDocumentCommand = `vscode-db2i.openSqlDocument`;
67

@@ -47,12 +48,28 @@ export class queryHistory implements TreeDataProvider<any> {
4748
}
4849
}),
4950

51+
commands.registerCommand(`vscode-db2i.queryHistory.toggleStar`, async (node: PastQueryNode) => {
52+
if (node && Config.ready) {
53+
let currentList = Config.getPastQueries();
54+
const existingQuery = currentList.findIndex(queryItem =>
55+
queryItem.unix === node.item.unix
56+
);
57+
58+
// If it exists, remove it
59+
if (existingQuery >= 0) {
60+
// Toggle the starred status
61+
currentList[existingQuery].starred = !(currentList[existingQuery].starred === true);
62+
await Config.setPastQueries(currentList);
63+
this.refresh();
64+
}
65+
}
66+
}),
67+
5068
commands.registerCommand(`vscode-db2i.queryHistory.remove`, async (node: PastQueryNode) => {
5169
if (node && Config.ready) {
5270
let currentList = Config.getPastQueries();
53-
const chosenQuery = node.query;
5471
const existingQuery = currentList.findIndex(queryItem =>
55-
queryItem.query.trim() === chosenQuery.trim()
72+
queryItem.unix === node.item.unix
5673
);
5774

5875
// If it exists, remove it
@@ -65,10 +82,11 @@ export class queryHistory implements TreeDataProvider<any> {
6582
}),
6683

6784
commands.registerCommand(`vscode-db2i.queryHistory.clear`, async () => {
68-
window.showInformationMessage(`Statement history`, {detail: `Are you sure you want to clear your statement history?`, modal: true}, `Clear`).then(async (result) => {
85+
window.showInformationMessage(`Statement history`, {detail: `Are you sure you want to clear your statement history? This will not remove starred items.`, modal: true}, `Clear`).then(async (result) => {
6986
if (result) {
7087
if (Config.ready) {
71-
await Config.setPastQueries([]);
88+
const starredItems = Config.getPastQueries().filter(queryItem => queryItem.starred === true);
89+
await Config.setPastQueries(starredItems);
7290
this.refresh();
7391
}
7492
}
@@ -110,13 +128,13 @@ export class queryHistory implements TreeDataProvider<any> {
110128
currentList.forEach(queryItem => {
111129
// The smaller the unix value, the older it is
112130
if (queryItem.unix < monthAgo) {
113-
olderQueries.push(new PastQueryNode(queryItem.query));
131+
olderQueries.push(new PastQueryNode(queryItem));
114132
} else if (queryItem.unix < weekAgo) {
115-
pastMonthQueries.push(new PastQueryNode(queryItem.query));
133+
pastMonthQueries.push(new PastQueryNode(queryItem));
116134
} else if (queryItem.unix < dayAgo) {
117-
pastWeekQueries.push(new PastQueryNode(queryItem.query));
135+
pastWeekQueries.push(new PastQueryNode(queryItem));
118136
} else {
119-
pastDayQueries.push(new PastQueryNode(queryItem.query));
137+
pastDayQueries.push(new PastQueryNode(queryItem));
120138
}
121139
});
122140

@@ -158,19 +176,19 @@ class TimePeriodNode extends TreeItem {
158176
}
159177

160178
class PastQueryNode extends TreeItem {
161-
constructor(public query: string) {
162-
super(query);
179+
constructor(public item: QueryHistoryItem) {
180+
super(item.query);
163181

164182
this.contextValue = `query`;
165183

166-
this.tooltip = new MarkdownString(['```sql', query, '```'].join(`\n`));
184+
this.tooltip = new MarkdownString(['```sql', item.query, '```'].join(`\n`));
167185

168186
this.command = {
169187
command: openSqlDocumentCommand,
170-
arguments: [query],
188+
arguments: [item.query],
171189
title: `Open into new document`
172190
};
173191

174-
this.iconPath = new ThemeIcon(`go-to-file`);
192+
this.iconPath = new ThemeIcon(item.starred ? `star` : `go-to-file`);
175193
}
176194
}

0 commit comments

Comments
 (0)