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
@@ -47,12 +48,28 @@ export class queryHistory implements TreeDataProvider<any> {
47
48
}
48
49
} ) ,
49
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
+
50
68
commands . registerCommand ( `vscode-db2i.queryHistory.remove` , async ( node : PastQueryNode ) => {
51
69
if ( node && Config . ready ) {
52
70
let currentList = Config . getPastQueries ( ) ;
53
- const chosenQuery = node . query ;
54
71
const existingQuery = currentList . findIndex ( queryItem =>
55
- queryItem . query . trim ( ) === chosenQuery . trim ( )
72
+ queryItem . unix === node . item . unix
56
73
) ;
57
74
58
75
// If it exists, remove it
@@ -65,10 +82,11 @@ export class queryHistory implements TreeDataProvider<any> {
65
82
} ) ,
66
83
67
84
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 ) => {
69
86
if ( result ) {
70
87
if ( Config . ready ) {
71
- await Config . setPastQueries ( [ ] ) ;
88
+ const starredItems = Config . getPastQueries ( ) . filter ( queryItem => queryItem . starred === true ) ;
89
+ await Config . setPastQueries ( starredItems ) ;
72
90
this . refresh ( ) ;
73
91
}
74
92
}
@@ -110,13 +128,13 @@ export class queryHistory implements TreeDataProvider<any> {
110
128
currentList . forEach ( queryItem => {
111
129
// The smaller the unix value, the older it is
112
130
if ( queryItem . unix < monthAgo ) {
113
- olderQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
131
+ olderQueries . push ( new PastQueryNode ( queryItem ) ) ;
114
132
} else if ( queryItem . unix < weekAgo ) {
115
- pastMonthQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
133
+ pastMonthQueries . push ( new PastQueryNode ( queryItem ) ) ;
116
134
} else if ( queryItem . unix < dayAgo ) {
117
- pastWeekQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
135
+ pastWeekQueries . push ( new PastQueryNode ( queryItem ) ) ;
118
136
} else {
119
- pastDayQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
137
+ pastDayQueries . push ( new PastQueryNode ( queryItem ) ) ;
120
138
}
121
139
} ) ;
122
140
@@ -158,19 +176,19 @@ class TimePeriodNode extends TreeItem {
158
176
}
159
177
160
178
class PastQueryNode extends TreeItem {
161
- constructor ( public query : string ) {
162
- super ( query ) ;
179
+ constructor ( public item : QueryHistoryItem ) {
180
+ super ( item . query ) ;
163
181
164
182
this . contextValue = `query` ;
165
183
166
- this . tooltip = new MarkdownString ( [ '```sql' , query , '```' ] . join ( `\n` ) ) ;
184
+ this . tooltip = new MarkdownString ( [ '```sql' , item . query , '```' ] . join ( `\n` ) ) ;
167
185
168
186
this . command = {
169
187
command : openSqlDocumentCommand ,
170
- arguments : [ query ] ,
188
+ arguments : [ item . query ] ,
171
189
title : `Open into new document`
172
190
} ;
173
191
174
- this . iconPath = new ThemeIcon ( `go-to-file` ) ;
192
+ this . iconPath = new ThemeIcon ( item . starred ? `star` : `go-to-file` ) ;
175
193
}
176
194
}
0 commit comments