Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import { RepositoriesFilterSortStateWithIds } from "./pure/variant-analysis-filt
import { DbModule } from "./databases/db-module";
import { redactableError } from "./pure/errors";
import { QueryHistoryDirs } from "./query-history/query-history-dirs";
import { DirResult } from "tmp";

/**
* extension.ts
Expand Down Expand Up @@ -744,32 +745,6 @@ async function activateWithInstalledDistribution(
});
ctx.subscriptions.push({ dispose: qhelpTmpDir.removeCallback });

async function previewQueryHelp(selectedQuery: Uri): Promise<void> {
// selectedQuery is unpopulated when executing through the command palette
const pathToQhelp = selectedQuery
? selectedQuery.fsPath
: window.activeTextEditor?.document.uri.fsPath;
if (pathToQhelp) {
// Create temporary directory
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
const absolutePathToMd = join(qhelpTmpDir.name, relativePathToMd);
const uri = Uri.file(absolutePathToMd);
try {
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
await commands.executeCommand("markdown.showPreviewToSide", uri);
} catch (e) {
const errorMessage = getErrorMessage(e).includes(
"Generating qhelp in markdown",
)
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
void showAndLogExceptionWithTelemetry(errorMessage, {
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
});
}
}
}

async function openReferencedFile(selectedQuery: Uri): Promise<void> {
// If no file is selected, the path of the file in the editor is selected
const path =
Expand Down Expand Up @@ -1316,7 +1291,9 @@ async function activateWithInstalledDistribution(
);

ctx.subscriptions.push(
commandRunner("codeQL.previewQueryHelp", previewQueryHelp),
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
await previewQueryHelp(cliServer, selectedQuery, qhelpTmpDir);
}),
);

ctx.subscriptions.push(
Expand Down Expand Up @@ -1875,6 +1852,36 @@ async function compileAndRunQueryOnMultipleDatabases(
}
}

async function previewQueryHelp(
cliServer: CodeQLCliServer,
selectedQuery: Uri,
qhelpTmpDir: DirResult,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better if the selectedQuery was the last argument since it's the argument to the command. We now have (from extension, from user, from extension), while I would expect everything from the extension to be grouped together.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've swapped these two around.

): Promise<void> {
// selectedQuery is unpopulated when executing through the command palette
const pathToQhelp = selectedQuery
? selectedQuery.fsPath
: window.activeTextEditor?.document.uri.fsPath;
if (pathToQhelp) {
// Create temporary directory
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
const absolutePathToMd = join(qhelpTmpDir.name, relativePathToMd);
const uri = Uri.file(absolutePathToMd);
try {
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
await commands.executeCommand("markdown.showPreviewToSide", uri);
} catch (e) {
const errorMessage = getErrorMessage(e).includes(
"Generating qhelp in markdown",
)
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
void showAndLogExceptionWithTelemetry(errorMessage, {
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
});
}
}
}

function addUnhandledRejectionListener() {
const handler = (error: unknown) => {
// This listener will be triggered for errors from other extensions as
Expand Down