From b85027dc47445c66f677e6ef809a4719292e53ce Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 1 Dec 2026 23:08:39 +0800 Subject: [PATCH] fix(database): colour quoted identifiers as names in DuckDB and Snowflake consoles (#7260) The DuckDB and Snowflake console dialects are built from lang-sql directly, which tags QuotedIdentifier like a string, so "schema"."table" was still painted in the string colour there after #7255 fixed the plain SQL mode. Both dialects now restyle QuotedIdentifier as a name. --- src/features/database/sql/duckdbHighlight.ts | 47 ++++++++++--------- .../database/sql/quotedIdentifiers.test.ts | 27 +++++++++++ .../database/sql/quotedIdentifiers.ts | 7 +++ .../database/sql/snowflakeHighlight.ts | 3 +- 4 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 src/features/database/sql/quotedIdentifiers.test.ts create mode 100644 src/features/database/sql/quotedIdentifiers.ts diff --git a/src/features/database/sql/duckdbHighlight.ts b/src/features/database/sql/duckdbHighlight.ts index 1529bda1a5..64a8dd2499 100644 --- a/src/features/database/sql/duckdbHighlight.ts +++ b/src/features/database/sql/duckdbHighlight.ts @@ -1,31 +1,34 @@ import { SQLDialect } from "@codemirror/lang-sql"; import type { LRLanguage } from "@codemirror/language"; +import { quotedIdentifiersAsNames } from "./quotedIdentifiers"; import { CLAUSE_WORDS, DATA_TYPES, functions, predicateKeywords, statementStarters } from "./dialect"; let language: LRLanguage | undefined; /** Token colouring for DuckDB; scope and completion come from the SQL editor. */ export function duckdbLanguage(): LRLanguage { - return (language ??= SQLDialect.define({ - keywords: [ - ...CLAUSE_WORDS, - ...statementStarters("duckdb"), - ...predicateKeywords("duckdb"), - "ASOF ANTI SEMI EXCLUDE REPLACE RENAME COLUMNS QUALIFY OVER PARTITION FILTER NULLS FIRST LAST", - "IF RECURSIVE DESCRIBE SUMMARIZE COMMENT INSTALL LOAD EXPORT IMPORT PRAGMA RESET VACUUM", - "SAMPLE REPEATABLE PERCENT ROWS MATERIALIZED TEMPORARY LAMBDA TRY_CAST AT TIME ZONE", - ] - .join(" ") - .toLowerCase(), - types: DATA_TYPES.duckdb - .filter((type) => type !== "UNION") - .join(" ") - .toLowerCase(), - builtin: functions("duckdb") - .map((f) => f.name) - .join(" "), - caseInsensitiveIdentifiers: true, - doubleDollarQuotedStrings: true, - specialVar: "?$", - }).language); + return (language ??= quotedIdentifiersAsNames( + SQLDialect.define({ + keywords: [ + ...CLAUSE_WORDS, + ...statementStarters("duckdb"), + ...predicateKeywords("duckdb"), + "ASOF ANTI SEMI EXCLUDE REPLACE RENAME COLUMNS QUALIFY OVER PARTITION FILTER NULLS FIRST LAST", + "IF RECURSIVE DESCRIBE SUMMARIZE COMMENT INSTALL LOAD EXPORT IMPORT PRAGMA RESET VACUUM", + "SAMPLE REPEATABLE PERCENT ROWS MATERIALIZED TEMPORARY LAMBDA TRY_CAST AT TIME ZONE", + ] + .join(" ") + .toLowerCase(), + types: DATA_TYPES.duckdb + .filter((type) => type !== "UNION") + .join(" ") + .toLowerCase(), + builtin: functions("duckdb") + .map((f) => f.name) + .join(" "), + caseInsensitiveIdentifiers: true, + doubleDollarQuotedStrings: true, + specialVar: "?$", + }).language, + )); } diff --git a/src/features/database/sql/quotedIdentifiers.test.ts b/src/features/database/sql/quotedIdentifiers.test.ts new file mode 100644 index 0000000000..8152dd5e52 --- /dev/null +++ b/src/features/database/sql/quotedIdentifiers.test.ts @@ -0,0 +1,27 @@ +import { highlightTree, tagHighlighter, tags as t } from "@lezer/highlight"; +import { describe, expect, it } from "vitest"; +import { duckdbLanguage } from "./duckdbHighlight"; +import { snowflakeLanguage } from "./snowflakeHighlight"; + +function coloured(language: ReturnType, doc: string): string[] { + const found: string[] = []; + highlightTree( + language.parser.parse(doc), + tagHighlighter([ + { tag: t.name, class: "name" }, + { tag: t.string, class: "string" }, + ]), + (from, to, cls) => found.push(`${cls}:${doc.slice(from, to)}`), + ); + return found; +} + +describe("quoted identifiers in dialect consoles", () => { + const doc = `SELECT "id" FROM "public"."t" WHERE 1 = 'a'`; + it.each([ + ["DuckDB", duckdbLanguage], + ["Snowflake", snowflakeLanguage], + ])("%s colours them as names and literals as strings", (_name, language) => { + expect(coloured(language(), doc)).toEqual(['name:"id"', 'name:"public"', 'name:"t"', "string:'a'"]); + }); +}); diff --git a/src/features/database/sql/quotedIdentifiers.ts b/src/features/database/sql/quotedIdentifiers.ts new file mode 100644 index 0000000000..80fcfc798e --- /dev/null +++ b/src/features/database/sql/quotedIdentifiers.ts @@ -0,0 +1,7 @@ +import { styleTags, tags as t } from "@lezer/highlight"; +import type { LRLanguage } from "@codemirror/language"; + +/** lang-sql tags `"schema"."table"` like a string; in SQL it names a schema, table or column. */ +export function quotedIdentifiersAsNames(language: LRLanguage): LRLanguage { + return language.configure({ props: [styleTags({ QuotedIdentifier: t.name })] }); +} diff --git a/src/features/database/sql/snowflakeHighlight.ts b/src/features/database/sql/snowflakeHighlight.ts index d0ae22aa01..081acfdccd 100644 --- a/src/features/database/sql/snowflakeHighlight.ts +++ b/src/features/database/sql/snowflakeHighlight.ts @@ -7,6 +7,7 @@ import { SQLDialect } from "@codemirror/lang-sql"; import type { LRLanguage } from "@codemirror/language"; import { ExternalTokenizer, type InputStream, type LRParser, type Stack } from "@lezer/lr"; import { DATA_TYPES, functions } from "./dialect"; +import { quotedIdentifiersAsNames } from "./quotedIdentifiers"; import { SNOWFLAKE_KEYWORDS } from "./snowflakeKeywords"; /** A grammar tokenizer as the parser calls it. */ @@ -69,5 +70,5 @@ let built: LRLanguage | undefined; /** The Snowflake SQL language (built on first use). */ export function snowflakeLanguage(): LRLanguage { - return (built ??= build()); + return (built ??= quotedIdentifiersAsNames(build())); }