Skip to content

Commit

Permalink
Merge pull request #198 from henrymercer/fix-sidebar-focus-change
Browse files Browse the repository at this point in the history
Fix sidebar focus change when showing results
  • Loading branch information
jcreedcmu authored Dec 12, 2019
2 parents f62a483 + 7f28669 commit 426477e
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions extensions/ql-vscode/src/query-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@ export class QueryHistoryManager {
this.selectedCallback = selectedCallback;
const treeDataProvider = this.treeDataProvider = new HistoryTreeDataProvider();
this.treeView = Window.createTreeView('codeQLQueryHistory', { treeDataProvider });
// Lazily update the tree view selection due to limitations of TreeView API (see
// `updateTreeViewSelectionIfVisible` doc for details)
this.treeView.onDidChangeVisibility(async _ev => this.updateTreeViewSelectionIfVisible());
// Don't allow the selection to become empty
this.treeView.onDidChangeSelection(async ev => {
if (ev.selection.length == 0) {
const current = this.treeDataProvider.getCurrent();
if (current != undefined)
this.treeView.reveal(current); // don't allow selection to become empty
this.updateTreeViewSelectionIfVisible();
}
});
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.openQuery', this.handleOpenQuery));
Expand All @@ -259,6 +261,26 @@ export class QueryHistoryManager {
push(evaluationInfo: EvaluationInfo) {
const item = new QueryHistoryItem(evaluationInfo, this.queryHistoryConfigListener);
this.treeDataProvider.push(item);
this.treeView.reveal(item, { select: true });
this.updateTreeViewSelectionIfVisible();
}

/**
* Update the tree view selection if the tree view is visible.
*
* If the tree view is not visible, we must wait until it becomes visible before updating the
* selection. This is because the only mechanism for updating the selection of the tree view
* has the side-effect of revealing the tree view. This changes the active sidebar to CodeQL,
* interrupting user workflows such as writing a commit message on the source control sidebar.
*/
private updateTreeViewSelectionIfVisible() {
if (this.treeView.visible) {
const current = this.treeDataProvider.getCurrent();
if (current != undefined) {
// We must fire the onDidChangeTreeData event to ensure the current element can be selected
// using `reveal` if the tree view was not visible when the current element was added.
this.treeDataProvider.refresh();
this.treeView.reveal(current);
}
}
}
}

0 comments on commit 426477e

Please sign in to comment.