Convert AST and CFG commands to typed commands

This commit is contained in:
Koen Vlaswinkel
2023-03-21 16:09:15 +01:00
parent 1909fee91f
commit b3092be5d3
3 changed files with 63 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
import { CancellationToken, ExtensionContext, Uri, window } from "vscode";
import { commandRunner, ProgressCallback, withProgress } from "./commandRunner";
import { CancellationToken, Uri, window } from "vscode";
import { ProgressCallback, withProgress } from "./commandRunner";
import { AstViewer } from "./astViewer";
import {
TemplatePrintAstProvider,
@@ -10,6 +10,7 @@ import { QueryRunner } from "./queryRunner";
import { QueryHistoryManager } from "./query-history/query-history-manager";
import { DatabaseUI } from "./local-databases-ui";
import { ResultsView } from "./interface";
import { AstCfgCommands } from "./common/commands";
type AstCfgOptions = {
queryRunner: QueryRunner;
@@ -23,19 +24,16 @@ type AstCfgOptions = {
cfgTemplateProvider: TemplatePrintCfgProvider;
};
export function registerAstCfgCommands(
ctx: ExtensionContext,
{
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
}: AstCfgOptions,
) {
export function getAstCfgCommands({
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
}: AstCfgOptions): AstCfgCommands {
const viewAstCommand = async (selectedFile: Uri) =>
withProgress(
async (progress, token) =>
@@ -79,29 +77,14 @@ export function registerAstCfgCommands(
},
);
ctx.subscriptions.push(commandRunner("codeQL.viewAst", viewAstCommand));
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
ctx.subscriptions.push(
commandRunner("codeQL.viewAstContextExplorer", viewAstCommand),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
ctx.subscriptions.push(
commandRunner("codeQL.viewAstContextEditor", viewAstCommand),
);
ctx.subscriptions.push(commandRunner("codeQL.viewCfg", viewCfgCommand));
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
ctx.subscriptions.push(
commandRunner("codeQL.viewCfgContextExplorer", viewCfgCommand),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
ctx.subscriptions.push(
commandRunner("codeQL.viewCfgContextEditor", viewCfgCommand),
);
return {
"codeQL.viewAst": viewAstCommand,
"codeQL.viewAstContextExplorer": viewAstCommand,
"codeQL.viewAstContextEditor": viewAstCommand,
"codeQL.viewCfg": viewCfgCommand,
"codeQL.viewCfgContextExplorer": viewCfgCommand,
"codeQL.viewCfgContextEditor": viewCfgCommand,
};
}
async function viewAst(

View File

@@ -123,11 +123,21 @@ export type DatabasePanelCommands = {
"codeQLVariantAnalysisRepositories.removeItemContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
};
export type AstCfgCommands = {
"codeQL.viewAst": (selectedFile: Uri) => Promise<void>;
"codeQL.viewAstContextExplorer": (selectedFile: Uri) => Promise<void>;
"codeQL.viewAstContextEditor": (selectedFile: Uri) => Promise<void>;
"codeQL.viewCfg": () => Promise<void>;
"codeQL.viewCfgContextExplorer": () => Promise<void>;
"codeQL.viewCfgContextEditor": () => Promise<void>;
};
export type AllCommands = BaseCommands &
QueryHistoryCommands &
LocalDatabasesCommands &
VariantAnalysisCommands &
DatabasePanelCommands;
DatabasePanelCommands &
AstCfgCommands;
export type AppCommandManager = CommandManager<AllCommands>;

View File

@@ -133,7 +133,7 @@ import {
getLocalQueryCommands,
showResultsForCompletedQuery,
} from "./local-queries";
import { registerAstCfgCommands } from "./ast-cfg-commands";
import { getAstCfgCommands } from "./ast-cfg-commands";
/**
* extension.ts
@@ -651,6 +651,15 @@ async function activateWithInstalledDistribution(
);
const queryStorageDir = join(ctx.globalStorageUri.fsPath, "queries");
await ensureDir(queryStorageDir);
// Store contextual queries in a temporary folder so that they are removed
// when the application closes. There is no need for the user to interact with them.
const contextualQueryStorageDir = join(
tmpDir.name,
"contextual-query-storage",
);
await ensureDir(contextualQueryStorageDir);
const labelProvider = new HistoryItemLabelProvider(
queryHistoryConfigurationListener,
);
@@ -787,6 +796,17 @@ async function activateWithInstalledDistribution(
ctx.subscriptions.push(testUIService);
}
const astViewer = new AstViewer();
const astTemplateProvider = new TemplatePrintAstProvider(
cliServer,
qs,
dbm,
contextualQueryStorageDir,
);
const cfgTemplateProvider = new TemplatePrintCfgProvider(cliServer, dbm);
ctx.subscriptions.push(astViewer);
void extLogger.log("Registering top-level command palette commands.");
const allCommands: AllCommands = {
@@ -795,6 +815,16 @@ async function activateWithInstalledDistribution(
...variantAnalysisManager.getCommands(),
...databaseUI.getCommands(),
...dbModule.getCommands(),
...getAstCfgCommands({
queryRunner: qs,
queryHistoryManager: qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
}),
};
for (const [commandName, command] of Object.entries(allCommands)) {
@@ -1057,13 +1087,6 @@ async function activateWithInstalledDistribution(
// Jump-to-definition and find-references
void extLogger.log("Registering jump-to-definition handlers.");
// Store contextual queries in a temporary folder so that they are removed
// when the application closes. There is no need for the user to interact with them.
const contextualQueryStorageDir = join(
tmpDir.name,
"contextual-query-storage",
);
await ensureDir(contextualQueryStorageDir);
languages.registerDefinitionProvider(
{ scheme: zipArchiveScheme },
new TemplateQueryDefinitionProvider(
@@ -1084,28 +1107,6 @@ async function activateWithInstalledDistribution(
),
);
const astViewer = new AstViewer();
const astTemplateProvider = new TemplatePrintAstProvider(
cliServer,
qs,
dbm,
contextualQueryStorageDir,
);
const cfgTemplateProvider = new TemplatePrintCfgProvider(cliServer, dbm);
ctx.subscriptions.push(astViewer);
registerAstCfgCommands(ctx, {
queryRunner: qs,
queryHistoryManager: qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
});
const mockServer = new VSCodeMockGitHubApiServer(ctx);
ctx.subscriptions.push(mockServer);
ctx.subscriptions.push(