Skip to content

Commit b641007

Browse files
authored
Merge pull request #1681 from github/koesie10/variant-analysis-view-title
Set variant analysis view title to query name
2 parents 1e1c7d4 + 31a28e7 commit b641007

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

extensions/ql-vscode/src/abstract-webview.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ViewColumn,
66
Uri,
77
WebviewPanelOptions,
8-
WebviewOptions
8+
WebviewOptions,
99
} from 'vscode';
1010
import * as path from 'path';
1111

@@ -27,6 +27,8 @@ export abstract class AbstractWebview<ToMessage extends WebviewMessage, FromMess
2727
protected panelLoaded = false;
2828
protected panelLoadedCallBacks: (() => void)[] = [];
2929

30+
private panelResolves?: Array<(panel: WebviewPanel) => void>;
31+
3032
constructor(
3133
protected readonly ctx: ExtensionContext
3234
) {
@@ -35,20 +37,36 @@ export abstract class AbstractWebview<ToMessage extends WebviewMessage, FromMess
3537

3638
public async restoreView(panel: WebviewPanel): Promise<void> {
3739
this.panel = panel;
38-
this.setupPanel(panel);
40+
const config = await this.getPanelConfig();
41+
this.setupPanel(panel, config);
3942
}
4043

4144
protected get isShowingPanel() {
4245
return !!this.panel;
4346
}
4447

45-
protected getPanel(): WebviewPanel {
48+
protected async getPanel(): Promise<WebviewPanel> {
4649
if (this.panel == undefined) {
4750
const { ctx } = this;
4851

49-
const config = this.getPanelConfig();
52+
// This is an async method, so in theory this method can be called concurrently. To ensure that we don't
53+
// create two panels, we use a promise that resolves when the panel is created. This way, if the panel is
54+
// being created, the promise will resolve when it is done.
55+
if (this.panelResolves !== undefined) {
56+
return new Promise((resolve) => {
57+
if (this.panel !== undefined) {
58+
resolve(this.panel);
59+
return;
60+
}
61+
62+
this.panelResolves?.push(resolve);
63+
});
64+
}
65+
this.panelResolves = [];
66+
67+
const config = await this.getPanelConfig();
5068

51-
this.panel = Window.createWebviewPanel(
69+
const panel = Window.createWebviewPanel(
5270
config.viewId,
5371
config.title,
5472
{ viewColumn: config.viewColumn, preserveFocus: config.preserveFocus },
@@ -64,14 +82,17 @@ export abstract class AbstractWebview<ToMessage extends WebviewMessage, FromMess
6482
],
6583
}
6684
);
67-
this.setupPanel(this.panel);
85+
this.panel = panel;
86+
87+
this.setupPanel(panel, config);
88+
89+
this.panelResolves.forEach((resolve) => resolve(panel));
90+
this.panelResolves = undefined;
6891
}
6992
return this.panel;
7093
}
7194

72-
protected setupPanel(panel: WebviewPanel): void {
73-
const config = this.getPanelConfig();
74-
95+
protected setupPanel(panel: WebviewPanel, config: WebviewPanelConfig): void {
7596
this.push(
7697
panel.onDidDispose(
7798
() => {
@@ -101,7 +122,7 @@ export abstract class AbstractWebview<ToMessage extends WebviewMessage, FromMess
101122
);
102123
}
103124

104-
protected abstract getPanelConfig(): WebviewPanelConfig;
125+
protected abstract getPanelConfig(): WebviewPanelConfig | Promise<WebviewPanelConfig>;
105126

106127
protected abstract onPanelDispose(): void;
107128

@@ -123,8 +144,9 @@ export abstract class AbstractWebview<ToMessage extends WebviewMessage, FromMess
123144
this.panelLoadedCallBacks = [];
124145
}
125146

126-
protected postMessage(msg: ToMessage): Thenable<boolean> {
127-
return this.getPanel().webview.postMessage(msg);
147+
protected async postMessage(msg: ToMessage): Promise<boolean> {
148+
const panel = await this.getPanel();
149+
return panel.webview.postMessage(msg);
128150
}
129151

130152
public dispose(disposeHandler?: DisposeHandler) {

extensions/ql-vscode/src/compare/compare-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export class CompareView extends AbstractWebview<ToCompareViewMessage, FromCompa
4646
selectedResultSetName?: string
4747
) {
4848
this.comparePair = { from, to };
49-
this.getPanel().reveal(undefined, true);
49+
const panel = await this.getPanel();
50+
panel.reveal(undefined, true);
5051

5152
await this.waitForPanelLoaded();
5253
const [

extensions/ql-vscode/src/interface.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
languages,
88
Uri,
99
window as Window,
10-
env
10+
env, WebviewPanel
1111
} from 'vscode';
1212
import * as cli from './cli';
1313
import { CodeQLCliServer } from './cli';
@@ -341,6 +341,8 @@ export class ResultsView extends AbstractWebview<IntoResultsViewMsg, FromResults
341341
return;
342342
}
343343

344+
const panel = await this.getPanel();
345+
344346
this._interpretation = undefined;
345347
const interpretationPage = await this.interpretResultsInfo(
346348
fullQuery.completedQuery.query,
@@ -350,12 +352,11 @@ export class ResultsView extends AbstractWebview<IntoResultsViewMsg, FromResults
350352
const sortedResultsMap: SortedResultsMap = {};
351353
Object.entries(fullQuery.completedQuery.sortedResultsInfo).forEach(
352354
([k, v]) =>
353-
(sortedResultsMap[k] = this.convertPathPropertiesToWebviewUris(v))
355+
(sortedResultsMap[k] = this.convertPathPropertiesToWebviewUris(panel, v))
354356
);
355357

356358
this._displayedQuery = fullQuery;
357359

358-
const panel = this.getPanel();
359360
await this.waitForPanelLoaded();
360361
if (!panel.visible) {
361362
if (forceReveal === WebviewReveal.Forced) {
@@ -426,6 +427,7 @@ export class ResultsView extends AbstractWebview<IntoResultsViewMsg, FromResults
426427
interpretation: interpretationPage,
427428
origResultsPaths: fullQuery.completedQuery.query.resultsPaths,
428429
resultsPath: this.convertPathToWebviewUri(
430+
panel,
429431
fullQuery.completedQuery.query.resultsPaths.resultsPath
430432
),
431433
parsedResultSets,
@@ -498,10 +500,12 @@ export class ResultsView extends AbstractWebview<IntoResultsViewMsg, FromResults
498500
throw new Error('trying to view a page of a query that is not loaded');
499501
}
500502

503+
const panel = await this.getPanel();
504+
501505
const sortedResultsMap: SortedResultsMap = {};
502506
Object.entries(results.completedQuery.sortedResultsInfo).forEach(
503507
([k, v]) =>
504-
(sortedResultsMap[k] = this.convertPathPropertiesToWebviewUris(v))
508+
(sortedResultsMap[k] = this.convertPathPropertiesToWebviewUris(panel, v))
505509
);
506510

507511
const resultSetSchemas = await this.getResultSetSchemas(results.completedQuery, sorted ? selectedTable : '');
@@ -544,6 +548,7 @@ export class ResultsView extends AbstractWebview<IntoResultsViewMsg, FromResults
544548
interpretation: this._interpretation,
545549
origResultsPaths: results.completedQuery.query.resultsPaths,
546550
resultsPath: this.convertPathToWebviewUri(
551+
panel,
547552
results.completedQuery.query.resultsPaths.resultsPath
548553
),
549554
parsedResultSets,
@@ -812,15 +817,16 @@ export class ResultsView extends AbstractWebview<IntoResultsViewMsg, FromResults
812817
this._diagnosticCollection.set(diagnostics);
813818
}
814819

815-
private convertPathToWebviewUri(path: string): string {
816-
return fileUriToWebviewUri(this.getPanel(), Uri.file(path));
820+
private convertPathToWebviewUri(panel: WebviewPanel, path: string): string {
821+
return fileUriToWebviewUri(panel, Uri.file(path));
817822
}
818823

819824
private convertPathPropertiesToWebviewUris(
825+
panel: WebviewPanel,
820826
info: SortedResultSetInfo
821827
): SortedResultSetInfo {
822828
return {
823-
resultsPath: this.convertPathToWebviewUri(info.resultsPath),
829+
resultsPath: this.convertPathToWebviewUri(panel, info.resultsPath),
824830
sortState: info.sortState,
825831
};
826832
}

extensions/ql-vscode/src/remote-queries/remote-queries-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export class RemoteQueriesView extends AbstractWebview<ToRemoteQueriesMessage, F
4949
}
5050

5151
async showResults(query: RemoteQuery, queryResult: RemoteQueryResult) {
52-
this.getPanel().reveal(undefined, true);
52+
const panel = await this.getPanel();
53+
panel.reveal(undefined, true);
5354

5455
await this.waitForPanelLoaded();
5556
const model = this.buildViewModel(query, queryResult);

extensions/ql-vscode/src/remote-queries/variant-analysis-view.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export class VariantAnalysisView extends AbstractWebview<ToVariantAnalysisMessag
2626
}
2727

2828
public async openView() {
29-
this.getPanel().reveal(undefined, true);
29+
const panel = await this.getPanel();
30+
panel.reveal(undefined, true);
3031

3132
await this.waitForPanelLoaded();
3233
}
@@ -40,6 +41,9 @@ export class VariantAnalysisView extends AbstractWebview<ToVariantAnalysisMessag
4041
t: 'setVariantAnalysis',
4142
variantAnalysis,
4243
});
44+
45+
const panel = await this.getPanel();
46+
panel.title = `${variantAnalysis.query.name} - CodeQL Query Results`;
4347
}
4448

4549
public async updateRepoState(repoState: VariantAnalysisScannedRepositoryState): Promise<void> {
@@ -64,10 +68,12 @@ export class VariantAnalysisView extends AbstractWebview<ToVariantAnalysisMessag
6468
});
6569
}
6670

67-
protected getPanelConfig(): WebviewPanelConfig {
71+
protected async getPanelConfig(): Promise<WebviewPanelConfig> {
72+
const variantAnalysis = await this.manager.getVariantAnalysis(this.variantAnalysisId);
73+
6874
return {
6975
viewId: VariantAnalysisView.viewType,
70-
title: `CodeQL Query Results for ${this.variantAnalysisId}`,
76+
title: variantAnalysis ? `${variantAnalysis.query.name} - CodeQL Query Results` : `Variant analysis ${this.variantAnalysisId} - CodeQL Query Results`,
7177
viewColumn: ViewColumn.Active,
7278
preserveFocus: true,
7379
view: 'variant-analysis',

0 commit comments

Comments
 (0)