fix(editor): honor case conversion for literal-only selections

Closes #10775
This commit is contained in:
dienaso authored and GitHub committed 2026-09-30 19:52:49 +08:00
1 parent dc503877f9
commit 4819565f28
2 files changed
+63 -6

No files matched your search

@@ -57,4 +57,45 @@ describe("convertSqlSelectionCase", () => {
expect(convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "lower", "mysql")).toBe("select 1 /*!40101 SET @Name = 'Mixed Value' */ from dual");
});
// #10775: a selection made only of string literals joined by punctuation —
// like the IN-list `('D','C','B','B','A')` — used to hit the string
// protection and come back unchanged, which read as "the command does
// nothing". With no identifiers or keywords inside the selection there is no
// SQL structure to protect, so the explicit request converts the strings.
it("converts an IN-list selection whose only word content is string literals", () => {
const sql = "select * from t where code in ('D','C','B','B','A')";
const from = sql.indexOf("('D'");
expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "lower")).toBe("('d','c','b','b','a')");
expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("('D','C','B','B','A')");
});
it("converts adjacent literals joined by operators or commas", () => {
const sql = "select * from t where a = 'AbC' || 'DeF' and b in ('GhI','JkL')";
expect(convertSqlSelectionCase(sql, { from: sql.indexOf("'AbC'"), to: sql.indexOf("||") - 1 }, "lower")).toBe("'abc'");
expect(convertSqlSelectionCase(sql, { from: sql.indexOf("'GhI'"), to: sql.indexOf("'JkL'") + 5 }, "lower")).toBe("'ghi','jkl'");
});
it("still protects literals when the selection reaches identifiers or keywords", () => {
const sql = "select * from t where code in ('D','C') and flag = 'E'";
const from = sql.indexOf("in (");
expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("IN ('D','C') AND FLAG = 'E'");
});
it("keeps converting a selection fully inside one literal", () => {
const sql = "select * from t where code = 'AbC001'";
const from = sql.indexOf("bC");
expect(convertSqlSelectionCase(sql, { from, to: from + 2 }, "upper")).toBe("BC");
});
it("treats number tokens outside literals as structure and keeps the protection", () => {
const sql = "select * from t where score > 60 and grade = 'AbC'";
const from = sql.indexOf("> 60");
expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("> 60 AND GRADE = 'AbC'");
});
});
+22 -6
View File
@@ -23,13 +23,29 @@ export function convertSqlSelectionCase(sql: string, range: SqlSelectionRange, m
});
if (protectedTokens.length === 0) return convertCase(sql.slice(from, to), mode);
// A selection fully contained inside one protected token (e.g. the user selected only
// text inside a string literal) carries no risk of collateral damage to surrounding SQL,
// so honor the explicit request instead of silently no-oping it.
const [only] = protectedTokens;
if (protectedTokens.length === 1 && only.span.start <= from && to <= only.span.end) {
return convertCase(sql.slice(from, to), mode);
// An explicit selection whose only word-like content sits inside protected
// tokens — a bare string literal, or a value list like `('D','C','B')` — has
// no SQL structure that case conversion could damage: everything outside the
// protected tokens is punctuation and whitespace, which the conversion leaves
// unchanged anyway. Honor the request fully instead of silently no-oping it
// (issue #10775: selecting `('D','C','B','B','A')` and converting did
// nothing). Selections that reach past protected tokens into identifiers or
// keywords keep the protection below.
const unprotectedWordCharPattern = /[\p{L}\p{N}_]/u;
let unprotectedTextHasWord = false;
let unprotectedScan = from;
for (const item of protectedTokens) {
const literalFrom = Math.max(from, item.span.start);
if (unprotectedWordCharPattern.test(sql.slice(unprotectedScan, literalFrom))) {
unprotectedTextHasWord = true;
break;
}
unprotectedScan = Math.min(to, Math.max(unprotectedScan, item.span.end));
}
if (!unprotectedTextHasWord && unprotectedWordCharPattern.test(sql.slice(unprotectedScan, to))) {
unprotectedTextHasWord = true;
}
if (!unprotectedTextHasWord) return convertCase(sql.slice(from, to), mode);
let converted = "";
let cursor = from;