Skip to content

Commit 9a32556

Browse files
committed
Move query language functions out of helpers
1 parent 2cd88ce commit 9a32556

File tree

5 files changed

+83
-80
lines changed

5 files changed

+83
-80
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { CodeQLCliServer } from "./cli";
2+
import { Uri, window } from "vscode";
3+
import { isQueryLanguage, QueryLanguage } from "../common/query-language";
4+
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
5+
import { extLogger } from "../common";
6+
import { UserCancellationException } from "../common/vscode/progress";
7+
import { showAndLogErrorMessage } from "../common/vscode/log";
8+
9+
/**
10+
* Finds the language that a query targets.
11+
* If it can't be autodetected, prompt the user to specify the language manually.
12+
*/
13+
export async function findLanguage(
14+
cliServer: CodeQLCliServer,
15+
queryUri: Uri | undefined,
16+
): Promise<QueryLanguage | undefined> {
17+
const uri = queryUri || window.activeTextEditor?.document.uri;
18+
if (uri !== undefined) {
19+
try {
20+
const queryInfo = await cliServer.resolveQueryByLanguage(
21+
getOnDiskWorkspaceFolders(),
22+
uri,
23+
);
24+
const language = Object.keys(queryInfo.byLanguage)[0];
25+
void extLogger.log(`Detected query language: ${language}`);
26+
27+
if (isQueryLanguage(language)) {
28+
return language;
29+
}
30+
31+
void extLogger.log(
32+
"Query language is unsupported. Select language manually.",
33+
);
34+
} catch (e) {
35+
void extLogger.log(
36+
"Could not autodetect query language. Select language manually.",
37+
);
38+
}
39+
}
40+
41+
// will be undefined if user cancels the quick pick.
42+
return await askForLanguage(cliServer, false);
43+
}
44+
45+
export async function askForLanguage(
46+
cliServer: CodeQLCliServer,
47+
throwOnEmpty = true,
48+
): Promise<QueryLanguage | undefined> {
49+
const language = await window.showQuickPick(
50+
await cliServer.getSupportedLanguages(),
51+
{
52+
placeHolder: "Select target language for your query",
53+
ignoreFocusOut: true,
54+
},
55+
);
56+
if (!language) {
57+
// This only happens if the user cancels the quick pick.
58+
if (throwOnEmpty) {
59+
throw new UserCancellationException("Cancelled.");
60+
} else {
61+
void showAndLogErrorMessage(
62+
"Language not found. Language must be specified manually.",
63+
);
64+
}
65+
return undefined;
66+
}
67+
68+
if (!isQueryLanguage(language)) {
69+
void showAndLogErrorMessage(
70+
`Language '${language}' is not supported. Only languages ${Object.values(
71+
QueryLanguage,
72+
).join(", ")} are supported.`,
73+
);
74+
return undefined;
75+
}
76+
77+
return language;
78+
}

extensions/ql-vscode/src/helpers.ts

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { ensureDir, ensureDirSync, writeFile } from "fs-extra";
22
import { join } from "path";
33
import { dirSync } from "tmp-promise";
4-
import { Uri, window as Window } from "vscode";
5-
import { CodeQLCliServer } from "./codeql-cli/cli";
6-
import { UserCancellationException } from "./common/vscode/progress";
74
import { extLogger } from "./common";
8-
import { isQueryLanguage, QueryLanguage } from "./common/query-language";
9-
import { getOnDiskWorkspaceFolders } from "./common/vscode/workspace-folders";
10-
import { showAndLogErrorMessage } from "./common/vscode/log";
115

126
// Shared temporary folder for the extension.
137
export const tmpDir = dirSync({
@@ -30,77 +24,6 @@ export const tmpDirDisposal = {
3024
},
3125
};
3226

33-
/**
34-
* Finds the language that a query targets.
35-
* If it can't be autodetected, prompt the user to specify the language manually.
36-
*/
37-
export async function findLanguage(
38-
cliServer: CodeQLCliServer,
39-
queryUri: Uri | undefined,
40-
): Promise<QueryLanguage | undefined> {
41-
const uri = queryUri || Window.activeTextEditor?.document.uri;
42-
if (uri !== undefined) {
43-
try {
44-
const queryInfo = await cliServer.resolveQueryByLanguage(
45-
getOnDiskWorkspaceFolders(),
46-
uri,
47-
);
48-
const language = Object.keys(queryInfo.byLanguage)[0];
49-
void extLogger.log(`Detected query language: ${language}`);
50-
51-
if (isQueryLanguage(language)) {
52-
return language;
53-
}
54-
55-
void extLogger.log(
56-
"Query language is unsupported. Select language manually.",
57-
);
58-
} catch (e) {
59-
void extLogger.log(
60-
"Could not autodetect query language. Select language manually.",
61-
);
62-
}
63-
}
64-
65-
// will be undefined if user cancels the quick pick.
66-
return await askForLanguage(cliServer, false);
67-
}
68-
69-
export async function askForLanguage(
70-
cliServer: CodeQLCliServer,
71-
throwOnEmpty = true,
72-
): Promise<QueryLanguage | undefined> {
73-
const language = await Window.showQuickPick(
74-
await cliServer.getSupportedLanguages(),
75-
{
76-
placeHolder: "Select target language for your query",
77-
ignoreFocusOut: true,
78-
},
79-
);
80-
if (!language) {
81-
// This only happens if the user cancels the quick pick.
82-
if (throwOnEmpty) {
83-
throw new UserCancellationException("Cancelled.");
84-
} else {
85-
void showAndLogErrorMessage(
86-
"Language not found. Language must be specified manually.",
87-
);
88-
}
89-
return undefined;
90-
}
91-
92-
if (!isQueryLanguage(language)) {
93-
void showAndLogErrorMessage(
94-
`Language '${language}' is not supported. Only languages ${Object.values(
95-
QueryLanguage,
96-
).join(", ")} are supported.`,
97-
);
98-
return undefined;
99-
}
100-
101-
return language;
102-
}
103-
10427
/**
10528
* Creates a file in the query directory that indicates when this query was created.
10629
* This is important for keeping track of when queries should be removed.

extensions/ql-vscode/src/local-queries/local-queries.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { extLogger, TeeLogger } from "../common";
1616
import { isCanary, MAX_QUERIES } from "../config";
1717
import { gatherQlFiles } from "../pure/files";
1818
import { basename } from "path";
19-
import { createTimestampFile, findLanguage } from "../helpers";
19+
import { createTimestampFile } from "../helpers";
2020
import { showBinaryChoiceDialog } from "../common/vscode/dialog";
2121
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
2222
import { displayQuickQuery } from "./quick-query";
@@ -47,6 +47,7 @@ import {
4747
showAndLogErrorMessage,
4848
showAndLogWarningMessage,
4949
} from "../common/vscode/log";
50+
import { findLanguage } from "../codeql-cli/query-language";
5051

5152
interface DatabaseQuickPickItem extends QuickPickItem {
5253
databaseItem: DatabaseItem;

extensions/ql-vscode/src/skeleton-query-wizard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { CodeQLCliServer } from "./codeql-cli/cli";
44
import { OutputChannelLogger } from "./common";
55
import { Credentials } from "./common/authentication";
66
import { QueryLanguage } from "./common/query-language";
7-
import { askForLanguage } from "./helpers";
87
import {
98
getFirstWorkspaceFolder,
109
isFolderAlreadyInWorkspace,
@@ -26,6 +25,7 @@ import {
2625
setQlPackLocation,
2726
} from "./config";
2827
import { existsSync } from "fs-extra";
28+
import { askForLanguage } from "./codeql-cli/query-language";
2929

3030
type QueryLanguagesToDatabaseMap = Record<string, string>;
3131

extensions/ql-vscode/src/variant-analysis/run-remote-query.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { relative, join, sep, dirname, parse, basename } from "path";
33
import { dump, load } from "js-yaml";
44
import { copy, writeFile, readFile, mkdirp } from "fs-extra";
55
import { dir, tmpName } from "tmp-promise";
6-
import { askForLanguage, findLanguage, tmpDir } from "../helpers";
6+
import { tmpDir } from "../helpers";
77
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
88
import { Credentials } from "../common/authentication";
99
import * as cli from "../codeql-cli/cli";
@@ -36,6 +36,7 @@ import {
3636
} from "../pure/ql";
3737
import { QueryLanguage } from "../common/query-language";
3838
import { tryGetQueryMetadata } from "../codeql-cli/query-metadata";
39+
import { askForLanguage, findLanguage } from "../codeql-cli/query-language";
3940

4041
export interface QlPack {
4142
name: string;

0 commit comments

Comments
 (0)