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: <img width="927" height="482" alt="image" src="https://github.com/user-attachments/assets/adf14a37-a92e-44df-8c0e-267b5c7677fb" /> 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`
This commit is contained in:
parent
5c9363b1c4
commit
913b9296d7
4 changed files with 41 additions and 12 deletions
|
@ -365,6 +365,8 @@ actions!(
|
||||||
ConvertToLowerCase,
|
ConvertToLowerCase,
|
||||||
/// Toggles the case of selected text.
|
/// Toggles the case of selected text.
|
||||||
ConvertToOppositeCase,
|
ConvertToOppositeCase,
|
||||||
|
/// Converts selected text to sentence case.
|
||||||
|
ConvertToSentenceCase,
|
||||||
/// Converts selected text to snake_case.
|
/// Converts selected text to snake_case.
|
||||||
ConvertToSnakeCase,
|
ConvertToSnakeCase,
|
||||||
/// Converts selected text to Title Case.
|
/// Converts selected text to Title Case.
|
||||||
|
|
|
@ -10878,17 +10878,6 @@ impl Editor {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_case(&mut self, _: &ToggleCase, window: &mut Window, cx: &mut Context<Self>) {
|
|
||||||
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<Fn>(
|
fn manipulate_immutable_lines<Fn>(
|
||||||
&mut self,
|
&mut self,
|
||||||
window: &mut Window,
|
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>,
|
||||||
|
) {
|
||||||
|
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>) {
|
||||||
|
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(
|
pub fn convert_to_rot13(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &ConvertToRot13,
|
_: &ConvertToRot13,
|
||||||
|
|
|
@ -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]
|
#[gpui::test]
|
||||||
async fn test_manipulate_text(cx: &mut TestAppContext) {
|
async fn test_manipulate_text(cx: &mut TestAppContext) {
|
||||||
init_test(cx, |_| {});
|
init_test(cx, |_| {});
|
||||||
|
|
|
@ -230,7 +230,6 @@ impl EditorElement {
|
||||||
register_action(editor, window, Editor::sort_lines_case_insensitive);
|
register_action(editor, window, Editor::sort_lines_case_insensitive);
|
||||||
register_action(editor, window, Editor::reverse_lines);
|
register_action(editor, window, Editor::reverse_lines);
|
||||||
register_action(editor, window, Editor::shuffle_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_spaces);
|
||||||
register_action(editor, window, Editor::convert_indentation_to_tabs);
|
register_action(editor, window, Editor::convert_indentation_to_tabs);
|
||||||
register_action(editor, window, Editor::convert_to_upper_case);
|
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_upper_camel_case);
|
||||||
register_action(editor, window, Editor::convert_to_lower_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_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_rot13);
|
||||||
register_action(editor, window, Editor::convert_to_rot47);
|
register_action(editor, window, Editor::convert_to_rot47);
|
||||||
register_action(editor, window, Editor::delete_to_previous_word_start);
|
register_action(editor, window, Editor::delete_to_previous_word_start);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue