Move query language functions out of helpers
This commit is contained in:
78
extensions/ql-vscode/src/codeql-cli/query-language.ts
Normal file
78
extensions/ql-vscode/src/codeql-cli/query-language.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { CodeQLCliServer } from "./cli";
|
||||
import { Uri, window } from "vscode";
|
||||
import { isQueryLanguage, QueryLanguage } from "../common/query-language";
|
||||
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
|
||||
import { extLogger } from "../common";
|
||||
import { UserCancellationException } from "../common/vscode/progress";
|
||||
import { showAndLogErrorMessage } from "../common/vscode/log";
|
||||
|
||||
/**
|
||||
* Finds the language that a query targets.
|
||||
* If it can't be autodetected, prompt the user to specify the language manually.
|
||||
*/
|
||||
export async function findLanguage(
|
||||
cliServer: CodeQLCliServer,
|
||||
queryUri: Uri | undefined,
|
||||
): Promise<QueryLanguage | undefined> {
|
||||
const uri = queryUri || window.activeTextEditor?.document.uri;
|
||||
if (uri !== undefined) {
|
||||
try {
|
||||
const queryInfo = await cliServer.resolveQueryByLanguage(
|
||||
getOnDiskWorkspaceFolders(),
|
||||
uri,
|
||||
);
|
||||
const language = Object.keys(queryInfo.byLanguage)[0];
|
||||
void extLogger.log(`Detected query language: ${language}`);
|
||||
|
||||
if (isQueryLanguage(language)) {
|
||||
return language;
|
||||
}
|
||||
|
||||
void extLogger.log(
|
||||
"Query language is unsupported. Select language manually.",
|
||||
);
|
||||
} catch (e) {
|
||||
void extLogger.log(
|
||||
"Could not autodetect query language. Select language manually.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// will be undefined if user cancels the quick pick.
|
||||
return await askForLanguage(cliServer, false);
|
||||
}
|
||||
|
||||
export async function askForLanguage(
|
||||
cliServer: CodeQLCliServer,
|
||||
throwOnEmpty = true,
|
||||
): Promise<QueryLanguage | undefined> {
|
||||
const language = await window.showQuickPick(
|
||||
await cliServer.getSupportedLanguages(),
|
||||
{
|
||||
placeHolder: "Select target language for your query",
|
||||
ignoreFocusOut: true,
|
||||
},
|
||||
);
|
||||
if (!language) {
|
||||
// This only happens if the user cancels the quick pick.
|
||||
if (throwOnEmpty) {
|
||||
throw new UserCancellationException("Cancelled.");
|
||||
} else {
|
||||
void showAndLogErrorMessage(
|
||||
"Language not found. Language must be specified manually.",
|
||||
);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!isQueryLanguage(language)) {
|
||||
void showAndLogErrorMessage(
|
||||
`Language '${language}' is not supported. Only languages ${Object.values(
|
||||
QueryLanguage,
|
||||
).join(", ")} are supported.`,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
@@ -1,13 +1,7 @@
|
||||
import { ensureDir, ensureDirSync, writeFile } from "fs-extra";
|
||||
import { join } from "path";
|
||||
import { dirSync } from "tmp-promise";
|
||||
import { Uri, window as Window } from "vscode";
|
||||
import { CodeQLCliServer } from "./codeql-cli/cli";
|
||||
import { UserCancellationException } from "./common/vscode/progress";
|
||||
import { extLogger } from "./common";
|
||||
import { isQueryLanguage, QueryLanguage } from "./common/query-language";
|
||||
import { getOnDiskWorkspaceFolders } from "./common/vscode/workspace-folders";
|
||||
import { showAndLogErrorMessage } from "./common/vscode/log";
|
||||
|
||||
// Shared temporary folder for the extension.
|
||||
export const tmpDir = dirSync({
|
||||
@@ -30,77 +24,6 @@ export const tmpDirDisposal = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds the language that a query targets.
|
||||
* If it can't be autodetected, prompt the user to specify the language manually.
|
||||
*/
|
||||
export async function findLanguage(
|
||||
cliServer: CodeQLCliServer,
|
||||
queryUri: Uri | undefined,
|
||||
): Promise<QueryLanguage | undefined> {
|
||||
const uri = queryUri || Window.activeTextEditor?.document.uri;
|
||||
if (uri !== undefined) {
|
||||
try {
|
||||
const queryInfo = await cliServer.resolveQueryByLanguage(
|
||||
getOnDiskWorkspaceFolders(),
|
||||
uri,
|
||||
);
|
||||
const language = Object.keys(queryInfo.byLanguage)[0];
|
||||
void extLogger.log(`Detected query language: ${language}`);
|
||||
|
||||
if (isQueryLanguage(language)) {
|
||||
return language;
|
||||
}
|
||||
|
||||
void extLogger.log(
|
||||
"Query language is unsupported. Select language manually.",
|
||||
);
|
||||
} catch (e) {
|
||||
void extLogger.log(
|
||||
"Could not autodetect query language. Select language manually.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// will be undefined if user cancels the quick pick.
|
||||
return await askForLanguage(cliServer, false);
|
||||
}
|
||||
|
||||
export async function askForLanguage(
|
||||
cliServer: CodeQLCliServer,
|
||||
throwOnEmpty = true,
|
||||
): Promise<QueryLanguage | undefined> {
|
||||
const language = await Window.showQuickPick(
|
||||
await cliServer.getSupportedLanguages(),
|
||||
{
|
||||
placeHolder: "Select target language for your query",
|
||||
ignoreFocusOut: true,
|
||||
},
|
||||
);
|
||||
if (!language) {
|
||||
// This only happens if the user cancels the quick pick.
|
||||
if (throwOnEmpty) {
|
||||
throw new UserCancellationException("Cancelled.");
|
||||
} else {
|
||||
void showAndLogErrorMessage(
|
||||
"Language not found. Language must be specified manually.",
|
||||
);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!isQueryLanguage(language)) {
|
||||
void showAndLogErrorMessage(
|
||||
`Language '${language}' is not supported. Only languages ${Object.values(
|
||||
QueryLanguage,
|
||||
).join(", ")} are supported.`,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a file in the query directory that indicates when this query was created.
|
||||
* This is important for keeping track of when queries should be removed.
|
||||
|
||||
@@ -16,7 +16,7 @@ import { extLogger, TeeLogger } from "../common";
|
||||
import { isCanary, MAX_QUERIES } from "../config";
|
||||
import { gatherQlFiles } from "../pure/files";
|
||||
import { basename } from "path";
|
||||
import { createTimestampFile, findLanguage } from "../helpers";
|
||||
import { createTimestampFile } from "../helpers";
|
||||
import { showBinaryChoiceDialog } from "../common/vscode/dialog";
|
||||
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
|
||||
import { displayQuickQuery } from "./quick-query";
|
||||
@@ -47,6 +47,7 @@ import {
|
||||
showAndLogErrorMessage,
|
||||
showAndLogWarningMessage,
|
||||
} from "../common/vscode/log";
|
||||
import { findLanguage } from "../codeql-cli/query-language";
|
||||
|
||||
interface DatabaseQuickPickItem extends QuickPickItem {
|
||||
databaseItem: DatabaseItem;
|
||||
|
||||
@@ -4,7 +4,6 @@ import { CodeQLCliServer } from "./codeql-cli/cli";
|
||||
import { OutputChannelLogger } from "./common";
|
||||
import { Credentials } from "./common/authentication";
|
||||
import { QueryLanguage } from "./common/query-language";
|
||||
import { askForLanguage } from "./helpers";
|
||||
import {
|
||||
getFirstWorkspaceFolder,
|
||||
isFolderAlreadyInWorkspace,
|
||||
@@ -26,6 +25,7 @@ import {
|
||||
setQlPackLocation,
|
||||
} from "./config";
|
||||
import { existsSync } from "fs-extra";
|
||||
import { askForLanguage } from "./codeql-cli/query-language";
|
||||
|
||||
type QueryLanguagesToDatabaseMap = Record<string, string>;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { relative, join, sep, dirname, parse, basename } from "path";
|
||||
import { dump, load } from "js-yaml";
|
||||
import { copy, writeFile, readFile, mkdirp } from "fs-extra";
|
||||
import { dir, tmpName } from "tmp-promise";
|
||||
import { askForLanguage, findLanguage, tmpDir } from "../helpers";
|
||||
import { tmpDir } from "../helpers";
|
||||
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
|
||||
import { Credentials } from "../common/authentication";
|
||||
import * as cli from "../codeql-cli/cli";
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
} from "../pure/ql";
|
||||
import { QueryLanguage } from "../common/query-language";
|
||||
import { tryGetQueryMetadata } from "../codeql-cli/query-metadata";
|
||||
import { askForLanguage, findLanguage } from "../codeql-cli/query-language";
|
||||
|
||||
export interface QlPack {
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user