2020-09-16 06:37:43 +02:00
|
|
|
use crate::completion::command::CommandCompleter;
|
|
|
|
use crate::completion::flag::FlagCompleter;
|
2020-09-17 16:52:58 +02:00
|
|
|
use crate::completion::matchers;
|
|
|
|
use crate::completion::matchers::Matcher;
|
2020-09-16 06:37:43 +02:00
|
|
|
use crate::completion::path::{PathCompleter, PathSuggestion};
|
|
|
|
use crate::completion::{self, Completer, Suggestion};
|
2020-07-25 01:39:12 +02:00
|
|
|
use crate::context;
|
2020-09-17 16:52:58 +02:00
|
|
|
use nu_source::Tag;
|
|
|
|
|
2020-09-17 08:02:30 +02:00
|
|
|
use std::borrow::Cow;
|
2020-09-17 16:52:58 +02:00
|
|
|
|
2020-08-21 21:37:51 +02:00
|
|
|
pub(crate) struct NuCompleter {}
|
2019-05-16 23:43:36 +02:00
|
|
|
|
2020-08-21 21:37:51 +02:00
|
|
|
impl NuCompleter {}
|
2020-05-24 02:27:52 +02:00
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
impl NuCompleter {
|
2020-08-21 21:37:51 +02:00
|
|
|
pub fn complete(
|
2019-05-16 23:43:36 +02:00
|
|
|
&self,
|
2019-05-18 04:30:57 +02:00
|
|
|
line: &str,
|
|
|
|
pos: usize,
|
2020-07-25 01:39:12 +02:00
|
|
|
context: &completion::Context,
|
2020-08-21 21:37:51 +02:00
|
|
|
) -> (usize, Vec<Suggestion>) {
|
2020-09-16 06:37:43 +02:00
|
|
|
use completion::engine::LocationType;
|
2019-12-08 07:23:31 +01:00
|
|
|
|
2020-08-21 21:37:51 +02:00
|
|
|
let nu_context: &context::Context = context.as_ref();
|
|
|
|
let lite_block = match nu_parser::lite_parse(line, 0) {
|
|
|
|
Ok(block) => Some(block),
|
|
|
|
Err(result) => result.partial,
|
|
|
|
};
|
2019-12-08 06:58:53 +01:00
|
|
|
|
2020-08-22 03:26:47 +02:00
|
|
|
let locations = lite_block
|
2020-08-21 21:37:51 +02:00
|
|
|
.map(|block| nu_parser::classify_block(&block, &nu_context.registry))
|
2020-09-16 06:37:43 +02:00
|
|
|
.map(|block| completion::engine::completion_location(line, &block.block, pos))
|
2020-08-22 03:26:47 +02:00
|
|
|
.unwrap_or_default();
|
2020-07-25 16:41:14 +02:00
|
|
|
|
2020-09-17 16:52:58 +02:00
|
|
|
let matcher = nu_data::config::config(Tag::unknown())
|
|
|
|
.ok()
|
|
|
|
.and_then(|cfg| cfg.get("line_editor").cloned())
|
|
|
|
.and_then(|le| {
|
|
|
|
le.row_entries()
|
|
|
|
.find(|(idx, _value)| idx.as_str() == "completion_match_method")
|
|
|
|
.and_then(|(_idx, value)| value.as_string().ok())
|
|
|
|
})
|
|
|
|
.unwrap_or_else(String::new);
|
|
|
|
|
|
|
|
let matcher = matcher.as_str();
|
|
|
|
let matcher: &dyn Matcher = match matcher {
|
|
|
|
"case-insensitive" => &matchers::case_insensitive::Matcher,
|
|
|
|
_ => &matchers::case_sensitive::Matcher,
|
|
|
|
};
|
|
|
|
|
2020-08-22 03:26:47 +02:00
|
|
|
if locations.is_empty() {
|
|
|
|
(pos, Vec::new())
|
|
|
|
} else {
|
|
|
|
let pos = locations[0].span.start();
|
|
|
|
let suggestions = locations
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|location| {
|
|
|
|
let partial = location.span.slice(line);
|
|
|
|
match location.item {
|
|
|
|
LocationType::Command => {
|
2020-09-16 06:37:43 +02:00
|
|
|
let command_completer = CommandCompleter;
|
2020-09-17 16:52:58 +02:00
|
|
|
command_completer.complete(context, partial, matcher.to_owned())
|
2020-08-22 03:26:47 +02:00
|
|
|
}
|
2020-07-25 16:41:14 +02:00
|
|
|
|
2020-08-22 03:26:47 +02:00
|
|
|
LocationType::Flag(cmd) => {
|
2020-09-16 06:37:43 +02:00
|
|
|
let flag_completer = FlagCompleter { cmd };
|
2020-09-17 16:52:58 +02:00
|
|
|
flag_completer.complete(context, partial, matcher.to_owned())
|
2020-08-22 03:26:47 +02:00
|
|
|
}
|
2019-05-18 16:06:01 +02:00
|
|
|
|
2020-08-29 01:50:46 +02:00
|
|
|
LocationType::Argument(cmd, _arg_name) => {
|
2020-09-16 06:37:43 +02:00
|
|
|
let path_completer = PathCompleter;
|
2020-08-31 05:29:13 +02:00
|
|
|
|
|
|
|
const QUOTE_CHARS: &[char] = &['\'', '"', '`'];
|
|
|
|
|
|
|
|
// TODO Find a better way to deal with quote chars. Can the completion
|
|
|
|
// engine relay this back to us? Maybe have two spans: inner and
|
|
|
|
// outer. The former is what we want to complete, the latter what
|
|
|
|
// we'd need to replace.
|
|
|
|
let (quote_char, partial) = if partial.starts_with(QUOTE_CHARS) {
|
|
|
|
let (head, tail) = partial.split_at(1);
|
|
|
|
(Some(head), tail)
|
|
|
|
} else {
|
|
|
|
(None, partial)
|
|
|
|
};
|
|
|
|
|
|
|
|
let partial = if let Some(quote_char) = quote_char {
|
|
|
|
if partial.ends_with(quote_char) {
|
2020-09-01 04:13:00 +02:00
|
|
|
&partial[..partial.len() - 1]
|
2020-08-31 05:29:13 +02:00
|
|
|
} else {
|
|
|
|
partial
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
partial
|
|
|
|
};
|
|
|
|
|
2020-09-17 16:52:58 +02:00
|
|
|
let completed_paths = path_completer.path_suggestions(partial, matcher);
|
2020-08-29 01:50:46 +02:00
|
|
|
match cmd.as_deref().unwrap_or("") {
|
|
|
|
"cd" => select_directory_suggestions(completed_paths),
|
|
|
|
_ => completed_paths,
|
2020-08-28 20:38:30 +02:00
|
|
|
}
|
2020-09-01 02:11:39 +02:00
|
|
|
.into_iter()
|
2020-09-10 00:32:20 +02:00
|
|
|
.map(|s| Suggestion {
|
|
|
|
replacement: requote(s.suggestion.replacement),
|
|
|
|
display: s.suggestion.display,
|
2020-09-01 02:11:39 +02:00
|
|
|
})
|
|
|
|
.collect()
|
2020-08-22 03:26:47 +02:00
|
|
|
}
|
2020-07-25 01:39:12 +02:00
|
|
|
|
2020-08-22 03:26:47 +02:00
|
|
|
LocationType::Variable => Vec::new(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-05-24 02:27:52 +02:00
|
|
|
|
2020-08-22 03:26:47 +02:00
|
|
|
(pos, suggestions)
|
2020-07-18 03:59:23 +02:00
|
|
|
}
|
2020-05-24 02:27:52 +02:00
|
|
|
}
|
2020-07-25 01:39:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 00:32:20 +02:00
|
|
|
fn select_directory_suggestions(completed_paths: Vec<PathSuggestion>) -> Vec<PathSuggestion> {
|
2020-08-29 01:50:46 +02:00
|
|
|
completed_paths
|
|
|
|
.into_iter()
|
|
|
|
.filter(|suggestion| {
|
2020-09-10 00:32:20 +02:00
|
|
|
suggestion
|
|
|
|
.path
|
|
|
|
.metadata()
|
2020-08-29 01:50:46 +02:00
|
|
|
.map(|md| md.is_dir())
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
.collect()
|
2020-08-28 20:38:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 08:02:30 +02:00
|
|
|
fn requote(orig_value: String) -> String {
|
|
|
|
let value: Cow<str> = rustyline::completion::unescape(&orig_value, Some('\\'));
|
2020-08-31 05:29:13 +02:00
|
|
|
|
|
|
|
let mut quotes = vec!['"', '\'', '`'];
|
|
|
|
let mut should_quote = false;
|
|
|
|
for c in value.chars() {
|
|
|
|
if c.is_whitespace() {
|
|
|
|
should_quote = true;
|
|
|
|
} else if let Some(index) = quotes.iter().position(|q| *q == c) {
|
|
|
|
should_quote = true;
|
|
|
|
quotes.swap_remove(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if should_quote {
|
|
|
|
if quotes.is_empty() {
|
|
|
|
// TODO we don't really have an escape character, so there isn't a great option right
|
|
|
|
// now. One possibility is `{{$(char backtick)}}`
|
|
|
|
value.to_string()
|
|
|
|
} else {
|
|
|
|
let quote = quotes[0];
|
|
|
|
format!("{}{}{}", quote, value, quote)
|
2020-08-21 21:37:51 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-31 05:29:13 +02:00
|
|
|
value.to_string()
|
2020-05-24 02:27:52 +02:00
|
|
|
}
|
2019-05-16 23:43:36 +02:00
|
|
|
}
|