2020-09-02 00:10:29 +02:00
|
|
|
use std::path::PathBuf;
|
2020-08-21 21:37:51 +02:00
|
|
|
|
2020-09-17 16:52:58 +02:00
|
|
|
use super::matchers::Matcher;
|
2020-09-19 23:29:51 +02:00
|
|
|
use crate::completion::{Completer, CompletionContext, Suggestion};
|
2020-08-21 21:37:51 +02:00
|
|
|
|
2020-08-31 04:28:09 +02:00
|
|
|
const SEP: char = std::path::MAIN_SEPARATOR;
|
2020-08-21 21:37:51 +02:00
|
|
|
|
2020-09-16 06:37:43 +02:00
|
|
|
pub struct PathCompleter;
|
2020-08-21 21:37:51 +02:00
|
|
|
|
2020-09-05 04:10:26 +02:00
|
|
|
pub struct PathSuggestion {
|
|
|
|
pub(crate) path: PathBuf,
|
|
|
|
pub(crate) suggestion: Suggestion,
|
|
|
|
}
|
|
|
|
|
2020-09-16 06:37:43 +02:00
|
|
|
impl PathCompleter {
|
2020-09-17 16:52:58 +02:00
|
|
|
pub fn path_suggestions(&self, partial: &str, matcher: &dyn Matcher) -> Vec<PathSuggestion> {
|
2020-08-21 21:37:51 +02:00
|
|
|
let expanded = nu_parser::expand_ndots(partial);
|
2021-03-23 04:20:01 +01:00
|
|
|
let expanded = expanded.replace(std::path::is_separator, &SEP.to_string());
|
|
|
|
let expanded: &str = expanded.as_ref();
|
2020-08-31 04:28:09 +02:00
|
|
|
|
|
|
|
let (base_dir_name, partial) = match expanded.rfind(SEP) {
|
|
|
|
Some(pos) => expanded.split_at(pos + SEP.len_utf8()),
|
|
|
|
None => ("", expanded),
|
|
|
|
};
|
|
|
|
|
2021-01-01 03:13:59 +01:00
|
|
|
let base_dir = if base_dir_name.is_empty() {
|
2020-09-02 00:10:29 +02:00
|
|
|
PathBuf::from(".")
|
2020-09-07 02:06:13 +02:00
|
|
|
} else {
|
2020-09-02 00:10:29 +02:00
|
|
|
#[cfg(feature = "directories")]
|
|
|
|
{
|
2020-09-07 02:06:13 +02:00
|
|
|
let home_prefix = format!("~{}", SEP);
|
|
|
|
if base_dir_name.starts_with(&home_prefix) {
|
2021-01-19 21:24:27 +01:00
|
|
|
let mut home_dir = dirs_next::home_dir().unwrap_or_else(|| PathBuf::from("~"));
|
2020-09-07 02:06:13 +02:00
|
|
|
home_dir.push(&base_dir_name[2..]);
|
|
|
|
home_dir
|
|
|
|
} else {
|
|
|
|
PathBuf::from(base_dir_name)
|
|
|
|
}
|
2020-09-02 00:10:29 +02:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "directories"))]
|
|
|
|
{
|
2020-09-07 02:06:13 +02:00
|
|
|
PathBuf::from(base_dir_name)
|
2020-09-02 00:10:29 +02:00
|
|
|
}
|
2020-08-31 04:28:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(result) = base_dir.read_dir() {
|
|
|
|
result
|
|
|
|
.filter_map(|entry| {
|
|
|
|
entry.ok().and_then(|entry| {
|
|
|
|
let mut file_name = entry.file_name().to_string_lossy().into_owned();
|
2020-09-17 16:52:58 +02:00
|
|
|
if matcher.matches(partial, file_name.as_str()) {
|
2020-08-31 04:28:09 +02:00
|
|
|
let mut path = format!("{}{}", base_dir_name, file_name);
|
|
|
|
if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
|
|
|
|
path.push(std::path::MAIN_SEPARATOR);
|
|
|
|
file_name.push(std::path::MAIN_SEPARATOR);
|
|
|
|
}
|
2020-08-21 21:37:51 +02:00
|
|
|
|
2020-09-05 04:10:26 +02:00
|
|
|
Some(PathSuggestion {
|
|
|
|
path: entry.path(),
|
|
|
|
suggestion: Suggestion {
|
|
|
|
replacement: path,
|
|
|
|
display: file_name,
|
|
|
|
},
|
2020-08-31 04:28:09 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-08-21 21:37:51 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 06:37:43 +02:00
|
|
|
|
|
|
|
impl Completer for PathCompleter {
|
2020-09-17 16:52:58 +02:00
|
|
|
fn complete(
|
|
|
|
&self,
|
2020-09-19 23:29:51 +02:00
|
|
|
_ctx: &CompletionContext<'_>,
|
2020-09-17 16:52:58 +02:00
|
|
|
partial: &str,
|
|
|
|
matcher: &dyn Matcher,
|
|
|
|
) -> Vec<Suggestion> {
|
|
|
|
self.path_suggestions(partial, matcher)
|
2020-09-16 06:37:43 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|ps| ps.suggestion)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|