Skip to content

Commit

Permalink
Don't special-case line endings in replace
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed May 11, 2024
1 parent 8c59720 commit 401db56
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use helix_core::{
history::UndoKind,
increment, indent,
indent::IndentStyle,
line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending},
line_ending::{get_line_ending_of_str, line_end_char_index},
match_brackets,
movement::{self, move_vertically_visual, Direction},
object, pos_at_coords,
Expand Down Expand Up @@ -1678,48 +1678,38 @@ fn replace(cx: &mut Context) {
// need to wait for next key
cx.on_next_key(move |cx, event| {
let (view, doc) = current!(cx.editor);
let ch: Option<&str> = match event {
let replacement = match event {
KeyEvent {
code: KeyCode::Char(ch),
..
} => Some(ch.encode_utf8(&mut buf[..])),
} => ch.encode_utf8(&mut buf[..]),
KeyEvent {
code: KeyCode::Enter,
..
} => Some(doc.line_ending.as_str()),
} => doc.line_ending.as_str(),
KeyEvent {
code: KeyCode::Tab, ..
} => Some("\t"),
_ => None,
} => "\t",
_ => return,
};

let selection = doc.selection(view.id);

if let Some(ch) = ch {
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
if !range.is_empty() {
let text: String =
RopeGraphemes::new(doc.text().slice(range.from()..range.to()))
.map(|g| {
let cow: Cow<str> = g.into();
if str_is_line_ending(&cow) {
cow
} else {
ch.into()
}
})
.collect();
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
if !range.is_empty() {
let text: Tendril = RopeGraphemes::new(doc.text().slice(range.from()..range.to()))
.map(|_grapheme| replacement)
.collect();

(range.from(), range.to(), Some(text.into()))
} else {
// No change.
(range.from(), range.to(), None)
}
});
(range.from(), range.to(), Some(text))
} else {
// No change.
(range.from(), range.to(), None)
}
});

doc.apply(&transaction, view.id);
exit_select_mode(cx);
}
doc.apply(&transaction, view.id);
exit_select_mode(cx);
})
}

Expand Down

0 comments on commit 401db56

Please sign in to comment.