Skip to content

Commit 297fa2e

Browse files
Merge pull request #2207 from github/robertbrignull/extension_commands
Start using app.commands.execute for all commands called from extension.ts
2 parents 844e58a + 9948b93 commit 297fa2e

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export type SingleSelectionCommandFunction<Item> = (
3232
* the implementation in the corresponding `getCommands` function.
3333
*/
3434

35+
// Builtin commands where the implementation is provided by VS Code and not by this extension.
36+
// See https://code.visualstudio.com/api/references/commands
37+
export type BuiltInVsCodeCommands = {
38+
"markdown.showPreviewToSide": (uri: Uri) => Promise<void>;
39+
"workbench.action.reloadWindow": () => Promise<void>;
40+
};
41+
3542
// Base commands not tied directly to a module like e.g. variant analysis.
3643
export type BaseCommands = {
3744
"codeQL.openDocumentation": () => Promise<void>;
@@ -230,7 +237,8 @@ export type MockGitHubApiServerCommands = {
230237
"codeQL.mockGitHubApiServer.unloadScenario": () => Promise<void>;
231238
};
232239

233-
export type AllCommands = BaseCommands &
240+
// All commands where the implementation is provided by this extension.
241+
export type AllExtensionCommands = BaseCommands &
234242
QueryEditorCommands &
235243
ResultsViewCommands &
236244
QueryHistoryCommands &
@@ -245,6 +253,8 @@ export type AllCommands = BaseCommands &
245253
Partial<TestUICommands> &
246254
MockGitHubApiServerCommands;
247255

256+
export type AllCommands = AllExtensionCommands & BuiltInVsCodeCommands;
257+
248258
export type AppCommandManager = CommandManager<AllCommands>;
249259

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

extensions/ql-vscode/src/extension.ts

Lines changed: 18 additions & 9 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,
@@ -111,7 +110,7 @@ import { DbModule } from "./databases/db-module";
111110
import { redactableError } from "./pure/errors";
112111
import { QueryHistoryDirs } from "./query-history/query-history-dirs";
113112
import {
114-
AllCommands,
113+
AllExtensionCommands,
115114
BaseCommands,
116115
QueryServerCommands,
117116
TestUICommands,
@@ -266,6 +265,8 @@ export async function activate(
266265
addUnhandledRejectionListener();
267266
install();
268267

268+
const app = new ExtensionApp(ctx);
269+
269270
const codelensProvider = new QuickEvalCodeLensProvider();
270271
languages.registerCodeLensProvider(
271272
{ scheme: "file", language: "ql" },
@@ -292,6 +293,7 @@ export async function activate(
292293
distributionConfigListener.onDidChangeConfiguration(() =>
293294
installOrUpdateThenTryActivate(
294295
ctx,
296+
app,
295297
distributionManager,
296298
distributionConfigListener,
297299
{
@@ -306,6 +308,7 @@ export async function activate(
306308
commandRunner(checkForUpdatesCommand, () =>
307309
installOrUpdateThenTryActivate(
308310
ctx,
311+
app,
309312
distributionManager,
310313
distributionConfigListener,
311314
{
@@ -325,6 +328,7 @@ export async function activate(
325328

326329
const codeQlExtension = await installOrUpdateThenTryActivate(
327330
ctx,
331+
app,
328332
distributionManager,
329333
distributionConfigListener,
330334
{
@@ -346,6 +350,7 @@ export async function activate(
346350

347351
async function installOrUpdateDistributionWithProgressTitle(
348352
ctx: ExtensionContext,
353+
app: ExtensionApp,
349354
distributionManager: DistributionManager,
350355
progressTitle: string,
351356
config: DistributionUpdateConfig,
@@ -390,7 +395,7 @@ async function installOrUpdateDistributionWithProgressTitle(
390395
"Restart and Upgrade",
391396
)
392397
) {
393-
await commands.executeCommand("workbench.action.reloadWindow");
398+
await app.commands.execute("workbench.action.reloadWindow");
394399
}
395400
} else {
396401
await withProgress(
@@ -417,6 +422,7 @@ async function installOrUpdateDistributionWithProgressTitle(
417422

418423
async function installOrUpdateDistribution(
419424
ctx: ExtensionContext,
425+
app: ExtensionApp,
420426
distributionManager: DistributionManager,
421427
config: DistributionUpdateConfig,
422428
): Promise<void> {
@@ -437,6 +443,7 @@ async function installOrUpdateDistribution(
437443
try {
438444
await installOrUpdateDistributionWithProgressTitle(
439445
ctx,
446+
app,
440447
distributionManager,
441448
messageText,
442449
config,
@@ -522,11 +529,12 @@ async function getDistributionDisplayingDistributionWarnings(
522529

523530
async function installOrUpdateThenTryActivate(
524531
ctx: ExtensionContext,
532+
app: ExtensionApp,
525533
distributionManager: DistributionManager,
526534
distributionConfigListener: DistributionConfigListener,
527535
config: DistributionUpdateConfig,
528536
): Promise<CodeQLExtensionInterface | Record<string, never>> {
529-
await installOrUpdateDistribution(ctx, distributionManager, config);
537+
await installOrUpdateDistribution(ctx, app, distributionManager, config);
530538

531539
try {
532540
await prepareCodeTour();
@@ -546,6 +554,7 @@ async function installOrUpdateThenTryActivate(
546554
) {
547555
extensionInterface = await activateWithInstalledDistribution(
548556
ctx,
557+
app,
549558
distributionManager,
550559
distributionConfigListener,
551560
);
@@ -563,6 +572,7 @@ async function installOrUpdateThenTryActivate(
563572
if (chosenAction === installActionName) {
564573
await installOrUpdateThenTryActivate(
565574
ctx,
575+
app,
566576
distributionManager,
567577
distributionConfigListener,
568578
{
@@ -589,6 +599,7 @@ const PACK_GLOBS = [
589599

590600
async function activateWithInstalledDistribution(
591601
ctx: ExtensionContext,
602+
app: ExtensionApp,
592603
distributionManager: DistributionManager,
593604
distributionConfigListener: DistributionConfigListener,
594605
): Promise<CodeQLExtensionInterface> {
@@ -597,8 +608,6 @@ async function activateWithInstalledDistribution(
597608
// of activation.
598609
errorStubs.forEach((stub) => stub.dispose());
599610

600-
const app = new ExtensionApp(ctx);
601-
602611
void extLogger.log("Initializing configuration listener...");
603612
const qlConfigurationListener =
604613
await QueryServerConfigListener.createQueryServerConfigListener(
@@ -831,7 +840,7 @@ async function activateWithInstalledDistribution(
831840

832841
void extLogger.log("Registering top-level command palette commands.");
833842

834-
const allCommands: AllCommands = {
843+
const allCommands: AllExtensionCommands = {
835844
...getCommands(cliServer, qs),
836845
...getQueryEditorCommands({
837846
queryRunner: qs,
@@ -864,7 +873,7 @@ async function activateWithInstalledDistribution(
864873
};
865874

866875
for (const [commandName, command] of Object.entries(allCommands)) {
867-
app.commands.register(commandName as keyof AllCommands, command);
876+
app.commands.register(commandName as keyof AllExtensionCommands, command);
868877
}
869878

870879
const queryServerCommands: QueryServerCommands = {
@@ -955,7 +964,7 @@ async function activateWithInstalledDistribution(
955964
),
956965
);
957966

958-
await commands.executeCommand("codeQLDatabases.removeOrphanedDatabases");
967+
await app.commands.execute("codeQLDatabases.removeOrphanedDatabases");
959968

960969
void extLogger.log("Reading query history");
961970
await qhm.readQueryHistory();

0 commit comments

Comments
 (0)