fix(editor): improve ASE SQL semantic highlighting

This commit is contained in:
zipg authored and GitHub committed 2026-07-16 17:00:55 +08:00
1 parent df1d31e74f
commit 3d23a58e36
14 files changed
+360 -22

No files matched your search

@@ -66,6 +66,7 @@ function codeMirrorBaseDialect(langSql: CodeMirrorSqlLanguageModule, dialectName
if (SQLITE_CODEMIRROR_DATABASE_TYPES.has(databaseType)) return langSql.SQLite;
if (databaseType === "sqlserver") return langSql.MSSQL;
if (databaseType === "cassandra") return langSql.Cassandra;
if (databaseType === "jdbc" && dialectName === "sqlserver") return langSql.MSSQL;
return langSql.StandardSQL;
}
return dialectName === "postgres" ? langSql.PostgreSQL : dialectName === "sqlserver" ? langSql.MSSQL : langSql.MySQL;
@@ -0,0 +1,39 @@
import { sqlSemanticTableNameSpans } from "@/lib/sql/semantic/model";
import type { SqlSemanticBuildOptions, SqlSemanticSpan } from "@/lib/sql/semantic/types";
type SqlSyntaxTree = ReturnType<typeof import("@codemirror/language").syntaxTree>;
const SUPPRESSED_NODE_NAMES = new Set(["String", "LineComment", "BlockComment"]);
export interface SqlSemanticHighlightWindow {
from: number;
to: number;
}
function maskSuppressedSyntax(sql: string, window: SqlSemanticHighlightWindow, tree: SqlSyntaxTree): string {
const ranges: SqlSemanticHighlightWindow[] = [];
tree.iterate({
from: window.from,
to: window.to,
enter(node) {
if (!SUPPRESSED_NODE_NAMES.has(node.name)) return;
ranges.push({ from: Math.max(window.from, node.from), to: Math.min(window.to, node.to) });
return false;
},
});
let cursor = window.from;
let masked = "";
for (const range of ranges) {
if (range.from < cursor) continue;
masked += sql.slice(cursor, range.from);
masked += sql.slice(range.from, range.to).replace(/[^\r\n]/g, " ");
cursor = range.to;
}
return masked + sql.slice(cursor, window.to);
}
export function sqlSemanticTableNameSpansForSyntaxTree(sql: string, window: SqlSemanticHighlightWindow, tree: SqlSyntaxTree, options: SqlSemanticBuildOptions = {}): SqlSemanticSpan[] {
const maskedSql = maskSuppressedSyntax(sql, window, tree);
return sqlSemanticTableNameSpans(maskedSql, options).map((span) => ({ start: span.start + window.from, end: span.end + window.from }));
}
+21 -4
View File
@@ -9,6 +9,7 @@ type LucideIconNode = Array<[string, Record<string, string>]>;
export const EDITOR_FONT_SIZE_CSS_VAR = "--dbx-editor-font-size";
export const EDITOR_FONT_FAMILY_CSS_VAR = "--dbx-editor-font-family";
export const SQL_TABLE_COLOR_CSS_VAR = "--dbx-sql-table-color";
const EDITOR_SELECTION_BACKGROUND_CSS_VAR = "--dbx-editor-selection-background";
export function createRunStatementButtonDom(ariaLabel = "Execute statement"): HTMLButtonElement {
@@ -21,6 +22,14 @@ export function createRunStatementButtonDom(ariaLabel = "Execute statement"): HT
return marker;
}
export function sqlSemanticHighlightTheme(EditorView: typeof import("@codemirror/view").EditorView): Extension {
return EditorView.theme({
".cm-sql-table-name, .cm-sql-table-name *": {
color: `var(${SQL_TABLE_COLOR_CSS_VAR}) !important`,
},
});
}
const SUPPORTS_COLOR_MIX = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("color", "color-mix(in oklch, black 50%, white)");
const SUPPORTS_OKLCH = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("color", "oklch(0.62 0.19 255)");
@@ -72,11 +81,8 @@ function createCustomTheme(EditorView: typeof import("@codemirror/view").EditorV
c.variable = colors.field;
c.property = colors.field;
}
if (colors.table) {
// 表名通常被识别为 propertyName,如果单独设置了表名颜色则覆盖
c.property = colors.table;
}
}
const tableColor = colors?.table || c.property;
const theme = EditorView.theme(
{
@@ -84,6 +90,7 @@ function createCustomTheme(EditorView: typeof import("@codemirror/view").EditorV
backgroundColor: c.background,
color: c.foreground,
[EDITOR_SELECTION_BACKGROUND_CSS_VAR]: c.selection,
[SQL_TABLE_COLOR_CSS_VAR]: tableColor,
},
".cm-content": {
caretColor: c.cursor,
@@ -212,6 +219,7 @@ type IdeEditorThemeColors = {
operator: string;
punctuation: string;
property: string;
table: string;
builtin: string;
meta: string;
invalid: string;
@@ -247,6 +255,7 @@ const IDE_EDITOR_THEMES = {
operator: "#080808",
punctuation: "#080808",
property: "#871094",
table: "#871094",
builtin: "#0033b3",
meta: "#9e880d",
invalid: "#f50000",
@@ -280,6 +289,7 @@ const IDE_EDITOR_THEMES = {
operator: "#cc7832",
punctuation: "#a9b7c6",
property: "#9876aa",
table: "#9876aa",
builtin: "#cc7832",
meta: "#bbb529",
invalid: "#ff0000",
@@ -310,6 +320,7 @@ const IDE_EDITOR_THEMES = {
operator: "#080808",
punctuation: "#080808",
property: "#871094",
table: "#871094",
builtin: "#0033b3",
meta: "#9e880d",
invalid: "#f50000",
@@ -343,6 +354,7 @@ const IDE_EDITOR_THEMES = {
operator: "#bcbec4",
punctuation: "#bcbec4",
property: "#c77dbb",
table: "#c77dbb",
builtin: "#cf8e6d",
meta: "#b3ae60",
invalid: "#f75464",
@@ -373,6 +385,7 @@ const IDE_EDITOR_THEMES = {
operator: "#b3003f",
punctuation: "#141414eb",
property: "#1f8a65",
table: "#1f8a65",
builtin: "#206595",
meta: "#1f8a65",
invalid: "#cf2d56",
@@ -403,6 +416,7 @@ const IDE_EDITOR_THEMES = {
operator: "#d6d6dd",
punctuation: "#d6d6dd",
property: "#82d2ce",
table: "#aaa0fa",
builtin: "#a8cc7c",
meta: "#a8cc7c",
invalid: "#e34671",
@@ -433,6 +447,7 @@ const IDE_EDITOR_THEMES = {
operator: "#6b5544",
punctuation: "#6b5544",
property: "#9a4f2e",
table: "#7a5aa8",
builtin: "#4f7d5d",
meta: "#8f6b2e",
invalid: "#c3493d",
@@ -463,6 +478,7 @@ const IDE_EDITOR_THEMES = {
operator: "#e0d0c0",
punctuation: "#c9b9a7",
property: "#d28a5f",
table: "#a08fcd",
builtin: "#74b195",
meta: "#d4ae63",
invalid: "#e2675f",
@@ -479,6 +495,7 @@ function createIdeEditorTheme(EditorView: typeof import("@codemirror/view").Edit
backgroundColor: c.background,
color: c.foreground,
[EDITOR_SELECTION_BACKGROUND_CSS_VAR]: c.selection,
[SQL_TABLE_COLOR_CSS_VAR]: c.table,
},
".cm-scroller": {
backgroundColor: c.background,