Skip to content

Commit e724577

Browse files
authored
Merge pull request #2196 from github/koesie10/ast-cfg-typed-commands
Convert AST and CFG commands to typed commands
2 parents 8a66bb4 + 9d4b19f commit e724577

File tree

3 files changed

+132
-199
lines changed

3 files changed

+132
-199
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { Uri, window } from "vscode";
2+
import { withProgress } from "./commandRunner";
3+
import { AstViewer } from "./astViewer";
4+
import {
5+
TemplatePrintAstProvider,
6+
TemplatePrintCfgProvider,
7+
} from "./contextual/templateProvider";
8+
import { compileAndRunQuery } from "./local-queries";
9+
import { QueryRunner } from "./queryRunner";
10+
import { QueryHistoryManager } from "./query-history/query-history-manager";
11+
import { DatabaseUI } from "./local-databases-ui";
12+
import { ResultsView } from "./interface";
13+
import { AstCfgCommands } from "./common/commands";
14+
15+
type AstCfgOptions = {
16+
queryRunner: QueryRunner;
17+
queryHistoryManager: QueryHistoryManager;
18+
databaseUI: DatabaseUI;
19+
localQueryResultsView: ResultsView;
20+
queryStorageDir: string;
21+
22+
astViewer: AstViewer;
23+
astTemplateProvider: TemplatePrintAstProvider;
24+
cfgTemplateProvider: TemplatePrintCfgProvider;
25+
};
26+
27+
export function getAstCfgCommands({
28+
queryRunner,
29+
queryHistoryManager,
30+
databaseUI,
31+
localQueryResultsView,
32+
queryStorageDir,
33+
astViewer,
34+
astTemplateProvider,
35+
cfgTemplateProvider,
36+
}: AstCfgOptions): AstCfgCommands {
37+
const viewAst = async (selectedFile: Uri) =>
38+
withProgress(
39+
async (progress, token) => {
40+
const ast = await astTemplateProvider.provideAst(
41+
progress,
42+
token,
43+
selectedFile ?? window.activeTextEditor?.document.uri,
44+
);
45+
if (ast) {
46+
astViewer.updateRoots(await ast.getRoots(), ast.db, ast.fileName);
47+
}
48+
},
49+
{
50+
cancellable: true,
51+
title: "Calculate AST",
52+
},
53+
);
54+
55+
const viewCfg = async () =>
56+
withProgress(
57+
async (progress, token) => {
58+
const res = await cfgTemplateProvider.provideCfgUri(
59+
window.activeTextEditor?.document,
60+
);
61+
if (res) {
62+
await compileAndRunQuery(
63+
queryRunner,
64+
queryHistoryManager,
65+
databaseUI,
66+
localQueryResultsView,
67+
queryStorageDir,
68+
false,
69+
res[0],
70+
progress,
71+
token,
72+
undefined,
73+
);
74+
}
75+
},
76+
{
77+
title: "Calculating Control Flow Graph",
78+
cancellable: true,
79+
},
80+
);
81+
82+
return {
83+
"codeQL.viewAst": viewAst,
84+
"codeQL.viewAstContextExplorer": viewAst,
85+
"codeQL.viewAstContextEditor": viewAst,
86+
"codeQL.viewCfg": viewCfg,
87+
"codeQL.viewCfgContextExplorer": viewCfg,
88+
"codeQL.viewCfgContextEditor": viewCfg,
89+
};
90+
}

extensions/ql-vscode/src/common/commands.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ export type DatabasePanelCommands = {
167167
"codeQLVariantAnalysisRepositories.removeItemContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
168168
};
169169

170+
export type AstCfgCommands = {
171+
"codeQL.viewAst": (selectedFile: Uri) => Promise<void>;
172+
"codeQL.viewAstContextExplorer": (selectedFile: Uri) => Promise<void>;
173+
"codeQL.viewAstContextEditor": (selectedFile: Uri) => Promise<void>;
174+
"codeQL.viewCfg": () => Promise<void>;
175+
"codeQL.viewCfgContextExplorer": () => Promise<void>;
176+
"codeQL.viewCfgContextEditor": () => Promise<void>;
177+
};
178+
170179
export type PackagingCommands = {
171180
"codeQL.installPackDependencies": () => Promise<void>;
172181
"codeQL.downloadPacks": () => Promise<void>;
@@ -181,6 +190,7 @@ export type AllCommands = BaseCommands &
181190
LocalDatabasesCommands &
182191
VariantAnalysisCommands &
183192
DatabasePanelCommands &
193+
AstCfgCommands &
184194
PackagingCommands &
185195
EvalLogViewerCommands;
186196

0 commit comments

Comments
 (0)