@@ -238,11 +238,13 @@ export class QueryHistoryManager {
238238 this . selectedCallback = selectedCallback ;
239239 const treeDataProvider = this . treeDataProvider = new HistoryTreeDataProvider ( ) ;
240240 this . treeView = Window . createTreeView ( 'codeQLQueryHistory' , { treeDataProvider } ) ;
241+ // Lazily update the tree view selection due to limitations of TreeView API (see
242+ // `updateTreeViewSelectionIfVisible` doc for details)
243+ this . treeView . onDidChangeVisibility ( async _ev => this . updateTreeViewSelectionIfVisible ( ) ) ;
244+ // Don't allow the selection to become empty
241245 this . treeView . onDidChangeSelection ( async ev => {
242246 if ( ev . selection . length == 0 ) {
243- const current = this . treeDataProvider . getCurrent ( ) ;
244- if ( current != undefined )
245- this . treeView . reveal ( current ) ; // don't allow selection to become empty
247+ this . updateTreeViewSelectionIfVisible ( ) ;
246248 }
247249 } ) ;
248250 ctx . subscriptions . push ( vscode . commands . registerCommand ( 'codeQLQueryHistory.openQuery' , this . handleOpenQuery ) ) ;
@@ -259,6 +261,26 @@ export class QueryHistoryManager {
259261 push ( evaluationInfo : EvaluationInfo ) {
260262 const item = new QueryHistoryItem ( evaluationInfo , this . queryHistoryConfigListener ) ;
261263 this . treeDataProvider . push ( item ) ;
262- this . treeView . reveal ( item , { select : true } ) ;
264+ this . updateTreeViewSelectionIfVisible ( ) ;
265+ }
266+
267+ /**
268+ * Update the tree view selection if the tree view is visible.
269+ *
270+ * If the tree view is not visible, we must wait until it becomes visible before updating the
271+ * selection. This is because the only mechanism for updating the selection of the tree view
272+ * has the side-effect of revealing the tree view. This changes the active sidebar to CodeQL,
273+ * interrupting user workflows such as writing a commit message on the source control sidebar.
274+ */
275+ private updateTreeViewSelectionIfVisible ( ) {
276+ if ( this . treeView . visible ) {
277+ const current = this . treeDataProvider . getCurrent ( ) ;
278+ if ( current != undefined ) {
279+ // We must fire the onDidChangeTreeData event to ensure the current element can be selected
280+ // using `reveal` if the tree view was not visible when the current element was added.
281+ this . treeDataProvider . refresh ( ) ;
282+ this . treeView . reveal ( current ) ;
283+ }
284+ }
263285 }
264286}
0 commit comments