Add | as a bracket and a motion

Although vim/nvim doesn't have | as brackets, it's common in langauges like Rust
and Ruby, and I expect it to work.
This commit is contained in:
Conrad Irwin 2023-10-22 22:04:55 -06:00
parent 9589f5573d
commit 3cf98c4fae
4 changed files with 119 additions and 181 deletions

View file

@ -40,6 +40,7 @@ pub enum Motion {
NextLineStart,
StartOfLineDownward,
EndOfLineDownward,
GoToColumn,
}
#[derive(Clone, Deserialize, PartialEq)]
@ -119,6 +120,7 @@ actions!(
NextLineStart,
StartOfLineDownward,
EndOfLineDownward,
GoToColumn,
]
);
impl_actions!(
@ -215,6 +217,7 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(|_: &mut Workspace, &EndOfLineDownward, cx: _| {
motion(Motion::EndOfLineDownward, cx)
});
cx.add_action(|_: &mut Workspace, &GoToColumn, cx: _| motion(Motion::GoToColumn, cx));
cx.add_action(|_: &mut Workspace, action: &RepeatFind, cx: _| {
repeat_motion(action.backwards, cx)
})
@ -292,6 +295,7 @@ impl Motion {
| Right
| StartOfLine { .. }
| EndOfLineDownward
| GoToColumn
| NextWordStart { .. }
| PreviousWordStart { .. }
| FirstNonWhitespace { .. }
@ -317,6 +321,7 @@ impl Motion {
| EndOfParagraph
| StartOfLineDownward
| EndOfLineDownward
| GoToColumn
| NextWordStart { .. }
| PreviousWordStart { .. }
| FirstNonWhitespace { .. }
@ -346,6 +351,7 @@ impl Motion {
| StartOfLineDownward
| StartOfParagraph
| EndOfParagraph
| GoToColumn
| NextWordStart { .. }
| PreviousWordStart { .. }
| FirstNonWhitespace { .. }
@ -429,6 +435,7 @@ impl Motion {
NextLineStart => (next_line_start(map, point, times), SelectionGoal::None),
StartOfLineDownward => (next_line_start(map, point, times - 1), SelectionGoal::None),
EndOfLineDownward => (next_line_end(map, point, times), SelectionGoal::None),
GoToColumn => (go_to_column(map, point, times), SelectionGoal::None),
};
(new_point != point || infallible).then_some((new_point, goal))
@ -919,6 +926,11 @@ fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) ->
first_non_whitespace(map, false, correct_line)
}
fn go_to_column(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
let correct_line = start_of_relative_buffer_row(map, point, 0);
right(map, correct_line, times.saturating_sub(1))
}
pub(crate) fn next_line_end(
map: &DisplaySnapshot,
mut point: DisplayPoint,

View file

@ -20,6 +20,7 @@ pub enum Object {
Quotes,
BackQuotes,
DoubleQuotes,
VerticalBars,
Parentheses,
SquareBrackets,
CurlyBrackets,
@ -40,6 +41,7 @@ actions!(
Quotes,
BackQuotes,
DoubleQuotes,
VerticalBars,
Parentheses,
SquareBrackets,
CurlyBrackets,
@ -64,6 +66,7 @@ pub fn init(cx: &mut AppContext) {
});
cx.add_action(|_: &mut Workspace, _: &CurlyBrackets, cx: _| object(Object::CurlyBrackets, cx));
cx.add_action(|_: &mut Workspace, _: &AngleBrackets, cx: _| object(Object::AngleBrackets, cx));
cx.add_action(|_: &mut Workspace, _: &VerticalBars, cx: _| object(Object::VerticalBars, cx));
}
fn object(object: Object, cx: &mut WindowContext) {
@ -79,9 +82,11 @@ fn object(object: Object, cx: &mut WindowContext) {
impl Object {
pub fn is_multiline(self) -> bool {
match self {
Object::Word { .. } | Object::Quotes | Object::BackQuotes | Object::DoubleQuotes => {
false
}
Object::Word { .. }
| Object::Quotes
| Object::BackQuotes
| Object::VerticalBars
| Object::DoubleQuotes => false,
Object::Sentence
| Object::Parentheses
| Object::AngleBrackets
@ -96,6 +101,7 @@ impl Object {
Object::Quotes
| Object::BackQuotes
| Object::DoubleQuotes
| Object::VerticalBars
| Object::Parentheses
| Object::SquareBrackets
| Object::CurlyBrackets
@ -111,6 +117,7 @@ impl Object {
| Object::Quotes
| Object::BackQuotes
| Object::DoubleQuotes
| Object::VerticalBars
| Object::Parentheses
| Object::SquareBrackets
| Object::CurlyBrackets
@ -142,6 +149,9 @@ impl Object {
Object::DoubleQuotes => {
surrounding_markers(map, relative_to, around, self.is_multiline(), '"', '"')
}
Object::VerticalBars => {
surrounding_markers(map, relative_to, around, self.is_multiline(), '|', '|')
}
Object::Parentheses => {
surrounding_markers(map, relative_to, around, self.is_multiline(), '(', ')')
}
@ -568,7 +578,10 @@ fn surrounding_markers(
mod test {
use indoc::indoc;
use crate::test::{ExemptionFeatures, NeovimBackedTestContext};
use crate::{
state::Mode,
test::{ExemptionFeatures, NeovimBackedTestContext, VimTestContext},
};
const WORD_LOCATIONS: &'static str = indoc! {"
The quick ˇbrowˇnˇ
@ -940,6 +953,47 @@ mod test {
.await;
}
#[gpui::test]
async fn test_vertical_bars(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.set_state(
indoc! {"
fn boop() {
baz(ˇ|a, b| { bar(|j, k| { })})
}"
},
Mode::Normal,
);
cx.simulate_keystrokes(["c", "i", "|"]);
cx.assert_state(
indoc! {"
fn boop() {
baz(|ˇ| { bar(|j, k| { })})
}"
},
Mode::Insert,
);
cx.simulate_keystrokes(["escape", "1", "8", "|"]);
cx.assert_state(
indoc! {"
fn boop() {
baz(|| { bar(ˇ|j, k| { })})
}"
},
Mode::Normal,
);
cx.simulate_keystrokes(["v", "a", "|"]);
cx.assert_state(
indoc! {"
fn boop() {
baz(|| { bar(«|j, k| ˇ»{ })})
}"
},
Mode::Visual,
);
}
#[gpui::test]
async fn test_delete_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;

View file

@ -8,7 +8,7 @@
{"Key":"i"}
{"Key":"{"}
{"Get":{"state":"func empty(a string) bool {\n if a == \"\" {\n« return true\nˇ» }\n return false\n}","mode":"Visual"}}
{"Put":{"state":"func empty(a string) bool {\n if a == \"\" {\n ˇreturn true\n }\n return false\n}"}}
{"Put":{"state":"func empty(a string) bool {\n if a == \"\" ˇ{\n return true\n }\n return false\n}"}}
{"Key":"v"}
{"Key":"i"}
{"Key":"{"}