Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Add action to sort lines by length (#33622)
Alex Povel8cc3b094d23a
diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rsindex def2a616a8a..70ec8ea00f5 100644--- a/crates/editor/src/actions.rs+++ b/crates/editor/src/actions.rs@@ -635,6 +635,8 @@ actions!(SignatureHelpNext,/// Navigates to the previous signature in the signature help popup.SignatureHelpPrevious,+ /// Sorts selected lines by length.+ SortLinesByLength,/// Sorts selected lines case-insensitively.SortLinesCaseInsensitive,/// Sorts selected lines case-sensitively.diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 47223aa59a8..6d529287a77 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -10204,6 +10204,17 @@ impl Editor {self.manipulate_immutable_lines(window, cx, |lines| lines.sort())}+ pub fn sort_lines_by_length(+ &mut self,+ _: &SortLinesByLength,+ window: &mut Window,+ cx: &mut Context<Self>,+ ) {+ self.manipulate_immutable_lines(window, cx, |lines| {+ lines.sort_by_key(|&line| line.chars().count())+ })+ }+pub fn sort_lines_case_insensitive(&mut self,_: &SortLinesCaseInsensitive,diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex ade9a9322bc..05280de02b6 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -4075,6 +4075,29 @@ async fn test_manipulate_immutable_lines_with_single_selection(cx: &mut TestAppCZˇ»"});+ // Test sort_lines_by_length()+ //+ // Demonstrates:+ // - ∞ is 3 bytes UTF-8, but sorted by its char count (1)+ // - sort is stable+ cx.set_state(indoc! {"+ «123+ æ+ 12+ ∞+ 1+ æˇ»+ "});+ cx.update_editor(|e, window, cx| e.sort_lines_by_length(&SortLinesByLength, window, cx));+ cx.assert_editor_state(indoc! {"+ «æ+ ∞+ 1+ æ+ 12+ 123ˇ»+ "});+// Test reverse_lines()cx.set_state(indoc! {"«5diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 3fa8697c193..49f4fc52ac7 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -225,6 +225,7 @@ impl EditorElement {register_action(editor, window, Editor::autoindent);register_action(editor, window, Editor::delete_line);register_action(editor, window, Editor::join_lines);+ register_action(editor, window, Editor::sort_lines_by_length);register_action(editor, window, Editor::sort_lines_case_sensitive);register_action(editor, window, Editor::sort_lines_case_insensitive);register_action(editor, window, Editor::reverse_lines);