on master help sign in
fix(commands): extend_line to proper line when count and current line selected (#5288)
commit1b1755240db1ca01cbe1371a5b4ac58b68615382 authorGabriel Dinner-David <82682503+gabydd@users.noreply.github.com> dateSat 31 Dec 2022, 07:23 (4 years ago) parentc9ed42c
2 files changed+51−11
helix-term/src/commands.rs+8−11 helix-term/tests/test/commands.rs+43
helix-term/src/commands.rs+8−11
@@ -2053,16 +2053,10 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
20532053 let selection = doc.selection(view.id).clone().transform(|range| {
20542054 let (start_line, end_line) = range.line_range(text.slice(..));
20552055
2056− let start = text.line_to_char(match extend {
2057− Extend::Above => start_line.saturating_sub(count - 1),
2058− Extend::Below => start_line,
2059− });
2056+ let start = text.line_to_char(start_line);
20602057 let end = text.line_to_char(
2061− match extend {
2062− Extend::Above => end_line + 1, // the start of next line
2063− Extend::Below => end_line + count,
2064− }
2065− .min(text.len_lines()),
2058+ (end_line + 1) // newline of end_line
2059+ .min(text.len_lines()),
20662060 );
20672061
20682062 // extend to previous/next line if current line is selected
@@ -2076,8 +2070,11 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
20762070 }
20772071 } else {
20782072 match extend {
2079− Extend::Above => (end, start),
2080− Extend::Below => (start, end),
2073+ Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count - 1))),
2074+ Extend::Below => (
2075+ start,
2076+ text.line_to_char((end_line + count).min(text.len_lines())),
2077+ ),
20812078 }
20822079 };
20832080
helix-term/tests/test/commands.rs+43
@@ -311,3 +311,46 @@ async fn test_undo_redo() -> anyhow::Result<()> {
311311
312312 Ok(())
313313}
314+
315+#[tokio::test(flavor = "multi_thread")]
316+async fn test_extend_line() -> anyhow::Result<()> {
317+ // extend with line selected then count
318+ test((
319+ platform_line(indoc! {"\
320+ #[l|]#orem
321+ ipsum
322+ dolor
323+
324+ "})
325+ .as_str(),
326+ "x2x",
327+ platform_line(indoc! {"\
328+ #[lorem
329+ ipsum
330+ dolor
331+ |]#
332+ "})
333+ .as_str(),
334+ ))
335+ .await?;
336+
337+ // extend with count on partial selection
338+ test((
339+ platform_line(indoc! {"\
340+ #[l|]#orem
341+ ipsum
342+
343+ "})
344+ .as_str(),
345+ "2x",
346+ platform_line(indoc! {"\
347+ #[lorem
348+ ipsum
349+ |]#
350+ "})
351+ .as_str(),
352+ ))
353+ .await?;
354+
355+ Ok(())
356+}