feat(query): name result tabs from SQL comments

This commit is contained in:
Eve
2026-09-14 14:28:09 +08:00
committed by GitHub
parent 193d875b8a
commit e561bd156f
2 changed files with 12 additions and 4 deletions
@@ -5,10 +5,12 @@ describe("queryResultNameFromPreamble", () => {
it("uses the nearest non-empty Name line comment", () => {
expect(queryResultNameFromPreamble("-- Name: Old name\n-- unrelated\r\n -- NAME : Latest name \r\n")).toBe("Latest name");
expect(queryResultNameFromPreamble("-- Name: kept\n-- Name: \n")).toBe("kept");
expect(queryResultNameFromPreamble("-- Name: explicit\n-- nearest ordinary comment\n")).toBe("explicit");
});
it("ignores unrelated, malformed, and block comments", () => {
expect(queryResultNameFromPreamble("-- Name without colon\n/*\n-- Name: block\n*/\n-- ordinary comment\n")).toBeUndefined();
it("falls back to the nearest non-empty line comment", () => {
expect(queryResultNameFromPreamble("-- older comment\n/*\n-- Name: block\n*/\n-- 当前时间\n")).toBe("当前时间");
expect(queryResultNameFromPreamble("/* -- block only */\n-- \n")).toBeUndefined();
});
});
@@ -9,12 +9,18 @@ export interface QueryResultSourceLabelOptions {
export function queryResultNameFromPreamble(preamble: string): string | undefined {
let name: string | undefined;
let fallback: string | undefined;
const withoutBlockComments = preamble.replace(/\/\*[\s\S]*?\*\//g, "");
for (const line of withoutBlockComments.split(/\r?\n/)) {
const candidate = line.match(/^\s*--\s*name\s*:\s*(.*)$/i)?.[1]?.trim();
const comment = line.match(/^\s*--\s*(.*)$/)?.[1]?.trim();
if (!comment) continue;
const nameMatch = comment.match(/^name\s*:\s*(.*)$/i);
const candidate = nameMatch?.[1]?.trim();
if (candidate) name = candidate;
else if (!nameMatch) fallback = comment;
}
return name;
return name ?? fallback;
}
function firstSourceOfKind(sources: SqlSemanticRowSource[], kind: SqlSemanticRowSource["kind"]): SqlSemanticRowSource | undefined {