Skip to content

Commit b7f2a23

Browse files
committed
Fix ast view and command registration
Two small bugs: 1. The AST view command was viewing the wrong ast when the command was selected from the context menu. It was always selecting the active editor instead of the item selected in the file menu. 2. The `codeql.showLogs` command was not being registered properly. With this change, there is uniform error handling, telemetry, and disposal.
1 parent 5a9b49b commit b7f2a23

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

extensions/ql-vscode/src/contextual/templateProvider.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
TextDocument,
1111
Uri
1212
} from 'vscode';
13+
import * as path from 'path';
1314

1415
import { decodeSourceArchiveUri, encodeArchiveBasePath, zipArchiveScheme } from '../archive-filesystem-provider';
1516
import { CodeQLCliServer } from '../cli';
@@ -142,19 +143,19 @@ export class TemplatePrintAstProvider {
142143
async provideAst(
143144
progress: ProgressCallback,
144145
token: CancellationToken,
145-
document?: TextDocument
146+
fileUri?: Uri
146147
): Promise<AstBuilder | undefined> {
147-
if (!document) {
148+
if (!fileUri) {
148149
throw new Error('Cannot view the AST. Please select a valid source file inside a CodeQL database.');
149150
}
150151
const { query, dbUri } = this.shouldCache()
151-
? await this.cache.get(document.uri.toString(), progress, token)
152-
: await this.getAst(document.uri.toString(), progress, token);
152+
? await this.cache.get(fileUri.toString(), progress, token)
153+
: await this.getAst(fileUri.toString(), progress, token);
153154

154155
return new AstBuilder(
155156
query, this.cli,
156157
this.dbm.findDatabaseItem(dbUri)!,
157-
document.fileName
158+
path.basename(fileUri.fsPath),
158159
);
159160
}
160161

extensions/ql-vscode/src/extension.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,11 @@ async function activateWithInstalledDistribution(
968968
}
969969
));
970970

971-
commands.registerCommand('codeQL.showLogs', () => {
972-
logger.show();
973-
});
971+
ctx.subscriptions.push(
972+
commandRunner('codeQL.showLogs', async () => {
973+
logger.show();
974+
})
975+
);
974976

975977
void logger.log('Starting language server.');
976978
ctx.subscriptions.push(client.start());
@@ -993,12 +995,14 @@ async function activateWithInstalledDistribution(
993995
ctx.subscriptions.push(astViewer);
994996
ctx.subscriptions.push(commandRunnerWithProgress('codeQL.viewAst', async (
995997
progress: ProgressCallback,
996-
token: CancellationToken
998+
token: CancellationToken,
999+
selectedFile: Uri,
9971000
) => {
1001+
9981002
const ast = await templateProvider.provideAst(
9991003
progress,
10001004
token,
1001-
window.activeTextEditor?.document,
1005+
selectedFile ?? window.activeTextEditor?.document.uri,
10021006
);
10031007
if (ast) {
10041008
astViewer.updateRoots(await ast.getRoots(), ast.db, ast.fileName);

0 commit comments

Comments
 (0)