@@ -289,19 +289,20 @@ export class QueryHistoryManager {
289289 singleItem : CompletedQuery ,
290290 multiSelect : CompletedQuery [ ]
291291 ) : Promise < void > {
292- if ( ! this . assertSingleQuery ( multiSelect ) ) {
292+ const { finalSingleItem, finalMultiSelect } = this . determineSelection ( singleItem , multiSelect ) ;
293+ if ( ! this . assertSingleQuery ( finalMultiSelect ) ) {
293294 return ;
294295 }
295296
296297 const textDocument = await vscode . workspace . openTextDocument (
297- vscode . Uri . file ( singleItem . query . program . queryPath )
298+ vscode . Uri . file ( finalSingleItem . query . program . queryPath )
298299 ) ;
299300 const editor = await vscode . window . showTextDocument (
300301 textDocument ,
301302 vscode . ViewColumn . One
302303 ) ;
303- const queryText = singleItem . options . queryText ;
304- if ( queryText !== undefined && singleItem . options . isQuickQuery ) {
304+ const queryText = finalSingleItem . options . queryText ;
305+ if ( queryText !== undefined && finalSingleItem . options . isQuickQuery ) {
305306 await editor . edit ( ( edit ) =>
306307 edit . replace (
307308 textDocument . validateRange (
@@ -317,7 +318,9 @@ export class QueryHistoryManager {
317318 singleItem : CompletedQuery ,
318319 multiSelect : CompletedQuery [ ]
319320 ) {
320- ( multiSelect || [ singleItem ] ) . forEach ( ( item ) => {
321+ const { finalSingleItem, finalMultiSelect } = this . determineSelection ( singleItem , multiSelect ) ;
322+
323+ ( finalMultiSelect || [ finalSingleItem ] ) . forEach ( ( item ) => {
321324 this . treeDataProvider . remove ( item ) ;
322325 item . dispose ( ) ;
323326 } ) ;
@@ -375,14 +378,15 @@ export class QueryHistoryManager {
375378 singleItem : CompletedQuery ,
376379 multiSelect : CompletedQuery [ ]
377380 ) {
378- if ( ! this . assertSingleQuery ( multiSelect ) ) {
381+ const { finalSingleItem, finalMultiSelect } = this . determineSelection ( singleItem , multiSelect ) ;
382+ if ( ! this . assertSingleQuery ( finalMultiSelect ) ) {
379383 return ;
380384 }
381- this . treeDataProvider . setCurrentItem ( singleItem ) ;
385+ this . treeDataProvider . setCurrentItem ( finalSingleItem ) ;
382386
383387 const now = new Date ( ) ;
384388 const prevItemClick = this . lastItemClick ;
385- this . lastItemClick = { time : now , item : singleItem } ;
389+ this . lastItemClick = { time : now , item : finalSingleItem } ;
386390
387391 if (
388392 prevItemClick !== undefined &&
@@ -629,6 +633,33 @@ the file in the file explorer and dragging it into the workspace.`
629633 }
630634 }
631635
636+ /**
637+ * If no items are selected, attempt to grab the selection from the treeview.
638+ * We need to use this method because when clicking on commands from the view title
639+ * bar, the selections are not passed in.
640+ *
641+ * @param singleItem the single item selected, or undefined if no item is selected
642+ * @param multiSelect a multi-select or undefined if no items are selected
643+ */
644+ private determineSelection (
645+ singleItem : CompletedQuery ,
646+ multiSelect : CompletedQuery [ ]
647+ ) : { finalSingleItem : CompletedQuery ; finalMultiSelect : CompletedQuery [ ] } {
648+ if ( singleItem === undefined && ( multiSelect === undefined || multiSelect . length === 0 || multiSelect [ 0 ] === undefined ) ) {
649+ const selection = this . treeView . selection ;
650+ if ( selection ) {
651+ return {
652+ finalSingleItem : selection [ 0 ] ,
653+ finalMultiSelect : selection
654+ } ;
655+ }
656+ }
657+ return {
658+ finalSingleItem : singleItem ,
659+ finalMultiSelect : multiSelect
660+ } ;
661+ }
662+
632663 async updateTreeItemContextValue ( element : CompletedQuery ) : Promise < void > {
633664 this . treeDataProvider . updateTreeItemContextValue ( element ) ;
634665 }
0 commit comments