Skip to content

Commit

Permalink
Fix the infinite loop when copying the cursor to the top of the file (#…
Browse files Browse the repository at this point in the history
…5888)

Example:
```
test
testitem
```

Select line 2 with x, then type Alt-C; Helix will go into an infinite
loop. The saturating_sub keeps the head_row and anchor_row pinned at 0,
and a selection is never made since the first line is too short.
  • Loading branch information
trink authored Feb 9, 2023
1 parent 7ebcf4e commit 9d73a0d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,10 @@ fn copy_selection_on_line(cx: &mut Context, direction: Direction) {
sels += 1;
}

if anchor_row == 0 && head_row == 0 {
break;
}

i += 1;
}
}
Expand Down
64 changes: 64 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,70 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
.as_str(),
))
.await?;

// Copy the selection to previous line, skipping the first line in the file
test((
platform_line(indoc! {"\
test
#[testitem|]#
"})
.as_str(),
"<A-C>",
platform_line(indoc! {"\
test
#[testitem|]#
"})
.as_str(),
))
.await?;

// Copy the selection to previous line, including the first line in the file
test((
platform_line(indoc! {"\
test
#[test|]#
"})
.as_str(),
"<A-C>",
platform_line(indoc! {"\
#[test|]#
#(test|)#
"})
.as_str(),
))
.await?;

// Copy the selection to next line, skipping the last line in the file
test((
platform_line(indoc! {"\
#[testitem|]#
test
"})
.as_str(),
"C",
platform_line(indoc! {"\
#[testitem|]#
test
"})
.as_str(),
))
.await?;

// Copy the selection to next line, including the last line in the file
test((
platform_line(indoc! {"\
#[test|]#
test
"})
.as_str(),
"C",
platform_line(indoc! {"\
#(test|)#
#[test|]#
"})
.as_str(),
))
.await?;
Ok(())
}

Expand Down

0 comments on commit 9d73a0d

Please sign in to comment.