Skip to content

Commit f5c9602

Browse files
authored
Merge pull request #2461 from aibaars/selected-line-column
View CFG: export selected line and column position
2 parents cf34378 + 51be504 commit f5c9602

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

extensions/ql-vscode/src/language-support/ast-viewer/ast-cfg-commands.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ export function getAstCfgCommands({
4242
const viewCfg = async () =>
4343
withProgress(
4444
async (progress, token) => {
45-
const res = await cfgTemplateProvider.provideCfgUri(
46-
window.activeTextEditor?.document,
47-
);
45+
const editor = window.activeTextEditor;
46+
const res = !editor
47+
? undefined
48+
: await cfgTemplateProvider.provideCfgUri(
49+
editor.document,
50+
editor.selection.active.line + 1,
51+
editor.selection.active.character + 1,
52+
);
4853
if (res) {
4954
await localQueries.compileAndRunQuery(
5055
QuickEvalType.None,

extensions/ql-vscode/src/language-support/contextual/location-finder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import { QueryResultType } from "../../pure/new-messages";
2424
import { fileRangeFromURI } from "./file-range-from-uri";
2525

2626
export const SELECT_QUERY_NAME = "#select";
27-
export const TEMPLATE_NAME = "selectedSourceFile";
27+
export const SELECTED_SOURCE_FILE = "selectedSourceFile";
28+
export const SELECTED_SOURCE_LINE = "selectedSourceLine";
29+
export const SELECTED_SOURCE_COLUMN = "selectedSourceColumn";
2830

2931
export interface FullLocationLink extends LocationLink {
3032
originUri: Uri;
@@ -124,7 +126,7 @@ async function getLinksFromResults(
124126

125127
function createTemplates(path: string): Record<string, string> {
126128
return {
127-
[TEMPLATE_NAME]: path,
129+
[SELECTED_SOURCE_FILE]: path,
128130
};
129131
}
130132

extensions/ql-vscode/src/language-support/contextual/template-provider.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import { KeyType } from "./key-type";
2323
import {
2424
FullLocationLink,
2525
getLocationsForUriString,
26-
TEMPLATE_NAME,
26+
SELECTED_SOURCE_FILE,
27+
SELECTED_SOURCE_LINE,
28+
SELECTED_SOURCE_COLUMN,
2729
} from "./location-finder";
2830
import {
2931
qlpackOfDatabase,
@@ -253,7 +255,7 @@ export class TemplatePrintAstProvider {
253255

254256
const query = queries[0];
255257
const templates: Record<string, string> = {
256-
[TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive,
258+
[SELECTED_SOURCE_FILE]: zippedArchive.pathWithinSourceArchive,
257259
};
258260

259261
const results = await runContextualQuery(
@@ -284,15 +286,17 @@ export class TemplatePrintCfgProvider {
284286
}
285287

286288
async provideCfgUri(
287-
document?: TextDocument,
289+
document: TextDocument,
290+
line: number,
291+
character: number,
288292
): Promise<[Uri, Record<string, string>] | undefined> {
289-
if (!document) {
290-
return;
291-
}
292-
293293
return this.shouldUseCache()
294-
? await this.cache.get(document.uri.toString())
295-
: await this.getCfgUri(document.uri.toString());
294+
? await this.cache.get(
295+
`${document.uri.toString()}#${line}:${character}`,
296+
line,
297+
character,
298+
)
299+
: await this.getCfgUri(document.uri.toString(), line, character);
296300
}
297301

298302
private shouldUseCache() {
@@ -301,6 +305,8 @@ export class TemplatePrintCfgProvider {
301305

302306
private async getCfgUri(
303307
uriString: string,
308+
line: number,
309+
character: number,
304310
): Promise<[Uri, Record<string, string>]> {
305311
const uri = Uri.parse(uriString, true);
306312
if (uri.scheme !== zipArchiveScheme) {
@@ -342,7 +348,9 @@ export class TemplatePrintCfgProvider {
342348
const queryUri = Uri.file(queries[0]);
343349

344350
const templates: Record<string, string> = {
345-
[TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive,
351+
[SELECTED_SOURCE_FILE]: zippedArchive.pathWithinSourceArchive,
352+
[SELECTED_SOURCE_LINE]: line.toString(),
353+
[SELECTED_SOURCE_COLUMN]: character.toString(),
346354
};
347355

348356
return [queryUri, templates];

0 commit comments

Comments
 (0)