From 98ea5e172ec114004116f996f51b46cdf5c438e4 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Thu, 2 May 2024 08:37:13 -0400 Subject: [PATCH] Add `convert to opposite case` command (#11290) --- crates/editor/src/actions.rs | 7 ++++--- crates/editor/src/editor.rs | 18 ++++++++++++++++++ crates/editor/src/editor_tests.rs | 8 ++++++++ crates/editor/src/element.rs | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 160b8a1e1b5263ce2ef15432ba535c567ead3626..143a5f4a5269d94654701272e12e54fc47d984ef 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -155,6 +155,7 @@ gpui::actions!( ConvertToKebabCase, ConvertToLowerCamelCase, ConvertToLowerCase, + ConvertToOppositeCase, ConvertToSnakeCase, ConvertToTitleCase, ConvertToUpperCamelCase, @@ -175,8 +176,9 @@ gpui::actions!( DeleteToPreviousSubwordStart, DeleteToPreviousWordStart, DisplayCursorNames, - DuplicateLineUp, DuplicateLineDown, + DuplicateLineUp, + ExpandAllHunkDiffs, ExpandMacroRecursively, FindAllReferences, Fold, @@ -267,7 +269,6 @@ gpui::actions!( ToggleGitBlame, ToggleGitBlameInline, ToggleHunkDiff, - ExpandAllHunkDiffs, ToggleInlayHints, ToggleLineNumbers, ToggleSoftWrap, @@ -275,7 +276,7 @@ gpui::actions!( Undo, UndoSelection, UnfoldLines, - UniqueLinesCaseSensitive, UniqueLinesCaseInsensitive, + UniqueLinesCaseSensitive, ] ); diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 61e1d5839f8daa47cf4820d7d2e093ab017b8f7c..c9d20c27658560daebe3d0b14525e7dc43b2d98f 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -5192,6 +5192,24 @@ impl Editor { self.manipulate_text(cx, |text| text.to_case(Case::Camel)) } + pub fn convert_to_opposite_case( + &mut self, + _: &ConvertToOppositeCase, + cx: &mut ViewContext, + ) { + self.manipulate_text(cx, |text| { + text.chars() + .fold(String::with_capacity(text.len()), |mut t, c| { + if c.is_uppercase() { + t.extend(c.to_lowercase()); + } else { + t.extend(c.to_uppercase()); + } + t + }) + }) + } + fn manipulate_text(&mut self, cx: &mut ViewContext, mut callback: Fn) where Fn: FnMut(&str) -> String, diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 17f2a3c363db27eadedeb1219d3e4f6852a09794..76db71bd0b69ae8b5449f36f84c42b78eb09bee4 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -3238,6 +3238,14 @@ async fn test_manipulate_text(cx: &mut TestAppContext) { cx.assert_editor_state(indoc! {" «aaaBbbˇ» «bbbCccˇ» «cccDddˇ» "}); + + cx.set_state(indoc! {" + «hElLo, WoRld!ˇ» + "}); + cx.update_editor(|e, cx| e.convert_to_opposite_case(&ConvertToOppositeCase, cx)); + cx.assert_editor_state(indoc! {" + «HeLlO, wOrLD!ˇ» + "}); } #[gpui::test] diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index eaed495c81c0685ecf8c6fcf42a2ace4e74cf48b..eda06a95c9df369eb3e1f9ea94709793c483bf12 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -176,6 +176,7 @@ impl EditorElement { register_action(view, cx, Editor::convert_to_kebab_case); register_action(view, cx, Editor::convert_to_upper_camel_case); register_action(view, cx, Editor::convert_to_lower_camel_case); + register_action(view, cx, Editor::convert_to_opposite_case); register_action(view, cx, Editor::delete_to_previous_word_start); register_action(view, cx, Editor::delete_to_previous_subword_start); register_action(view, cx, Editor::delete_to_next_word_end);