Skip to content

Commit 8a4b6fd

Browse files
committed
Add "Preview Query Help" command
1 parent 31dc11e commit 8a4b6fd

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

extensions/ql-vscode/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"onCommand:codeQL.setCurrentDatabase",
4747
"onCommand:codeQL.viewAst",
4848
"onCommand:codeQL.openReferencedFile",
49+
"onCommand:codeQL.previewQueryHelp",
4950
"onCommand:codeQL.chooseDatabaseFolder",
5051
"onCommand:codeQL.chooseDatabaseArchive",
5152
"onCommand:codeQL.chooseDatabaseInternet",
@@ -295,6 +296,10 @@
295296
"command": "codeQL.openReferencedFile",
296297
"title": "CodeQL: Open Referenced File"
297298
},
299+
{
300+
"command": "codeQL.previewQueryHelp",
301+
"title": "CodeQL: Preview Query Help File"
302+
},
298303
{
299304
"command": "codeQL.quickQuery",
300305
"title": "CodeQL: Quick Query"
@@ -681,6 +686,11 @@
681686
"group": "9_qlCommands",
682687
"when": "view == codeQLQueryHistory"
683688
},
689+
{
690+
"command": "codeQL.previewQueryHelp",
691+
"group": "9_qlCommands",
692+
"when": "resourceScheme == .qhelp"
693+
},
684694
{
685695
"command": "codeQLTests.showOutputDifferences",
686696
"group": "qltest@1",
@@ -712,6 +722,11 @@
712722
"command": "codeQL.openReferencedFile",
713723
"group": "9_qlCommands",
714724
"when": "resourceExtname == .qlref"
725+
},
726+
{
727+
"command": "codeQL.previewQueryHelp",
728+
"group": "9_qlCommands",
729+
"when": "resourceExtname == .qhelp"
715730
}
716731
],
717732
"commandPalette": [
@@ -743,6 +758,10 @@
743758
"command": "codeQL.openReferencedFile",
744759
"when": "resourceExtname == .qlref"
745760
},
761+
{
762+
"command": "codeQL.previewQueryHelp",
763+
"when": "resourceExtname == .qhelp"
764+
},
746765
{
747766
"command": "codeQL.setCurrentDatabase",
748767
"when": "false"
@@ -900,6 +919,10 @@
900919
{
901920
"command": "codeQL.openReferencedFile",
902921
"when": "resourceExtname == .qlref"
922+
},
923+
{
924+
"command": "codeQL.previewQueryHelp",
925+
"when": "resourceExtname == .qhelp"
903926
}
904927
]
905928
},

extensions/ql-vscode/src/cli.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,21 @@ export class CodeQLCliServer implements Disposable {
613613

614614
return await this.runCodeQlCliCommand(['database', 'unbundle'], subcommandArgs, `Extracting ${archivePath} to directory ${target}`);
615615
}
616+
617+
/**
618+
* Uses a .qhelp file to generate Query Help documentation in a specified format.
619+
* @param pathToQhelp The path to the .qhelp file
620+
* @param format The format in which the query help should be generated {@link https://codeql.github.com/docs/codeql-cli/manual/generate-query-help/#cmdoption-codeql-generate-query-help-format}
621+
* @param outputDirectory The output directory for the generated file
622+
*/
623+
async generateQueryHelp(pathToQhelp:string, format: string, outputDirectory?: string): Promise<string> {
624+
const subcommandArgs = [];
625+
subcommandArgs.push('--format', format);
626+
if(outputDirectory) subcommandArgs.push('--output', outputDirectory);
627+
subcommandArgs.push(pathToQhelp);
628+
629+
return await this.runCodeQlCliCommand(['generate', 'query-help'], subcommandArgs, `Generating qhelp in ${format} format at ${outputDirectory}`);
630+
}
616631

617632
/**
618633
* Gets the results from a bqrs.

extensions/ql-vscode/src/extension.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { LanguageClient } from 'vscode-languageclient';
1717
import * as os from 'os';
1818
import * as path from 'path';
19+
import * as tmp from 'tmp';
1920
import { testExplorerExtensionId, TestHub } from 'vscode-test-adapter-api';
2021

2122
import { AstViewer } from './astViewer';
@@ -493,6 +494,29 @@ async function activateWithInstalledDistribution(
493494
}
494495
}
495496

497+
async function previewQueryHelp(
498+
selectedQuery: Uri
499+
): Promise<void> {
500+
const path = selectedQuery ? selectedQuery.path : window.activeTextEditor!.document.uri.fsPath;
501+
if(path) {
502+
// Create temporary directory
503+
const tmpDir = tmp.dirSync();
504+
// Get library name and query file name in the format 'libname/queryname.md
505+
const mdFilePath = path.split('/').slice(-2).join('/').replace('.qhelp', '.md');
506+
const absolutePathToMd = `${tmpDir.name}${mdFilePath}`;
507+
const uri = Uri.file(absolutePathToMd);
508+
try {
509+
await cliServer.generateQueryHelp(path , 'markdown', absolutePathToMd);
510+
await commands.executeCommand('markdown.showPreviewToSide', uri);
511+
} catch (err) {
512+
const errorMessage = err.message.includes('Generating qhelp in markdown') ? (
513+
`Could not generate markdown from ${path}: Bad formatting in .qhelp file.`
514+
) : `Could not open a preview of the generated file (${absolutePathToMd}).`;
515+
void helpers.showAndLogErrorMessage(errorMessage);
516+
}
517+
}
518+
}
519+
496520
async function openReferencedFile(
497521
selectedQuery: Uri
498522
): Promise<void> {
@@ -732,6 +756,13 @@ async function activateWithInstalledDistribution(
732756
)
733757
);
734758

759+
ctx.subscriptions.push(
760+
commandRunner(
761+
'codeQL.previewQueryHelp',
762+
previewQueryHelp
763+
)
764+
);
765+
735766
ctx.subscriptions.push(
736767
commandRunnerWithProgress('codeQL.restartQueryServer', async (
737768
progress: ProgressCallback,

0 commit comments

Comments
 (0)