Skip to content

Commit eb49c5e

Browse files
mickX-devmickael-covierb-van-b
authored
fix selecting characters with the crlf mode (#15617)
Co-authored-by: mickael-covier <mick@laptop-7m7lk7bt.localdomain> Co-authored-by: b-van-b <115096508+b-van-b@users.noreply.github.com>
1 parent 6d0974d commit eb49c5e

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

helix-core/src/selection.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,68 @@ mod test {
11701170
);
11711171
}
11721172

1173+
#[test]
1174+
fn test_select_on_matches_crlf() {
1175+
let r = Rope::from_str("This\r\nString\r\n\r\ncontains multiple\r\nlines");
1176+
let s = r.slice(..);
1177+
1178+
let start_of_line = rope::RegexBuilder::new()
1179+
.syntax(rope::Config::new().multi_line(true).crlf(true))
1180+
.build(r"^")
1181+
.unwrap();
1182+
let end_of_line = rope::RegexBuilder::new()
1183+
.syntax(rope::Config::new().multi_line(true).crlf(true))
1184+
.build(r"$")
1185+
.unwrap();
1186+
1187+
// line without ending
1188+
assert_eq!(
1189+
select_on_matches(s, &Selection::single(0, 4), &start_of_line),
1190+
Some(Selection::single(0, 0))
1191+
);
1192+
assert_eq!(
1193+
select_on_matches(s, &Selection::single(0, 4), &end_of_line),
1194+
None
1195+
);
1196+
// line with ending
1197+
assert_eq!(
1198+
select_on_matches(s, &Selection::single(0, 5), &start_of_line),
1199+
Some(Selection::single(0, 0))
1200+
);
1201+
assert_eq!(
1202+
select_on_matches(s, &Selection::single(0, 5), &end_of_line),
1203+
Some(Selection::single(4, 4))
1204+
);
1205+
// line with start of next line
1206+
assert_eq!(
1207+
select_on_matches(s, &Selection::single(0, 7), &start_of_line),
1208+
Some(Selection::new(
1209+
smallvec![Range::point(0), Range::point(6)],
1210+
0
1211+
))
1212+
);
1213+
assert_eq!(
1214+
select_on_matches(s, &Selection::single(0, 6), &end_of_line),
1215+
Some(Selection::single(4, 4))
1216+
);
1217+
1218+
// multiple lines
1219+
assert_eq!(
1220+
select_on_matches(
1221+
s,
1222+
&Selection::single(0, s.len_chars()),
1223+
&rope::RegexBuilder::new()
1224+
.syntax(rope::Config::new().multi_line(true).crlf(true))
1225+
.build(r"^[a-z ]*$")
1226+
.unwrap()
1227+
),
1228+
Some(Selection::new(
1229+
smallvec![Range::point(14), Range::new(16, 33), Range::new(35, 40)],
1230+
0
1231+
))
1232+
);
1233+
}
1234+
11731235
#[test]
11741236
fn test_line_range() {
11751237
let r = Rope::from_str("\r\nHi\r\nthere!");

helix-term/src/commands.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2409,11 +2409,13 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
24092409
false
24102410
};
24112411
let wrap_around = search_config.wrap_around;
2412+
let is_crlf = doc!(cx.editor).line_ending == LineEnding::Crlf;
24122413
if let Ok(regex) = rope::RegexBuilder::new()
24132414
.syntax(
24142415
rope::Config::new()
24152416
.case_insensitive(case_insensitive)
2416-
.multi_line(true),
2417+
.multi_line(true)
2418+
.crlf(is_crlf),
24172419
)
24182420
.build(&query)
24192421
{

helix-term/src/ui/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ pub fn raw_regex_prompt(
126126
false
127127
};
128128

129+
let is_crlf = doc!(cx.editor).line_ending == helix_core::LineEnding::Crlf;
129130
match rope::RegexBuilder::new()
130131
.syntax(
131132
rope::Config::new()
132133
.case_insensitive(case_insensitive)
133-
.multi_line(true),
134+
.multi_line(true)
135+
.crlf(is_crlf),
134136
)
135137
.build(input)
136138
{

0 commit comments

Comments
 (0)