@@ -8,7 +8,7 @@ import { Readable } from 'stream';
88import { StringDecoder } from 'string_decoder' ;
99import * as tk from 'tree-kill' ;
1010import { promisify } from 'util' ;
11- import { CancellationToken , Disposable } from 'vscode' ;
11+ import { CancellationToken , Disposable , Uri , window } from 'vscode' ;
1212
1313import { BQRSInfo , DecodedBqrsChunk } from './pure/bqrs-cli-types' ;
1414import { CliConfig } from './config' ;
@@ -482,6 +482,36 @@ 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+ async findLanguage ( workspaces : string [ ] , queryUri ?: Uri ) : Promise < string > {
490+ let language = '' ;
491+ if ( queryUri ) {
492+ const subcommandArgs = [
493+ '--format' , 'bylanguage' ,
494+ queryUri . fsPath ,
495+ '--additional-packs' ,
496+ workspaces . join ( path . delimiter )
497+ ] ;
498+ try {
499+ const JSONResults = await this . runCodeQlCliCommand ( [ 'resolve' , 'queries' ] , subcommandArgs , 'Finding query language' ) ;
500+
501+ // This corresponds to `codeql resolve queries <query> --format bylanguage | jq -r '.byLanguage | keys[]'`
502+ language = Object . keys ( JSON . parse ( JSONResults ) . byLanguage ) [ 0 ] ;
503+ }
504+ catch ( e ) {
505+ // Add an option to manually specify the language, in case automatic language detection fails.
506+ void this . logger . log ( 'Failed to detect language. Using manual prompt instead.' ) ;
507+ language = await window . showQuickPick (
508+ [ 'cpp' , 'csharp' , 'go' , 'java' , 'javascript' , 'python' ] , { placeHolder : 'Select language' }
509+ ) || '' ;
510+ }
511+ }
512+ return language ;
513+ }
514+
485515 /**
486516 * Finds all available QL tests in a given directory.
487517 * @param testPath Root of directory tree to search for tests.
0 commit comments