Skip to content

Commit 5a5f990

Browse files
committed
First attempt at language detection
using "resolve queries"
1 parent d2d1a09 commit 5a5f990

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

extensions/ql-vscode/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@
245245
"command": "codeQL.runQuery",
246246
"title": "CodeQL: Run Query"
247247
},
248+
{
249+
"command": "codeQL.findLanguage",
250+
"title": "CodeQL: Find Language"
251+
},
248252
{
249253
"command": "codeQL.runQueryOnMultipleDatabases",
250254
"title": "CodeQL: Run Query on Multiple Databases"
@@ -684,6 +688,10 @@
684688
"command": "codeQL.runQuery",
685689
"when": "resourceLangId == ql && resourceExtname == .ql"
686690
},
691+
{
692+
"command": "codeQL.findLanguage",
693+
"when": "resourceLangId == ql && resourceExtname == .ql"
694+
},
687695
{
688696
"command": "codeQL.runQueryOnMultipleDatabases",
689697
"when": "resourceLangId == ql && resourceExtname == .ql"

extensions/ql-vscode/src/cli.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Readable } from 'stream';
88
import { StringDecoder } from 'string_decoder';
99
import * as tk from 'tree-kill';
1010
import { promisify } from 'util';
11-
import { CancellationToken, Disposable } from 'vscode';
11+
import { CancellationToken, Disposable, Uri, window } from 'vscode';
1212

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

485+
/**
486+
* Finds the language that a query is analyzing.
487+
* @param queryUri The URI of the query
488+
*/
489+
490+
// Note: this currently fails for some queries, e.g. the "example" snippets..."
491+
async findLanguage(queryUri?: Uri): Promise<string> {
492+
let language = '';
493+
if (queryUri) {
494+
try {
495+
const JSONResults = await this.runCodeQlCliCommand(['resolve', 'queries'], ['--format', 'bylanguage', queryUri.fsPath], 'Finding query language');
496+
497+
language = Object.keys(JSON.parse(JSONResults).byLanguage)[0];
498+
}
499+
catch (e) {
500+
void this.logger.log('Failed to detect language. Using manual prompt instead.');
501+
language = await window.showQuickPick(
502+
['cpp', 'csharp', 'go', 'java', 'javascript', 'python'], { placeHolder: 'Select language' }
503+
) || '';
504+
}
505+
}
506+
return language;
507+
}
508+
485509
/**
486510
* Finds all available QL tests in a given directory.
487511
* @param testPath Root of directory tree to search for tests.

extensions/ql-vscode/src/extension.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,14 @@ async function activateWithInstalledDistribution(
559559
}
560560
)
561561
);
562+
ctx.subscriptions.push(
563+
commandRunner('codeQL.findLanguage', async (
564+
uri: Uri | undefined
565+
) => {
566+
const language = await cliServer.findLanguage(uri || window.activeTextEditor?.document.uri);
567+
void helpers.showAndLogInformationMessage(language);
568+
})
569+
);
562570
interface DatabaseQuickPickItem extends QuickPickItem {
563571
databaseItem: DatabaseItem;
564572
}

0 commit comments

Comments
 (0)