fix(editor): prevent SQL folding hangs on separator comments
This commit is contained in:
1 parent
787899f201
commit
380f869e50
2 files changed
+64
-10
No files matched your search
@@ -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", () => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user