From c307b7574bf5ae94e531b744d0ac4cd353c0df42 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Thu, 24 Jul 2025 04:49:04 -0400 Subject: [PATCH] Add `editor: convert to sentence case` (#35015) This PR adds an `editor: convert to sentence case` action. I frequently find myself copying branch names and then removing the hyphens and ensuring the first letter is capitalized, and then using the result text for the commit message. For example: image You can achieve this with a combination of other text manipulation commands, but this action makes it even easier. Also, moved `toggle_case` down into the area where all other commands internally using `manipulate_text` are located. Release Notes: - Added `editor: convert to sentence case` --- crates/editor/src/actions.rs | 2 ++ crates/editor/src/editor.rs | 31 ++++++++++++++++++++----------- crates/editor/src/editor_tests.rs | 17 +++++++++++++++++ crates/editor/src/element.rs | 3 ++- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index f80a6afbbb..1212651cb3 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -365,6 +365,8 @@ actions!( ConvertToLowerCase, /// Toggles the case of selected text. ConvertToOppositeCase, + /// Converts selected text to sentence case. + ConvertToSentenceCase, /// Converts selected text to snake_case. ConvertToSnakeCase, /// Converts selected text to Title Case. diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 069d8cffb3..8f57fb1a20 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -10878,17 +10878,6 @@ impl Editor { }); } - pub fn toggle_case(&mut self, _: &ToggleCase, window: &mut Window, cx: &mut Context) { - self.manipulate_text(window, cx, |text| { - let has_upper_case_characters = text.chars().any(|c| c.is_uppercase()); - if has_upper_case_characters { - text.to_lowercase() - } else { - text.to_uppercase() - } - }) - } - fn manipulate_immutable_lines( &mut self, window: &mut Window, @@ -11144,6 +11133,26 @@ impl Editor { }) } + pub fn convert_to_sentence_case( + &mut self, + _: &ConvertToSentenceCase, + window: &mut Window, + cx: &mut Context, + ) { + self.manipulate_text(window, cx, |text| text.to_case(Case::Sentence)) + } + + pub fn toggle_case(&mut self, _: &ToggleCase, window: &mut Window, cx: &mut Context) { + self.manipulate_text(window, cx, |text| { + let has_upper_case_characters = text.chars().any(|c| c.is_uppercase()); + if has_upper_case_characters { + text.to_lowercase() + } else { + text.to_uppercase() + } + }) + } + pub fn convert_to_rot13( &mut self, _: &ConvertToRot13, diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 8d121972d0..03b047e92e 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -4724,6 +4724,23 @@ async fn test_toggle_case(cx: &mut TestAppContext) { "}); } +#[gpui::test] +async fn test_convert_to_sentence_case(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + + cx.set_state(indoc! {" + «implement-windows-supportˇ» + "}); + cx.update_editor(|e, window, cx| { + e.convert_to_sentence_case(&ConvertToSentenceCase, window, cx) + }); + cx.assert_editor_state(indoc! {" + «Implement windows supportˇ» + "}); +} + #[gpui::test] async fn test_manipulate_text(cx: &mut TestAppContext) { init_test(cx, |_| {}); diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index d2ee9d6b0a..5fd6b028f4 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -230,7 +230,6 @@ impl EditorElement { register_action(editor, window, Editor::sort_lines_case_insensitive); register_action(editor, window, Editor::reverse_lines); register_action(editor, window, Editor::shuffle_lines); - register_action(editor, window, Editor::toggle_case); register_action(editor, window, Editor::convert_indentation_to_spaces); register_action(editor, window, Editor::convert_indentation_to_tabs); register_action(editor, window, Editor::convert_to_upper_case); @@ -241,6 +240,8 @@ impl EditorElement { register_action(editor, window, Editor::convert_to_upper_camel_case); register_action(editor, window, Editor::convert_to_lower_camel_case); register_action(editor, window, Editor::convert_to_opposite_case); + register_action(editor, window, Editor::convert_to_sentence_case); + register_action(editor, window, Editor::toggle_case); register_action(editor, window, Editor::convert_to_rot13); register_action(editor, window, Editor::convert_to_rot47); register_action(editor, window, Editor::delete_to_previous_word_start);