2021-03-27 04:06:40 +01:00
|
|
|
mod completion;
|
2021-08-08 07:07:14 +02:00
|
|
|
pub(crate) mod editor;
|
2021-06-19 17:54:37 +02:00
|
|
|
mod info;
|
2021-03-05 08:07:46 +01:00
|
|
|
mod markdown;
|
2021-02-09 07:40:30 +01:00
|
|
|
mod menu;
|
2020-12-17 10:08:16 +01:00
|
|
|
mod picker;
|
2021-02-25 10:07:47 +01:00
|
|
|
mod popup;
|
2020-12-13 05:35:30 +01:00
|
|
|
mod prompt;
|
2021-06-20 21:31:18 +02:00
|
|
|
mod spinner;
|
2021-03-02 10:24:24 +01:00
|
|
|
mod text;
|
2020-12-13 05:35:30 +01:00
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
pub use completion::Completion;
|
2020-12-13 05:35:30 +01:00
|
|
|
pub use editor::EditorView;
|
2021-03-05 08:07:46 +01:00
|
|
|
pub use markdown::Markdown;
|
2021-02-09 07:40:30 +01:00
|
|
|
pub use menu::Menu;
|
2021-08-12 09:00:42 +02:00
|
|
|
pub use picker::{FilePicker, Picker};
|
2021-02-25 10:07:47 +01:00
|
|
|
pub use popup::Popup;
|
2020-12-15 11:29:56 +01:00
|
|
|
pub use prompt::{Prompt, PromptEvent};
|
2021-06-20 21:31:18 +02:00
|
|
|
pub use spinner::{ProgressSpinners, Spinner};
|
2021-03-02 10:24:24 +01:00
|
|
|
pub use text::Text;
|
2020-12-13 05:35:30 +01:00
|
|
|
|
2021-01-22 09:13:14 +01:00
|
|
|
use helix_core::regex::Regex;
|
2021-09-20 06:45:07 +02:00
|
|
|
use helix_core::regex::RegexBuilder;
|
2021-07-01 19:41:20 +02:00
|
|
|
use helix_view::{Document, Editor, View};
|
2021-01-22 09:13:14 +01:00
|
|
|
|
2021-07-01 19:41:20 +02:00
|
|
|
use std::path::PathBuf;
|
2021-03-24 08:26:53 +01:00
|
|
|
|
2021-01-22 09:13:14 +01:00
|
|
|
pub fn regex_prompt(
|
|
|
|
cx: &mut crate::commands::Context,
|
2021-08-31 11:29:24 +02:00
|
|
|
prompt: std::borrow::Cow<'static, str>,
|
2021-09-08 07:52:09 +02:00
|
|
|
history_register: Option<char>,
|
2021-11-04 04:26:01 +01:00
|
|
|
completion_fn: impl FnMut(&str) -> Vec<prompt::Completion> + 'static,
|
2021-09-21 18:03:12 +02:00
|
|
|
fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
|
2021-01-22 09:13:14 +01:00
|
|
|
) -> Prompt {
|
2021-06-18 00:09:10 +02:00
|
|
|
let (view, doc) = current!(cx.editor);
|
|
|
|
let view_id = view.id;
|
|
|
|
let snapshot = doc.selection(view_id).clone();
|
2021-11-10 02:46:55 +01:00
|
|
|
let offset_snapshot = view.offset;
|
2021-01-22 09:13:14 +01:00
|
|
|
|
|
|
|
Prompt::new(
|
|
|
|
prompt,
|
2021-09-08 07:52:09 +02:00
|
|
|
history_register,
|
2021-11-04 04:26:01 +01:00
|
|
|
completion_fn,
|
2021-06-22 21:49:55 +02:00
|
|
|
move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
|
2021-01-22 09:13:14 +01:00
|
|
|
match event {
|
|
|
|
PromptEvent::Abort => {
|
2021-06-22 21:49:55 +02:00
|
|
|
let (view, doc) = current!(cx.editor);
|
2021-04-01 03:39:46 +02:00
|
|
|
doc.set_selection(view.id, snapshot.clone());
|
2021-11-10 02:46:55 +01:00
|
|
|
view.offset = offset_snapshot;
|
2021-01-22 09:13:14 +01:00
|
|
|
}
|
|
|
|
PromptEvent::Validate => {
|
2021-03-29 09:32:42 +02:00
|
|
|
// TODO: push_jump to store selection just before jump
|
2021-09-21 18:03:12 +02:00
|
|
|
|
|
|
|
match Regex::new(input) {
|
|
|
|
Ok(regex) => {
|
|
|
|
let (view, doc) = current!(cx.editor);
|
|
|
|
fun(view, doc, regex, event);
|
|
|
|
}
|
|
|
|
Err(_err) => (), // TODO: mark command line as error
|
|
|
|
}
|
2021-01-22 09:13:14 +01:00
|
|
|
}
|
|
|
|
PromptEvent::Update => {
|
2021-03-03 09:55:56 +01:00
|
|
|
// skip empty input, TODO: trigger default
|
|
|
|
if input.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-20 06:45:07 +02:00
|
|
|
let case_insensitive = if cx.editor.config.smart_case {
|
|
|
|
!input.chars().any(char::is_uppercase)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
match RegexBuilder::new(input)
|
|
|
|
.case_insensitive(case_insensitive)
|
|
|
|
.build()
|
|
|
|
{
|
2021-01-22 09:13:14 +01:00
|
|
|
Ok(regex) => {
|
2021-06-22 21:49:55 +02:00
|
|
|
let (view, doc) = current!(cx.editor);
|
2021-01-22 09:13:14 +01:00
|
|
|
|
|
|
|
// revert state to what it was before the last update
|
2021-04-01 03:39:46 +02:00
|
|
|
doc.set_selection(view.id, snapshot.clone());
|
2021-01-22 09:13:14 +01:00
|
|
|
|
2021-09-21 18:03:12 +02:00
|
|
|
fun(view, doc, regex, event);
|
2021-03-16 10:27:57 +01:00
|
|
|
|
2021-08-08 07:07:14 +02:00
|
|
|
view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
|
2021-01-22 09:13:14 +01:00
|
|
|
}
|
|
|
|
Err(_err) => (), // TODO: mark command line as error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-20 15:23:36 +01:00
|
|
|
pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePicker<PathBuf> {
|
2021-10-22 03:02:05 +02:00
|
|
|
use ignore::{types::TypesBuilder, WalkBuilder};
|
2021-06-26 04:09:17 +02:00
|
|
|
use std::time;
|
2021-10-22 03:02:05 +02:00
|
|
|
|
|
|
|
// We want to exclude files that the editor can't handle yet
|
|
|
|
let mut type_builder = TypesBuilder::new();
|
|
|
|
let mut walk_builder = WalkBuilder::new(&root);
|
2021-11-20 15:23:36 +01:00
|
|
|
walk_builder
|
|
|
|
.hidden(config.file_picker.hidden)
|
|
|
|
.parents(config.file_picker.parents)
|
|
|
|
.ignore(config.file_picker.ignore)
|
|
|
|
.git_ignore(config.file_picker.git_ignore)
|
|
|
|
.git_global(config.file_picker.git_global)
|
|
|
|
.git_exclude(config.file_picker.git_exclude)
|
|
|
|
.max_depth(config.file_picker.max_depth);
|
|
|
|
|
2021-10-22 03:02:05 +02:00
|
|
|
let walk_builder = match type_builder.add(
|
|
|
|
"compressed",
|
|
|
|
"*.{zip,gz,bz2,zst,lzo,sz,tgz,tbz2,lz,lz4,lzma,lzo,z,Z,xz,7z,rar,cab}",
|
|
|
|
) {
|
|
|
|
Err(_) => &walk_builder,
|
|
|
|
_ => {
|
|
|
|
type_builder.negate("all");
|
|
|
|
let excluded_types = type_builder.build().unwrap();
|
|
|
|
walk_builder.types(excluded_types)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let files = walk_builder.build().filter_map(|entry| {
|
2021-08-12 09:00:42 +02:00
|
|
|
let entry = entry.ok()?;
|
|
|
|
// Path::is_dir() traverses symlinks, so we use it over DirEntry::is_dir
|
|
|
|
if entry.path().is_dir() {
|
|
|
|
// Will give a false positive if metadata cannot be read (eg. permission error)
|
|
|
|
return None;
|
2020-12-18 11:19:50 +01:00
|
|
|
}
|
2021-08-12 09:00:42 +02:00
|
|
|
|
|
|
|
let time = entry.metadata().map_or(time::UNIX_EPOCH, |metadata| {
|
|
|
|
metadata
|
|
|
|
.accessed()
|
|
|
|
.or_else(|_| metadata.modified())
|
|
|
|
.or_else(|_| metadata.created())
|
|
|
|
.unwrap_or(time::UNIX_EPOCH)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some((entry.into_path(), time))
|
2020-12-18 11:19:50 +01:00
|
|
|
});
|
|
|
|
|
2021-06-26 04:09:17 +02:00
|
|
|
let mut files: Vec<_> = if root.join(".git").is_dir() {
|
2021-06-08 20:36:27 +02:00
|
|
|
files.collect()
|
|
|
|
} else {
|
|
|
|
const MAX: usize = 8192;
|
|
|
|
files.take(MAX).collect()
|
|
|
|
};
|
2020-12-18 11:19:50 +01:00
|
|
|
|
2021-06-28 06:08:38 +02:00
|
|
|
files.sort_by_key(|file| std::cmp::Reverse(file.1));
|
2021-06-26 04:09:17 +02:00
|
|
|
|
|
|
|
let files = files.into_iter().map(|(path, _)| path).collect();
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
FilePicker::new(
|
2021-06-08 20:36:27 +02:00
|
|
|
files,
|
2021-03-29 10:04:12 +02:00
|
|
|
move |path: &PathBuf| {
|
2020-12-18 11:19:50 +01:00
|
|
|
// format_fn
|
2021-04-01 04:01:11 +02:00
|
|
|
path.strip_prefix(&root)
|
|
|
|
.unwrap_or(path)
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.into()
|
2020-12-18 11:19:50 +01:00
|
|
|
},
|
2021-03-29 08:21:48 +02:00
|
|
|
move |editor: &mut Editor, path: &PathBuf, action| {
|
2021-07-01 20:57:12 +02:00
|
|
|
editor
|
2021-03-29 08:21:48 +02:00
|
|
|
.open(path.into(), action)
|
2021-03-24 06:28:26 +01:00
|
|
|
.expect("editor.open failed");
|
2020-12-18 11:19:50 +01:00
|
|
|
},
|
2021-08-12 09:00:42 +02:00
|
|
|
|_editor, path| Some((path.clone(), None)),
|
2020-12-18 11:19:50 +01:00
|
|
|
)
|
|
|
|
}
|
2020-12-21 08:23:05 +01:00
|
|
|
|
2021-03-01 10:02:31 +01:00
|
|
|
pub mod completers {
|
2021-03-22 05:16:56 +01:00
|
|
|
use crate::ui::prompt::Completion;
|
2021-06-19 13:27:06 +02:00
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
|
|
|
use fuzzy_matcher::FuzzyMatcher;
|
|
|
|
use helix_view::theme;
|
2021-07-01 19:41:20 +02:00
|
|
|
use std::borrow::Cow;
|
2021-06-19 13:27:06 +02:00
|
|
|
use std::cmp::Reverse;
|
2021-05-07 10:19:45 +02:00
|
|
|
|
|
|
|
pub type Completer = fn(&str) -> Vec<Completion>;
|
|
|
|
|
2021-06-19 13:27:06 +02:00
|
|
|
pub fn theme(input: &str) -> Vec<Completion> {
|
|
|
|
let mut names = theme::Loader::read_names(&helix_core::runtime_dir().join("themes"));
|
|
|
|
names.extend(theme::Loader::read_names(
|
|
|
|
&helix_core::config_dir().join("themes"),
|
|
|
|
));
|
|
|
|
names.push("default".into());
|
2021-11-14 13:26:48 +01:00
|
|
|
names.push("base16_default".into());
|
2021-06-19 13:27:06 +02:00
|
|
|
|
|
|
|
let mut names: Vec<_> = names
|
|
|
|
.into_iter()
|
|
|
|
.map(|name| ((0..), Cow::from(name)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let matcher = Matcher::default();
|
|
|
|
|
|
|
|
let mut matches: Vec<_> = names
|
|
|
|
.into_iter()
|
2021-07-01 20:57:12 +02:00
|
|
|
.filter_map(|(_range, name)| {
|
2021-06-27 06:27:35 +02:00
|
|
|
matcher.fuzzy_match(&name, input).map(|score| (name, score))
|
2021-06-19 13:27:06 +02:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
|
|
|
|
names = matches.into_iter().map(|(name, _)| ((0..), name)).collect();
|
|
|
|
|
|
|
|
names
|
|
|
|
}
|
|
|
|
|
2021-03-22 05:16:56 +01:00
|
|
|
pub fn filename(input: &str) -> Vec<Completion> {
|
2021-06-21 17:40:27 +02:00
|
|
|
filename_impl(input, |entry| {
|
|
|
|
let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
|
|
|
|
|
|
|
|
if is_dir {
|
|
|
|
FileMatch::AcceptIncomplete
|
|
|
|
} else {
|
|
|
|
FileMatch::Accept
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn directory(input: &str) -> Vec<Completion> {
|
|
|
|
filename_impl(input, |entry| {
|
|
|
|
let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
|
|
|
|
|
|
|
|
if is_dir {
|
|
|
|
FileMatch::Accept
|
|
|
|
} else {
|
|
|
|
FileMatch::Reject
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
enum FileMatch {
|
|
|
|
/// Entry should be ignored
|
|
|
|
Reject,
|
|
|
|
/// Entry is usable but can't be the end (for instance if the entry is a directory and we
|
|
|
|
/// try to match a file)
|
|
|
|
AcceptIncomplete,
|
|
|
|
/// Entry is usable and can be the end of the match
|
|
|
|
Accept,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: we could return an iter/lazy thing so it can fetch as many as it needs.
|
|
|
|
fn filename_impl<F>(input: &str, filter_fn: F) -> Vec<Completion>
|
|
|
|
where
|
|
|
|
F: Fn(&ignore::DirEntry) -> FileMatch,
|
|
|
|
{
|
2021-03-01 10:02:31 +01:00
|
|
|
// Rust's filename handling is really annoying.
|
|
|
|
|
|
|
|
use ignore::WalkBuilder;
|
2021-07-01 19:41:20 +02:00
|
|
|
use std::path::Path;
|
2021-03-01 10:02:31 +01:00
|
|
|
|
2021-06-18 08:19:34 +02:00
|
|
|
let is_tilde = input.starts_with('~') && input.len() == 1;
|
2021-08-25 03:04:05 +02:00
|
|
|
let path = helix_core::path::expand_tilde(Path::new(input));
|
2021-03-01 10:02:31 +01:00
|
|
|
|
|
|
|
let (dir, file_name) = if input.ends_with('/') {
|
2021-06-18 08:19:34 +02:00
|
|
|
(path, None)
|
2021-03-01 10:02:31 +01:00
|
|
|
} else {
|
|
|
|
let file_name = path
|
|
|
|
.file_name()
|
|
|
|
.map(|file| file.to_str().unwrap().to_owned());
|
|
|
|
|
|
|
|
let path = match path.parent() {
|
|
|
|
Some(path) if !path.as_os_str().is_empty() => path.to_path_buf(),
|
|
|
|
// Path::new("h")'s parent is Some("")...
|
|
|
|
_ => std::env::current_dir().expect("couldn't determine current directory"),
|
|
|
|
};
|
|
|
|
|
|
|
|
(path, file_name)
|
|
|
|
};
|
|
|
|
|
2021-07-01 20:57:12 +02:00
|
|
|
let end = input.len()..;
|
2021-03-21 06:13:49 +01:00
|
|
|
|
2021-08-26 17:30:47 +02:00
|
|
|
let mut files: Vec<_> = WalkBuilder::new(&dir)
|
|
|
|
.hidden(false)
|
2021-03-01 10:02:31 +01:00
|
|
|
.max_depth(Some(1))
|
|
|
|
.build()
|
|
|
|
.filter_map(|file| {
|
2021-06-21 17:40:27 +02:00
|
|
|
file.ok().and_then(|entry| {
|
|
|
|
let fmatch = filter_fn(&entry);
|
|
|
|
|
|
|
|
if fmatch == FileMatch::Reject {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-07-01 20:57:12 +02:00
|
|
|
//let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
|
2021-03-01 10:02:31 +01:00
|
|
|
|
2021-04-01 04:01:11 +02:00
|
|
|
let path = entry.path();
|
2021-06-18 08:19:34 +02:00
|
|
|
let mut path = if is_tilde {
|
|
|
|
// if it's a single tilde an absolute path is displayed so that when `TAB` is pressed on
|
|
|
|
// one of the directories the tilde will be replaced with a valid path not with a relative
|
|
|
|
// home directory name.
|
|
|
|
// ~ -> <TAB> -> /home/user
|
|
|
|
// ~/ -> <TAB> -> ~/first_entry
|
|
|
|
path.to_path_buf()
|
|
|
|
} else {
|
|
|
|
path.strip_prefix(&dir).unwrap_or(path).to_path_buf()
|
|
|
|
};
|
2021-03-01 10:02:31 +01:00
|
|
|
|
2021-06-21 17:40:27 +02:00
|
|
|
if fmatch == FileMatch::AcceptIncomplete {
|
2021-03-01 10:02:31 +01:00
|
|
|
path.push("");
|
|
|
|
}
|
2021-06-21 17:40:27 +02:00
|
|
|
|
2021-04-01 04:01:11 +02:00
|
|
|
let path = path.to_str().unwrap().to_owned();
|
2021-06-21 17:40:27 +02:00
|
|
|
Some((end.clone(), Cow::from(path)))
|
2021-03-01 10:02:31 +01:00
|
|
|
})
|
|
|
|
}) // TODO: unwrap or skip
|
2021-03-21 06:13:49 +01:00
|
|
|
.filter(|(_, path)| !path.is_empty()) // TODO
|
2021-03-01 10:02:31 +01:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// if empty, return a list of dirs and files in current dir
|
|
|
|
if let Some(file_name) = file_name {
|
|
|
|
let matcher = Matcher::default();
|
|
|
|
|
|
|
|
// inefficient, but we need to calculate the scores, filter out None, then sort.
|
|
|
|
let mut matches: Vec<_> = files
|
|
|
|
.into_iter()
|
2021-07-01 20:57:12 +02:00
|
|
|
.filter_map(|(_range, file)| {
|
2021-03-01 10:02:31 +01:00
|
|
|
matcher
|
|
|
|
.fuzzy_match(&file, &file_name)
|
|
|
|
.map(|score| (file, score))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-07-01 20:57:12 +02:00
|
|
|
let range = (input.len().saturating_sub(file_name.len()))..;
|
2021-03-21 06:13:49 +01:00
|
|
|
|
2021-03-01 10:02:31 +01:00
|
|
|
matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
|
2021-03-21 06:13:49 +01:00
|
|
|
files = matches
|
|
|
|
.into_iter()
|
|
|
|
.map(|(file, _)| (range.clone(), file))
|
|
|
|
.collect();
|
2021-03-01 10:02:31 +01:00
|
|
|
|
|
|
|
// TODO: complete to longest common match
|
|
|
|
}
|
|
|
|
|
|
|
|
files
|
|
|
|
}
|
|
|
|
}
|