Skip to content

Commit b3092be

Browse files
committed
Convert AST and CFG commands to typed commands
1 parent 1909fee commit b3092be

File tree

3 files changed

+63
-69
lines changed

3 files changed

+63
-69
lines changed

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

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CancellationToken, ExtensionContext, Uri, window } from "vscode";
2-
import { commandRunner, ProgressCallback, withProgress } from "./commandRunner";
1+
import { CancellationToken, Uri, window } from "vscode";
2+
import { ProgressCallback, withProgress } from "./commandRunner";
33
import { AstViewer } from "./astViewer";
44
import {
55
TemplatePrintAstProvider,
@@ -10,6 +10,7 @@ import { QueryRunner } from "./queryRunner";
1010
import { QueryHistoryManager } from "./query-history/query-history-manager";
1111
import { DatabaseUI } from "./local-databases-ui";
1212
import { ResultsView } from "./interface";
13+
import { AstCfgCommands } from "./common/commands";
1314

1415
type AstCfgOptions = {
1516
queryRunner: QueryRunner;
@@ -23,19 +24,16 @@ type AstCfgOptions = {
2324
cfgTemplateProvider: TemplatePrintCfgProvider;
2425
};
2526

26-
export function registerAstCfgCommands(
27-
ctx: ExtensionContext,
28-
{
29-
queryRunner,
30-
queryHistoryManager,
31-
databaseUI,
32-
localQueryResultsView,
33-
queryStorageDir,
34-
astViewer,
35-
astTemplateProvider,
36-
cfgTemplateProvider,
37-
}: AstCfgOptions,
38-
) {
27+
export function getAstCfgCommands({
28+
queryRunner,
29+
queryHistoryManager,
30+
databaseUI,
31+
localQueryResultsView,
32+
queryStorageDir,
33+
astViewer,
34+
astTemplateProvider,
35+
cfgTemplateProvider,
36+
}: AstCfgOptions): AstCfgCommands {
3937
const viewAstCommand = async (selectedFile: Uri) =>
4038
withProgress(
4139
async (progress, token) =>
@@ -79,29 +77,14 @@ export function registerAstCfgCommands(
7977
},
8078
);
8179

82-
ctx.subscriptions.push(commandRunner("codeQL.viewAst", viewAstCommand));
83-
84-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
85-
ctx.subscriptions.push(
86-
commandRunner("codeQL.viewAstContextExplorer", viewAstCommand),
87-
);
88-
89-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command
90-
ctx.subscriptions.push(
91-
commandRunner("codeQL.viewAstContextEditor", viewAstCommand),
92-
);
93-
94-
ctx.subscriptions.push(commandRunner("codeQL.viewCfg", viewCfgCommand));
95-
96-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
97-
ctx.subscriptions.push(
98-
commandRunner("codeQL.viewCfgContextExplorer", viewCfgCommand),
99-
);
100-
101-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command
102-
ctx.subscriptions.push(
103-
commandRunner("codeQL.viewCfgContextEditor", viewCfgCommand),
104-
);
80+
return {
81+
"codeQL.viewAst": viewAstCommand,
82+
"codeQL.viewAstContextExplorer": viewAstCommand,
83+
"codeQL.viewAstContextEditor": viewAstCommand,
84+
"codeQL.viewCfg": viewCfgCommand,
85+
"codeQL.viewCfgContextExplorer": viewCfgCommand,
86+
"codeQL.viewCfgContextEditor": viewCfgCommand,
87+
};
10588
}
10689

10790
async function viewAst(

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,21 @@ export type DatabasePanelCommands = {
123123
"codeQLVariantAnalysisRepositories.removeItemContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
124124
};
125125

126+
export type AstCfgCommands = {
127+
"codeQL.viewAst": (selectedFile: Uri) => Promise<void>;
128+
"codeQL.viewAstContextExplorer": (selectedFile: Uri) => Promise<void>;
129+
"codeQL.viewAstContextEditor": (selectedFile: Uri) => Promise<void>;
130+
"codeQL.viewCfg": () => Promise<void>;
131+
"codeQL.viewCfgContextExplorer": () => Promise<void>;
132+
"codeQL.viewCfgContextEditor": () => Promise<void>;
133+
};
134+
126135
export type AllCommands = BaseCommands &
127136
QueryHistoryCommands &
128137
LocalDatabasesCommands &
129138
VariantAnalysisCommands &
130-
DatabasePanelCommands;
139+
DatabasePanelCommands &
140+
AstCfgCommands;
131141

132142
export type AppCommandManager = CommandManager<AllCommands>;
133143

extensions/ql-vscode/src/extension.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ import {
133133
getLocalQueryCommands,
134134
showResultsForCompletedQuery,
135135
} from "./local-queries";
136-
import { registerAstCfgCommands } from "./ast-cfg-commands";
136+
import { getAstCfgCommands } from "./ast-cfg-commands";
137137

138138
/**
139139
* extension.ts
@@ -651,6 +651,15 @@ async function activateWithInstalledDistribution(
651651
);
652652
const queryStorageDir = join(ctx.globalStorageUri.fsPath, "queries");
653653
await ensureDir(queryStorageDir);
654+
655+
// Store contextual queries in a temporary folder so that they are removed
656+
// when the application closes. There is no need for the user to interact with them.
657+
const contextualQueryStorageDir = join(
658+
tmpDir.name,
659+
"contextual-query-storage",
660+
);
661+
await ensureDir(contextualQueryStorageDir);
662+
654663
const labelProvider = new HistoryItemLabelProvider(
655664
queryHistoryConfigurationListener,
656665
);
@@ -787,6 +796,17 @@ async function activateWithInstalledDistribution(
787796
ctx.subscriptions.push(testUIService);
788797
}
789798

799+
const astViewer = new AstViewer();
800+
const astTemplateProvider = new TemplatePrintAstProvider(
801+
cliServer,
802+
qs,
803+
dbm,
804+
contextualQueryStorageDir,
805+
);
806+
const cfgTemplateProvider = new TemplatePrintCfgProvider(cliServer, dbm);
807+
808+
ctx.subscriptions.push(astViewer);
809+
790810
void extLogger.log("Registering top-level command palette commands.");
791811

792812
const allCommands: AllCommands = {
@@ -795,6 +815,16 @@ async function activateWithInstalledDistribution(
795815
...variantAnalysisManager.getCommands(),
796816
...databaseUI.getCommands(),
797817
...dbModule.getCommands(),
818+
...getAstCfgCommands({
819+
queryRunner: qs,
820+
queryHistoryManager: qhm,
821+
databaseUI,
822+
localQueryResultsView,
823+
queryStorageDir,
824+
astViewer,
825+
astTemplateProvider,
826+
cfgTemplateProvider,
827+
}),
798828
};
799829

800830
for (const [commandName, command] of Object.entries(allCommands)) {
@@ -1057,13 +1087,6 @@ async function activateWithInstalledDistribution(
10571087
// Jump-to-definition and find-references
10581088
void extLogger.log("Registering jump-to-definition handlers.");
10591089

1060-
// Store contextual queries in a temporary folder so that they are removed
1061-
// when the application closes. There is no need for the user to interact with them.
1062-
const contextualQueryStorageDir = join(
1063-
tmpDir.name,
1064-
"contextual-query-storage",
1065-
);
1066-
await ensureDir(contextualQueryStorageDir);
10671090
languages.registerDefinitionProvider(
10681091
{ scheme: zipArchiveScheme },
10691092
new TemplateQueryDefinitionProvider(
@@ -1084,28 +1107,6 @@ async function activateWithInstalledDistribution(
10841107
),
10851108
);
10861109

1087-
const astViewer = new AstViewer();
1088-
const astTemplateProvider = new TemplatePrintAstProvider(
1089-
cliServer,
1090-
qs,
1091-
dbm,
1092-
contextualQueryStorageDir,
1093-
);
1094-
const cfgTemplateProvider = new TemplatePrintCfgProvider(cliServer, dbm);
1095-
1096-
ctx.subscriptions.push(astViewer);
1097-
1098-
registerAstCfgCommands(ctx, {
1099-
queryRunner: qs,
1100-
queryHistoryManager: qhm,
1101-
databaseUI,
1102-
localQueryResultsView,
1103-
queryStorageDir,
1104-
astViewer,
1105-
astTemplateProvider,
1106-
cfgTemplateProvider,
1107-
});
1108-
11091110
const mockServer = new VSCodeMockGitHubApiServer(ctx);
11101111
ctx.subscriptions.push(mockServer);
11111112
ctx.subscriptions.push(

0 commit comments

Comments
 (0)