Skip to content

Commit 39d4675

Browse files
Start using app.commands.execute for all commands called from extension.ts
1 parent e724577 commit 39d4675

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export type SingleSelectionCommandFunction<Item> = (
3030
* the implementation in the corresponding `getCommands` function.
3131
*/
3232

33+
// Builtin commands where the implementation is provided by VS Code and not by this extension.
34+
export type VSCodeCommands = {
35+
"markdown.showPreviewToSide": (uri: Uri) => Promise<void>;
36+
"workbench.action.reloadWindow": () => Promise<void>;
37+
};
38+
3339
// Base commands not tied directly to a module like e.g. variant analysis.
3440
export type BaseCommands = {
3541
"codeQL.openDocumentation": () => Promise<void>;
@@ -185,7 +191,8 @@ export type EvalLogViewerCommands = {
185191
"codeQLEvalLogViewer.clear": () => Promise<void>;
186192
};
187193

188-
export type AllCommands = BaseCommands &
194+
// All commands where the implementation is provided by this extension.
195+
export type AllCodeQLCommands = BaseCommands &
189196
QueryHistoryCommands &
190197
LocalDatabasesCommands &
191198
VariantAnalysisCommands &
@@ -194,6 +201,8 @@ export type AllCommands = BaseCommands &
194201
PackagingCommands &
195202
EvalLogViewerCommands;
196203

204+
export type AllCommands = AllCodeQLCommands & VSCodeCommands;
205+
197206
export type AppCommandManager = CommandManager<AllCommands>;
198207

199208
// Separate command manager because it uses a different logger

extensions/ql-vscode/src/extension.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import "source-map-support/register";
22
import {
33
CancellationToken,
4-
commands,
54
Disposable,
65
env,
76
ExtensionContext,
@@ -112,7 +111,7 @@ import { redactableError } from "./pure/errors";
112111
import { QueryHistoryDirs } from "./query-history/query-history-dirs";
113112
import { DirResult } from "tmp";
114113
import {
115-
AllCommands,
114+
AllCodeQLCommands,
116115
BaseCommands,
117116
QueryServerCommands,
118117
} from "./common/commands";
@@ -265,6 +264,8 @@ export async function activate(
265264
addUnhandledRejectionListener();
266265
install();
267266

267+
const app = new ExtensionApp(ctx);
268+
268269
const codelensProvider = new QuickEvalCodeLensProvider();
269270
languages.registerCodeLensProvider(
270271
{ scheme: "file", language: "ql" },
@@ -291,6 +292,7 @@ export async function activate(
291292
distributionConfigListener.onDidChangeConfiguration(() =>
292293
installOrUpdateThenTryActivate(
293294
ctx,
295+
app,
294296
distributionManager,
295297
distributionConfigListener,
296298
{
@@ -305,6 +307,7 @@ export async function activate(
305307
commandRunner(checkForUpdatesCommand, () =>
306308
installOrUpdateThenTryActivate(
307309
ctx,
310+
app,
308311
distributionManager,
309312
distributionConfigListener,
310313
{
@@ -324,6 +327,7 @@ export async function activate(
324327

325328
const codeQlExtension = await installOrUpdateThenTryActivate(
326329
ctx,
330+
app,
327331
distributionManager,
328332
distributionConfigListener,
329333
{
@@ -345,6 +349,7 @@ export async function activate(
345349

346350
async function installOrUpdateDistributionWithProgressTitle(
347351
ctx: ExtensionContext,
352+
app: ExtensionApp,
348353
distributionManager: DistributionManager,
349354
progressTitle: string,
350355
config: DistributionUpdateConfig,
@@ -389,7 +394,7 @@ async function installOrUpdateDistributionWithProgressTitle(
389394
"Restart and Upgrade",
390395
)
391396
) {
392-
await commands.executeCommand("workbench.action.reloadWindow");
397+
await app.commands.execute("workbench.action.reloadWindow");
393398
}
394399
} else {
395400
await withProgress(
@@ -416,6 +421,7 @@ async function installOrUpdateDistributionWithProgressTitle(
416421

417422
async function installOrUpdateDistribution(
418423
ctx: ExtensionContext,
424+
app: ExtensionApp,
419425
distributionManager: DistributionManager,
420426
config: DistributionUpdateConfig,
421427
): Promise<void> {
@@ -436,6 +442,7 @@ async function installOrUpdateDistribution(
436442
try {
437443
await installOrUpdateDistributionWithProgressTitle(
438444
ctx,
445+
app,
439446
distributionManager,
440447
messageText,
441448
config,
@@ -521,11 +528,12 @@ async function getDistributionDisplayingDistributionWarnings(
521528

522529
async function installOrUpdateThenTryActivate(
523530
ctx: ExtensionContext,
531+
app: ExtensionApp,
524532
distributionManager: DistributionManager,
525533
distributionConfigListener: DistributionConfigListener,
526534
config: DistributionUpdateConfig,
527535
): Promise<CodeQLExtensionInterface | Record<string, never>> {
528-
await installOrUpdateDistribution(ctx, distributionManager, config);
536+
await installOrUpdateDistribution(ctx, app, distributionManager, config);
529537

530538
try {
531539
await prepareCodeTour();
@@ -545,6 +553,7 @@ async function installOrUpdateThenTryActivate(
545553
) {
546554
extensionInterface = await activateWithInstalledDistribution(
547555
ctx,
556+
app,
548557
distributionManager,
549558
distributionConfigListener,
550559
);
@@ -562,6 +571,7 @@ async function installOrUpdateThenTryActivate(
562571
if (chosenAction === installActionName) {
563572
await installOrUpdateThenTryActivate(
564573
ctx,
574+
app,
565575
distributionManager,
566576
distributionConfigListener,
567577
{
@@ -588,6 +598,7 @@ const PACK_GLOBS = [
588598

589599
async function activateWithInstalledDistribution(
590600
ctx: ExtensionContext,
601+
app: ExtensionApp,
591602
distributionManager: DistributionManager,
592603
distributionConfigListener: DistributionConfigListener,
593604
): Promise<CodeQLExtensionInterface> {
@@ -596,8 +607,6 @@ async function activateWithInstalledDistribution(
596607
// of activation.
597608
errorStubs.forEach((stub) => stub.dispose());
598609

599-
const app = new ExtensionApp(ctx);
600-
601610
void extLogger.log("Initializing configuration listener...");
602611
const qlConfigurationListener =
603612
await QueryServerConfigListener.createQueryServerConfigListener(
@@ -821,7 +830,7 @@ async function activateWithInstalledDistribution(
821830

822831
void extLogger.log("Registering top-level command palette commands.");
823832

824-
const allCommands: AllCommands = {
833+
const allCommands: AllCodeQLCommands = {
825834
...getCommands(cliServer, qs),
826835
...qhm.getCommands(),
827836
...variantAnalysisManager.getCommands(),
@@ -844,7 +853,7 @@ async function activateWithInstalledDistribution(
844853
};
845854

846855
for (const [commandName, command] of Object.entries(allCommands)) {
847-
app.commands.register(commandName as keyof AllCommands, command);
856+
app.commands.register(commandName as keyof AllCodeQLCommands, command);
848857
}
849858

850859
const queryServerCommands: QueryServerCommands = {
@@ -895,7 +904,7 @@ async function activateWithInstalledDistribution(
895904

896905
ctx.subscriptions.push(
897906
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
898-
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery);
907+
await previewQueryHelp(app, cliServer, qhelpTmpDir, selectedQuery);
899908
}),
900909
);
901910

@@ -1002,7 +1011,7 @@ async function activateWithInstalledDistribution(
10021011
),
10031012
);
10041013

1005-
await commands.executeCommand("codeQLDatabases.removeOrphanedDatabases");
1014+
await app.commands.execute("codeQLDatabases.removeOrphanedDatabases");
10061015

10071016
void extLogger.log("Reading query history");
10081017
await qhm.readQueryHistory();
@@ -1040,6 +1049,7 @@ async function showResultsForComparison(
10401049
}
10411050

10421051
async function previewQueryHelp(
1052+
app: ExtensionApp,
10431053
cliServer: CodeQLCliServer,
10441054
qhelpTmpDir: DirResult,
10451055
selectedQuery: Uri,
@@ -1055,7 +1065,7 @@ async function previewQueryHelp(
10551065
const uri = Uri.file(absolutePathToMd);
10561066
try {
10571067
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
1058-
await commands.executeCommand("markdown.showPreviewToSide", uri);
1068+
await app.commands.execute("markdown.showPreviewToSide", uri);
10591069
} catch (e) {
10601070
const errorMessage = getErrorMessage(e).includes(
10611071
"Generating qhelp in markdown",

0 commit comments

Comments
 (0)