Skip to content

Commit 6bcfdda

Browse files
Merge pull request #2217 from github/robertbrignull/use_app_commands_1
Convert call sites to use typed commands (part 1)
2 parents 1645363 + e0dd8c7 commit 6bcfdda

18 files changed

Lines changed: 95 additions & 51 deletions

File tree

extensions/ql-vscode/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Readable } from "stream";
99
import { StringDecoder } from "string_decoder";
1010
import tk from "tree-kill";
1111
import { promisify } from "util";
12-
import { CancellationToken, commands, Disposable, Uri } from "vscode";
12+
import { CancellationToken, Disposable, Uri } from "vscode";
1313

1414
import { BQRSInfo, DecodedBqrsChunk } from "./pure/bqrs-cli-types";
1515
import { allowCanaryQueryServer, CliConfig } from "./config";
@@ -1375,7 +1375,7 @@ export class CodeQLCliServer implements Disposable {
13751375
if (!this._version) {
13761376
this._version = this.refreshVersion();
13771377
// this._version is only undefined upon config change, so we reset CLI-based context key only when necessary.
1378-
await commands.executeCommand(
1378+
await this.app.commands.execute(
13791379
"setContext",
13801380
"codeql.supportsEvalLog",
13811381
await this.cliConstraints.supportsPerQueryEvalLog(),

extensions/ql-vscode/src/common/commands.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CommandManager } from "../packages/commands";
2-
import type { Uri, Range } from "vscode";
2+
import type { Uri, Range, TextDocumentShowOptions } from "vscode";
33
import type { AstItem } from "../astViewer";
44
import type { DbTreeViewItem } from "../databases/ui/db-tree-view-item";
55
import type { DatabaseItem } from "../local-databases";
@@ -35,12 +35,22 @@ export type SingleSelectionCommandFunction<Item> = (
3535
// Builtin commands where the implementation is provided by VS Code and not by this extension.
3636
// See https://code.visualstudio.com/api/references/commands
3737
export type BuiltInVsCodeCommands = {
38+
// The codeQLDatabases.focus command is provided by VS Code because we've registered the custom view
39+
"codeQLDatabases.focus": () => Promise<void>;
3840
"markdown.showPreviewToSide": (uri: Uri) => Promise<void>;
3941
setContext: (
4042
key: `${"codeql" | "codeQL"}${string}`,
4143
value: unknown,
4244
) => Promise<void>;
4345
"workbench.action.reloadWindow": () => Promise<void>;
46+
"vscode.diff": (
47+
leftSideResource: Uri,
48+
rightSideResource: Uri,
49+
title?: string,
50+
columnOrOptions?: TextDocumentShowOptions,
51+
) => Promise<void>;
52+
"vscode.open": (uri: Uri) => Promise<void>;
53+
"vscode.openFolder": (uri: Uri) => Promise<void>;
4454
};
4555

4656
// Commands that are available before the extension is fully activated.

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fetch, { Response } from "node-fetch";
22
import { zip } from "zip-a-folder";
33
import { Open } from "unzipper";
4-
import { Uri, CancellationToken, commands, window } from "vscode";
4+
import { Uri, CancellationToken, window } from "vscode";
55
import { CodeQLCliServer } from "./cli";
66
import {
77
ensureDir,
@@ -26,6 +26,7 @@ import {
2626
isValidGitHubNwo,
2727
} from "./common/github-url-identifier-helper";
2828
import { Credentials } from "./common/authentication";
29+
import { AppCommandManager } from "./common/commands";
2930

3031
/**
3132
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
@@ -34,6 +35,7 @@ import { Credentials } from "./common/authentication";
3435
* @param storagePath where to store the unzipped database.
3536
*/
3637
export async function promptImportInternetDatabase(
38+
commandManager: AppCommandManager,
3739
databaseManager: DatabaseManager,
3840
storagePath: string,
3941
progress: ProgressCallback,
@@ -61,7 +63,7 @@ export async function promptImportInternetDatabase(
6163
);
6264

6365
if (item) {
64-
await commands.executeCommand("codeQLDatabases.focus");
66+
await commandManager.execute("codeQLDatabases.focus");
6567
void showAndLogInformationMessage(
6668
"Database downloaded and imported successfully.",
6769
);
@@ -78,6 +80,7 @@ export async function promptImportInternetDatabase(
7880
* @param storagePath where to store the unzipped database.
7981
*/
8082
export async function promptImportGithubDatabase(
83+
commandManager: AppCommandManager,
8184
databaseManager: DatabaseManager,
8285
storagePath: string,
8386
credentials: Credentials | undefined,
@@ -141,7 +144,7 @@ export async function promptImportGithubDatabase(
141144
cli,
142145
);
143146
if (item) {
144-
await commands.executeCommand("codeQLDatabases.focus");
147+
await commandManager.execute("codeQLDatabases.focus");
145148
void showAndLogInformationMessage(
146149
"Database downloaded and imported successfully.",
147150
);
@@ -158,6 +161,7 @@ export async function promptImportGithubDatabase(
158161
* @param storagePath where to store the unzipped database.
159162
*/
160163
export async function importArchiveDatabase(
164+
commandManager: AppCommandManager,
161165
databaseUrl: string,
162166
databaseManager: DatabaseManager,
163167
storagePath: string,
@@ -177,7 +181,7 @@ export async function importArchiveDatabase(
177181
cli,
178182
);
179183
if (item) {
180-
await commands.executeCommand("codeQLDatabases.focus");
184+
await commandManager.execute("codeQLDatabases.focus");
181185
void showAndLogInformationMessage(
182186
"Database unzipped and imported successfully.",
183187
);

extensions/ql-vscode/src/databases/db-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class DbModule extends DisposableObject {
4343

4444
await this.dbConfigStore.initialize();
4545

46-
this.dbPanel = new DbPanel(this.dbManager, app.credentials);
46+
this.dbPanel = new DbPanel(app, this.dbManager);
4747

4848
this.push(this.dbPanel);
4949
this.push(this.dbConfigStore);

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
commands,
32
QuickPickItem,
43
TreeView,
54
TreeViewExpansionEvent,
@@ -31,8 +30,8 @@ import { DbTreeViewItem } from "./db-tree-view-item";
3130
import { getGitHubUrl } from "./db-tree-view-item-action";
3231
import { getControllerRepo } from "../../variant-analysis/run-remote-query";
3332
import { getErrorMessage } from "../../pure/helpers-pure";
34-
import { Credentials } from "../../common/authentication";
3533
import { DatabasePanelCommands } from "../../common/commands";
34+
import { App } from "../../common/app";
3635

3736
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
3837
kind: string;
@@ -47,8 +46,8 @@ export class DbPanel extends DisposableObject {
4746
private readonly treeView: TreeView<DbTreeViewItem>;
4847

4948
public constructor(
49+
private readonly app: App,
5050
private readonly dbManager: DbManager,
51-
private readonly credentials: Credentials,
5251
) {
5352
super();
5453

@@ -369,13 +368,13 @@ export class DbPanel extends DisposableObject {
369368
);
370369
}
371370

372-
await commands.executeCommand("vscode.open", Uri.parse(githubUrl));
371+
await this.app.commands.execute("vscode.open", Uri.parse(githubUrl));
373372
}
374373

375374
private async setupControllerRepository(): Promise<void> {
376375
try {
377376
// This will also validate that the controller repository is valid
378-
await getControllerRepo(this.credentials);
377+
await getControllerRepo(this.app.credentials);
379378
} catch (e: unknown) {
380379
if (e instanceof UserCancellationException) {
381380
return;

extensions/ql-vscode/src/extension.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ async function installOrUpdateThenTryActivate(
575575
await installOrUpdateDistribution(ctx, app, distributionManager, config);
576576

577577
try {
578-
await prepareCodeTour();
578+
await prepareCodeTour(app.commands);
579579
} catch (e: unknown) {
580580
void extLogger.log(
581581
`Could not open tutorial workspace automatically: ${getErrorMessage(e)}`,
@@ -669,7 +669,12 @@ async function activateWithInstalledDistribution(
669669
ctx.subscriptions.push(statusBar);
670670

671671
void extLogger.log("Initializing query server client.");
672-
const qs = await createQueryServer(qlConfigurationListener, cliServer, ctx);
672+
const qs = await createQueryServer(
673+
app,
674+
qlConfigurationListener,
675+
cliServer,
676+
ctx,
677+
);
673678

674679
for (const glob of PACK_GLOBS) {
675680
const fsWatcher = workspace.createFileSystemWatcher(glob);
@@ -680,7 +685,7 @@ async function activateWithInstalledDistribution(
680685
}
681686

682687
void extLogger.log("Initializing database manager.");
683-
const dbm = new DatabaseManager(ctx, qs, cliServer, extLogger);
688+
const dbm = new DatabaseManager(ctx, app, qs, cliServer, extLogger);
684689

685690
// Let this run async.
686691
void dbm.loadPersistedState();
@@ -853,7 +858,7 @@ async function activateWithInstalledDistribution(
853858
);
854859
ctx.subscriptions.push(testAdapterFactory);
855860

856-
const testUIService = new TestUIService(testHub);
861+
const testUIService = new TestUIService(app, testHub);
857862
ctx.subscriptions.push(testUIService);
858863

859864
testUiCommands = testUIService.getCommands();
@@ -881,6 +886,7 @@ async function activateWithInstalledDistribution(
881886
const allCommands: AllExtensionCommands = {
882887
...getCommands(app, cliServer, qs),
883888
...getQueryEditorCommands({
889+
commandManager: app.commands,
884890
queryRunner: qs,
885891
cliServer,
886892
qhelpTmpDir: qhelpTmpDir.name,
@@ -1046,6 +1052,7 @@ function addUnhandledRejectionListener() {
10461052
}
10471053

10481054
async function createQueryServer(
1055+
app: ExtensionApp,
10491056
qlConfigurationListener: QueryServerConfigListener,
10501057
cliServer: CodeQLCliServer,
10511058
ctx: ExtensionContext,
@@ -1076,6 +1083,7 @@ async function createQueryServer(
10761083
return new NewQueryRunner(qs);
10771084
} else {
10781085
const qs = new LegacyQueryServerClient(
1086+
app,
10791087
qlConfigurationListener,
10801088
cliServer,
10811089
qsOpts,

extensions/ql-vscode/src/helpers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
window as Window,
1717
workspace,
1818
env,
19-
commands,
2019
} from "vscode";
2120
import { CodeQLCliServer, QlpacksInfo } from "./cli";
2221
import { UserCancellationException } from "./progress";
@@ -27,6 +26,7 @@ import { RedactableError } from "./pure/errors";
2726
import { getQlPackPath } from "./pure/ql";
2827
import { dbSchemeToLanguage } from "./common/query-language";
2928
import { isCodespacesTemplate } from "./config";
29+
import { AppCommandManager } from "./common/commands";
3030

3131
// Shared temporary folder for the extension.
3232
export const tmpDir = dirSync({
@@ -271,7 +271,9 @@ export function isFolderAlreadyInWorkspace(folderName: string) {
271271
/** Check if the current workspace is the CodeTour and open the workspace folder.
272272
* Without this, we can't run the code tour correctly.
273273
**/
274-
export async function prepareCodeTour(): Promise<void> {
274+
export async function prepareCodeTour(
275+
commandManager: AppCommandManager,
276+
): Promise<void> {
275277
if (workspace.workspaceFolders?.length) {
276278
const currentFolder = workspace.workspaceFolders[0].uri.fsPath;
277279

@@ -308,7 +310,7 @@ export async function prepareCodeTour(): Promise<void> {
308310
`In prepareCodeTour() method, going to open the tutorial workspace file: ${tutorialWorkspacePath}`,
309311
);
310312

311-
await commands.executeCommand("vscode.openFolder", tutorialWorkspaceUri);
313+
await commandManager.execute("vscode.openFolder", tutorialWorkspaceUri);
312314
}
313315
}
314316
}

extensions/ql-vscode/src/legacy-query-server/queryserver-client.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ensureFile } from "fs-extra";
22

33
import { DisposableObject } from "../pure/disposable-object";
4-
import { CancellationToken, commands } from "vscode";
4+
import { CancellationToken } from "vscode";
55
import { createMessageConnection, RequestType } from "vscode-jsonrpc/node";
66
import * as cli from "../cli";
77
import { QueryServerConfig } from "../config";
@@ -15,6 +15,7 @@ import {
1515
} from "../pure/legacy-messages";
1616
import { ProgressCallback, ProgressTask } from "../progress";
1717
import { ServerProcess } from "../json-rpc-server";
18+
import { App } from "../common/app";
1819

1920
type WithProgressReporting = (
2021
task: (
@@ -56,6 +57,7 @@ export class QueryServerClient extends DisposableObject {
5657
public activeQueryLogger: Logger;
5758

5859
constructor(
60+
app: App,
5961
readonly config: QueryServerConfig,
6062
readonly cliServer: cli.CodeQLCliServer,
6163
readonly opts: ServerOpts,
@@ -69,7 +71,7 @@ export class QueryServerClient extends DisposableObject {
6971
if (config.onDidChangeConfiguration !== undefined) {
7072
this.push(
7173
config.onDidChangeConfiguration(() =>
72-
commands.executeCommand("codeQL.restartQueryServer"),
74+
app.commands.execute("codeQL.restartQueryServer"),
7375
),
7476
);
7577
}

extensions/ql-vscode/src/local-databases-ui.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ export class DatabaseUI extends DisposableObject {
451451
return withProgress(
452452
async (progress, token) => {
453453
await promptImportInternetDatabase(
454+
this.app.commands,
454455
this.databaseManager,
455456
this.storagePath,
456457
progress,
@@ -470,6 +471,7 @@ export class DatabaseUI extends DisposableObject {
470471
const credentials = isCanary() ? this.app.credentials : undefined;
471472

472473
await promptImportGithubDatabase(
474+
this.app.commands,
473475
this.databaseManager,
474476
this.storagePath,
475477
credentials,
@@ -607,6 +609,7 @@ export class DatabaseUI extends DisposableObject {
607609
// Assume user has selected an archive if the file has a .zip extension
608610
if (uri.path.endsWith(".zip")) {
609611
await importArchiveDatabase(
612+
this.app.commands,
610613
uri.toString(true),
611614
this.databaseManager,
612615
this.storagePath,
@@ -762,6 +765,7 @@ export class DatabaseUI extends DisposableObject {
762765
// we are selecting a database archive. Must unzip into a workspace-controlled area
763766
// before importing.
764767
return await importArchiveDatabase(
768+
this.app.commands,
765769
uri.toString(true),
766770
this.databaseManager,
767771
this.storagePath,

extensions/ql-vscode/src/local-databases.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { redactableError } from "./pure/errors";
2828
import { isCodespacesTemplate } from "./config";
2929
import { QlPackGenerator } from "./qlpack-generator";
3030
import { QueryLanguage } from "./common/query-language";
31+
import { App } from "./common/app";
3132

3233
/**
3334
* databases.ts
@@ -593,6 +594,7 @@ export class DatabaseManager extends DisposableObject {
593594

594595
constructor(
595596
private readonly ctx: ExtensionContext,
597+
private readonly app: App,
596598
private readonly qs: QueryRunner,
597599
private readonly cli: cli.CodeQLCliServer,
598600
public logger: Logger,
@@ -875,7 +877,7 @@ export class DatabaseManager extends DisposableObject {
875877
this._currentDatabaseItem = item;
876878
this.updatePersistedCurrentDatabaseItem();
877879

878-
await vscode.commands.executeCommand(
880+
await this.app.commands.execute(
879881
"setContext",
880882
"codeQL.currentDatabaseItem",
881883
item?.name,

0 commit comments

Comments
 (0)