diff --git a/crates/ide-core/src/editor_settings.rs b/crates/ide-core/src/editor_settings.rs index f947deb447..7ae347257b 100644 --- a/crates/ide-core/src/editor_settings.rs +++ b/crates/ide-core/src/editor_settings.rs @@ -314,6 +314,30 @@ pub struct SmartKeysSettings { pub insert_doc_stub: bool, /// Backspace › Unindent: `autoindent` (proper indent position) | `nearest` indent position | `none`. pub backspace_unindent: String, + /// The styles Cycle Identifier Case goes through. + pub cycle_case: CycleCaseSettings, +} + +/// Editor › General › Smart Keys › Cycle Identifier Case: the styles the action goes through. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)] +#[serde(default, rename_all = "camelCase")] +pub struct CycleCaseSettings { + /// `snake_case` + pub snake: bool, + /// `SCREAMING_SNAKE_CASE` + pub screaming: bool, + /// `kebab-case` + pub kebab: bool, + /// `camelCase` + pub camel: bool, + /// `PascalCase` + pub pascal: bool, +} + +impl Default for CycleCaseSettings { + fn default() -> Self { + Self { snake: true, screaming: true, kebab: true, camel: true, pascal: true } + } } impl Default for SmartKeysSettings { @@ -335,6 +359,7 @@ impl Default for SmartKeysSettings { smart_indent: true, insert_doc_stub: true, backspace_unindent: "autoindent".into(), + cycle_case: CycleCaseSettings::default(), } } } diff --git a/src/bindings.ts b/src/bindings.ts index 25e744dc84..06873652a3 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -5975,6 +5975,20 @@ export type CustomKeymap = { overrides?: { [key in string]: string[] }, }; +/** Editor › General › Smart Keys › Cycle Identifier Case: the styles the action goes through. */ +export type CycleCaseSettings = { + /** `snake_case` */ + snake?: boolean, + /** `SCREAMING_SNAKE_CASE` */ + screaming?: boolean, + /** `kebab-case` */ + kebab?: boolean, + /** `camelCase` */ + camel?: boolean, + /** `PascalCase` */ + pascal?: boolean, +}; + /** * A field watchpoint (DAP data breakpoint) on a `dataId` from [`DebugManager::data_breakpoint_info`] * (java-debug ids, `Class#field`, stay valid across sessions). @@ -15073,6 +15087,8 @@ export type SmartKeysSettings = { insertDocStub?: boolean, /** Backspace › Unindent: `autoindent` (proper indent position) | `nearest` indent position | `none`. */ backspaceUnindent?: string, + /** The styles Cycle Identifier Case goes through. */ + cycleCase?: CycleCaseSettings, }; export type SourceFolder = { diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts index da499ec4af..8deae2b7b0 100644 --- a/src/editor/core/actions.ts +++ b/src/editor/core/actions.ts @@ -1348,7 +1348,7 @@ export function registerEditorActions(): () => void { edit("code.joinLines", "Join Lines", (v) => joinLines(v)), edit("code.toggleCase", "Toggle Case", (v) => toggleCase(v), { keywords: ["upper", "lower"] }), edit("code.transpose", "Transpose", (v) => transpose(v), { category: "Editor Actions" }), - edit("code.cycleCase", "Cycle Identifier Case", (v) => cycleCase(v), { + edit("code.cycleCase", "Cycle Identifier Case", (v) => cycleCase(v, editorPrefs().smartKeys.cycleCase), { keywords: ["camel", "snake", "kebab", "pascal", "screaming"], }), caret("code.addCaretsToLineEnds", "Add Carets to Ends of Selected Lines", (v) => addCaretsToLineEnds(v), { diff --git a/src/editor/core/cm/editing.ts b/src/editor/core/cm/editing.ts index d73c708c96..76bcc51e1f 100644 --- a/src/editor/core/cm/editing.ts +++ b/src/editor/core/cm/editing.ts @@ -36,7 +36,7 @@ import { } from "@codemirror/commands"; import { SearchCursor } from "@codemirror/search"; import { humpLeft, humpRight, wordStartLeft } from "../model/camelHumps"; -import { cycleIdentifierCase, wordSpan } from "../model/identifierCase"; +import { cycleIdentifierCase, wordSpan, type IdentifierCase } from "../model/identifierCase"; import { restartSticky } from "./stickySelection"; type Node = ReturnType["resolveInner"]>; @@ -1087,7 +1087,10 @@ const NAME_CHAR = /[\p{L}\p{N}_$-]/u; * or of each selection, from the style it is written in: snake_case → * SCREAMING_SNAKE_CASE → kebab-case → camelCase → PascalCase. */ -export const cycleCase: Command = (view) => { +export const cycleCase = ( + view: EditorView, + enabled: Readonly>> = {}, +): boolean => { const { state } = view; if (state.readOnly) return false; /** The `-`-joined name at or right before `pos`. */ @@ -1107,7 +1110,7 @@ export const cycleCase: Command = (view) => { const span = wordSpan(state.sliceDoc(name.from, name.to)); if (!span) return { range: r }; const from = name.from + span.from; - const insert = cycleIdentifierCase(state.sliceDoc(from, name.from + span.to)); + const insert = cycleIdentifierCase(state.sliceDoc(from, name.from + span.to), enabled); if (insert === null) return { range: r }; changed = true; const set = state.changes({ from, to: name.from + span.to, insert }); diff --git a/src/editor/core/model/identifierCase.test.ts b/src/editor/core/model/identifierCase.test.ts index 40761cc3b7..f70aa8f443 100644 --- a/src/editor/core/model/identifierCase.test.ts +++ b/src/editor/core/model/identifierCase.test.ts @@ -39,6 +39,21 @@ describe("cycle identifier case", () => { expect(cycleIdentifierCase("MyVarName")).toBe("my_var_name"); }); + it("goes only through the styles turned on, starting from the one the name is written in", () => { + const camelSnake = { screaming: false, kebab: false, pascal: false }; + expect(cycleIdentifierCase("myVarName", camelSnake)).toBe("my_var_name"); + expect(cycleIdentifierCase("my_var_name", camelSnake)).toBe("myVarName"); + // A name in a style that is turned off still moves on to the next one that is on. + expect(cycleIdentifierCase("my-var-name", camelSnake)).toBe("myVarName"); + expect(cycleIdentifierCase("MyVarName", camelSnake)).toBe("my_var_name"); + // Only the name's own style on: nothing else to go to. + expect( + cycleIdentifierCase("myVarName", { snake: false, screaming: false, kebab: false, pascal: false }), + ).toBeNull(); + // One word in snake_case is written as in camelCase, so with Name and NAME off it stays. + expect(cycleIdentifierCase("name", { pascal: false, screaming: false })).toBeNull(); + }); + it("gives one word its three spellings: name → Name → NAME", () => { expect(cycleIdentifierCase("name")).toBe("Name"); expect(cycleIdentifierCase("Name")).toBe("NAME"); diff --git a/src/editor/core/model/identifierCase.ts b/src/editor/core/model/identifierCase.ts index c35d0a536c..f6ac8cac95 100644 --- a/src/editor/core/model/identifierCase.ts +++ b/src/editor/core/model/identifierCase.ts @@ -81,14 +81,20 @@ export function toIdentifierCase(text: string, style: IdentifierCase): string { /** * `text` in the next style of the cycle, skipping the styles that write it the - * same way; null when none writes it differently. + * same way and those `enabled` turns off (Settings › Editor › General › Smart + * Keys); null when none writes it differently. */ -export function cycleIdentifierCase(text: string): string | null { +export function cycleIdentifierCase( + text: string, + enabled: Readonly>> = {}, +): string | null { const current = identifierCase(text); if (!current) return null; // One word has three spellings, not five: name → Name → NAME. const single = identifierWords(text).length === 1 && !SEPARATOR.test(text); - const order: readonly IdentifierCase[] = single ? ["camel", "pascal", "screaming", "snake"] : identifierCases; + const all: readonly IdentifierCase[] = single ? ["camel", "pascal", "screaming", "snake"] : identifierCases; + // The style the name is written in stays the starting point even when it is turned off. + const order = all.filter((style) => style === current || enabled[style] !== false); const start = Math.max(0, order.indexOf(current)); for (let step = 1; step <= order.length; step++) { const next = toIdentifierCase(text, order[(start + step) % order.length]!); diff --git a/src/features/settings/pages/editor/general.tsx b/src/features/settings/pages/editor/general.tsx index 0d0b0b497b..394bbf5264 100644 --- a/src/features/settings/pages/editor/general.tsx +++ b/src/features/settings/pages/editor/general.tsx @@ -819,6 +819,9 @@ const SMART_KEYS = editorPage({ "whitespace", "indent", "semicolon", + "case", + "snake case", + "camel case", ], sections: [ { @@ -913,6 +916,16 @@ const SMART_KEYS = editorPage({ }, ], }, + { + title: "Cycle Identifier Case", + items: [ + { t: "check", key: `${P}.smartKeys.cycleCase.snake`, label: "snake_case" }, + { t: "check", key: `${P}.smartKeys.cycleCase.screaming`, label: "SCREAMING_SNAKE_CASE" }, + { t: "check", key: `${P}.smartKeys.cycleCase.kebab`, label: "kebab-case" }, + { t: "check", key: `${P}.smartKeys.cycleCase.camel`, label: "camelCase" }, + { t: "check", key: `${P}.smartKeys.cycleCase.pascal`, label: "PascalCase" }, + ], + }, ], }); diff --git a/src/platform/editorSettings.ts b/src/platform/editorSettings.ts index 9b8f60675a..c577945e44 100644 --- a/src/platform/editorSettings.ts +++ b/src/platform/editorSettings.ts @@ -233,6 +233,8 @@ export const EDITOR_EXTRA_DEFAULTS = { smartIndent: true, insertDocStub: true, backspaceUnindent: "autoindent", + /** The styles Cycle Identifier Case goes through. */ + cycleCase: { snake: true, screaming: true, kebab: true, camel: true, pascal: true }, }, codeEditing: { highlightUsages: true, diff --git a/src/platform/i18n.test.ts b/src/platform/i18n.test.ts index 444a26fc12..05eafc657d 100644 --- a/src/platform/i18n.test.ts +++ b/src/platform/i18n.test.ts @@ -284,6 +284,8 @@ describe("catalogs", () => { const OWN_SCRIPT = new Set(["zh", "ko"]); /** Product names intentionally kept as written in every locale. */ const SAME_AS_ENGLISH = new Set([ + "SCREAMING_SNAKE_CASE", + "camelCase", // PostgreSQL storage parameters, the placeholder of Modify Table's Storage options field. "fillfactor=70, autovacuum_enabled=false", // The placeholder of an Xcode run configuration's project or workspace field. diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 272f375564..e820416a57 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -1189,6 +1189,8 @@ "Join Lines": "合并行", "Toggle Case": "切换大小写", "Cycle Identifier Case": "循环切换标识符大小写", + "SCREAMING_SNAKE_CASE": "SCREAMING_SNAKE_CASE", + "camelCase": "camelCase", "Add or Remove Caret": "添加或移除插入点", "Create Rectangular Selection on Mouse Drag": "拖动鼠标创建矩形选区", "Middle Click": "鼠标中键",