Start using app.commands.execute for all commands called from extension.ts

This commit is contained in:
Robert
2023-03-22 14:16:55 +00:00
parent e724577d82
commit 39d4675b44
2 changed files with 31 additions and 12 deletions

View File

@@ -30,6 +30,12 @@ export type SingleSelectionCommandFunction<Item> = (
* 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<void>;
"workbench.action.reloadWindow": () => Promise<void>;
};
// Base commands not tied directly to a module like e.g. variant analysis.
export type BaseCommands = {
"codeQL.openDocumentation": () => Promise<void>;
@@ -185,7 +191,8 @@ export type EvalLogViewerCommands = {
"codeQLEvalLogViewer.clear": () => Promise<void>;
};
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<AllCommands>;
// Separate command manager because it uses a different logger

View File

@@ -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<void> {
@@ -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<CodeQLExtensionInterface | Record<string, never>> {
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<CodeQLExtensionInterface> {
@@ -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",