From 76189513839123900aef2cac701e70563b47c0c3 Mon Sep 17 00:00:00 2001 From: shati-patel <42641846+shati-patel@users.noreply.github.com> Date: Thu, 26 Aug 2021 13:03:49 +0100 Subject: [PATCH 1/3] Move `findLanguage` function into helpers.ts --- extensions/ql-vscode/src/extension.ts | 4 +-- extensions/ql-vscode/src/helpers.ts | 31 ++++++++++++++++++ extensions/ql-vscode/src/run-remote-query.ts | 33 +------------------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index b760d923ad0..5ed95c42673 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -73,7 +73,7 @@ import { import { CodeQlStatusBarHandler } from './status-bar'; import { Credentials } from './authentication'; -import { runRemoteQuery, findLanguage } from './run-remote-query'; +import { runRemoteQuery } from './run-remote-query'; /** * extension.ts @@ -576,7 +576,7 @@ async function activateWithInstalledDistribution( return; } // If possible, only show databases with the right language (otherwise show all databases). - const queryLanguage = await findLanguage(cliServer, uri); + const queryLanguage = await helpers.findLanguage(cliServer, uri); if (queryLanguage) { filteredDBs = dbm.databaseItems.filter(db => db.language === queryLanguage); if (filteredDBs.length === 0) { diff --git a/extensions/ql-vscode/src/helpers.ts b/extensions/ql-vscode/src/helpers.ts index 75d610f1a18..38db60f7cb5 100644 --- a/extensions/ql-vscode/src/helpers.ts +++ b/extensions/ql-vscode/src/helpers.ts @@ -424,3 +424,34 @@ export async function isLikelyDatabaseRoot(maybeRoot: string) { export function isLikelyDbLanguageFolder(dbPath: string) { return !!path.basename(dbPath).startsWith('db-'); } + +/** + * 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 { + 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 logger.log(`Detected query language: ${language}`); + return language; + } catch (e) { + void logger.log('Could not autodetect query language. Select language manually.'); + } + } + const availableLanguages = Object.keys(await cliServer.resolveLanguages()); + const language = await Window.showQuickPick( + availableLanguages, + { placeHolder: 'Select target language for your query', ignoreFocusOut: true } + ); + if (!language) { + // This only happens if the user cancels the quick pick. + void showAndLogErrorMessage('Language not found. Language must be specified manually.'); + } + return language; +} diff --git a/extensions/ql-vscode/src/run-remote-query.ts b/extensions/ql-vscode/src/run-remote-query.ts index e1d0fa92e23..d7472ce4f60 100644 --- a/extensions/ql-vscode/src/run-remote-query.ts +++ b/extensions/ql-vscode/src/run-remote-query.ts @@ -1,7 +1,7 @@ import { QuickPickItem, Uri, window } from 'vscode'; import * as yaml from 'js-yaml'; import * as fs from 'fs-extra'; -import { getOnDiskWorkspaceFolders, showAndLogErrorMessage, showAndLogInformationMessage } from './helpers'; +import { findLanguage, showAndLogErrorMessage, showAndLogInformationMessage } from './helpers'; import { Credentials } from './authentication'; import * as cli from './cli'; import { logger } from './logging'; @@ -16,37 +16,6 @@ interface Config { const OWNER = 'dsp-testing'; const REPO = 'qc-controller'; -/** - * 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: cli.CodeQLCliServer, - queryUri: Uri | undefined -): Promise { - 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 logger.log(`Detected query language: ${language}`); - return language; - } catch (e) { - void logger.log('Could not autodetect query language. Select language manually.'); - } - } - const availableLanguages = Object.keys(await cliServer.resolveLanguages()); - const language = await window.showQuickPick( - availableLanguages, - { placeHolder: 'Select target language for your query', ignoreFocusOut: true } - ); - if (!language) { - // This only happens if the user cancels the quick pick. - void showAndLogErrorMessage('Language not found. Language must be specified manually.'); - } - return language; -} - interface RepoListQuickPickItem extends QuickPickItem { repoList: string[]; } From 80095f48918d138246f9ca8aa66f8a271667533d Mon Sep 17 00:00:00 2001 From: shati-patel <42641846+shati-patel@users.noreply.github.com> Date: Tue, 31 Aug 2021 11:10:12 +0100 Subject: [PATCH 2/3] Add "resolveLanguages" test --- .../src/vscode-tests/cli-integration/run-cli.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts b/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts index 1a272017fc5..65b14acb708 100644 --- a/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts +++ b/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts @@ -52,4 +52,15 @@ describe('Use cli', function() { expect(qlpacks['codeql-javascript']).not.to.be.undefined; expect(qlpacks['codeql-python']).not.to.be.undefined; }); + + it('should resolve languages', async function() { + skipIfNoCodeQL(this); + const languages = await cli.resolveLanguages(); + // should have a bunch of languages. just check that a few known ones exist + expect(languages['cpp']).not.to.be.undefined; + expect(languages['csharp']).not.to.be.undefined; + expect(languages['java']).not.to.be.undefined; + expect(languages['javascript']).not.to.be.undefined; + expect(languages['python']).not.to.be.undefined; + }); }); From 46abd0232207dc3b2928ab0b4b08bcc1bfdeceac Mon Sep 17 00:00:00 2001 From: shati-patel <42641846+shati-patel@users.noreply.github.com> Date: Wed, 1 Sep 2021 10:29:00 +0100 Subject: [PATCH 3/3] Update syntax --- .../src/vscode-tests/cli-integration/run-cli.test.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts b/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts index 65b14acb708..9f132aff7ec 100644 --- a/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts +++ b/extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts @@ -56,11 +56,8 @@ describe('Use cli', function() { it('should resolve languages', async function() { skipIfNoCodeQL(this); const languages = await cli.resolveLanguages(); - // should have a bunch of languages. just check that a few known ones exist - expect(languages['cpp']).not.to.be.undefined; - expect(languages['csharp']).not.to.be.undefined; - expect(languages['java']).not.to.be.undefined; - expect(languages['javascript']).not.to.be.undefined; - expect(languages['python']).not.to.be.undefined; + for (const expectedLanguage of ['cpp', 'csharp', 'go', 'java', 'javascript', 'python']) { + expect(languages).to.have.property(expectedLanguage).that.is.not.undefined; + } }); });