From fe6ff6801a52b5c62bdf80abee1d1db66c7d79dc Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Wed, 22 Mar 2023 17:02:16 +0100 Subject: [PATCH] Convert some base commands to typed commands --- extensions/ql-vscode/src/common/commands.ts | 3 + extensions/ql-vscode/src/extension.ts | 71 ++++++++++----------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/extensions/ql-vscode/src/common/commands.ts b/extensions/ql-vscode/src/common/commands.ts index 07754d509..36ef03576 100644 --- a/extensions/ql-vscode/src/common/commands.ts +++ b/extensions/ql-vscode/src/common/commands.ts @@ -35,7 +35,10 @@ export type SingleSelectionCommandFunction = ( // Base commands not tied directly to a module like e.g. variant analysis. export type BaseCommands = { "codeQL.openDocumentation": () => Promise; + "codeQL.showLogs": () => Promise; + "codeQL.authenticateToGitHub": () => Promise; + "codeQL.copyVersion": () => Promise; "codeQL.restartQueryServer": () => Promise; }; diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index 4d6a2c9f0..514dc8e57 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -123,6 +123,7 @@ import { showResultsForCompletedQuery, } from "./local-queries"; import { getAstCfgCommands } from "./ast-cfg-commands"; +import { App } from "./common/app"; /** * extension.ts @@ -158,9 +159,18 @@ const extension = extensions.getExtension(extensionId); * Return all commands that are not tied to the more specific managers. */ function getCommands( + app: App, cliServer: CodeQLCliServer, queryRunner: QueryRunner, ): BaseCommands { + const getCliVersion = async () => { + try { + return await cliServer.getVersion(); + } catch { + return ""; + } + }; + return { "codeQL.openDocumentation": async () => { await env.openExternal(Uri.parse("https://codeql.github.com/docs/")); @@ -179,6 +189,27 @@ function getCommands( title: "Restarting Query Server", }, ), + "codeQL.copyVersion": async () => { + const text = `CodeQL extension version: ${ + extension?.packageJSON.version + } \nCodeQL CLI version: ${await getCliVersion()} \nPlatform: ${platform()} ${arch()}`; + await env.clipboard.writeText(text); + void showAndLogInformationMessage(text); + }, + "codeQL.authenticateToGitHub": async () => { + /** + * Credentials for authenticating to GitHub. + * These are used when making API calls. + */ + const octokit = await app.credentials.getOctokit(); + const userInfo = await octokit.users.getAuthenticated(); + void showAndLogInformationMessage( + `Authenticated to GitHub as user: ${userInfo.data.login}`, + ); + }, + "codeQL.showLogs": async () => { + extLogger.show(); + }, }; } @@ -833,7 +864,7 @@ async function activateWithInstalledDistribution( void extLogger.log("Registering top-level command palette commands."); const allCommands: AllCommands = { - ...getCommands(cliServer, qs), + ...getCommands(app, cliServer, qs), ...localQueryResultsView.getCommands(), ...qhm.getCommands(), ...variantAnalysisManager.getCommands(), @@ -915,44 +946,6 @@ async function activateWithInstalledDistribution( }), ); - ctx.subscriptions.push( - commandRunner("codeQL.copyVersion", async () => { - const text = `CodeQL extension version: ${ - extension?.packageJSON.version - } \nCodeQL CLI version: ${await getCliVersion()} \nPlatform: ${platform()} ${arch()}`; - await env.clipboard.writeText(text); - void showAndLogInformationMessage(text); - }), - ); - - const getCliVersion = async () => { - try { - return await cliServer.getVersion(); - } catch { - return ""; - } - }; - - ctx.subscriptions.push( - commandRunner("codeQL.authenticateToGitHub", async () => { - /** - * Credentials for authenticating to GitHub. - * These are used when making API calls. - */ - const octokit = await app.credentials.getOctokit(); - const userInfo = await octokit.users.getAuthenticated(); - void showAndLogInformationMessage( - `Authenticated to GitHub as user: ${userInfo.data.login}`, - ); - }), - ); - - ctx.subscriptions.push( - commandRunner("codeQL.showLogs", async () => { - extLogger.show(); - }), - ); - void extLogger.log("Starting language server."); await client.start(); ctx.subscriptions.push({