11import * as grpc from '@grpc/grpc-js' ;
2+ import { Duration } from 'luxon' ;
23import * as vscode from 'vscode' ;
34import { event } from 'vscode-common' ;
45import { CommandCodeLensProvider } from '../../api' ;
@@ -154,6 +155,22 @@ export class CodeSearchCodeLens implements CommandCodeLensProvider, vscode.Dispo
154155
155156 async createScope ( opts : CodesearchIndexOptions , client : BzlCodesearch , ws : Workspace , output : OutputChannel ) : Promise < void > {
156157
158+ let command = 'query' ;
159+ const query : string [ ] = [ ] ;
160+ const options : string [ ] = [ ] ;
161+ let sink = query ;
162+ for ( let i = 0 ; i < opts . args . length ; i ++ ) {
163+ const arg = opts . args [ i ] ;
164+ if ( i === 0 && ( arg === 'query' || arg === 'cquery' ) ) {
165+ command = arg ;
166+ continue ;
167+ }
168+ if ( arg === '--' ) {
169+ sink = options ;
170+ continue ;
171+ }
172+ sink . push ( arg ) ;
173+ }
157174 const queryExpression = opts . args . join ( ' ' ) ;
158175 const scopeName = md5Hash ( queryExpression ) ;
159176
@@ -162,17 +179,20 @@ export class CodeSearchCodeLens implements CommandCodeLensProvider, vscode.Dispo
162179 outputBase : ws . outputBase ,
163180 name : scopeName ,
164181 bazelQuery : {
165- expression : queryExpression ,
182+ command : command ,
183+ expression : query . join ( ' ' ) ,
184+ options : options ,
166185 } ,
167186 } ;
168187
169188 output . clear ( ) ;
170189 output . show ( ) ;
190+ output . appendLine ( `Indexing ${ queryExpression } ...` ) ;
171191
172192 return vscode . window . withProgress < void > (
173193 {
174194 location : vscode . ProgressLocation . Notification ,
175- title : `Indexing ${ queryExpression } ` ,
195+ title : `Indexing ${ queryExpression } ... ` ,
176196 cancellable : false ,
177197 } , async ( progress : vscode . Progress < { message : string | undefined } > , token : vscode . CancellationToken ) : Promise < void > => {
178198
@@ -232,11 +252,11 @@ export class CodeSearchCodeLens implements CommandCodeLensProvider, vscode.Dispo
232252 cwd : ws . cwd ,
233253 outputBase : ws . outputBase ,
234254 name : scopeName ,
235- } ) ;
255+ } ) ;
236256 } catch ( err ) {
237257 if ( err . code !== grpc . status . NOT_FOUND ) {
238258 const e : grpc . ServiceError = err as grpc . ServiceError ;
239- vscode . window . showErrorMessage ( `${ e . message } (${ e . code } )` ) ;
259+ vscode . window . showErrorMessage ( `${ e . message } (${ e . code } )` ) ;
240260 }
241261 }
242262
@@ -252,27 +272,45 @@ export class CodeSearchCodeLens implements CommandCodeLensProvider, vscode.Dispo
252272 ) ;
253273
254274 queryDidChange ( async ( q ) => {
275+ const start = Date . now ( ) ;
276+
255277 if ( ! q . line ) {
256- panel . onDidChangeHTMLSummary . fire ( '' ) ;
278+ panel . onDidChangeHTMLSummary . fire ( 'Searching ' + queryExpression ) ;
257279 panel . onDidChangeHTMLResults . fire ( '' ) ;
258280 return ;
259281 }
260282
283+ panel . onDidChangeHTMLSummary . fire ( 'Working...' ) ;
284+ panel . onDidChangeHTMLResults . fire ( '<progress></progress>' ) ;
285+ const timeoutID = setTimeout ( ( ) => {
286+ panel . onDidChangeHTMLSummary . fire ( 'Timed out.' ) ;
287+ panel . onDidChangeHTMLResults . fire ( '' ) ;
288+ } , 1000 ) ;
289+
261290 try {
262291 const result = await client . searchScope ( {
263292 scopeName : scopeName ,
264293 query : q ,
265294 } ) ;
266- panel . onDidChangeHTMLSummary . fire ( await this . renderer . renderSummary ( result ) ) ;
267- panel . onDidChangeHTMLResults . fire ( await this . renderer . renderResults ( result , ws ) ) ;
295+ clearTimeout ( timeoutID ) ;
296+ panel . onDidChangeHTMLSummary . fire ( 'Rendering results...' ) ;
297+ const resultsHTML = await this . renderer . renderResults ( result , ws ) ;
298+ let summaryHTML = await this . renderer . renderSummary ( q , result ) ;
299+ const dur = Duration . fromMillis ( Date . now ( ) - start ) ;
300+ summaryHTML += ` [${ dur . milliseconds } ms]` ;
301+ panel . onDidChangeHTMLSummary . fire ( summaryHTML ) ;
302+ panel . onDidChangeHTMLResults . fire ( resultsHTML ) ;
268303 } catch ( e ) {
304+ clearTimeout ( timeoutID ) ;
269305 const err = e as grpc . ServiceError ;
270306 panel . onDidChangeHTMLSummary . fire ( err . message ) ;
271307 panel . onDidChangeHTMLResults . fire ( '' ) ;
272308 }
273309 } ) ;
274310
275- return this . renderSearchPanel ( ws , queryExpression , scope , panel , query , queryChangeEmitter ) ;
311+ await this . renderSearchPanel ( ws , queryExpression , scope , panel , query , queryChangeEmitter ) ;
312+
313+ panel . onDidChangeHTMLSummary . fire ( 'Searching ' + queryExpression ) ;
276314 }
277315
278316 async renderSearchPanel ( ws : Workspace , queryExpression : string , scope : Scope | undefined , panel : CodesearchRenderProvider , query : Query , queryChangeEmitter : vscode . EventEmitter < Query > ) : Promise < void > {
@@ -282,7 +320,7 @@ export class CodeSearchCodeLens implements CommandCodeLensProvider, vscode.Dispo
282320 files = Long . fromValue ( scope . size || 0 ) . toInt ( ) ;
283321 if ( scope . createdAt ) {
284322 lastIndexed = getRelativeDateFromTimestamp ( scope . createdAt ) ;
285- }
323+ }
286324 }
287325
288326 let heading = `codesearch <span class="text-hl">${ files } </span> files, last indexed <span class="text-hl">${ lastIndexed } </span>` ;
@@ -309,7 +347,7 @@ export class CodeSearchCodeLens implements CommandCodeLensProvider, vscode.Dispo
309347 label : 'Query' ,
310348 type : 'text' ,
311349 name : 'number' ,
312- placeholder : ` Search ${ queryExpression } ` ,
350+ placeholder : ' Search expression' ,
313351 display : 'inline-block' ,
314352 size : 40 ,
315353 autofocus : true ,
0 commit comments