@@ -3,6 +3,7 @@ import { ExtensionContext, window as Window } from 'vscode';
3
3
import { EvaluationInfo } from './queries' ;
4
4
import * as helpers from './helpers' ;
5
5
import * as messages from './messages' ;
6
+ import { QueryHistoryConfig } from './config' ;
6
7
/**
7
8
* query-history.ts
8
9
* ------------
@@ -21,7 +22,11 @@ export class QueryHistoryItem {
21
22
databaseName : string ;
22
23
info : EvaluationInfo ;
23
24
24
- constructor ( info : EvaluationInfo ) {
25
+ constructor (
26
+ info : EvaluationInfo ,
27
+ public config : QueryHistoryConfig ,
28
+ public label ?: string , // user-settable label
29
+ ) {
25
30
this . queryName = helpers . getQueryName ( info ) ;
26
31
this . databaseName = info . database . name ;
27
32
this . info = info ;
@@ -44,9 +49,29 @@ export class QueryHistoryItem {
44
49
}
45
50
}
46
51
52
+ interpolate ( template : string ) : string {
53
+ const { databaseName, queryName, time, statusString } = this ;
54
+ const replacements : { [ k : string ] : string } = {
55
+ t : time ,
56
+ q : queryName ,
57
+ d : databaseName ,
58
+ s : statusString ,
59
+ '%' : '%' ,
60
+ } ;
61
+ return template . replace ( / % ( .) / g, ( match , key ) => {
62
+ const replacement = replacements [ key ] ;
63
+ return replacement !== undefined ? replacement : match ;
64
+ } ) ;
65
+ }
66
+
67
+ getLabel ( ) : string {
68
+ if ( this . label !== undefined )
69
+ return this . label ;
70
+ return this . config . format ;
71
+ }
72
+
47
73
toString ( ) : string {
48
- const { databaseName, queryName, time } = this ;
49
- return `[${ time } ] ${ queryName } on ${ databaseName } - ${ this . statusString } ` ;
74
+ return this . interpolate ( this . getLabel ( ) ) ;
50
75
}
51
76
}
52
77
@@ -109,7 +134,7 @@ class HistoryTreeDataProvider implements vscode.TreeDataProvider<QueryHistoryIte
109
134
push ( item : QueryHistoryItem ) : void {
110
135
this . current = item ;
111
136
this . history . push ( item ) ;
112
- this . _onDidChangeTreeData . fire ( ) ;
137
+ this . refresh ( ) ;
113
138
}
114
139
115
140
setCurrentItem ( item : QueryHistoryItem ) {
@@ -127,9 +152,13 @@ class HistoryTreeDataProvider implements vscode.TreeDataProvider<QueryHistoryIte
127
152
// are any available.
128
153
this . current = this . history [ Math . min ( index , this . history . length - 1 ) ] ;
129
154
}
130
- this . _onDidChangeTreeData . fire ( ) ;
155
+ this . refresh ( ) ;
131
156
}
132
157
}
158
+
159
+ refresh ( ) {
160
+ this . _onDidChangeTreeData . fire ( ) ;
161
+ }
133
162
}
134
163
135
164
/**
@@ -166,6 +195,23 @@ export class QueryHistoryManager {
166
195
}
167
196
}
168
197
198
+ async handleSetLabel ( queryHistoryItem : QueryHistoryItem ) {
199
+ const response = await vscode . window . showInputBox ( {
200
+ prompt : 'Label:' ,
201
+ placeHolder : '(use default)' ,
202
+ value : queryHistoryItem . getLabel ( ) ,
203
+ } ) ;
204
+ // undefined response means the user cancelled the dialog; don't change anything
205
+ if ( response !== undefined ) {
206
+ if ( response === '' )
207
+ // Interpret empty string response as "go back to using default"
208
+ queryHistoryItem . label = undefined ;
209
+ else
210
+ queryHistoryItem . label = response ;
211
+ this . treeDataProvider . refresh ( ) ;
212
+ }
213
+ }
214
+
169
215
async handleItemClicked ( queryHistoryItem : QueryHistoryItem ) {
170
216
this . treeDataProvider . setCurrentItem ( queryHistoryItem ) ;
171
217
@@ -185,7 +231,11 @@ export class QueryHistoryManager {
185
231
}
186
232
}
187
233
188
- constructor ( ctx : ExtensionContext , selectedCallback ?: ( item : QueryHistoryItem ) => Promise < void > ) {
234
+ constructor (
235
+ ctx : ExtensionContext ,
236
+ private queryHistoryConfigListener : QueryHistoryConfig ,
237
+ selectedCallback ?: ( item : QueryHistoryItem ) => Promise < void >
238
+ ) {
189
239
this . ctx = ctx ;
190
240
this . selectedCallback = selectedCallback ;
191
241
const treeDataProvider = this . treeDataProvider = new HistoryTreeDataProvider ( ctx ) ;
@@ -199,12 +249,17 @@ export class QueryHistoryManager {
199
249
} ) ;
200
250
ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.openQuery' , this . handleOpenQuery ) ) ;
201
251
ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.removeHistoryItem' , this . handleRemoveHistoryItem . bind ( this ) ) ) ;
252
+ ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.setLabel' , this . handleSetLabel . bind ( this ) ) ) ;
202
253
ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.itemClicked' , async ( item ) => {
203
254
return this . handleItemClicked ( item ) ;
204
255
} ) ) ;
256
+ queryHistoryConfigListener . onDidChangeQueryHistoryConfiguration ( ( ) => {
257
+ this . treeDataProvider . refresh ( ) ;
258
+ } ) ;
205
259
}
206
260
207
- push ( item : QueryHistoryItem ) {
261
+ push ( evaluationInfo : EvaluationInfo ) {
262
+ const item = new QueryHistoryItem ( evaluationInfo , this . queryHistoryConfigListener ) ;
208
263
this . treeDataProvider . push ( item ) ;
209
264
this . treeView . reveal ( item , { select : true } ) ;
210
265
}
0 commit comments