@@ -1,13 +1,15 @@
11use helix_core::{
22 comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
3− line_ending::{get_line_ending, get_line_ending_of_str, line_end_char_index},
3+ line_ending::{
4+ get_line_ending, get_line_ending_of_str, line_end_char_index, str_is_line_ending,
5+ },
46 match_brackets,
57 movement::{self, Direction},
68 object, pos_at_coords,
79 regex::{self, Regex},
810 register::{self, Register, Registers},
9− search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection,
10− SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING,
11+ search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeGraphemes,
12+ RopeSlice, Selection, SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING,
1113};
1214
1315use helix_view::{
@@ -577,32 +579,37 @@ fn extend_first_nonwhitespace(cx: &mut Context) {
577579}
578580
579581fn replace(cx: &mut Context) {
582+ let mut buf = [0u8; 4]; // To hold utf8 encoded char.
583+
580584 // need to wait for next key
581585 cx.on_next_key(move |cx, event| {
586+ let (view, doc) = current!(cx.editor);
582587 let ch = match event {
583588 KeyEvent {
584589 code: KeyCode::Char(ch),
585590 ..
586− } => Some(ch),
591+ } => Some(&ch.encode_utf8(&mut buf[..])[..]),
587592 KeyEvent {
588593 code: KeyCode::Enter,
589594 ..
590− } => Some('\n'), // TODO: use the document's default line ending.
595+ } => Some(doc.line_ending.as_str()),
591596 _ => None,
592597 };
593598
594599 if let Some(ch) = ch {
595− let (view, doc) = current!(cx.editor);
596−
597600 let transaction =
598601 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
599602 let max_to = doc.text().len_chars().saturating_sub(1);
600603 let to = std::cmp::min(max_to, range.to() + 1);
601− let text: String = doc
602− .text()
603− .slice(range.from()..to)
604− .chars()
605− .map(|c| if c == '\n' { '\n' } else { ch })
604+ let text: String = RopeGraphemes::new(doc.text().slice(range.from()..to))
605+ .map(|g| {
606+ let cow: Cow<str> = g.into();
607+ if str_is_line_ending(&cow) {
608+ cow
609+ } else {
610+ ch.into()
611+ }
612+ })
606613 .collect();
607614
608615 (range.from(), to, Some(text.into()))