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.
This commit is contained in:
Andrew Eisenberg
2022-02-02 13:18:01 -08:00
parent 66c9879ce3
commit da0090aa99
2 changed files with 15 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ import {
TextDocument,
Uri
} from 'vscode';
import * as path from 'path';
import { decodeSourceArchiveUri, encodeArchiveBasePath, zipArchiveScheme } from '../archive-filesystem-provider';
import { CodeQLCliServer } from '../cli';
@@ -142,19 +143,19 @@ export class TemplatePrintAstProvider {
async provideAst(
progress: ProgressCallback,
token: CancellationToken,
document?: TextDocument
fileUri?: Uri
): Promise<AstBuilder | undefined> {
if (!document) {
if (!fileUri) {
throw new Error('Cannot view the AST. Please select a valid source file inside a CodeQL database.');
}
const { query, dbUri } = this.shouldCache()
? await this.cache.get(document.uri.toString(), progress, token)
: await this.getAst(document.uri.toString(), progress, token);
? await this.cache.get(fileUri.toString(), progress, token)
: await this.getAst(fileUri.toString(), progress, token);
return new AstBuilder(
query, this.cli,
this.dbm.findDatabaseItem(dbUri)!,
document.fileName
path.basename(fileUri.fsPath),
);
}

View File

@@ -974,9 +974,11 @@ async function activateWithInstalledDistribution(
}
));
commands.registerCommand('codeQL.showLogs', () => {
logger.show();
});
ctx.subscriptions.push(
commandRunner('codeQL.showLogs', async () => {
logger.show();
})
);
void logger.log('Starting language server.');
ctx.subscriptions.push(client.start());
@@ -999,12 +1001,14 @@ async function activateWithInstalledDistribution(
ctx.subscriptions.push(astViewer);
ctx.subscriptions.push(commandRunnerWithProgress('codeQL.viewAst', async (
progress: ProgressCallback,
token: CancellationToken
token: CancellationToken,
selectedFile: Uri,
) => {
const ast = await templateProvider.provideAst(
progress,
token,
window.activeTextEditor?.document,
selectedFile ?? window.activeTextEditor?.document.uri,
);
if (ast) {
astViewer.updateRoots(await ast.getRoots(), ast.db, ast.fileName);