From 1410e26dc3827a1ae07db46e1606e3df63aeb767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Mon, 7 Jun 2021 18:49:27 +0200 Subject: [PATCH 1/5] commands: Replace all characters in selection --- helix-term/src/commands.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 00556e182d82..df1e53a6820b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -435,7 +435,11 @@ pub fn replace(cx: &mut Context) { let transaction = Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { - (range.from(), range.to() + 1, Some(text.clone())) + ( + range.from(), + range.to() + 1, + Some(text.repeat(range.to() - range.from() + 1).into()), + ) }); doc.apply(&transaction, view.id); From b1653a5288f5303b72a863545fbd74c2aba5b8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Mon, 7 Jun 2021 19:03:44 +0200 Subject: [PATCH 2/5] Add bounds checks to replace --- helix-term/src/commands.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index df1e53a6820b..d75ad11daeb3 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -435,10 +435,12 @@ pub fn replace(cx: &mut Context) { let transaction = Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { + let max_to = doc.text().len_chars().saturating_sub(1); + let to = std::cmp::min(max_to, range.to() + 1); ( range.from(), - range.to() + 1, - Some(text.repeat(range.to() - range.from() + 1).into()), + to, + Some(text.repeat(to - range.from()).into()), ) }); From b0bbec62f7b9f85cba5841d98c4e01e99ab5965a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Mon, 7 Jun 2021 21:28:04 +0200 Subject: [PATCH 3/5] Don't replace newlines --- helix-term/src/commands.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d75ad11daeb3..1c0f3a762eea 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -429,19 +429,20 @@ pub fn replace(cx: &mut Context) { .. } = event { - let text = Tendril::from_char(ch); - let (view, doc) = cx.current(); let transaction = Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { let max_to = doc.text().len_chars().saturating_sub(1); let to = std::cmp::min(max_to, range.to() + 1); - ( - range.from(), - to, - Some(text.repeat(to - range.from()).into()), - ) + let text: String = doc + .text() + .slice(range.from()..to) + .chars() + .map(|c| if c == '\n' { '\n' } else { ch }) + .collect(); + + (range.from(), to, Some(text.as_str().into())) }); doc.apply(&transaction, view.id); From 48ef2469f6935d1650debd502e776b26c0cdb9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Tue, 8 Jun 2021 06:42:35 +0200 Subject: [PATCH 4/5] Remove unwanted `as_str` --- helix-term/src/commands.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1c0f3a762eea..82079fd73bb3 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -442,7 +442,7 @@ pub fn replace(cx: &mut Context) { .map(|c| if c == '\n' { '\n' } else { ch }) .collect(); - (range.from(), to, Some(text.as_str().into())) + (range.from(), to, Some(text.into())) }); doc.apply(&transaction, view.id); From e0329ea6c7300a75eb8ce3abc85de1ceb7d67d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Tue, 8 Jun 2021 08:44:26 +0200 Subject: [PATCH 5/5] Make `r` work --- helix-term/src/commands.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 82079fd73bb3..ae90a92b0275 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -424,11 +424,19 @@ pub fn extend_prev_char(cx: &mut Context) { pub fn replace(cx: &mut Context) { // need to wait for next key cx.on_next_key(move |cx, event| { - if let KeyEvent { - code: KeyCode::Char(ch), - .. - } = event - { + let ch = match event { + KeyEvent { + code: KeyCode::Char(ch), + .. + } => Some(ch), + KeyEvent { + code: KeyCode::Enter, + .. + } => Some('\n'), + _ => None, + }; + + if let Some(ch) = ch { let (view, doc) = cx.current(); let transaction =