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

fix(commands): extend_line to proper line when count and current line selected #5288

Merged
merged 3 commits into from
Dec 31, 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
19 changes: 8 additions & 11 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,16 +2053,10 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
let selection = doc.selection(view.id).clone().transform(|range| {
let (start_line, end_line) = range.line_range(text.slice(..));

let start = text.line_to_char(match extend {
Extend::Above => start_line.saturating_sub(count - 1),
Extend::Below => start_line,
});
let start = text.line_to_char(start_line);
let end = text.line_to_char(
match extend {
Extend::Above => end_line + 1, // the start of next line
Extend::Below => end_line + count,
}
.min(text.len_lines()),
(end_line + 1) // newline of end_line
.min(text.len_lines()),
);

// extend to previous/next line if current line is selected
Expand All @@ -2076,8 +2070,11 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
}
} else {
match extend {
Extend::Above => (end, start),
Extend::Below => (start, end),
Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count - 1))),
Extend::Below => (
start,
text.line_to_char((end_line + count).min(text.len_lines())),
),
}
};

Expand Down
43 changes: 43 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,46 @@ async fn test_undo_redo() -> anyhow::Result<()> {

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_extend_line() -> anyhow::Result<()> {
// extend with line selected then count
test((
platform_line(indoc! {"\
#[l|]#orem
ipsum
dolor
"})
.as_str(),
"x2x",
platform_line(indoc! {"\
#[lorem
ipsum
dolor
|]#
"})
.as_str(),
))
.await?;

// extend with count on partial selection
test((
platform_line(indoc! {"\
#[l|]#orem
ipsum
"})
.as_str(),
"2x",
platform_line(indoc! {"\
#[lorem
ipsum
|]#
"})
.as_str(),
))
.await?;

Ok(())
}