Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(editor): prevent SQL folding hangs on separator comments

KangYosep380f869e50dd
diff --git a/apps/desktop/src/lib/editor/__tests__/codemirrorSqlBlockFolding.spec.ts b/apps/desktop/src/lib/editor/__tests__/codemirrorSqlBlockFolding.spec.ts
index 21e6c93c21..adcf857215 100644
--- a/apps/desktop/src/lib/editor/__tests__/codemirrorSqlBlockFolding.spec.ts
+++ b/apps/desktop/src/lib/editor/__tests__/codemirrorSqlBlockFolding.spec.ts
@@ -310,17 +310,42 @@ SELECT * FROM (SELECT 1 AS value) one_line;`;
expect(foldedTextAtLine(state, 1)).toBeNull();
});
- it("regression: a comment between END and CASE/TRY/CATCH must not break continuation detection", () => {
+ it.each(["/* comment */", "/* outer /* inner */ note */"])("regression: a comment between END and CASE/TRY/CATCH must not break continuation detection (%s)", (comment) => {
// Before the fix, the gap check required pure whitespace, so a comment there made `END`
// look like a bare closer; the literal `CASE`/`TRY`/`CATCH` word right after was then
// pushed as an unmatched new opener, desyncing every fold pairing after it.
- const sql = "BEGIN\n CASE v\n WHEN 1 THEN SELECT 1;\n END /* comment */ CASE;\nEND";
+ const sql = `BEGIN\n CASE v\n WHEN 1 THEN SELECT 1;\n END ${comment} CASE;\nEND`;
const state = stateFor(sql);
expect(foldedTextAtLine(state, 2)).toBe("\n WHEN 1 THEN SELECT 1;\n ");
const folded = foldedTextAtLine(state, 1);
expect(folded).not.toBeNull();
- expect(folded).toContain("END /* comment */ CASE;");
+ expect(folded).toContain(`END ${comment} CASE;`);
+ });
+
+ it("keeps the enclosing block fold when nested comments separate transaction keywords", () => {
+ const sql = "BEGIN\n BEGIN /* outer /* inner */ note */ DISTRIBUTED /* outer /* inner */ note */ TRANSACTION;\n SELECT 1;\n COMMIT;\nEND";
+ const state = stateFor(sql, "sqlserver", createSqlBlockFoldService("sqlserver"));
+
+ expect(foldedTextAtLine(state, 1)).toBe(sql.slice(sql.indexOf("\n"), sql.lastIndexOf("END")));
+ });
+
+ it.each(["\n", "\r\n"])("bounds folding time when a separator comment precedes a procedure label (%j)", (newline) => {
+ const sql = ["BEGIN", " SELECT 1;", "END", "-".repeat(47), "next_label:", "SELECT 2;"].join(newline);
+ const state = stateFor(sql, "sqlserver", createSqlBlockFoldService("sqlserver"));
+ const start = performance.now();
+
+ // EditorState normalizes CRLF input to its default LF line separator.
+ expect(foldedTextAtLine(state, 1)).toBe("\n SELECT 1;\n");
+ expect(performance.now() - start).toBeLessThan(500);
+ });
+
+ it("keeps END CASE paired across long separator comments", () => {
+ const sql = `BEGIN\n CASE v\n WHEN 1 THEN SELECT 1;\n END\n${"-".repeat(1000)}\n CASE;\nEND`;
+ const state = stateFor(sql, "mysql", createSqlBlockFoldService("mysql"));
+
+ expect(foldedTextAtLine(state, 2)).toBe("\n WHEN 1 THEN SELECT 1;\n ");
+ expect(foldedTextAtLine(state, 1)).toContain(`${"-".repeat(1000)}\n CASE;`);
});
it("still folds correctly after a dialect reconfigure with the document unchanged", () => {
diff --git a/apps/desktop/src/lib/editor/codemirrorSqlBlockFolding.ts b/apps/desktop/src/lib/editor/codemirrorSqlBlockFolding.ts
index 009dde5108..dc87069358 100644
--- a/apps/desktop/src/lib/editor/codemirrorSqlBlockFolding.ts
+++ b/apps/desktop/src/lib/editor/codemirrorSqlBlockFolding.ts
@@ -38,10 +38,39 @@ const TRACKED_END_CONTINUATIONS = new Set(["CASE", "TRY", "CATCH"]);
// the stack; nothing on it belongs to them.
const UNTRACKED_END_CONTINUATIONS = new Set(["IF", "WHILE", "LOOP"]);
-// A comment is legal SQL wherever whitespace is, so "END /* note */ CASE" must still read as a
-// continuation; this matches a run of pure whitespace and/or `--`/`/* */` comments so real code
-// between the two words (e.g. a `;` starting an unrelated statement) still fails the check.
-const GAP_IS_WHITESPACE_OR_COMMENT = /^(?:\s|--[^\n]*|\/\*[\s\S]*?\*\/)*$/;
+// Comments are legal wherever whitespace is, so "END /* note */ CASE" is still a
+// continuation. Scan once: a repeated regex can split a long `-----` comment in
+// exponentially many ways when a label or other code follows it.
+function gapIsWhitespaceOrComment(gap: string): boolean {
+ let index = 0;
+ while (index < gap.length) {
+ if (/\s/.test(gap[index])) {
+ index++;
+ } else if (gap.startsWith("--", index)) {
+ const newline = gap.indexOf("\n", index + 2);
+ if (newline < 0) return true;
+ index = newline + 1;
+ } else if (gap.startsWith("/*", index)) {
+ let depth = 1;
+ index += 2;
+ while (depth > 0 && index < gap.length) {
+ if (gap.startsWith("/*", index)) {
+ depth++;
+ index += 2;
+ } else if (gap.startsWith("*/", index)) {
+ depth--;
+ index += 2;
+ } else {
+ index++;
+ }
+ }
+ if (depth > 0) return false;
+ } else {
+ return false;
+ }
+ }
+ return true;
+}
// T-SQL transaction openers do not have a matching `END`, so they must not consume the closer
// of an enclosing procedural `BEGIN...END` block. Match complete keyword tokens only: `TRAN` is
@@ -50,14 +79,14 @@ const SQLSERVER_TRANSACTION_BEGIN_WORDS = new Set(["TRAN", "TRANSACTION"]);
function isSqlServerTransactionBegin(state: EditorState, tokens: SyntaxNode[], index: number): boolean {
const next = tokens[index + 1];
- if (!next || !GAP_IS_WHITESPACE_OR_COMMENT.test(state.sliceDoc(tokens[index].to, next.from))) return false;
+ if (!next || !gapIsWhitespaceOrComment(state.sliceDoc(tokens[index].to, next.from))) return false;
const nextText = state.sliceDoc(next.from, next.to).toUpperCase();
if (SQLSERVER_TRANSACTION_BEGIN_WORDS.has(nextText)) return true;
if (nextText !== "DISTRIBUTED") return false;
const transaction = tokens[index + 2];
- return Boolean(transaction && GAP_IS_WHITESPACE_OR_COMMENT.test(state.sliceDoc(next.to, transaction.from)) && state.sliceDoc(transaction.from, transaction.to).toUpperCase() === "TRANSACTION");
+ return Boolean(transaction && gapIsWhitespaceOrComment(state.sliceDoc(next.to, transaction.from)) && state.sliceDoc(transaction.from, transaction.to).toUpperCase() === "TRANSACTION");
}
// Keyed by the `Tree` instance (not `Text`): the syntax tree is also invalidated when the SQL
@@ -189,7 +218,7 @@ export function computeBlockFoldRanges(state: EditorState, databaseType?: Databa
const text = state.sliceDoc(token.from, token.to).toUpperCase();
if (text === "END") {
const next = tokens[i + 1];
- const continuation = next && GAP_IS_WHITESPACE_OR_COMMENT.test(state.sliceDoc(token.to, next.from)) ? state.sliceDoc(next.from, next.to).toUpperCase() : null;
+ const continuation = next && gapIsWhitespaceOrComment(state.sliceDoc(token.to, next.from)) ? state.sliceDoc(next.from, next.to).toUpperCase() : null;
if (continuation && UNTRACKED_END_CONTINUATIONS.has(continuation)) {
i++; // "END IF"/"END WHILE"/... -- closes something we don't track; skip past it untouched
continue;