Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Add SelectInsideDelimiters and SelectAroundDelimiters actions (#53789)

subeaxf2fbe40b5daf
diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs
index 906f4b00f26..830333706e1 100644
--- a/crates/editor/src/actions.rs
+++ b/crates/editor/src/actions.rs
@@ -654,6 +654,12 @@ actions!(
MoveToBeginning,
/// Moves cursor to the enclosing bracket.
MoveToEnclosingBracket,
+ /// Selects the content inside the nearest enclosing delimiters
+ /// (brackets, braces, parentheses, or quotes).
+ SelectInsideDelimiters,
+ /// Selects the content inside the nearest surrounding delimiters,
+ /// including the delimiters themselves.
+ SelectAroundDelimiters,
/// Moves cursor to the end of the document.
MoveToEnd,
/// Moves cursor to the end of the paragraph.
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index e165c765f26..c45e298f5c8 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -7529,6 +7529,51 @@ impl Editor {
}
}
+ pub fn select_inside_delimiters(
+ &mut self,
+ _: &SelectInsideDelimiters,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.select_delimiters_impl(false, window, cx);
+ }
+
+ pub fn select_around_delimiters(
+ &mut self,
+ _: &SelectAroundDelimiters,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.select_delimiters_impl(true, window, cx);
+ }
+
+ fn select_delimiters_impl(
+ &mut self,
+ include_brackets: bool,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.change_selections(Default::default(), window, cx, |s| {
+ s.move_offsets_with(&mut |snapshot, selection| {
+ let Some((open, close)) = snapshot
+ .innermost_enclosing_bracket_ranges(selection.start..selection.end, None)
+ else {
+ return;
+ };
+
+ let (start, end) = if include_brackets {
+ (open.start, close.end)
+ } else {
+ (open.end, close.start)
+ };
+ selection.start = start;
+ selection.end = end;
+ selection.reversed = false;
+ selection.goal = SelectionGoal::None;
+ })
+ });
+ }
+
pub fn rename(
&mut self,
_: &Rename,
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 6a71776648e..1305a2ad4a0 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -38419,3 +38419,156 @@ async fn test_toggle_markdown_block_quote(cx: &mut TestAppContext) {
fourthˇ»
"});
}
+
+#[track_caller]
+fn assert_select_delimiters(around: bool, before: &str, after: &str, cx: &mut EditorTestContext) {
+ let _state_context = cx.set_state(before);
+ cx.run_until_parked();
+
+ cx.update_editor(|editor, window, cx| {
+ if around {
+ editor.select_around_delimiters(&SelectAroundDelimiters, window, cx);
+ } else {
+ editor.select_inside_delimiters(&SelectInsideDelimiters, window, cx);
+ }
+ });
+
+ cx.run_until_parked();
+ cx.assert_editor_state(after);
+}
+
+#[gpui::test]
+async fn test_select_delimiters(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorLspTestContext::new_typescript(Default::default(), cx).await;
+
+ // Inside.
+ assert_select_delimiters(false, "foo(ˇbar);", "foo(«barˇ»);", &mut cx);
+ assert_select_delimiters(false, "foo(a, ˇb, c);", "foo(«a, b, cˇ»);", &mut cx);
+ assert_select_delimiters(false, "foo([1, ˇ2, 3]);", "foo([«1, 2, 3ˇ»]);", &mut cx);
+ assert_select_delimiters(false, "let x = { aˇ: 1 };", "let x = {« a: 1 ˇ»};", &mut cx);
+ assert_select_delimiters(false, "let xˇ = 42;", "let xˇ = 42;", &mut cx);
+ assert_select_delimiters(false, "foo(a, «bˇ», c);", "foo(«a, b, cˇ»);", &mut cx);
+ assert_select_delimiters(
+ false,
+ "const s = \"hello ˇworld\";",
+ "const s = \"«hello worldˇ»\";",
+ &mut cx,
+ );
+
+ assert_select_delimiters(
+ false,
+ "const s = \"ˇhello world\";",
+ "const s = \"«hello worldˇ»\";",
+ &mut cx,
+ );
+
+ assert_select_delimiters(
+ false,
+ "const s = \"hello worldˇ\";",
+ "const s = \"«hello worldˇ»\";",
+ &mut cx,
+ );
+
+ assert_select_delimiters(
+ false,
+ "console.log(\"deˇbug\");",
+ "console.log(\"«debugˇ»\");",
+ &mut cx,
+ );
+
+ // Around.
+ assert_select_delimiters(true, "foo(ˇbar);", "foo«(bar)ˇ»;", &mut cx);
+ assert_select_delimiters(true, "foo([1, ˇ2, 3]);", "foo(«[1, 2, 3]ˇ»);", &mut cx);
+ assert_select_delimiters(true, "let x = {ˇ a: 1 };", "let x = «{ a: 1 }ˇ»;", &mut cx);
+ assert_select_delimiters(true, "let xˇ = 42;", "let xˇ = 42;", &mut cx);
+ assert_select_delimiters(
+ true,
+ "console.log(\"deˇbug\");",
+ "console.log(«\"debug\"ˇ»);",
+ &mut cx,
+ );
+}
+
+#[gpui::test]
+async fn test_select_delimiters_in_markdown(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+ cx.update_buffer(|buffer, cx| buffer.set_language(Some(markdown_lang()), cx));
+
+ // Inside.
+ assert_select_delimiters(
+ false,
+ r#"This is "ˇhello, world!"."#,
+ r#"This is "«hello, world!ˇ»"."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is "hello, ˇworld!"."#,
+ r#"This is "«hello, world!ˇ»"."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is "hello, world!ˇ"."#,
+ r#"This is "«hello, world!ˇ»"."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is ˇ"hello, world!"."#,
+ r#"This is "«hello, world!ˇ»"."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is "hello, world!"ˇ."#,
+ r#"This is "«hello, world!ˇ»"."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is 'hello, ˇworld!'."#,
+ r#"This is '«hello, world!ˇ»'."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is `hello, ˇworld!`."#,
+ r#"This is `«hello, world!ˇ»`."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is ("hello, ˇworld!")."#,
+ r#"This is ("«hello, world!ˇ»")."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ false,
+ r#"This is hello, ˇworld!."#,
+ r#"This is hello, ˇworld!."#,
+ &mut cx,
+ );
+
+ // Around.
+ assert_select_delimiters(
+ true,
+ r#"This is "hello, ˇworld!"."#,
+ r#"This is «"hello, world!"ˇ»."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ true,
+ r#"This is 'hello, ˇworld!'."#,
+ r#"This is «'hello, world!'ˇ»."#,
+ &mut cx,
+ );
+ assert_select_delimiters(
+ true,
+ r#"This is `hello, ˇworld!`."#,
+ r#"This is «`hello, world!`ˇ»."#,
+ &mut cx,
+ );
+}
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index c557603b710..7cfd1b9ce72 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -337,6 +337,8 @@ impl EditorElement {
register_action(editor, window, Editor::select_enclosing_symbol);
register_action(editor, window, Editor::select_inside_enclosing_bracket);
register_action(editor, window, Editor::move_to_enclosing_bracket);
+ register_action(editor, window, Editor::select_inside_delimiters);
+ register_action(editor, window, Editor::select_around_delimiters);
register_action(editor, window, Editor::undo_selection);
register_action(editor, window, Editor::redo_selection);
if editor.read(cx).buffer_kind(cx) == ItemBufferKind::Multibuffer {