Skip to content

Commit 418ec4b

Browse files
authored
Merge pull request #586 from OpenCoven/fix/570-kill-line-gE
fix(tui): kill_line stops at a CRLF's CR; gE stops at the start
2 parents 43419e7 + a19cc6d commit 418ec4b

1 file changed

Lines changed: 77 additions & 6 deletions

File tree

‎src-rust/crates/tui/src/prompt_input.rs‎

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,10 @@ fn vim_g(
11101110
while i > 0 && chars[i - 1].is_whitespace() {
11111111
i -= 1;
11121112
}
1113+
if i == 0 {
1114+
*cursor = 0;
1115+
break;
1116+
}
11131117
while i > 1 && !chars[i - 2].is_whitespace() {
11141118
i -= 1;
11151119
}
@@ -2208,13 +2212,19 @@ impl PromptInputState {
22082212
if self.mode == InputMode::Readonly {
22092213
return;
22102214
}
2211-
let line_end = self.text[self.cursor..]
2212-
.find('\n')
2213-
.map(|p| self.cursor + p)
2214-
.unwrap_or(self.text.len());
2215+
// The line ends at its LF, or at the CR of a CRLF, which belongs to
2216+
// the line break. A CR not followed by an LF is line content.
2217+
let rest = &self.text[self.cursor..];
2218+
let line_len = match rest.find('\n') {
2219+
Some(lf) => rest[..lf].strip_suffix('\r').map_or(lf, str::len),
2220+
None => rest.len(),
2221+
};
22152222

2216-
if line_end > self.cursor {
2217-
let killed = self.text.drain(self.cursor..line_end).collect::<String>();
2223+
if line_len > 0 {
2224+
let killed = self
2225+
.text
2226+
.drain(self.cursor..self.cursor + line_len)
2227+
.collect::<String>();
22182228
self.kill_ring.push(killed);
22192229
self.update_token_estimate();
22202230
}
@@ -4232,6 +4242,67 @@ mod tests {
42324242
assert!(!s.is_empty());
42334243
}
42344244

4245+
/// A CRLF line ends at its CR (#570). At the end of such a line there
4246+
/// is nothing to cut; from inside it the cut leaves the CRLF whole.
4247+
#[test]
4248+
fn kill_line_stops_before_the_cr_of_a_crlf_line() {
4249+
let mut s = composer("a\r\nb", 1);
4250+
s.kill_line();
4251+
assert_eq!(s.text, "a\r\nb");
4252+
assert!(s.kill_ring.entries.is_empty());
4253+
4254+
let mut s = composer("abc\r\nd", 1);
4255+
s.kill_line();
4256+
assert_eq!(s.text, "a\r\nd");
4257+
assert_eq!(s.kill_ring.entries, ["bc"]);
4258+
4259+
// A line ending in a bare LF is cut up to the LF, as before.
4260+
let mut s = composer("abc\nd", 1);
4261+
s.kill_line();
4262+
assert_eq!(s.text, "a\nd");
4263+
assert_eq!(s.kill_ring.entries, ["bc"]);
4264+
4265+
// A cursor between the CR and the LF cuts nothing.
4266+
let mut s = composer("a\r\nb", 2);
4267+
s.kill_line();
4268+
assert_eq!(s.text, "a\r\nb");
4269+
assert!(s.kill_ring.entries.is_empty());
4270+
4271+
// A CR with no LF after it is line content, as it is drawn.
4272+
let mut s = composer("ab\r", 0);
4273+
s.kill_line();
4274+
assert_eq!(s.text, "");
4275+
assert_eq!(s.kill_ring.entries, ["ab\r"]);
4276+
4277+
// Only the CR right before the LF belongs to the line break.
4278+
let mut s = composer("a\r\r\nb", 1);
4279+
s.kill_line();
4280+
assert_eq!(s.text, "a\r\nb");
4281+
assert_eq!(s.kill_ring.entries, ["\r"]);
4282+
}
4283+
4284+
/// `gE` with only whitespace before the cursor goes to the start of the
4285+
/// text (#570), as `ge` does. It used to subtract past zero.
4286+
#[test]
4287+
fn g_upper_e_after_only_whitespace_moves_to_the_start() {
4288+
let mut s = vim_composer(" a", 2);
4289+
s.vim_command("g");
4290+
s.vim_command("E");
4291+
assert_eq!(s.cursor, 0);
4292+
4293+
let mut s = vim_composer(" a", 2);
4294+
s.vim_command("g");
4295+
s.vim_command("e");
4296+
assert_eq!(s.cursor, 0);
4297+
4298+
// With a count, the step that runs out of words stops at the start.
4299+
let mut s = vim_composer(" a b", 4);
4300+
for key in ["2", "g", "E"] {
4301+
s.vim_command(key);
4302+
}
4303+
assert_eq!(s.cursor, 0);
4304+
}
4305+
42354306
// ---- handle_paste ---------------------------------------------------
42364307

42374308
#[test]

0 commit comments

Comments
 (0)