Skip to content

Commit 58a4d3b

Browse files
committed
Be nicer about where to open the results webview
Currently, the webview _always_ opens next to the currently active editor. This is a pain if you already have 2 columns open since this means that the webview will open in a third column, which is rarely what you want. This change uses a more sophisticated approach to opening the webview: 1. If there is only one column, open webview to the right of it 2. If there are multiple columns and the active editor is _not_ the last column, open to the right of the active editor 3. Otherwise open in the first column. This will avoid opening a new column unless there is only one column open right now. There is no native API that vscode exposed to compare column locations, so this uses the `ViewColumn` api is a slightly non-standard way. A limitation is that if the last column is empty and the active editor is to the left of it, then the webview will not be opened there (which would be nice). Instead, it will be opened in column 1.
1 parent bf35077 commit 58a4d3b

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

extensions/ql-vscode/src/interface.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ export class InterfaceManager extends DisposableObject {
160160
getPanel(): vscode.WebviewPanel {
161161
if (this._panel == undefined) {
162162
const { ctx } = this;
163+
const webViewColumn = this.chooseColumnForWebview();
163164
const panel = (this._panel = Window.createWebviewPanel(
164165
'resultsView', // internal name
165166
'CodeQL Query Results', // user-visible name
166-
{ viewColumn: vscode.ViewColumn.Beside, preserveFocus: true },
167+
{ viewColumn: webViewColumn, preserveFocus: true },
167168
{
168169
enableScripts: true,
169170
enableFindWidget: true,
@@ -203,6 +204,29 @@ export class InterfaceManager extends DisposableObject {
203204
return this._panel;
204205
}
205206

207+
/**
208+
* Choose where to open the webview.
209+
*
210+
* If there is a single view column, then open beside it.
211+
* If there are multiple view columns, then open beside the active column,
212+
* unless the active editor is the last column. In this case, open in the first column.
213+
*
214+
* The goal is to avoid opening new columns when there already are two columns open.
215+
*/
216+
private chooseColumnForWebview(): vscode.ViewColumn {
217+
// This is not a great way to determine the number of view columns, but I
218+
// can't find a vscode API that does it any better.
219+
// Here, iterate through all the visible editors and determing the max view column.
220+
// This won't work if the largest view column is empty.
221+
const colCount = Window.visibleTextEditors.reduce((maxVal, editor) =>
222+
Math.max(maxVal, Number.parseInt(editor.viewColumn?.toFixed() || '0', 10)), 0);
223+
if (colCount <= 1) {
224+
return vscode.ViewColumn.Beside;
225+
}
226+
const activeViewColumnNum = Number.parseInt(Window.activeTextEditor?.viewColumn?.toFixed() || '0', 10);
227+
return activeViewColumnNum === colCount ? vscode.ViewColumn.One : vscode.ViewColumn.Beside;
228+
}
229+
206230
private async changeInterpretedSortState(
207231
sortState: InterpretedResultsSortState | undefined
208232
): Promise<void> {

0 commit comments

Comments
 (0)