1
1
import { commands , EventEmitter , ExtensionContext , MarkdownString , ThemeIcon , TreeItem , TreeItemCollapsibleState , window , workspace , Event } from "vscode" ;
2
2
import { TreeDataProvider } from "vscode" ;
3
3
import { Config } from "../../config" ;
4
+ import { QueryHistoryItem } from "../../Storage" ;
4
5
5
6
const openSqlDocumentCommand = `vscode-db2i.openSqlDocument` ;
6
7
@@ -18,6 +19,10 @@ export class queryHistory implements TreeDataProvider<any> {
18
19
window . showTextDocument ( doc ) ;
19
20
} ) ;
20
21
} ) ,
22
+ commands . registerCommand ( `vscode-db2i.queryHistory.find` , async ( ) => {
23
+ commands . executeCommand ( 'queryHistory.focus' ) ;
24
+ commands . executeCommand ( 'list.find' ) ;
25
+ } ) ,
21
26
22
27
commands . registerCommand ( `vscode-db2i.queryHistory.prepend` , async ( newQuery ?: string ) => {
23
28
if ( newQuery && Config . ready ) {
@@ -43,12 +48,28 @@ export class queryHistory implements TreeDataProvider<any> {
43
48
}
44
49
} ) ,
45
50
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
+
46
68
commands . registerCommand ( `vscode-db2i.queryHistory.remove` , async ( node : PastQueryNode ) => {
47
69
if ( node && Config . ready ) {
48
70
let currentList = Config . getPastQueries ( ) ;
49
- const chosenQuery = node . query ;
50
71
const existingQuery = currentList . findIndex ( queryItem =>
51
- queryItem . query . trim ( ) === chosenQuery . trim ( )
72
+ queryItem . unix === node . item . unix
52
73
) ;
53
74
54
75
// If it exists, remove it
@@ -61,10 +82,15 @@ export class queryHistory implements TreeDataProvider<any> {
61
82
} ) ,
62
83
63
84
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
+ } ) ;
68
94
} ) ,
69
95
)
70
96
}
@@ -98,24 +124,29 @@ export class queryHistory implements TreeDataProvider<any> {
98
124
let pastWeekQueries : PastQueryNode [ ] = [ ] ;
99
125
let pastMonthQueries : PastQueryNode [ ] = [ ] ;
100
126
let olderQueries : PastQueryNode [ ] = [ ] ;
127
+ const starredQueries = currentList . filter ( queryItem => queryItem . starred ) ;
128
+ const hasStarredQueries = starredQueries . length > 0 ;
101
129
102
130
currentList . forEach ( queryItem => {
103
131
// The smaller the unix value, the older it is
104
132
if ( queryItem . unix < monthAgo ) {
105
- olderQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
133
+ olderQueries . push ( new PastQueryNode ( queryItem ) ) ;
106
134
} else if ( queryItem . unix < weekAgo ) {
107
- pastMonthQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
135
+ pastMonthQueries . push ( new PastQueryNode ( queryItem ) ) ;
108
136
} else if ( queryItem . unix < dayAgo ) {
109
- pastWeekQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
137
+ pastWeekQueries . push ( new PastQueryNode ( queryItem ) ) ;
110
138
} else {
111
- pastDayQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
139
+ pastDayQueries . push ( new PastQueryNode ( queryItem ) ) ;
112
140
}
113
141
} ) ;
114
142
115
143
let nodes : TimePeriodNode [ ] = [ ] ;
116
144
145
+ if ( hasStarredQueries ) {
146
+ nodes . push ( new TimePeriodNode ( `Starred` , starredQueries . map ( q => new PastQueryNode ( q ) ) , { expanded : true , stars : true } ) ) ;
147
+ }
117
148
if ( pastDayQueries . length > 0 ) {
118
- nodes . push ( new TimePeriodNode ( `Past day` , pastDayQueries , true ) ) ;
149
+ nodes . push ( new TimePeriodNode ( `Past day` , pastDayQueries , { expanded : ! hasStarredQueries } ) ) ;
119
150
}
120
151
if ( pastWeekQueries . length > 0 ) {
121
152
nodes . push ( new TimePeriodNode ( `Past week` , pastWeekQueries ) ) ;
@@ -137,11 +168,11 @@ export class queryHistory implements TreeDataProvider<any> {
137
168
}
138
169
139
170
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 ) ;
142
173
this . contextValue = `timePeriod` ;
143
174
144
- this . iconPath = new ThemeIcon ( `calendar` ) ;
175
+ this . iconPath = new ThemeIcon ( opts . stars ? `star-full` : `calendar` ) ;
145
176
}
146
177
147
178
getChildren ( ) {
@@ -150,19 +181,19 @@ class TimePeriodNode extends TreeItem {
150
181
}
151
182
152
183
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 ) ;
155
186
156
187
this . contextValue = `query` ;
157
188
158
- this . tooltip = new MarkdownString ( [ '```sql' , query , '```' ] . join ( `\n` ) ) ;
189
+ this . tooltip = new MarkdownString ( [ '```sql' , item . query , '```' ] . join ( `\n` ) ) ;
159
190
160
191
this . command = {
161
192
command : openSqlDocumentCommand ,
162
- arguments : [ query ] ,
193
+ arguments : [ item . query ] ,
163
194
title : `Open into new document`
164
195
} ;
165
196
166
- this . iconPath = new ThemeIcon ( `go-to-file` ) ;
197
+ this . iconPath = new ThemeIcon ( item . starred ? `star` : `go-to-file` ) ;
167
198
}
168
199
}
0 commit comments