on master help sign in
Implement replace command (r<key>).
42d07b0Blaž Hrastnik
helix-term/src/commands.rs+23
@@ -380,6 +380,29 @@ pub fn extend_prev_char(cx: &mut Context) {
380 )
380 )
381}
381}
382
382
383+pub fn replace(cx: &mut Context) {
384+ // need to wait for next key
385+ cx.on_next_key(move |cx, event| {
386+ if let KeyEvent {
387+ code: KeyCode::Char(ch),
388+ ..
389+ } = event
390+ {
391+ let text = Tendril::from_char(ch);
392+
393+ let mut doc = cx.doc();
394+
395+ let transaction =
396+ Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
397+ (range.from(), range.to() + 1, Some(text.clone()))
398+ });
399+
400+ doc.apply(&transaction);
401+ doc.append_changes_to_history();
402+ }
403+ })
404+}
405+
383fn scroll(view: &mut View, offset: usize, direction: Direction) {
406fn scroll(view: &mut View, offset: usize, direction: Direction) {
384 use Direction::*;
407 use Direction::*;
385 // we use short lived borrows since view's methods read from doc too
408 // we use short lived borrows since view's methods read from doc too
helix-term/src/keymap.rs+2
@@ -141,6 +141,8 @@ pub fn default() -> Keymaps {
141 shift!('T') => commands::till_prev_char,
141 shift!('T') => commands::till_prev_char,
142 shift!('F') => commands::find_prev_char,
142 shift!('F') => commands::find_prev_char,
143 // and matching set for select mode (extend)
143 // and matching set for select mode (extend)
144+ //
145+ key!('r') => commands::replace,
144
146
145 key!('0') => commands::move_line_start,
147 key!('0') => commands::move_line_start,
146 key!('$') => commands::move_line_end,
148 key!('$') => commands::move_line_end,