forked from extern/nushell
Add path completion to existing completions
This commit is contained in:
parent
52716d0c24
commit
1c44de4bba
@ -15,7 +15,7 @@ impl completion::Completer for Completer {
|
|||||||
_pos: usize,
|
_pos: usize,
|
||||||
_ctx: &Context<'_>,
|
_ctx: &Context<'_>,
|
||||||
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
|
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
|
||||||
let pairs = self
|
let pairs: Vec<completion::Pair> = self
|
||||||
.commands
|
.commands
|
||||||
.keys()
|
.keys()
|
||||||
.map(|k| completion::Pair {
|
.map(|k| completion::Pair {
|
||||||
|
@ -1,26 +1,48 @@
|
|||||||
use rustyline::completion::{Candidate, Completer};
|
use rustyline::completion::Completer;
|
||||||
|
use rustyline::completion::{self, FilenameCompleter};
|
||||||
use rustyline::line_buffer::LineBuffer;
|
use rustyline::line_buffer::LineBuffer;
|
||||||
|
|
||||||
#[derive(Debug)]
|
crate struct NuCompleter {
|
||||||
crate struct NuCompleter;
|
pub file_completer: FilenameCompleter,
|
||||||
|
}
|
||||||
|
|
||||||
impl Completer for NuCompleter {
|
impl Completer for NuCompleter {
|
||||||
type Candidate = NuPair;
|
type Candidate = completion::Pair;
|
||||||
|
|
||||||
fn complete(
|
fn complete(
|
||||||
&self,
|
&self,
|
||||||
_line: &str,
|
line: &str,
|
||||||
_pos: usize,
|
pos: usize,
|
||||||
_context: &rustyline::Context,
|
context: &rustyline::Context,
|
||||||
) -> rustyline::Result<(usize, Vec<NuPair>)> {
|
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
|
||||||
Ok((
|
let mut pairs = vec![
|
||||||
0,
|
completion::Pair {
|
||||||
vec![
|
display: "exit".to_string(),
|
||||||
NuPair("exit", "exit"),
|
replacement: "exit".to_string(),
|
||||||
NuPair("ls", "ls"),
|
},
|
||||||
NuPair("ps", "ps"),
|
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) {
|
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
|
||||||
@ -28,14 +50,3 @@ impl Completer for NuCompleter {
|
|||||||
line.replace(start..end, elected)
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -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::error::ReadlineError;
|
||||||
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
||||||
use rustyline::hint::{Hinter, HistoryHinter};
|
use rustyline::hint::{Hinter, HistoryHinter};
|
||||||
@ -15,7 +15,9 @@ crate struct Helper {
|
|||||||
impl Helper {
|
impl Helper {
|
||||||
crate fn new() -> Helper {
|
crate fn new() -> Helper {
|
||||||
Helper {
|
Helper {
|
||||||
completer: NuCompleter,
|
completer: NuCompleter {
|
||||||
|
file_completer: FilenameCompleter::new(),
|
||||||
|
},
|
||||||
highlighter: MatchingBracketHighlighter::new(),
|
highlighter: MatchingBracketHighlighter::new(),
|
||||||
hinter: HistoryHinter {},
|
hinter: HistoryHinter {},
|
||||||
}
|
}
|
||||||
@ -23,14 +25,14 @@ impl Helper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Completer for Helper {
|
impl Completer for Helper {
|
||||||
type Candidate = NuPair;
|
type Candidate = completion::Pair;
|
||||||
|
|
||||||
fn complete(
|
fn complete(
|
||||||
&self,
|
&self,
|
||||||
line: &str,
|
line: &str,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
ctx: &rustyline::Context<'_>,
|
ctx: &rustyline::Context<'_>,
|
||||||
) -> Result<(usize, Vec<NuPair>), ReadlineError> {
|
) -> Result<(usize, Vec<completion::Pair>), ReadlineError> {
|
||||||
self.completer.complete(line, pos, ctx)
|
self.completer.complete(line, pos, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user