fix(editor): handle trailing comments in SQL ranges

This commit is contained in:
dadong authored and GitHub committed 2026-08-18 23:16:59 +08:00
1 parent 1ecac25d52
commit c4105d1e65
2 files changed
+80 -1

No files matched your search

+43 -1
View File
@@ -24,7 +24,49 @@ export function cursorBelongsToTrailingStatementDelimiter(source: StatementDelim
if (cursorPos <= delimiterPos + 1) return true;
const afterDelimiter = sliceSource(source, delimiterPos + 1, cursorPos);
return !afterDelimiter.includes("\n") && afterDelimiter.trim() === "";
// 分号到光标之间允许是空白或尾部注释(如 `SELECT 1; -- 备注`),
// 块注释自身可以跨行,但注释之外的换行仍表示光标已进入下一行
// 否则行尾注释后的光标会被下一条语句的前置区域错误认领
return trailingGapIsWhitespaceOrComment(afterDelimiter);
}
/**
* 判断语句分号与光标之间的间隙是否只包含空白和注释。
* 块注释内部允许换行;注释之外的换行表示光标已离开分号所在行。
*/
function trailingGapIsWhitespaceOrComment(gap: string): boolean {
let i = 0;
while (i < gap.length) {
const ch = gap[i] ?? "";
const next = gap[i + 1] ?? "";
// 普通空白直接跳过
if (ch === " " || ch === "\t" || ch === "\r") {
i += 1;
continue;
}
if (ch === "\n") return false;
// 行注释:`--`(SQL 标准)或 `//`(MongoDB shell),其后内容直到行尾都是注释
if ((ch === "-" && next === "-") || (ch === "/" && next === "/")) return true;
// `#` 行注释(MySQL 等),但 `#{` 是 MyBatis 参数语法,不是注释
if (ch === "#" && next !== "{") return true;
// 块注释 /* ... */:跳过整段后继续检查剩余间隙
if (ch === "/" && next === "*") {
const close = gap.indexOf("*/", i + 2);
// 未闭合的块注释:剩余部分全部视为注释
if (close === -1) return true;
i = close + 2;
continue;
}
// 出现其他实际内容,说明光标后面还有语句文本,不能归属上一条语句
return false;
}
return true;
}
function sliceSource(source: StatementDelimiterSource, from: number, to: number): string {