Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paragraph motion and textobject #1627

Merged
merged 7 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaire
| `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` |
| `]o` | Go to next comment (**TS**) | `goto_next_comment` |
| `[o` | Go to previous comment (**TS**) | `goto_prev_comment` |
| `]p` | Go to next paragraph | `goto_next_paragraph` |
| `[p` | Go to previous paragraph | `goto_prev_paragraph` |
| `[space` | Add newline above | `add_newline_above` |
| `]space` | Add newline below | `add_newline_below` |

Expand Down
1 change: 1 addition & 0 deletions helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod shellwords;
mod state;
pub mod surround;
pub mod syntax;
pub mod test;
pub mod textobject;
mod transaction;

Expand Down
5 changes: 5 additions & 0 deletions helix-core/src/line_ending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ pub fn str_is_line_ending(s: &str) -> bool {
LineEnding::from_str(s).is_some()
}

#[inline]
pub fn rope_is_line_ending(r: RopeSlice) -> bool {
r.chunks().all(str_is_line_ending)
}

/// Attempts to detect what line ending the passed document uses.
pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
// Return first matched line ending. Not all possible line endings
Expand Down
242 changes: 242 additions & 0 deletions helix-core/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary,
prev_grapheme_boundary,
},
line_ending::rope_is_line_ending,
pos_at_coords,
syntax::LanguageConfiguration,
textobject::TextObject,
Expand Down Expand Up @@ -149,6 +150,87 @@ fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTar
})
}

pub fn move_prev_paragraph(
slice: RopeSlice,
range: Range,
count: usize,
behavior: Movement,
) -> Range {
let mut line = range.cursor_line(slice);
let first_char = slice.line_to_char(line) == range.cursor(slice);
let prev_line_empty = rope_is_line_ending(slice.line(line.saturating_sub(1)));
let curr_line_empty = rope_is_line_ending(slice.line(line));
let prev_empty_to_line = prev_line_empty && !curr_line_empty;

// skip character before paragraph boundary
if prev_empty_to_line && !first_char {
line += 1;
}
let mut lines = slice.lines_at(line);
lines.reverse();
let mut lines = lines.map(rope_is_line_ending).peekable();
for _ in 0..count {
while lines.next_if(|&e| e).is_some() {
line -= 1;
}
while lines.next_if(|&e| !e).is_some() {
line -= 1;
}
}

let head = slice.line_to_char(line);
let anchor = if behavior == Movement::Move {
// exclude first character after paragraph boundary
if prev_empty_to_line && first_char {
range.cursor(slice)
} else {
range.head
}
} else {
range.put_cursor(slice, head, true).anchor
};
Range::new(anchor, head)
}

pub fn move_next_paragraph(
slice: RopeSlice,
range: Range,
count: usize,
behavior: Movement,
) -> Range {
let mut line = range.cursor_line(slice);
let last_char =
prev_grapheme_boundary(slice, slice.line_to_char(line + 1)) == range.cursor(slice);
let curr_line_empty = rope_is_line_ending(slice.line(line));
let next_line_empty = rope_is_line_ending(slice.line(line.saturating_sub(1)));
let curr_empty_to_line = curr_line_empty && !next_line_empty;

// skip character after paragraph boundary
if curr_empty_to_line && last_char {
line += 1;
}
let mut lines = slice.lines_at(line).map(rope_is_line_ending).peekable();
for _ in 0..count {
while lines.next_if(|&e| !e).is_some() {
line += 1;
}
while lines.next_if(|&e| e).is_some() {
line += 1;
}
}
let head = slice.line_to_char(line);
let anchor = if behavior == Movement::Move {
if curr_empty_to_line && last_char {
range.head
} else {
range.cursor(slice)
}
} else {
range.put_cursor(slice, head, true).anchor
};
Range::new(anchor, head)
}

// ---- util ------------

#[inline]
Expand Down Expand Up @@ -1179,4 +1261,164 @@ mod test {
}
}
}

#[test]
fn test_behaviour_when_moving_to_prev_paragraph_single() {
let tests = [
("#[|]#", "#[|]#"),
("#[s|]#tart at\nfirst char\n", "#[|s]#tart at\nfirst char\n"),
("start at\nlast char#[\n|]#", "#[|start at\nlast char\n]#"),
(
"goto\nfirst\n\n#[p|]#aragraph",
"#[|goto\nfirst\n\n]#paragraph",
),
(
"goto\nfirst\n#[\n|]#paragraph",
"#[|goto\nfirst\n\n]#paragraph",
),
(
"goto\nsecond\n\np#[a|]#ragraph",
"goto\nsecond\n\n#[|pa]#ragraph",
),
(
"here\n\nhave\nmultiple\nparagraph\n\n\n\n\n#[|]#",
"here\n\n#[|have\nmultiple\nparagraph\n\n\n\n\n]#",
),
];

for (before, expected) in tests {
let (s, selection) = crate::test::print(before);
let text = Rope::from(s.as_str());
let selection =
selection.transform(|r| move_prev_paragraph(text.slice(..), r, 1, Movement::Move));
let actual = crate::test::plain(&s, selection);
assert_eq!(actual, expected, "\nbefore: `{before:?}`");
}
}

#[test]
fn test_behaviour_when_moving_to_prev_paragraph_double() {
let tests = [
(
"on#[e|]#\n\ntwo\n\nthree\n\n",
"#[|one]#\n\ntwo\n\nthree\n\n",
),
(
"one\n\ntwo\n\nth#[r|]#ee\n\n",
"one\n\n#[|two\n\nthr]#ee\n\n",
),
];

for (before, expected) in tests {
let (s, selection) = crate::test::print(before);
let text = Rope::from(s.as_str());
let selection =
selection.transform(|r| move_prev_paragraph(text.slice(..), r, 2, Movement::Move));
let actual = crate::test::plain(&s, selection);
assert_eq!(actual, expected, "\nbefore: `{before:?}`");
}
}

#[test]
fn test_behaviour_when_moving_to_prev_paragraph_extend() {
let tests = [
(
"one\n\n#[|two\n\n]#three\n\n",
"#[|one\n\ntwo\n\n]#three\n\n",
),
(
"#[|one\n\ntwo\n\n]#three\n\n",
"#[|one\n\ntwo\n\n]#three\n\n",
),
];

for (before, expected) in tests {
let (s, selection) = crate::test::print(before);
let text = Rope::from(s.as_str());
let selection = selection
.transform(|r| move_prev_paragraph(text.slice(..), r, 1, Movement::Extend));
let actual = crate::test::plain(&s, selection);
assert_eq!(actual, expected, "\nbefore: `{before:?}`");
}
}

#[test]
fn test_behaviour_when_moving_to_next_paragraph_single() {
let tests = [
("#[|]#", "#[|]#"),
("#[s|]#tart at\nfirst char\n", "#[start at\nfirst char\n|]#"),
("start at\nlast char#[\n|]#", "start at\nlast char#[\n|]#"),
(
"a\nb\n\n#[g|]#oto\nthird\n\nparagraph",
"a\nb\n\n#[goto\nthird\n\n|]#paragraph",
),
(
"a\nb\n#[\n|]#goto\nthird\n\nparagraph",
"a\nb\n\n#[goto\nthird\n\n|]#paragraph",
),
(
"a\nb#[\n|]#\ngoto\nsecond\n\nparagraph",
"a\nb#[\n\n|]#goto\nsecond\n\nparagraph",
),
(
"here\n\nhave\n#[m|]#ultiple\nparagraph\n\n\n\n\n",
"here\n\nhave\n#[multiple\nparagraph\n\n\n\n\n|]#",
),
];

for (before, expected) in tests {
let (s, selection) = crate::test::print(before);
let text = Rope::from(s.as_str());
let selection =
selection.transform(|r| move_next_paragraph(text.slice(..), r, 1, Movement::Move));
let actual = crate::test::plain(&s, selection);
assert_eq!(actual, expected, "\nbefore: `{before:?}`");
}
}

#[test]
fn test_behaviour_when_moving_to_next_paragraph_double() {
let tests = [
(
"one\n\ntwo\n\nth#[r|]#ee\n\n",
"one\n\ntwo\n\nth#[ree\n\n|]#",
),
(
"on#[e|]#\n\ntwo\n\nthree\n\n",
"on#[e\n\ntwo\n\n|]#three\n\n",
),
];

for (before, expected) in tests {
let (s, selection) = crate::test::print(before);
let text = Rope::from(s.as_str());
let selection =
selection.transform(|r| move_next_paragraph(text.slice(..), r, 2, Movement::Move));
let actual = crate::test::plain(&s, selection);
assert_eq!(actual, expected, "\nbefore: `{before:?}`");
}
}

#[test]
fn test_behaviour_when_moving_to_next_paragraph_extend() {
let tests = [
(
"one\n\n#[two\n\n|]#three\n\n",
"one\n\n#[two\n\nthree\n\n|]#",
),
(
"one\n\n#[two\n\nthree\n\n|]#",
"one\n\n#[two\n\nthree\n\n|]#",
),
];

for (before, expected) in tests {
let (s, selection) = crate::test::print(before);
let text = Rope::from(s.as_str());
let selection = selection
.transform(|r| move_next_paragraph(text.slice(..), r, 1, Movement::Extend));
let actual = crate::test::plain(&s, selection);
assert_eq!(actual, expected, "\nbefore: `{before:?}`");
}
}
}
Loading