Skip to content

Commit

Permalink
Revert change to Completer and introduce an Helper trait
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Sep 23, 2017
1 parent 85327e1 commit 3c97b9d
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 90 deletions.
7 changes: 2 additions & 5 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
extern crate log;
extern crate rustyline;

use std::cell::RefCell;
use std::io::{self, Write};
use std::rc::Rc;
use log::{LogLevel, LogLevelFilter, LogMetadata, LogRecord, SetLoggerError};

use rustyline::completion::FilenameCompleter;
Expand All @@ -23,7 +21,7 @@ static PROMPT: &'static str = ">> ";
struct Hints {}

impl Hinter for Hints {
fn hint(&mut self, line: &str, _pos: usize) -> Option<String> {
fn hint(&self, line: &str, _pos: usize) -> Option<String> {
if line == "hello" {
Some(" \x1b[1mWorld\x1b[m".to_owned())
} else {
Expand All @@ -41,8 +39,7 @@ fn main() {
.build();
let c = FilenameCompleter::new();
let mut rl = Editor::with_config(config);
rl.set_completer(Some(Rc::new(RefCell::new(c))));
rl.set_hinter(Some(Rc::new(RefCell::new(Hints {}))));
rl.set_helper(Some((c, Hints {})));
rl.bind_sequence(KeyPress::Meta('N'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyPress::Meta('P'), Cmd::HistorySearchBackward);
if rl.load_history("history.txt").is_err() {
Expand Down
44 changes: 40 additions & 4 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,50 @@ pub trait Completer {
/// partial word to be completed.
///
/// "ls /usr/loc" => Ok((3, vec!["/usr/local/"]))
fn complete(&mut self, line: &str, pos: usize) -> Result<(usize, Vec<String>)>;
fn complete(&self, line: &str, pos: usize) -> Result<(usize, Vec<String>)>;
/// Updates the edited `line` with the `elected` candidate.
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
let end = line.pos();
line.replace(start..end, elected)
}
}

impl Completer for () {
fn complete(&self, _line: &str, _pos: usize) -> Result<(usize, Vec<String>)> {
Ok((0, Vec::with_capacity(0)))
}
fn update(&self, _line: &mut LineBuffer, _start: usize, _elected: &str) {
unreachable!()
}
}

impl<'c, C: ?Sized + Completer> Completer for &'c C {
fn complete(&self, line: &str, pos: usize) -> Result<(usize, Vec<String>)> {
(**self).complete(line, pos)
}
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
(**self).update(line, start, elected)
}
}
macro_rules! box_completer {
($($id: ident)*) => {
$(
impl<C: ?Sized + Completer> Completer for $id<C> {
fn complete(&self, line: &str, pos: usize) -> Result<(usize, Vec<String>)> {
(**self).complete(line, pos)
}
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
(**self).update(line, start, elected)
}
}
)*
}
}

use std::rc::Rc;
use std::sync::Arc;
box_completer! { Box Rc Arc }

/// A `Completer` for file and folder names.
pub struct FilenameCompleter {
break_chars: BTreeSet<char>,
Expand Down Expand Up @@ -94,7 +130,7 @@ impl Default for FilenameCompleter {
}

impl Completer for FilenameCompleter {
fn complete(&mut self, line: &str, pos: usize) -> Result<(usize, Vec<String>)> {
fn complete(&self, line: &str, pos: usize) -> Result<(usize, Vec<String>)> {
let (start, path) = extract_word(line, pos, ESCAPE_CHAR, &self.break_chars);
let path = unescape(path, ESCAPE_CHAR);
let matches = try!(filename_complete(&path, ESCAPE_CHAR, &self.break_chars));
Expand Down Expand Up @@ -250,8 +286,8 @@ pub fn longest_common_prefix(candidates: &[String]) -> Option<&str> {
for (i, c1) in candidates.iter().enumerate().take(candidates.len() - 1) {
let b1 = c1.as_bytes();
let b2 = candidates[i + 1].as_bytes();
if b1.len() <= longest_common_prefix || b2.len() <= longest_common_prefix ||
b1[longest_common_prefix] != b2[longest_common_prefix]
if b1.len() <= longest_common_prefix || b2.len() <= longest_common_prefix
|| b1[longest_common_prefix] != b2[longest_common_prefix]
{
break 'o;
}
Expand Down
8 changes: 7 additions & 1 deletion src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ pub trait Hinter {
/// Takes the currently edited `line` with the cursor `pos`ition and
/// returns the string that should be displayed or `None`
/// if no hint is available for the text the user currently typed.
fn hint(&mut self, line: &str, pos: usize) -> Option<String>;
fn hint(&self, line: &str, pos: usize) -> Option<String>;
}

impl Hinter for () {
fn hint(&self, _line: &str, _pos: usize) -> Option<String> {
None
}
}
6 changes: 3 additions & 3 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl History {
if self.max_len == 0 {
return false;
}
if line.as_ref().is_empty() ||
(self.ignore_space &&
line.as_ref()
if line.as_ref().is_empty()
|| (self.ignore_space
&& line.as_ref()
.chars()
.next()
.map_or(true, |c| c.is_whitespace()))
Expand Down
Loading

0 comments on commit 3c97b9d

Please sign in to comment.