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
@@ -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
139170class 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
152183class 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