11import { commands , EventEmitter , ExtensionContext , MarkdownString , ThemeIcon , TreeItem , TreeItemCollapsibleState , window , workspace , Event } from "vscode" ;
22import { TreeDataProvider } from "vscode" ;
33import { Config } from "../../config" ;
4+ import { QueryHistoryItem } from "../../Storage" ;
45
56const 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
160178class 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