diff --git a/extensions/ql-vscode/src/language-support/ast-viewer/ast-cfg-commands.ts b/extensions/ql-vscode/src/language-support/ast-viewer/ast-cfg-commands.ts index 3262fbc8f..76e6eed06 100644 --- a/extensions/ql-vscode/src/language-support/ast-viewer/ast-cfg-commands.ts +++ b/extensions/ql-vscode/src/language-support/ast-viewer/ast-cfg-commands.ts @@ -42,9 +42,14 @@ export function getAstCfgCommands({ const viewCfg = async () => withProgress( async (progress, token) => { - const res = await cfgTemplateProvider.provideCfgUri( - window.activeTextEditor?.document, - ); + const editor = window.activeTextEditor; + const res = !editor + ? undefined + : await cfgTemplateProvider.provideCfgUri( + editor.document, + editor.selection.active.line + 1, + editor.selection.active.character + 1, + ); if (res) { await localQueries.compileAndRunQuery( QuickEvalType.None, diff --git a/extensions/ql-vscode/src/language-support/contextual/location-finder.ts b/extensions/ql-vscode/src/language-support/contextual/location-finder.ts index ef998a10a..980327475 100644 --- a/extensions/ql-vscode/src/language-support/contextual/location-finder.ts +++ b/extensions/ql-vscode/src/language-support/contextual/location-finder.ts @@ -24,7 +24,9 @@ import { QueryResultType } from "../../pure/new-messages"; import { fileRangeFromURI } from "./file-range-from-uri"; export const SELECT_QUERY_NAME = "#select"; -export const TEMPLATE_NAME = "selectedSourceFile"; +export const SELECTED_SOURCE_FILE = "selectedSourceFile"; +export const SELECTED_SOURCE_LINE = "selectedSourceLine"; +export const SELECTED_SOURCE_COLUMN = "selectedSourceColumn"; export interface FullLocationLink extends LocationLink { originUri: Uri; @@ -124,7 +126,7 @@ async function getLinksFromResults( function createTemplates(path: string): Record { return { - [TEMPLATE_NAME]: path, + [SELECTED_SOURCE_FILE]: path, }; } diff --git a/extensions/ql-vscode/src/language-support/contextual/template-provider.ts b/extensions/ql-vscode/src/language-support/contextual/template-provider.ts index 26ba9d72f..177510855 100644 --- a/extensions/ql-vscode/src/language-support/contextual/template-provider.ts +++ b/extensions/ql-vscode/src/language-support/contextual/template-provider.ts @@ -23,7 +23,9 @@ import { KeyType } from "./key-type"; import { FullLocationLink, getLocationsForUriString, - TEMPLATE_NAME, + SELECTED_SOURCE_FILE, + SELECTED_SOURCE_LINE, + SELECTED_SOURCE_COLUMN, } from "./location-finder"; import { qlpackOfDatabase, @@ -253,7 +255,7 @@ export class TemplatePrintAstProvider { const query = queries[0]; const templates: Record = { - [TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive, + [SELECTED_SOURCE_FILE]: zippedArchive.pathWithinSourceArchive, }; const results = await runContextualQuery( @@ -284,15 +286,17 @@ export class TemplatePrintCfgProvider { } async provideCfgUri( - document?: TextDocument, + document: TextDocument, + line: number, + character: number, ): Promise<[Uri, Record] | undefined> { - if (!document) { - return; - } - return this.shouldUseCache() - ? await this.cache.get(document.uri.toString()) - : await this.getCfgUri(document.uri.toString()); + ? await this.cache.get( + `${document.uri.toString()}#${line}:${character}`, + line, + character, + ) + : await this.getCfgUri(document.uri.toString(), line, character); } private shouldUseCache() { @@ -301,6 +305,8 @@ export class TemplatePrintCfgProvider { private async getCfgUri( uriString: string, + line: number, + character: number, ): Promise<[Uri, Record]> { const uri = Uri.parse(uriString, true); if (uri.scheme !== zipArchiveScheme) { @@ -342,7 +348,9 @@ export class TemplatePrintCfgProvider { const queryUri = Uri.file(queries[0]); const templates: Record = { - [TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive, + [SELECTED_SOURCE_FILE]: zippedArchive.pathWithinSourceArchive, + [SELECTED_SOURCE_LINE]: line.toString(), + [SELECTED_SOURCE_COLUMN]: character.toString(), }; return [queryUri, templates];