From 1c44de4bba8363e4e711bd49fe01f2a827ba5d02 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 17 May 2019 19:30:57 -0700 Subject: [PATCH] Add path completion to existing completions --- src/parser/completer.rs | 2 +- src/shell/completer.rs | 65 ++++++++++++++++++++++++----------------- src/shell/helper.rs | 12 ++++---- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/parser/completer.rs b/src/parser/completer.rs index 8591475e6..f0efbf751 100644 --- a/src/parser/completer.rs +++ b/src/parser/completer.rs @@ -15,7 +15,7 @@ impl completion::Completer for Completer { _pos: usize, _ctx: &Context<'_>, ) -> rustyline::Result<(usize, Vec)> { - let pairs = self + let pairs: Vec = self .commands .keys() .map(|k| completion::Pair { diff --git a/src/shell/completer.rs b/src/shell/completer.rs index 0f68b3354..f693aa274 100644 --- a/src/shell/completer.rs +++ b/src/shell/completer.rs @@ -1,26 +1,48 @@ -use rustyline::completion::{Candidate, Completer}; +use rustyline::completion::Completer; +use rustyline::completion::{self, FilenameCompleter}; use rustyline::line_buffer::LineBuffer; -#[derive(Debug)] -crate struct NuCompleter; +crate struct NuCompleter { + pub file_completer: FilenameCompleter, +} impl Completer for NuCompleter { - type Candidate = NuPair; + type Candidate = completion::Pair; fn complete( &self, - _line: &str, - _pos: usize, - _context: &rustyline::Context, - ) -> rustyline::Result<(usize, Vec)> { - Ok(( - 0, - vec![ - NuPair("exit", "exit"), - NuPair("ls", "ls"), - NuPair("ps", "ps"), - ], - )) + line: &str, + pos: usize, + context: &rustyline::Context, + ) -> rustyline::Result<(usize, Vec)> { + let mut pairs = vec![ + completion::Pair { + display: "exit".to_string(), + replacement: "exit".to_string(), + }, + completion::Pair { + display: "ls".to_string(), + replacement: "ls".to_string(), + }, + completion::Pair { + display: "ps".to_string(), + replacement: "ps".to_string(), + }, + ]; + + let mut completions = self.file_completer.complete(line, pos, context)?.1; + completions.append(&mut pairs); + + let line_chars: Vec<_> = line.chars().collect(); + let mut replace_pos = pos; + while replace_pos > 0 { + if line_chars[replace_pos - 1] == ' ' { + break; + } + replace_pos -= 1; + } + + Ok((replace_pos, completions)) } fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) { @@ -28,14 +50,3 @@ impl Completer for NuCompleter { line.replace(start..end, elected) } } - -crate struct NuPair(&'static str, &'static str); - -impl Candidate for NuPair { - fn display(&self) -> &str { - self.0 - } - fn replacement(&self) -> &str { - self.1 - } -} diff --git a/src/shell/helper.rs b/src/shell/helper.rs index aaa5bd9f3..287c5e5b8 100644 --- a/src/shell/helper.rs +++ b/src/shell/helper.rs @@ -1,6 +1,6 @@ -use crate::shell::completer::{NuCompleter, NuPair}; +use crate::shell::completer::NuCompleter; -use rustyline::completion::Completer; +use rustyline::completion::{self, Completer, FilenameCompleter}; use rustyline::error::ReadlineError; use rustyline::highlight::{Highlighter, MatchingBracketHighlighter}; use rustyline::hint::{Hinter, HistoryHinter}; @@ -15,7 +15,9 @@ crate struct Helper { impl Helper { crate fn new() -> Helper { Helper { - completer: NuCompleter, + completer: NuCompleter { + file_completer: FilenameCompleter::new(), + }, highlighter: MatchingBracketHighlighter::new(), hinter: HistoryHinter {}, } @@ -23,14 +25,14 @@ impl Helper { } impl Completer for Helper { - type Candidate = NuPair; + type Candidate = completion::Pair; fn complete( &self, line: &str, pos: usize, ctx: &rustyline::Context<'_>, - ) -> Result<(usize, Vec), ReadlineError> { + ) -> Result<(usize, Vec), ReadlineError> { self.completer.complete(line, pos, ctx) } }