on master help sign in
fix(commands): extend_line to proper line when count and current line selected (#5288)
1b17552Gabriel Dinner-David
helix-term/src/commands.rs+8−11
@@ -2053,16 +2053,10 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
2053 let selection = doc.selection(view.id).clone().transform(|range| {
2053 let selection = doc.selection(view.id).clone().transform(|range| {
2054 let (start_line, end_line) = range.line_range(text.slice(..));
2054 let (start_line, end_line) = range.line_range(text.slice(..));
2055
2055
2056− let start = text.line_to_char(match extend {
2056+ let start = text.line_to_char(start_line);
2057− Extend::Above => start_line.saturating_sub(count - 1),
2058− Extend::Below => start_line,
2059− });
2060 let end = text.line_to_char(
2057 let end = text.line_to_char(
2061− match extend {
2058+ (end_line + 1) // newline of end_line
2062− Extend::Above => end_line + 1, // the start of next line
2063− Extend::Below => end_line + count,
2064− }
2065− .min(text.len_lines()),
2059+ .min(text.len_lines()),
2066 );
2060 );
2067
2061
2068 // extend to previous/next line if current line is selected
2062 // extend to previous/next line if current line is selected
@@ -2076,8 +2070,11 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
2076 }
2070 }
2077 } else {
2071 } else {
2078 match extend {
2072 match extend {
2079− Extend::Above => (end, start),
2073+ Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count - 1))),
2080− Extend::Below => (start, end),
2074+ Extend::Below => (
2075+ start,
2076+ text.line_to_char((end_line + count).min(text.len_lines())),
2077+ ),
2081 }
2078 }
2082 };
2079 };
2083
2080
helix-term/tests/test/commands.rs+43
@@ -311,3 +311,46 @@ async fn test_undo_redo() -> anyhow::Result<()> {
311
311
312 Ok(())
312 Ok(())
313}
313}
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+}