Skip to content

Commit 6ff2670

Browse files
committed
Remove duplication of AST and CFG command implementations
1 parent 0379575 commit 6ff2670

File tree

1 file changed

+60
-135
lines changed

1 file changed

+60
-135
lines changed

extensions/ql-vscode/src/ast-cfg-commands.ts

Lines changed: 60 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -36,161 +36,86 @@ export function registerAstCfgCommands(
3636
cfgTemplateProvider,
3737
}: AstCfgOptions,
3838
) {
39+
const viewAstCommand = async (
40+
progress: ProgressCallback,
41+
token: CancellationToken,
42+
selectedFile: Uri,
43+
) =>
44+
await viewAst(
45+
astViewer,
46+
astTemplateProvider,
47+
progress,
48+
token,
49+
selectedFile,
50+
);
51+
52+
const viewCfgCommand = async (
53+
progress: ProgressCallback,
54+
token: CancellationToken,
55+
) => {
56+
const res = await cfgTemplateProvider.provideCfgUri(
57+
window.activeTextEditor?.document,
58+
);
59+
if (res) {
60+
await compileAndRunQuery(
61+
queryRunner,
62+
queryHistoryManager,
63+
databaseUI,
64+
localQueryResultsView,
65+
queryStorageDir,
66+
false,
67+
res[0],
68+
progress,
69+
token,
70+
undefined,
71+
);
72+
}
73+
};
74+
3975
ctx.subscriptions.push(
40-
commandRunnerWithProgress(
41-
"codeQL.viewAst",
42-
async (
43-
progress: ProgressCallback,
44-
token: CancellationToken,
45-
selectedFile: Uri,
46-
) =>
47-
await viewAst(
48-
astViewer,
49-
astTemplateProvider,
50-
progress,
51-
token,
52-
selectedFile,
53-
),
54-
{
55-
cancellable: true,
56-
title: "Calculate AST",
57-
},
58-
),
76+
commandRunnerWithProgress("codeQL.viewAst", viewAstCommand, {
77+
cancellable: true,
78+
title: "Calculate AST",
79+
}),
5980
);
6081

6182
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
6283
ctx.subscriptions.push(
63-
commandRunnerWithProgress(
64-
"codeQL.viewAstContextExplorer",
65-
async (
66-
progress: ProgressCallback,
67-
token: CancellationToken,
68-
selectedFile: Uri,
69-
) =>
70-
await viewAst(
71-
astViewer,
72-
astTemplateProvider,
73-
progress,
74-
token,
75-
selectedFile,
76-
),
77-
{
78-
cancellable: true,
79-
title: "Calculate AST",
80-
},
81-
),
84+
commandRunnerWithProgress("codeQL.viewAstContextExplorer", viewAstCommand, {
85+
cancellable: true,
86+
title: "Calculate AST",
87+
}),
8288
);
8389

8490
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
8591
ctx.subscriptions.push(
86-
commandRunnerWithProgress(
87-
"codeQL.viewAstContextEditor",
88-
async (
89-
progress: ProgressCallback,
90-
token: CancellationToken,
91-
selectedFile: Uri,
92-
) =>
93-
await viewAst(
94-
astViewer,
95-
astTemplateProvider,
96-
progress,
97-
token,
98-
selectedFile,
99-
),
100-
{
101-
cancellable: true,
102-
title: "Calculate AST",
103-
},
104-
),
92+
commandRunnerWithProgress("codeQL.viewAstContextEditor", viewAstCommand, {
93+
cancellable: true,
94+
title: "Calculate AST",
95+
}),
10596
);
10697

10798
ctx.subscriptions.push(
108-
commandRunnerWithProgress(
109-
"codeQL.viewCfg",
110-
async (progress: ProgressCallback, token: CancellationToken) => {
111-
const res = await cfgTemplateProvider.provideCfgUri(
112-
window.activeTextEditor?.document,
113-
);
114-
if (res) {
115-
await compileAndRunQuery(
116-
queryRunner,
117-
queryHistoryManager,
118-
databaseUI,
119-
localQueryResultsView,
120-
queryStorageDir,
121-
false,
122-
res[0],
123-
progress,
124-
token,
125-
undefined,
126-
);
127-
}
128-
},
129-
{
130-
title: "Calculating Control Flow Graph",
131-
cancellable: true,
132-
},
133-
),
99+
commandRunnerWithProgress("codeQL.viewCfg", viewCfgCommand, {
100+
title: "Calculating Control Flow Graph",
101+
cancellable: true,
102+
}),
134103
);
135104

136105
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
137106
ctx.subscriptions.push(
138-
commandRunnerWithProgress(
139-
"codeQL.viewCfgContextExplorer",
140-
async (progress: ProgressCallback, token: CancellationToken) => {
141-
const res = await cfgTemplateProvider.provideCfgUri(
142-
window.activeTextEditor?.document,
143-
);
144-
if (res) {
145-
await compileAndRunQuery(
146-
queryRunner,
147-
queryHistoryManager,
148-
databaseUI,
149-
localQueryResultsView,
150-
queryStorageDir,
151-
false,
152-
res[0],
153-
progress,
154-
token,
155-
undefined,
156-
);
157-
}
158-
},
159-
{
160-
title: "Calculating Control Flow Graph",
161-
cancellable: true,
162-
},
163-
),
107+
commandRunnerWithProgress("codeQL.viewCfgContextExplorer", viewCfgCommand, {
108+
title: "Calculating Control Flow Graph",
109+
cancellable: true,
110+
}),
164111
);
165112

166113
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
167114
ctx.subscriptions.push(
168-
commandRunnerWithProgress(
169-
"codeQL.viewCfgContextEditor",
170-
async (progress: ProgressCallback, token: CancellationToken) => {
171-
const res = await cfgTemplateProvider.provideCfgUri(
172-
window.activeTextEditor?.document,
173-
);
174-
if (res) {
175-
await compileAndRunQuery(
176-
queryRunner,
177-
queryHistoryManager,
178-
databaseUI,
179-
localQueryResultsView,
180-
queryStorageDir,
181-
false,
182-
res[0],
183-
progress,
184-
token,
185-
undefined,
186-
);
187-
}
188-
},
189-
{
190-
title: "Calculating Control Flow Graph",
191-
cancellable: true,
192-
},
193-
),
115+
commandRunnerWithProgress("codeQL.viewCfgContextEditor", viewCfgCommand, {
116+
title: "Calculating Control Flow Graph",
117+
cancellable: true,
118+
}),
194119
);
195120
}
196121

0 commit comments

Comments
 (0)