From 39d4675b440706dd5bfbe91e7797bc8aec5182af Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 22 Mar 2023 14:16:55 +0000 Subject: [PATCH 1/3] Start using app.commands.execute for all commands called from extension.ts --- extensions/ql-vscode/src/common/commands.ts | 11 ++++++- extensions/ql-vscode/src/extension.ts | 32 ++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/extensions/ql-vscode/src/common/commands.ts b/extensions/ql-vscode/src/common/commands.ts index ff52f56a9dc..8e9c4bb9c1d 100644 --- a/extensions/ql-vscode/src/common/commands.ts +++ b/extensions/ql-vscode/src/common/commands.ts @@ -30,6 +30,12 @@ export type SingleSelectionCommandFunction = ( * the implementation in the corresponding `getCommands` function. */ +// Builtin commands where the implementation is provided by VS Code and not by this extension. +export type VSCodeCommands = { + "markdown.showPreviewToSide": (uri: Uri) => Promise; + "workbench.action.reloadWindow": () => Promise; +}; + // Base commands not tied directly to a module like e.g. variant analysis. export type BaseCommands = { "codeQL.openDocumentation": () => Promise; @@ -185,7 +191,8 @@ export type EvalLogViewerCommands = { "codeQLEvalLogViewer.clear": () => Promise; }; -export type AllCommands = BaseCommands & +// All commands where the implementation is provided by this extension. +export type AllCodeQLCommands = BaseCommands & QueryHistoryCommands & LocalDatabasesCommands & VariantAnalysisCommands & @@ -194,6 +201,8 @@ export type AllCommands = BaseCommands & PackagingCommands & EvalLogViewerCommands; +export type AllCommands = AllCodeQLCommands & VSCodeCommands; + export type AppCommandManager = CommandManager; // Separate command manager because it uses a different logger diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index 7d3653296a1..49d5fe674a7 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -1,7 +1,6 @@ import "source-map-support/register"; import { CancellationToken, - commands, Disposable, env, ExtensionContext, @@ -112,7 +111,7 @@ import { redactableError } from "./pure/errors"; import { QueryHistoryDirs } from "./query-history/query-history-dirs"; import { DirResult } from "tmp"; import { - AllCommands, + AllCodeQLCommands, BaseCommands, QueryServerCommands, } from "./common/commands"; @@ -265,6 +264,8 @@ export async function activate( addUnhandledRejectionListener(); install(); + const app = new ExtensionApp(ctx); + const codelensProvider = new QuickEvalCodeLensProvider(); languages.registerCodeLensProvider( { scheme: "file", language: "ql" }, @@ -291,6 +292,7 @@ export async function activate( distributionConfigListener.onDidChangeConfiguration(() => installOrUpdateThenTryActivate( ctx, + app, distributionManager, distributionConfigListener, { @@ -305,6 +307,7 @@ export async function activate( commandRunner(checkForUpdatesCommand, () => installOrUpdateThenTryActivate( ctx, + app, distributionManager, distributionConfigListener, { @@ -324,6 +327,7 @@ export async function activate( const codeQlExtension = await installOrUpdateThenTryActivate( ctx, + app, distributionManager, distributionConfigListener, { @@ -345,6 +349,7 @@ export async function activate( async function installOrUpdateDistributionWithProgressTitle( ctx: ExtensionContext, + app: ExtensionApp, distributionManager: DistributionManager, progressTitle: string, config: DistributionUpdateConfig, @@ -389,7 +394,7 @@ async function installOrUpdateDistributionWithProgressTitle( "Restart and Upgrade", ) ) { - await commands.executeCommand("workbench.action.reloadWindow"); + await app.commands.execute("workbench.action.reloadWindow"); } } else { await withProgress( @@ -416,6 +421,7 @@ async function installOrUpdateDistributionWithProgressTitle( async function installOrUpdateDistribution( ctx: ExtensionContext, + app: ExtensionApp, distributionManager: DistributionManager, config: DistributionUpdateConfig, ): Promise { @@ -436,6 +442,7 @@ async function installOrUpdateDistribution( try { await installOrUpdateDistributionWithProgressTitle( ctx, + app, distributionManager, messageText, config, @@ -521,11 +528,12 @@ async function getDistributionDisplayingDistributionWarnings( async function installOrUpdateThenTryActivate( ctx: ExtensionContext, + app: ExtensionApp, distributionManager: DistributionManager, distributionConfigListener: DistributionConfigListener, config: DistributionUpdateConfig, ): Promise> { - await installOrUpdateDistribution(ctx, distributionManager, config); + await installOrUpdateDistribution(ctx, app, distributionManager, config); try { await prepareCodeTour(); @@ -545,6 +553,7 @@ async function installOrUpdateThenTryActivate( ) { extensionInterface = await activateWithInstalledDistribution( ctx, + app, distributionManager, distributionConfigListener, ); @@ -562,6 +571,7 @@ async function installOrUpdateThenTryActivate( if (chosenAction === installActionName) { await installOrUpdateThenTryActivate( ctx, + app, distributionManager, distributionConfigListener, { @@ -588,6 +598,7 @@ const PACK_GLOBS = [ async function activateWithInstalledDistribution( ctx: ExtensionContext, + app: ExtensionApp, distributionManager: DistributionManager, distributionConfigListener: DistributionConfigListener, ): Promise { @@ -596,8 +607,6 @@ async function activateWithInstalledDistribution( // of activation. errorStubs.forEach((stub) => stub.dispose()); - const app = new ExtensionApp(ctx); - void extLogger.log("Initializing configuration listener..."); const qlConfigurationListener = await QueryServerConfigListener.createQueryServerConfigListener( @@ -821,7 +830,7 @@ async function activateWithInstalledDistribution( void extLogger.log("Registering top-level command palette commands."); - const allCommands: AllCommands = { + const allCommands: AllCodeQLCommands = { ...getCommands(cliServer, qs), ...qhm.getCommands(), ...variantAnalysisManager.getCommands(), @@ -844,7 +853,7 @@ async function activateWithInstalledDistribution( }; for (const [commandName, command] of Object.entries(allCommands)) { - app.commands.register(commandName as keyof AllCommands, command); + app.commands.register(commandName as keyof AllCodeQLCommands, command); } const queryServerCommands: QueryServerCommands = { @@ -895,7 +904,7 @@ async function activateWithInstalledDistribution( ctx.subscriptions.push( commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => { - await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery); + await previewQueryHelp(app, cliServer, qhelpTmpDir, selectedQuery); }), ); @@ -1002,7 +1011,7 @@ async function activateWithInstalledDistribution( ), ); - await commands.executeCommand("codeQLDatabases.removeOrphanedDatabases"); + await app.commands.execute("codeQLDatabases.removeOrphanedDatabases"); void extLogger.log("Reading query history"); await qhm.readQueryHistory(); @@ -1040,6 +1049,7 @@ async function showResultsForComparison( } async function previewQueryHelp( + app: ExtensionApp, cliServer: CodeQLCliServer, qhelpTmpDir: DirResult, selectedQuery: Uri, @@ -1055,7 +1065,7 @@ async function previewQueryHelp( const uri = Uri.file(absolutePathToMd); try { await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd); - await commands.executeCommand("markdown.showPreviewToSide", uri); + await app.commands.execute("markdown.showPreviewToSide", uri); } catch (e) { const errorMessage = getErrorMessage(e).includes( "Generating qhelp in markdown", From 31af28e73b6afa8f5d5d05fbc77cba3e1231f9a7 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 22 Mar 2023 14:28:10 +0000 Subject: [PATCH 2/3] Add link to docs --- extensions/ql-vscode/src/common/commands.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions/ql-vscode/src/common/commands.ts b/extensions/ql-vscode/src/common/commands.ts index 8e9c4bb9c1d..ba5a2210c60 100644 --- a/extensions/ql-vscode/src/common/commands.ts +++ b/extensions/ql-vscode/src/common/commands.ts @@ -31,6 +31,7 @@ export type SingleSelectionCommandFunction = ( */ // Builtin commands where the implementation is provided by VS Code and not by this extension. +// See https://code.visualstudio.com/api/references/commands export type VSCodeCommands = { "markdown.showPreviewToSide": (uri: Uri) => Promise; "workbench.action.reloadWindow": () => Promise; From 5d6a2e6d7fa5a7b47d0cb16be425bd15f632d61b Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 22 Mar 2023 15:37:39 +0000 Subject: [PATCH 3/3] rename types --- extensions/ql-vscode/src/common/commands.ts | 6 +++--- extensions/ql-vscode/src/extension.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/extensions/ql-vscode/src/common/commands.ts b/extensions/ql-vscode/src/common/commands.ts index 7c93ca427b2..bda47fb071c 100644 --- a/extensions/ql-vscode/src/common/commands.ts +++ b/extensions/ql-vscode/src/common/commands.ts @@ -33,7 +33,7 @@ export type SingleSelectionCommandFunction = ( // Builtin commands where the implementation is provided by VS Code and not by this extension. // See https://code.visualstudio.com/api/references/commands -export type VSCodeCommands = { +export type BuiltInVsCodeCommands = { "markdown.showPreviewToSide": (uri: Uri) => Promise; "workbench.action.reloadWindow": () => Promise; }; @@ -220,7 +220,7 @@ export type MockGitHubApiServerCommands = { }; // All commands where the implementation is provided by this extension. -export type AllCodeQLCommands = BaseCommands & +export type AllExtensionCommands = BaseCommands & QueryHistoryCommands & LocalDatabasesCommands & VariantAnalysisCommands & @@ -232,7 +232,7 @@ export type AllCodeQLCommands = BaseCommands & SummaryLanguageSupportCommands & MockGitHubApiServerCommands; -export type AllCommands = AllCodeQLCommands & VSCodeCommands; +export type AllCommands = AllExtensionCommands & BuiltInVsCodeCommands; export type AppCommandManager = CommandManager; diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index be2cce61721..a69270e89de 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -112,7 +112,7 @@ import { redactableError } from "./pure/errors"; import { QueryHistoryDirs } from "./query-history/query-history-dirs"; import { DirResult } from "tmp"; import { - AllCodeQLCommands, + AllExtensionCommands, BaseCommands, QueryServerCommands, } from "./common/commands"; @@ -837,7 +837,7 @@ async function activateWithInstalledDistribution( void extLogger.log("Registering top-level command palette commands."); - const allCommands: AllCodeQLCommands = { + const allCommands: AllExtensionCommands = { ...getCommands(cliServer, qs), ...localQueryResultsView.getCommands(), ...qhm.getCommands(), @@ -864,7 +864,7 @@ async function activateWithInstalledDistribution( }; for (const [commandName, command] of Object.entries(allCommands)) { - app.commands.register(commandName as keyof AllCodeQLCommands, command); + app.commands.register(commandName as keyof AllExtensionCommands, command); } const queryServerCommands: QueryServerCommands = {