From 690ca8491d64045acd62d67d37c74ceea9efedce Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 7 Apr 2022 00:47:33 +0800 Subject: [PATCH 1/2] Make textobject select last paragraph Last paragraph shoud be selected if the cursor was placed on the whitespace paragraph part and `map` is done, otherwise it would do nothing useful, but now we select backwards for the last paragraph which behaves similarly to kakoune, making `map` useful for the last paragraph with whitespace. Example usecase is to copy and paste last ledger cli paragraph quickly by `mapyp` to duplicate last entry. --- helix-core/src/textobject.rs | 39 +++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs index 67bf40a65a98..4d592f2ba0a2 100644 --- a/helix-core/src/textobject.rs +++ b/helix-core/src/textobject.rs @@ -132,9 +132,9 @@ pub fn textobject_paragraph( if prev_empty_to_line || curr_empty_to_line { line_back += 1; } - let mut lines = slice.lines_at(line_back); // do not include current paragraph on paragraph end (include next) if !(curr_empty_to_line && last_char) { + let mut lines = slice.lines_at(line_back); lines.reverse(); let mut lines = lines.map(rope_is_line_ending).peekable(); while lines.next_if(|&e| e).is_some() { @@ -150,25 +150,46 @@ pub fn textobject_paragraph( line += 1; } let mut lines = slice.lines_at(line).map(rope_is_line_ending).peekable(); - for _ in 0..count - 1 { + let mut count_done = 0; // count how many non-whitespace paragraphs done + for _ in 0..count { + let mut done = false; while lines.next_if(|&e| !e).is_some() { line += 1; + done = true; } while lines.next_if(|&e| e).is_some() { line += 1; } + count_done += done as usize; } - while lines.next_if(|&e| !e).is_some() { - line += 1; + + // search one paragraph backwards backwards for last paragraph + // makes `map` at the end of the paragraph with trailing newlines useful + let last_paragraph = count_done != count && lines.peek().is_none(); + if last_paragraph { + let mut lines = slice.lines_at(line_back); + lines.reverse(); + let mut lines = lines.map(rope_is_line_ending).peekable(); + while lines.next_if(|&e| e).is_some() { + line_back -= 1; + } + while lines.next_if(|&e| !e).is_some() { + line_back -= 1; + } } + // handle last whitespaces part separately depending on textobject match textobject { - TextObject::Around => { + TextObject::Around => {} + TextObject::Inside => { + // remove last whitespace paragraph + let mut lines = slice.lines_at(line); + lines.reverse(); + let mut lines = lines.map(rope_is_line_ending).peekable(); while lines.next_if(|&e| e).is_some() { - line += 1; + line -= 1; } } - TextObject::Inside => {} TextObject::Movement => unreachable!(), } @@ -364,7 +385,7 @@ mod test { "second\n\n#[paragraph\n|]#\n", ), ("#[f|]#irst char\n\n", "#[first char\n|]#\n"), - ("last char\n#[\n|]#", "last char\n\n#[|]#"), + ("last char\n#[\n|]#", "#[last char\n|]#\n"), ( "empty to line\n#[\n|]#paragraph boundary\n\n", "empty to line\n\n#[paragraph boundary\n|]#\n", @@ -418,7 +439,7 @@ mod test { "second\n\n#[paragraph\n\n|]#", ), ("#[f|]#irst char\n\n", "#[first char\n\n|]#"), - ("last char\n#[\n|]#", "last char\n\n#[|]#"), + ("last char\n#[\n|]#", "#[last char\n\n|]#"), ( "empty to line\n#[\n|]#paragraph boundary\n\n", "empty to line\n\n#[paragraph boundary\n\n|]#", From 0d66683f2b25cfcaeb992badbd3146700da416d9 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 13 Apr 2022 00:05:34 +0800 Subject: [PATCH 2/2] Fix typo in core textobject Co-authored-by: Michael Davis --- helix-core/src/textobject.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs index 4d592f2ba0a2..adf5426242e3 100644 --- a/helix-core/src/textobject.rs +++ b/helix-core/src/textobject.rs @@ -163,7 +163,7 @@ pub fn textobject_paragraph( count_done += done as usize; } - // search one paragraph backwards backwards for last paragraph + // search one paragraph backwards for last paragraph // makes `map` at the end of the paragraph with trailing newlines useful let last_paragraph = count_done != count && lines.peek().is_none(); if last_paragraph {