fix(editor): format PostgreSQL SQL with backticks

Closes #6741
This commit is contained in:
t8y2 committed 2026-08-20 06:18:51 +00:00
1 parent dc74d2a77f
commit 12a2dd07f7
2 files changed
+40 -1

No files matched your search

+15 -1
View File
@@ -105,9 +105,23 @@ export async function formatSqlText(sql: string, dialect: SqlFormatDialect = "ge
const normalizedSettings = normalizeSqlFormatterSettings(settings);
const options = sqlFormatterOptions(normalizedSettings);
const language = formatterLanguage(dialect);
const formatterOptions =
language === "postgresql"
? {
...options,
paramTypes: {
...options.paramTypes,
// PostgreSQL itself uses double quotes, but users commonly format
// imported MySQL-style SQL before correcting it. Treat complete
// backtick spans as opaque tokens so the PostgreSQL lexer can keep
// formatting the surrounding statement without rewriting them.
custom: [...options.paramTypes.custom, { regex: "`(?:``|[^`])*`" }],
},
}
: options;
const formatWithFallback = (input: string): string => {
try {
return format(input, { language, ...options });
return format(input, { language, ...formatterOptions });
} catch (err) {
// The generic "sql" dialect can't parse many real-world constructs (PostgreSQL
// `::` casts, GaussDB/openGauss materialized-view DDL, T-SQL specifics, ...).