Move AST and CFG commands to separate file

This commit is contained in:
Koen Vlaswinkel
2023-03-21 15:58:12 +01:00
parent 7950c1c982
commit 0379575256
2 changed files with 224 additions and 175 deletions

View File

@@ -0,0 +1,212 @@
import { CancellationToken, ExtensionContext, Uri, window } from "vscode";
import { commandRunnerWithProgress, ProgressCallback } from "./commandRunner";
import { AstViewer } from "./astViewer";
import {
TemplatePrintAstProvider,
TemplatePrintCfgProvider,
} from "./contextual/templateProvider";
import { compileAndRunQuery } from "./local-queries";
import { QueryRunner } from "./queryRunner";
import { QueryHistoryManager } from "./query-history/query-history-manager";
import { DatabaseUI } from "./local-databases-ui";
import { ResultsView } from "./interface";
type AstCfgOptions = {
queryRunner: QueryRunner;
queryHistoryManager: QueryHistoryManager;
databaseUI: DatabaseUI;
localQueryResultsView: ResultsView;
queryStorageDir: string;
astViewer: AstViewer;
astTemplateProvider: TemplatePrintAstProvider;
cfgTemplateProvider: TemplatePrintCfgProvider;
};
export function registerAstCfgCommands(
ctx: ExtensionContext,
{
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
}: AstCfgOptions,
) {
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewAst",
async (
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
) =>
await viewAst(
astViewer,
astTemplateProvider,
progress,
token,
selectedFile,
),
{
cancellable: true,
title: "Calculate AST",
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewAstContextExplorer",
async (
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
) =>
await viewAst(
astViewer,
astTemplateProvider,
progress,
token,
selectedFile,
),
{
cancellable: true,
title: "Calculate AST",
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewAstContextEditor",
async (
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
) =>
await viewAst(
astViewer,
astTemplateProvider,
progress,
token,
selectedFile,
),
{
cancellable: true,
title: "Calculate AST",
},
),
);
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewCfg",
async (progress: ProgressCallback, token: CancellationToken) => {
const res = await cfgTemplateProvider.provideCfgUri(
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
res[0],
progress,
token,
undefined,
);
}
},
{
title: "Calculating Control Flow Graph",
cancellable: true,
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewCfgContextExplorer",
async (progress: ProgressCallback, token: CancellationToken) => {
const res = await cfgTemplateProvider.provideCfgUri(
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
res[0],
progress,
token,
undefined,
);
}
},
{
title: "Calculating Control Flow Graph",
cancellable: true,
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewCfgContextEditor",
async (progress: ProgressCallback, token: CancellationToken) => {
const res = await cfgTemplateProvider.provideCfgUri(
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
res[0],
progress,
token,
undefined,
);
}
},
{
title: "Calculating Control Flow Graph",
cancellable: true,
},
),
);
}
async function viewAst(
astViewer: AstViewer,
printAstTemplateProvider: TemplatePrintAstProvider,
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
): Promise<void> {
const ast = await printAstTemplateProvider.provideAst(
progress,
token,
selectedFile ?? window.activeTextEditor?.document.uri,
);
if (ast) {
astViewer.updateRoots(await ast.getRoots(), ast.db, ast.fileName);
}
}

View File

@@ -130,10 +130,10 @@ import {
QueryServerCommands,
} from "./common/commands";
import {
compileAndRunQuery,
getLocalQueryCommands,
showResultsForCompletedQuery,
} from "./local-queries";
import { registerAstCfgCommands } from "./ast-cfg-commands";
/**
* extension.ts
@@ -1085,7 +1085,7 @@ async function activateWithInstalledDistribution(
);
const astViewer = new AstViewer();
const printAstTemplateProvider = new TemplatePrintAstProvider(
const astTemplateProvider = new TemplatePrintAstProvider(
cliServer,
qs,
dbm,
@@ -1095,162 +1095,16 @@ async function activateWithInstalledDistribution(
ctx.subscriptions.push(astViewer);
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewAst",
async (
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
) =>
await viewAst(
astViewer,
printAstTemplateProvider,
progress,
token,
selectedFile,
),
{
cancellable: true,
title: "Calculate AST",
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewAstContextExplorer",
async (
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
) =>
await viewAst(
astViewer,
printAstTemplateProvider,
progress,
token,
selectedFile,
),
{
cancellable: true,
title: "Calculate AST",
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewAstContextEditor",
async (
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
) =>
await viewAst(
astViewer,
printAstTemplateProvider,
progress,
token,
selectedFile,
),
{
cancellable: true,
title: "Calculate AST",
},
),
);
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewCfg",
async (progress: ProgressCallback, token: CancellationToken) => {
const res = await cfgTemplateProvider.provideCfgUri(
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
qs,
qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
res[0],
progress,
token,
undefined,
);
}
},
{
title: "Calculating Control Flow Graph",
cancellable: true,
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewCfgContextExplorer",
async (progress: ProgressCallback, token: CancellationToken) => {
const res = await cfgTemplateProvider.provideCfgUri(
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
qs,
qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
res[0],
progress,
token,
undefined,
);
}
},
{
title: "Calculating Control Flow Graph",
cancellable: true,
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
ctx.subscriptions.push(
commandRunnerWithProgress(
"codeQL.viewCfgContextEditor",
async (progress: ProgressCallback, token: CancellationToken) => {
const res = await cfgTemplateProvider.provideCfgUri(
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
qs,
qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
res[0],
progress,
token,
undefined,
);
}
},
{
title: "Calculating Control Flow Graph",
cancellable: true,
},
),
);
registerAstCfgCommands(ctx, {
queryRunner: qs,
queryHistoryManager: qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
});
const mockServer = new VSCodeMockGitHubApiServer(ctx);
ctx.subscriptions.push(mockServer);
@@ -1367,23 +1221,6 @@ async function openReferencedFile(
}
}
async function viewAst(
astViewer: AstViewer,
printAstTemplateProvider: TemplatePrintAstProvider,
progress: ProgressCallback,
token: CancellationToken,
selectedFile: Uri,
): Promise<void> {
const ast = await printAstTemplateProvider.provideAst(
progress,
token,
selectedFile ?? window.activeTextEditor?.document.uri,
);
if (ast) {
astViewer.updateRoots(await ast.getRoots(), ast.db, ast.fileName);
}
}
function addUnhandledRejectionListener() {
const handler = (error: unknown) => {
// This listener will be triggered for errors from other extensions as