Skip to content

Commit

Permalink
Merge pull request #666 from gwenn/listeners
Browse files Browse the repository at this point in the history
Refactor listeners
  • Loading branch information
gwenn authored Jan 15, 2023
2 parents 0b46056 + 0405223 commit c3cbd4f
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 384 deletions.
4 changes: 2 additions & 2 deletions rustyline-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub fn completer_macro_derive(input: TokenStream) -> TokenStream {
::rustyline::completion::Completer::complete(&self.#field_name_or_index, line, pos, ctx)
}

fn update(&self, line: &mut ::rustyline::line_buffer::LineBuffer, start: usize, elected: &str) {
::rustyline::completion::Completer::update(&self.#field_name_or_index, line, start, elected)
fn update(&self, line: &mut ::rustyline::line_buffer::LineBuffer, start: usize, elected: &str, cl: &mut ::rustyline::Changeset) {
::rustyline::completion::Completer::update(&self.#field_name_or_index, line, start, elected, cl)
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::{Arc, Mutex};

use crate::complete_hint_line;
use crate::config::Config;
use crate::edit::State;
Expand All @@ -20,7 +18,7 @@ pub fn execute<H: Helper>(
cmd: Cmd,
s: &mut State<'_, '_, H>,
input_state: &InputState,
kill_ring: &Arc<Mutex<KillRing>>,
kill_ring: &mut KillRing,
config: &Config,
) -> Result<Status> {
use Status::{Proceed, Submit};
Expand Down Expand Up @@ -61,7 +59,7 @@ pub fn execute<H: Helper>(
}
Cmd::ReplaceChar(n, c) => s.edit_replace_char(c, n)?,
Cmd::Replace(mvt, text) => {
s.edit_kill(&mvt)?;
s.edit_kill(&mvt, kill_ring)?;
if let Some(text) = text {
s.edit_insert_text(&text)?;
}
Expand Down Expand Up @@ -115,14 +113,12 @@ pub fn execute<H: Helper>(
}
Cmd::Yank(n, anchor) => {
// retrieve (yank) last item killed
let mut kill_ring = kill_ring.lock().unwrap();
if let Some(text) = kill_ring.yank() {
s.edit_yank(input_state, text, anchor, n)?;
}
}
Cmd::ViYankTo(ref mvt) => {
if let Some(text) = s.line.copy(mvt) {
let mut kill_ring = kill_ring.lock().unwrap();
kill_ring.kill(&text, Mode::Append);
}
}
Expand Down Expand Up @@ -171,7 +167,7 @@ pub fn execute<H: Helper>(
s.edit_word(WordAction::Capitalize)?;
}
Cmd::Kill(ref mvt) => {
s.edit_kill(mvt)?;
s.edit_kill(mvt, kill_ring)?;
}
Cmd::Move(Movement::ForwardWord(n, at, word_def)) => {
// move forwards one word
Expand Down Expand Up @@ -205,14 +201,13 @@ pub fn execute<H: Helper>(
}
Cmd::YankPop => {
// yank-pop
let mut kill_ring = kill_ring.lock().unwrap();
if let Some((yank_size, text)) = kill_ring.yank_pop() {
s.edit_yank_pop(yank_size, text)?;
}
}
Cmd::Move(Movement::ViCharSearch(n, cs)) => s.edit_move_to(cs, n)?,
Cmd::Undo(n) => {
if s.changes.borrow_mut().undo(&mut s.line, n) {
if s.changes.undo(&mut s.line, n) {
s.refresh_line()?;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ pub trait Completer {
Ok((0, Vec::with_capacity(0)))
}
/// Updates the edited `line` with the `elected` candidate.
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) {
let end = line.pos();
line.replace(start..end, elected);
line.replace(start..end, elected, cl);
}
}

impl Completer for () {
type Candidate = String;

fn update(&self, _line: &mut LineBuffer, _start: usize, _elected: &str) {
fn update(&self, _line: &mut LineBuffer, _start: usize, _elected: &str, _cl: &mut Changeset) {
unreachable!();
}
}
Expand All @@ -115,8 +115,8 @@ impl<'c, C: ?Sized + Completer> Completer for &'c C {
(**self).complete(line, pos, ctx)
}

fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
(**self).update(line, start, elected);
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) {
(**self).update(line, start, elected, cl);
}
}
macro_rules! box_completer {
Expand All @@ -128,14 +128,15 @@ macro_rules! box_completer {
fn complete(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Result<(usize, Vec<Self::Candidate>)> {
(**self).complete(line, pos, ctx)
}
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
(**self).update(line, start, elected)
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) {
(**self).update(line, start, elected, cl)
}
}
)*
}
}

use crate::undo::Changeset;
use std::rc::Rc;
use std::sync::Arc;
box_completer! { Box Rc Arc }
Expand Down
Loading

0 comments on commit c3cbd4f

Please sign in to comment.