Skip to content
Merged
6 changes: 6 additions & 0 deletions crates/editor/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
153 changes: 153 additions & 0 deletions crates/editor/src/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
2 changes: 2 additions & 0 deletions crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,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 {
Expand Down
Loading