Skip to content

Commit 6e2e72a

Browse files
Be nicer about where to open the results webview (#1037)
* 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. Co-authored-by: Shati Patel <[email protected]>
1 parent d0953fb commit 6e2e72a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

extensions/ql-vscode/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [UNRELEASED]
44

5+
- Avoid creating a third column when opening the results view. The results view will always open to the right of the active editor, unless the active editor is in the rightmost editor column. In that case open in the leftmost column. [#1037](https://github.com/github/vscode-codeql/pull/1037)
56
- Add a CodeLens to make the Quick Evaluation command more accessible. Click the `Quick Evaluation` prompt above a predicate definition in the editor to evaluate that predicate on its own. [#1035](https://github.com/github/vscode-codeql/pull/1035)
67
- Fix a bug where the _Alerts_ option would show in the results view even if there is no alerts table available. [#1038](https://github.com/github/vscode-codeql/pull/1038)
78

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 determine 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)