@@ -3,6 +3,7 @@ import { ExtensionContext, window as Window } from 'vscode';
33import { EvaluationInfo } from './queries' ;
44import * as helpers from './helpers' ;
55import * as messages from './messages' ;
6+ import { QueryHistoryConfig } from './config' ;
67/**
78 * query-history.ts
89 * ------------
@@ -21,7 +22,11 @@ export class QueryHistoryItem {
2122 databaseName : string ;
2223 info : EvaluationInfo ;
2324
24- constructor ( info : EvaluationInfo ) {
25+ constructor (
26+ info : EvaluationInfo ,
27+ public config : QueryHistoryConfig ,
28+ public label ?: string , // user-settable label
29+ ) {
2530 this . queryName = helpers . getQueryName ( info ) ;
2631 this . databaseName = info . database . name ;
2732 this . info = info ;
@@ -44,9 +49,29 @@ export class QueryHistoryItem {
4449 }
4550 }
4651
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+
4773 toString ( ) : string {
48- const { databaseName, queryName, time } = this ;
49- return `[${ time } ] ${ queryName } on ${ databaseName } - ${ this . statusString } ` ;
74+ return this . interpolate ( this . getLabel ( ) ) ;
5075 }
5176}
5277
@@ -109,7 +134,7 @@ class HistoryTreeDataProvider implements vscode.TreeDataProvider<QueryHistoryIte
109134 push ( item : QueryHistoryItem ) : void {
110135 this . current = item ;
111136 this . history . push ( item ) ;
112- this . _onDidChangeTreeData . fire ( ) ;
137+ this . refresh ( ) ;
113138 }
114139
115140 setCurrentItem ( item : QueryHistoryItem ) {
@@ -127,9 +152,13 @@ class HistoryTreeDataProvider implements vscode.TreeDataProvider<QueryHistoryIte
127152 // are any available.
128153 this . current = this . history [ Math . min ( index , this . history . length - 1 ) ] ;
129154 }
130- this . _onDidChangeTreeData . fire ( ) ;
155+ this . refresh ( ) ;
131156 }
132157 }
158+
159+ refresh ( ) {
160+ this . _onDidChangeTreeData . fire ( ) ;
161+ }
133162}
134163
135164/**
@@ -166,6 +195,23 @@ export class QueryHistoryManager {
166195 }
167196 }
168197
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+
169215 async handleItemClicked ( queryHistoryItem : QueryHistoryItem ) {
170216 this . treeDataProvider . setCurrentItem ( queryHistoryItem ) ;
171217
@@ -185,7 +231,11 @@ export class QueryHistoryManager {
185231 }
186232 }
187233
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+ ) {
189239 this . ctx = ctx ;
190240 this . selectedCallback = selectedCallback ;
191241 const treeDataProvider = this . treeDataProvider = new HistoryTreeDataProvider ( ctx ) ;
@@ -199,12 +249,17 @@ export class QueryHistoryManager {
199249 } ) ;
200250 ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.openQuery' , this . handleOpenQuery ) ) ;
201251 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 ) ) ) ;
202253 ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.itemClicked' , async ( item ) => {
203254 return this . handleItemClicked ( item ) ;
204255 } ) ) ;
256+ queryHistoryConfigListener . onDidChangeQueryHistoryConfiguration ( ( ) => {
257+ this . treeDataProvider . refresh ( ) ;
258+ } ) ;
205259 }
206260
207- push ( item : QueryHistoryItem ) {
261+ push ( evaluationInfo : EvaluationInfo ) {
262+ const item = new QueryHistoryItem ( evaluationInfo , this . queryHistoryConfigListener ) ;
208263 this . treeDataProvider . push ( item ) ;
209264 this . treeView . reveal ( item , { select : true } ) ;
210265 }
0 commit comments