Skip to content

Commit

Permalink
Attempt to autodetect language using "resolve queries"
Browse files Browse the repository at this point in the history
  • Loading branch information
shati-patel committed Jul 26, 2021
1 parent d2d1a09 commit dac4201
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@
"command": "codeQL.runQuery",
"title": "CodeQL: Run Query"
},
{
"command": "codeQL.findLanguage",
"title": "CodeQL: Find Language"
},
{
"command": "codeQL.runQueryOnMultipleDatabases",
"title": "CodeQL: Run Query on Multiple Databases"
Expand Down Expand Up @@ -684,6 +688,10 @@
"command": "codeQL.runQuery",
"when": "resourceLangId == ql && resourceExtname == .ql"
},
{
"command": "codeQL.findLanguage",
"when": "resourceLangId == ql && resourceExtname == .ql"
},
{
"command": "codeQL.runQueryOnMultipleDatabases",
"when": "resourceLangId == ql && resourceExtname == .ql"
Expand Down
32 changes: 31 additions & 1 deletion extensions/ql-vscode/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Readable } from 'stream';
import { StringDecoder } from 'string_decoder';
import * as tk from 'tree-kill';
import { promisify } from 'util';
import { CancellationToken, Disposable } from 'vscode';
import { CancellationToken, Disposable, Uri, window } from 'vscode';

import { BQRSInfo, DecodedBqrsChunk } from './pure/bqrs-cli-types';
import { CliConfig } from './config';
Expand Down Expand Up @@ -482,6 +482,36 @@ export class CodeQLCliServer implements Disposable {
return await this.runJsonCodeQlCliCommand<QuerySetup>(['resolve', 'library-path'], subcommandArgs, 'Resolving library paths');
}

/**
* Finds the language that a query is analyzing.
* @param queryUri The URI of the query
*/
async findLanguage(workspaces: string[], queryUri?: Uri): Promise<string> {
let language = '';
if (queryUri) {
const subcommandArgs = [
'--format', 'bylanguage',
queryUri.fsPath,
'--additional-packs',
workspaces.join(path.delimiter)
];
try {
const JSONResults = await this.runCodeQlCliCommand(['resolve', 'queries'], subcommandArgs, 'Finding query language');

// This corresponds to `codeql resolve queries <query> --format bylanguage | jq -r '.byLanguage | keys[]'`
language = Object.keys(JSON.parse(JSONResults).byLanguage)[0];
}
catch (e) {
// Add an option to manually specify the language, in case automatic language detection fails.
void this.logger.log('Failed to detect language. Using manual prompt instead.');
language = await window.showQuickPick(
['cpp', 'csharp', 'go', 'java', 'javascript', 'python'], { placeHolder: 'Select language' }
) || '';
}
}
return language;
}

/**
* Finds all available QL tests in a given directory.
* @param testPath Root of directory tree to search for tests.
Expand Down
11 changes: 11 additions & 0 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,17 @@ async function activateWithInstalledDistribution(
}
)
);
// TODO: Remove this and the package.json changes
// (Temporary new command to test `cliServer.findLanguage`)
ctx.subscriptions.push(
commandRunner('codeQL.findLanguage', async (
uri: Uri | undefined
) => {
const diskWorkspaceFolders = helpers.getOnDiskWorkspaceFolders();
const language = await cliServer.findLanguage(diskWorkspaceFolders, uri || window.activeTextEditor?.document.uri);
void helpers.showAndLogInformationMessage(language);
})
);
interface DatabaseQuickPickItem extends QuickPickItem {
databaseItem: DatabaseItem;
}
Expand Down

0 comments on commit dac4201

Please sign in to comment.