Merge pull request #2461 from aibaars/selected-line-column

View CFG: export selected line and column position
This commit is contained in:
Andrew Eisenberg
2023-06-01 10:15:54 -07:00
committed by GitHub
3 changed files with 30 additions and 15 deletions

View File

@@ -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,

View File

@@ -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<string, string> {
return {
[TEMPLATE_NAME]: path,
[SELECTED_SOURCE_FILE]: path,
};
}

View File

@@ -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<string, string> = {
[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<string, string>] | 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<string, string>]> {
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<string, string> = {
[TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive,
[SELECTED_SOURCE_FILE]: zippedArchive.pathWithinSourceArchive,
[SELECTED_SOURCE_LINE]: line.toString(),
[SELECTED_SOURCE_COLUMN]: character.toString(),
};
return [queryUri, templates];