Remove special handling of line ending characters in selection replacement (#10786)

* Remove special-casing of line ending characters in selection replacement

* Refactor line ending handling and integration test to address code review comments
This commit is contained in:
Chris Pyles 2024-06-10 08:07:43 -07:00 committed by GitHub
parent aa1630a41a
commit 03813bbc2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 12 deletions

View file

@ -26,7 +26,7 @@ use helix_core::{
history::UndoKind, history::UndoKind,
increment, indent, increment, indent,
indent::IndentStyle, 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, match_brackets,
movement::{self, move_vertically_visual, Direction}, movement::{self, move_vertically_visual, Direction},
object, pos_at_coords, object, pos_at_coords,
@ -1605,19 +1605,11 @@ fn replace(cx: &mut Context) {
if let Some(ch) = ch { if let Some(ch) = ch {
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| { let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
if !range.is_empty() { if !range.is_empty() {
let text: String = let text: Tendril =
RopeGraphemes::new(doc.text().slice(range.from()..range.to())) RopeGraphemes::new(doc.text().slice(range.from()..range.to()))
.map(|g| { .map(|_g| ch)
let cow: Cow<str> = g.into();
if str_is_line_ending(&cow) {
cow
} else {
ch.into()
}
})
.collect(); .collect();
(range.from(), range.to(), Some(text))
(range.from(), range.to(), Some(text.into()))
} else { } else {
// No change. // No change.
(range.from(), range.to(), None) (range.from(), range.to(), None)

View file

@ -722,5 +722,19 @@ fn foo() {
)) ))
.await?; .await?;
test((
indoc! {"\
#[a
b
c
d
e|]#
f
"},
"s\\n<ret>r,",
"a#[,|]#b#(,|)#c#(,|)#d#(,|)#e\nf\n",
))
.await?;
Ok(()) Ok(())
} }