Skip to content

Commit eb25f31

Browse files
authored
Merge pull request #2117 from github/nora/split-run-query
Split usage of runQuery and runQueryOnMultipleDatabases
2 parents badbee1 + 7188d8d commit eb25f31

File tree

2 files changed

+112
-66
lines changed

2 files changed

+112
-66
lines changed

extensions/ql-vscode/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,18 @@
306306
"command": "codeQL.runQuery",
307307
"title": "CodeQL: Run Query on Selected Database"
308308
},
309+
{
310+
"command": "codeQL.runQueryContextEditor",
311+
"title": "CodeQL: Run Query on Selected Database"
312+
},
309313
{
310314
"command": "codeQL.runQueryOnMultipleDatabases",
311315
"title": "CodeQL: Run Query on Multiple Databases"
312316
},
317+
{
318+
"command": "codeQL.runQueryOnMultipleDatabasesContextEditor",
319+
"title": "CodeQL: Run Query on Multiple Databases"
320+
},
313321
{
314322
"command": "codeQL.runVariantAnalysis",
315323
"title": "CodeQL: Run Variant Analysis"
@@ -1211,11 +1219,11 @@
12111219
],
12121220
"editor/context": [
12131221
{
1214-
"command": "codeQL.runQuery",
1222+
"command": "codeQL.runQueryContextEditor",
12151223
"when": "editorLangId == ql && resourceExtname == .ql"
12161224
},
12171225
{
1218-
"command": "codeQL.runQueryOnMultipleDatabases",
1226+
"command": "codeQL.runQueryOnMultipleDatabasesContextEditor",
12191227
"when": "editorLangId == ql && resourceExtname == .ql"
12201228
},
12211229
{

extensions/ql-vscode/src/extension.ts

Lines changed: 102 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,74 @@ async function activateWithInstalledDistribution(
771771
}
772772
}
773773

774+
async function compileAndRunQueryOnMultipleDatabases(
775+
progress: ProgressCallback,
776+
token: CancellationToken,
777+
uri: Uri | undefined,
778+
): Promise<void> {
779+
let filteredDBs = dbm.databaseItems;
780+
if (filteredDBs.length === 0) {
781+
void showAndLogErrorMessage(
782+
"No databases found. Please add a suitable database to your workspace.",
783+
);
784+
return;
785+
}
786+
// If possible, only show databases with the right language (otherwise show all databases).
787+
const queryLanguage = await findLanguage(cliServer, uri);
788+
if (queryLanguage) {
789+
filteredDBs = dbm.databaseItems.filter(
790+
(db) => db.language === queryLanguage,
791+
);
792+
if (filteredDBs.length === 0) {
793+
void showAndLogErrorMessage(
794+
`No databases found for language ${queryLanguage}. Please add a suitable database to your workspace.`,
795+
);
796+
return;
797+
}
798+
}
799+
const quickPickItems = filteredDBs.map<DatabaseQuickPickItem>((dbItem) => ({
800+
databaseItem: dbItem,
801+
label: dbItem.name,
802+
description: dbItem.language,
803+
}));
804+
/**
805+
* Databases that were selected in the quick pick menu.
806+
*/
807+
const quickpick = await window.showQuickPick<DatabaseQuickPickItem>(
808+
quickPickItems,
809+
{ canPickMany: true, ignoreFocusOut: true },
810+
);
811+
if (quickpick !== undefined) {
812+
// Collect all skipped databases and display them at the end (instead of popping up individual errors)
813+
const skippedDatabases = [];
814+
const errors = [];
815+
for (const item of quickpick) {
816+
try {
817+
await compileAndRunQuery(
818+
false,
819+
uri,
820+
progress,
821+
token,
822+
item.databaseItem,
823+
);
824+
} catch (e) {
825+
skippedDatabases.push(item.label);
826+
errors.push(getErrorMessage(e));
827+
}
828+
}
829+
if (skippedDatabases.length > 0) {
830+
void extLogger.log(`Errors:\n${errors.join("\n")}`);
831+
void showAndLogWarningMessage(
832+
`The following databases were skipped:\n${skippedDatabases.join(
833+
"\n",
834+
)}.\nFor details about the errors, see the logs.`,
835+
);
836+
}
837+
} else {
838+
void showAndLogErrorMessage("No databases selected.");
839+
}
840+
}
841+
774842
const qhelpTmpDir = dirSync({
775843
prefix: "qhelp_",
776844
keep: false,
@@ -871,6 +939,25 @@ async function activateWithInstalledDistribution(
871939
queryServerLogger,
872940
),
873941
);
942+
943+
// Since we are tracking extension usage through commands, this command mirrors the runQuery command
944+
ctx.subscriptions.push(
945+
commandRunnerWithProgress(
946+
"codeQL.runQueryContextEditor",
947+
async (
948+
progress: ProgressCallback,
949+
token: CancellationToken,
950+
uri: Uri | undefined,
951+
) => await compileAndRunQuery(false, uri, progress, token, undefined),
952+
{
953+
title: "Running query",
954+
cancellable: true,
955+
},
956+
957+
// Open the query server logger on error since that's usually where the interesting errors appear.
958+
queryServerLogger,
959+
),
960+
);
874961
interface DatabaseQuickPickItem extends QuickPickItem {
875962
databaseItem: DatabaseItem;
876963
}
@@ -881,71 +968,22 @@ async function activateWithInstalledDistribution(
881968
progress: ProgressCallback,
882969
token: CancellationToken,
883970
uri: Uri | undefined,
884-
) => {
885-
let filteredDBs = dbm.databaseItems;
886-
if (filteredDBs.length === 0) {
887-
void showAndLogErrorMessage(
888-
"No databases found. Please add a suitable database to your workspace.",
889-
);
890-
return;
891-
}
892-
// If possible, only show databases with the right language (otherwise show all databases).
893-
const queryLanguage = await findLanguage(cliServer, uri);
894-
if (queryLanguage) {
895-
filteredDBs = dbm.databaseItems.filter(
896-
(db) => db.language === queryLanguage,
897-
);
898-
if (filteredDBs.length === 0) {
899-
void showAndLogErrorMessage(
900-
`No databases found for language ${queryLanguage}. Please add a suitable database to your workspace.`,
901-
);
902-
return;
903-
}
904-
}
905-
const quickPickItems = filteredDBs.map<DatabaseQuickPickItem>(
906-
(dbItem) => ({
907-
databaseItem: dbItem,
908-
label: dbItem.name,
909-
description: dbItem.language,
910-
}),
911-
);
912-
/**
913-
* Databases that were selected in the quick pick menu.
914-
*/
915-
const quickpick = await window.showQuickPick<DatabaseQuickPickItem>(
916-
quickPickItems,
917-
{ canPickMany: true, ignoreFocusOut: true },
918-
);
919-
if (quickpick !== undefined) {
920-
// Collect all skipped databases and display them at the end (instead of popping up individual errors)
921-
const skippedDatabases = [];
922-
const errors = [];
923-
for (const item of quickpick) {
924-
try {
925-
await compileAndRunQuery(
926-
false,
927-
uri,
928-
progress,
929-
token,
930-
item.databaseItem,
931-
);
932-
} catch (e) {
933-
skippedDatabases.push(item.label);
934-
errors.push(getErrorMessage(e));
935-
}
936-
}
937-
if (skippedDatabases.length > 0) {
938-
void extLogger.log(`Errors:\n${errors.join("\n")}`);
939-
void showAndLogWarningMessage(
940-
`The following databases were skipped:\n${skippedDatabases.join(
941-
"\n",
942-
)}.\nFor details about the errors, see the logs.`,
943-
);
944-
}
945-
} else {
946-
void showAndLogErrorMessage("No databases selected.");
947-
}
971+
) => await compileAndRunQueryOnMultipleDatabases(progress, token, uri),
972+
{
973+
title: "Running query on selected databases",
974+
cancellable: true,
948975
},
976+
),
977+
);
978+
// Since we are tracking extension usage through commands, this command mirrors the runQueryOnMultipleDatabases command
979+
ctx.subscriptions.push(
980+
commandRunnerWithProgress(
981+
"codeQL.runQueryOnMultipleDatabasesContextEditor",
982+
async (
983+
progress: ProgressCallback,
984+
token: CancellationToken,
985+
uri: Uri | undefined,
986+
) => await compileAndRunQueryOnMultipleDatabases(progress, token, uri),
949987
{
950988
title: "Running query on selected databases",
951989
cancellable: true,

0 commit comments

Comments
 (0)