feat(editor): add toggle case shortcut for selection

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 10:02:06 +08:00
1 parent eeeea648c1
commit f47c209b15
21 files changed
+111 -3

No files matched your search

@@ -41,6 +41,7 @@ describe("shortcutRegistry editor actions", () => {
"selectAllSelectionOccurrences",
"uppercaseSelection",
"lowercaseSelection",
"toggleCaseSelection",
"exPasteSqlInCondition",
"toggleFold",
"foldAll",
@@ -292,6 +293,7 @@ describe("shortcutRegistry editor actions", () => {
expect(shortcuts.selectAllSelectionOccurrences).toBe(selectionOccurrenceDefaultShortcut("selectAllSelectionOccurrences"));
expect(shortcuts.uppercaseSelection).toBe("Shift+Alt+U");
expect(shortcuts.lowercaseSelection).toBe("Shift+Alt+L");
expect(shortcuts.toggleCaseSelection).toBe("Mod+Shift+U");
expect(shortcuts.exPasteSqlInCondition).toBe("");
expect(shortcuts.toggleFold).toBe("Mod+.");
expect(shortcuts.foldAll).toBe(foldAllDefaultShortcut("foldAll"));
@@ -308,6 +310,13 @@ describe("shortcutRegistry editor actions", () => {
expect(normalizeShortcutSettings({ foldAll: "", unfoldAll: "" }, "Win32")).toMatchObject({ foldAll: "", unfoldAll: "" });
});
it("registers toggle case as a configurable editor shortcut (#5085)", () => {
const definition = SHORTCUT_DEFINITIONS.find((item) => item.id === "toggleCaseSelection");
expect(definition).toMatchObject({ scope: "editor", defaultShortcut: "Mod+Shift+U", labelKey: "settings.shortcutToggleCaseSelection" });
expect(DEFAULT_SHORTCUT_SETTINGS.toggleCaseSelection).toBe("Mod+Shift+U");
});
it("registers IntelliJ-style extend selection as a configurable editor shortcut", () => {
const definition = SHORTCUT_DEFINITIONS.find((item) => item.id === "extendSelection");
@@ -98,4 +98,51 @@ describe("convertSqlSelectionCase", () => {
expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("> 60 AND GRADE = 'AbC'");
});
describe("toggle mode (#5085)", () => {
it("converts lowercase text to uppercase", () => {
const sql = "select id, name from users";
expect(convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "toggle")).toBe("SELECT ID, NAME FROM USERS");
});
it("defaults mixed case to uppercase first", () => {
const sql = "Select id, userName From Users";
expect(convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "toggle")).toBe("SELECT ID, USERNAME FROM USERS");
});
it("converts all-uppercase text to lowercase", () => {
const sql = "SELECT ID, NAME FROM USERS";
expect(convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "toggle")).toBe("select id, name from users");
});
it("cycles between uppercase and lowercase on repeated toggles", () => {
const initial = "Select Name From Users";
const first = convertSqlSelectionCase(initial, { from: 0, to: initial.length }, "toggle");
expect(first).toBe("SELECT NAME FROM USERS");
const second = convertSqlSelectionCase(first, { from: 0, to: first.length }, "toggle");
expect(second).toBe("select name from users");
const third = convertSqlSelectionCase(second, { from: 0, to: second.length }, "toggle");
expect(third).toBe("SELECT NAME FROM USERS");
});
it("protects string literals when toggling case", () => {
const sql = "select name from users where status = 'active'";
const upper = convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "toggle");
expect(upper).toBe("SELECT NAME FROM USERS WHERE STATUS = 'active'");
const lower = convertSqlSelectionCase(upper, { from: 0, to: upper.length }, "toggle");
expect(lower).toBe("select name from users where status = 'active'");
});
it("toggles bare string literals when selected directly", () => {
const sql = "('first', 'second')";
const upper = convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "toggle");
expect(upper).toBe("('FIRST', 'SECOND')");
const lower = convertSqlSelectionCase(upper, { from: 0, to: upper.length }, "toggle");
expect(lower).toBe("('first', 'second')");
});
});
});
@@ -29,6 +29,7 @@ export type ShortcutActionId =
| "selectAllSelectionOccurrences"
| "uppercaseSelection"
| "lowercaseSelection"
| "toggleCaseSelection"
| "convertNamingStyle"
| "exPasteSqlInCondition"
| "toggleFold"
@@ -330,6 +331,12 @@ export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
scope: "editor",
defaultShortcut: "Shift+Alt+L",
},
{
id: "toggleCaseSelection",
labelKey: "settings.shortcutToggleCaseSelection",
scope: "editor",
defaultShortcut: "Mod+Shift+U",
},
{
id: "convertNamingStyle",
labelKey: "settings.shortcutConvertNamingStyle",
+10 -2
View File
@@ -1,19 +1,27 @@
import { tokenizeSqlSemantic } from "@/lib/sql/semantic/tokens";
export type SqlSelectionCaseMode = "upper" | "lower";
export type SqlSelectionCaseMode = "upper" | "lower" | "toggle";
type SqlSelectionRange = {
from: number;
to: number;
};
function convertCase(text: string, mode: SqlSelectionCaseMode): string {
function convertCase(text: string, mode: "upper" | "lower"): string {
return mode === "upper" ? text.toUpperCase() : text.toLowerCase();
}
export function convertSqlSelectionCase(sql: string, range: SqlSelectionRange, mode: SqlSelectionCaseMode, dialectId?: "mysql" | "postgres" | "sqlserver"): string {
const from = Math.max(0, Math.min(range.from, sql.length));
const to = Math.max(from, Math.min(range.to, sql.length));
if (mode === "toggle") {
const upperResult = convertSqlSelectionCase(sql, range, "upper", dialectId);
const currentText = sql.slice(from, to);
if (upperResult !== currentText) {
return upperResult;
}
return convertSqlSelectionCase(sql, range, "lower", dialectId);
}
const protectedTokens = tokenizeSqlSemantic(sql, dialectId).filter((item) => {
if (item.span.end <= from || item.span.start >= to) return false;
if (item.kind === "string") return true;