@@ -1251,6 +1251,65 @@ fn extend_next_long_word_end(cx: &mut Context) {
1251 extend_word_impl(cx, movement::move_next_long_word_end)
1251 extend_word_impl(cx, movement::move_next_long_word_end)
1254+/// Separate branch to find_char designed only for <ret> char.
1256+// This is necessary because the one document can have different line endings inside. And we
1257+// cannot predict what character to find when <ret> is pressed. On the current line it can be `lf`
1258+// but on the next line it can be `crlf`. That's why [`find_char_impl`] cannot be applied here.
1259+fn find_char_line_ending(
1262+ direction: Direction,
1266+ let (view, doc) = current!(cx.editor);
1267+ let text = doc.text().slice(..);
1269+ let selection = doc.selection(view.id).clone().transform(|range| {
1270+ let cursor = range.cursor(text);
1271+ let cursor_line = range.cursor_line(text);
1273+ // Finding the line where we're going to find <ret>. Depends mostly on
1274+ // `count`, but also takes into account edge cases where we're already at the end
1275+ // of a line or the beginning of a line
1276+ let find_on_line = match direction {
1277+ Direction::Forward => {
1278+ let on_edge = line_end_char_index(&text, cursor_line) == cursor;
1279+ let line = cursor_line + count - 1 + (on_edge as usize);
1280+ if line >= text.len_lines() - 1 {
1286+ Direction::Backward => {
1287+ let on_edge = text.line_to_char(cursor_line) == cursor && !inclusive;
1288+ let line = cursor_line as isize - (count as isize - 1 + on_edge as isize);
1297+ let pos = match (direction, inclusive) {
1298+ (Direction::Forward, true) => line_end_char_index(&text, find_on_line),
1299+ (Direction::Forward, false) => line_end_char_index(&text, find_on_line) - 1,
1300+ (Direction::Backward, true) => line_end_char_index(&text, find_on_line - 1),
1301+ (Direction::Backward, false) => text.line_to_char(find_on_line),
1305+ range.put_cursor(text, pos, true)
1307+ Range::point(range.cursor(text)).put_cursor(text, pos, true)
1310+ doc.set_selection(view.id, selection);
1254fn find_char(cx: &mut Context, direction: Direction, inclusive: bool, extend: bool) {
1313fn find_char(cx: &mut Context, direction: Direction, inclusive: bool, extend: bool) {
1255 // TODO: count is reset to 1 before next key so we move it into the closure here.
1314 // TODO: count is reset to 1 before next key so we move it into the closure here.
1256 // Would be nice to carry over.
1315 // Would be nice to carry over.
@@ -1264,13 +1323,9 @@ fn find_char(cx: &mut Context, direction: Direction, inclusive: bool, extend: bo
1264 KeyEvent {
1323 KeyEvent {
1265 code: KeyCode::Enter,
1324 code: KeyCode::Enter,
1268− // TODO: this isn't quite correct when CRLF is involved.
1327+ find_char_line_ending(cx, count, direction, inclusive, extend);
1269− // This hack will work in most cases, since documents don't
1328+ return;
1270− // usually mix line endings. But we should fix it eventually
1273− doc!(cx.editor).line_ending.as_str().chars().next().unwrap()
1276 KeyEvent {
1331 KeyEvent {