Skip to content

Commit ed221c1

Browse files
authored
Merge pull request #401 from codefori/feature/statement_history_improvements
Filter and stars
2 parents a7983de + a8c3dd2 commit ed221c1

File tree

5 files changed

+113
-41
lines changed

5 files changed

+113
-41
lines changed

package.json

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
"id": "queryHistory",
321321
"name": "Statement History",
322322
"visibility": "visible",
323-
"when": "code-for-ibmi:connected == true"
323+
"when": "code-for-ibmi:connected == true && vscode-db2i:jobManager.hasJob"
324324
},
325325
{
326326
"id": "jobManager",
@@ -591,6 +591,18 @@
591591
"category": "Db2 for i",
592592
"icon": "$(trash)"
593593
},
594+
{
595+
"command": "vscode-db2i.queryHistory.find",
596+
"title": "Search query history",
597+
"category": "Db2 for i",
598+
"icon": "$(search)"
599+
},
600+
{
601+
"command": "vscode-db2i.queryHistory.toggleStar",
602+
"title": "Star statement",
603+
"category": "Db2 for i",
604+
"icon": "$(star)"
605+
},
594606
{
595607
"command": "vscode-db2i.openSqlDocument",
596608
"title": "Open SQL Document",
@@ -870,6 +882,14 @@
870882
"command": "vscode-db2i.queryHistory.remove",
871883
"when": "never"
872884
},
885+
{
886+
"command": "vscode-db2i.queryHistory.find",
887+
"when": "never"
888+
},
889+
{
890+
"command": "vscode-db2i.queryHistory.toggleStar",
891+
"when": "never"
892+
},
873893
{
874894
"command": "vscode-db2i.jobManager.closeJob",
875895
"when": "never"
@@ -983,6 +1003,11 @@
9831003
"group": "navigation",
9841004
"when": "view == queryHistory"
9851005
},
1006+
{
1007+
"command": "vscode-db2i.queryHistory.find",
1008+
"group": "navigation",
1009+
"when": "view == queryHistory"
1010+
},
9861011
{
9871012
"command": "vscode-db2i.jobManager.newJob",
9881013
"group": "navigation@1",
@@ -1154,6 +1179,11 @@
11541179
"when": "view == queryHistory && viewItem == query",
11551180
"group": "inline"
11561181
},
1182+
{
1183+
"command": "vscode-db2i.queryHistory.toggleStar",
1184+
"when": "view == queryHistory && viewItem == query",
1185+
"group": "inline"
1186+
},
11571187
{
11581188
"command": "vscode-db2i.jobManager.closeJob",
11591189
"when": "view == jobManager && viewItem == sqlJob",

src/Storage.ts

Lines changed: 1 addition & 18 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[];
@@ -59,24 +60,6 @@ export class ConnectionStorage extends Storage {
5960
return this.set(SERVERCOMPONENT_KEY, name);
6061
}
6162

62-
/**
63-
* Eventually we will want to remove this function, but for now we need to fix the past queries
64-
*/
65-
fixPastQueries() {
66-
const currentList = this.getPastQueries() as (string|QueryHistoryItem)[];
67-
const hasOldFormat = currentList.some(item => typeof item === `string`);
68-
if (hasOldFormat) {
69-
const newList = currentList.map(item => {
70-
if (typeof item === `string`) {
71-
return { query: item, unix: Math.floor(Date.now() / 1000) - 86400 };
72-
} else {
73-
return item;
74-
}
75-
});
76-
return this.setPastQueries(newList);
77-
}
78-
}
79-
8063
getPastQueries() {
8164
return this.get<QueryList>(QUERIES_KEY) || [];
8265
}

src/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export async function onConnectOrServerInstall(): Promise<boolean> {
1919

2020
Config.setConnectionName(instance.getConnection().currentConnectionName);
2121

22-
await Config.fixPastQueries();
23-
2422
osDetail = new IBMiDetail();
2523

2624
await osDetail.fetchSystemInfo();

src/views/queryHistoryView/contributes.json

Lines changed: 31 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"
9+
"when": "code-for-ibmi:connected == true && vscode-db2i:jobManager.hasJob"
1010
}
1111
]
1212
},
@@ -28,27 +28,57 @@
2828
"title": "Clear statement history",
2929
"category": "Db2 for i",
3030
"icon": "$(trash)"
31+
},
32+
{
33+
"command": "vscode-db2i.queryHistory.find",
34+
"title": "Search query history",
35+
"category": "Db2 for i",
36+
"icon": "$(search)"
37+
},
38+
{
39+
"command": "vscode-db2i.queryHistory.toggleStar",
40+
"title": "Star statement",
41+
"category": "Db2 for i",
42+
"icon": "$(star)"
3143
}
3244
],
3345
"menus": {
3446
"commandPalette": [
3547
{
3648
"command": "vscode-db2i.queryHistory.remove",
3749
"when": "never"
50+
},
51+
{
52+
"command": "vscode-db2i.queryHistory.find",
53+
"when": "never"
54+
},
55+
{
56+
"command": "vscode-db2i.queryHistory.toggleStar",
57+
"when": "never"
3858
}
3959
],
4060
"view/title": [
4161
{
4262
"command": "vscode-db2i.queryHistory.clear",
4363
"group": "navigation",
4464
"when": "view == queryHistory"
65+
},
66+
{
67+
"command": "vscode-db2i.queryHistory.find",
68+
"group": "navigation",
69+
"when": "view == queryHistory"
4570
}
4671
],
4772
"view/item/context": [
4873
{
4974
"command": "vscode-db2i.queryHistory.remove",
5075
"when": "view == queryHistory && viewItem == query",
5176
"group": "inline"
77+
},
78+
{
79+
"command": "vscode-db2i.queryHistory.toggleStar",
80+
"when": "view == queryHistory && viewItem == query",
81+
"group": "inline"
5282
}
5383
]
5484
}

src/views/queryHistoryView/index.ts

Lines changed: 50 additions & 19 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

@@ -18,6 +19,10 @@ export class queryHistory implements TreeDataProvider<any> {
1819
window.showTextDocument(doc);
1920
});
2021
}),
22+
commands.registerCommand(`vscode-db2i.queryHistory.find`, async () => {
23+
commands.executeCommand('queryHistory.focus');
24+
commands.executeCommand('list.find');
25+
}),
2126

2227
commands.registerCommand(`vscode-db2i.queryHistory.prepend`, async (newQuery?: string) => {
2328
if (newQuery && Config.ready) {
@@ -43,12 +48,28 @@ export class queryHistory implements TreeDataProvider<any> {
4348
}
4449
}),
4550

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+
4668
commands.registerCommand(`vscode-db2i.queryHistory.remove`, async (node: PastQueryNode) => {
4769
if (node && Config.ready) {
4870
let currentList = Config.getPastQueries();
49-
const chosenQuery = node.query;
5071
const existingQuery = currentList.findIndex(queryItem =>
51-
queryItem.query.trim() === chosenQuery.trim()
72+
queryItem.unix === node.item.unix
5273
);
5374

5475
// If it exists, remove it
@@ -61,10 +82,15 @@ export class queryHistory implements TreeDataProvider<any> {
6182
}),
6283

6384
commands.registerCommand(`vscode-db2i.queryHistory.clear`, async () => {
64-
if (Config.ready) {
65-
await Config.setPastQueries([]);
66-
this.refresh();
67-
}
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) => {
86+
if (result) {
87+
if (Config.ready) {
88+
const starredItems = Config.getPastQueries().filter(queryItem => queryItem.starred === true);
89+
await Config.setPastQueries(starredItems);
90+
this.refresh();
91+
}
92+
}
93+
});
6894
}),
6995
)
7096
}
@@ -98,24 +124,29 @@ export class queryHistory implements TreeDataProvider<any> {
98124
let pastWeekQueries: PastQueryNode[] = [];
99125
let pastMonthQueries: PastQueryNode[] = [];
100126
let olderQueries: PastQueryNode[] = [];
127+
const starredQueries = currentList.filter(queryItem => queryItem.starred);
128+
const hasStarredQueries = starredQueries.length > 0;
101129

102130
currentList.forEach(queryItem => {
103131
// The smaller the unix value, the older it is
104132
if (queryItem.unix < monthAgo) {
105-
olderQueries.push(new PastQueryNode(queryItem.query));
133+
olderQueries.push(new PastQueryNode(queryItem));
106134
} else if (queryItem.unix < weekAgo) {
107-
pastMonthQueries.push(new PastQueryNode(queryItem.query));
135+
pastMonthQueries.push(new PastQueryNode(queryItem));
108136
} else if (queryItem.unix < dayAgo) {
109-
pastWeekQueries.push(new PastQueryNode(queryItem.query));
137+
pastWeekQueries.push(new PastQueryNode(queryItem));
110138
} else {
111-
pastDayQueries.push(new PastQueryNode(queryItem.query));
139+
pastDayQueries.push(new PastQueryNode(queryItem));
112140
}
113141
});
114142

115143
let nodes: TimePeriodNode[] = [];
116144

145+
if (hasStarredQueries) {
146+
nodes.push(new TimePeriodNode(`Starred`, starredQueries.map(q => new PastQueryNode(q)), {expanded: true, stars: true}));
147+
}
117148
if (pastDayQueries.length > 0) {
118-
nodes.push(new TimePeriodNode(`Past day`, pastDayQueries, true));
149+
nodes.push(new TimePeriodNode(`Past day`, pastDayQueries, {expanded: !hasStarredQueries}));
119150
}
120151
if (pastWeekQueries.length > 0) {
121152
nodes.push(new TimePeriodNode(`Past week`, pastWeekQueries));
@@ -137,11 +168,11 @@ export class queryHistory implements TreeDataProvider<any> {
137168
}
138169

139170
class TimePeriodNode extends TreeItem {
140-
constructor(public period: string, private nodes: PastQueryNode[], expanded = false) {
141-
super(period, expanded ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed);
171+
constructor(title: string, private nodes: PastQueryNode[], opts: { expanded?: boolean, stars?: boolean } = {}) {
172+
super(title, opts.expanded ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed);
142173
this.contextValue = `timePeriod`;
143174

144-
this.iconPath = new ThemeIcon(`calendar`);
175+
this.iconPath = new ThemeIcon(opts.stars ? `star-full` : `calendar`);
145176
}
146177

147178
getChildren() {
@@ -150,19 +181,19 @@ class TimePeriodNode extends TreeItem {
150181
}
151182

152183
class PastQueryNode extends TreeItem {
153-
constructor(public query: string) {
154-
super(query.length > 63 ? query.substring(0, 60) + `...` : query);
184+
constructor(public item: QueryHistoryItem) {
185+
super(item.query);
155186

156187
this.contextValue = `query`;
157188

158-
this.tooltip = new MarkdownString(['```sql', query, '```'].join(`\n`));
189+
this.tooltip = new MarkdownString(['```sql', item.query, '```'].join(`\n`));
159190

160191
this.command = {
161192
command: openSqlDocumentCommand,
162-
arguments: [query],
193+
arguments: [item.query],
163194
title: `Open into new document`
164195
};
165196

166-
this.iconPath = new ThemeIcon(`go-to-file`);
197+
this.iconPath = new ThemeIcon(item.starred ? `star` : `go-to-file`);
167198
}
168199
}

0 commit comments

Comments
 (0)