2021-03-27 04:06:40 +01:00
|
|
|
mod completion;
|
2020-12-13 05:35:30 +01:00
|
|
|
mod editor;
|
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-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;
|
2020-12-17 10:08:16 +01:00
|
|
|
pub use picker::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-03-02 10:24:24 +01:00
|
|
|
pub use text::Text;
|
2020-12-13 05:35:30 +01:00
|
|
|
|
|
|
|
pub use tui::layout::Rect;
|
|
|
|
pub use tui::style::{Color, Modifier, Style};
|
|
|
|
|
2021-01-22 09:13:14 +01:00
|
|
|
use helix_core::regex::Regex;
|
2021-06-15 05:26:05 +02:00
|
|
|
use helix_core::register::Registers;
|
2021-04-14 08:30:15 +02:00
|
|
|
use helix_view::{Document, Editor, View};
|
2021-01-22 09:13:14 +01:00
|
|
|
|
2021-03-24 08:26:53 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2021-01-22 09:13:14 +01:00
|
|
|
pub fn regex_prompt(
|
|
|
|
cx: &mut crate::commands::Context,
|
|
|
|
prompt: String,
|
2021-06-15 05:26:05 +02:00
|
|
|
fun: impl Fn(&mut View, &mut Document, &mut Registers, Regex) + 'static,
|
2021-01-22 09:13:14 +01:00
|
|
|
) -> Prompt {
|
2021-03-31 10:17:01 +02:00
|
|
|
let view_id = cx.view().id;
|
|
|
|
let snapshot = cx.doc().selection(view_id).clone();
|
2021-01-22 09:13:14 +01:00
|
|
|
|
|
|
|
Prompt::new(
|
|
|
|
prompt,
|
|
|
|
|input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
|
|
|
|
move |editor: &mut Editor, input: &str, event: PromptEvent| {
|
|
|
|
match event {
|
|
|
|
PromptEvent::Abort => {
|
2021-03-18 07:07:02 +01:00
|
|
|
// TODO: also revert text
|
2021-04-01 03:39:46 +02:00
|
|
|
let (view, doc) = editor.current();
|
|
|
|
doc.set_selection(view.id, snapshot.clone());
|
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-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-01-22 09:13:14 +01:00
|
|
|
match Regex::new(input) {
|
|
|
|
Ok(regex) => {
|
2021-06-15 05:26:05 +02:00
|
|
|
let (view, doc, registers) = editor.current_with_registers();
|
2021-01-22 09:13:14 +01:00
|
|
|
|
|
|
|
// revert state to what it was before the last update
|
2021-03-18 07:07:02 +01:00
|
|
|
// TODO: also revert text
|
2021-04-01 03:39:46 +02:00
|
|
|
doc.set_selection(view.id, snapshot.clone());
|
2021-01-22 09:13:14 +01:00
|
|
|
|
2021-06-15 05:26:05 +02:00
|
|
|
fun(view, doc, registers, regex);
|
2021-03-16 10:27:57 +01:00
|
|
|
|
2021-04-01 03:39:46 +02:00
|
|
|
view.ensure_cursor_in_view(doc);
|
2021-01-22 09:13:14 +01:00
|
|
|
}
|
|
|
|
Err(_err) => (), // TODO: mark command line as error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:04:12 +02:00
|
|
|
pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
|
2020-12-18 11:19:50 +01:00
|
|
|
use ignore::Walk;
|
2021-03-29 10:04:12 +02:00
|
|
|
let files = Walk::new(root.clone()).filter_map(|entry| match entry {
|
2020-12-18 11:19:50 +01:00
|
|
|
Ok(entry) => {
|
|
|
|
// filter dirs, but we might need special handling for symlinks!
|
2021-04-01 04:01:11 +02:00
|
|
|
if !entry.file_type().map_or(false, |entry| entry.is_dir()) {
|
2020-12-18 11:19:50 +01:00
|
|
|
Some(entry.into_path())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_err) => None,
|
|
|
|
});
|
|
|
|
|
2021-06-08 20:36:27 +02:00
|
|
|
let files = if root.join(".git").is_dir() {
|
|
|
|
files.collect()
|
|
|
|
} else {
|
|
|
|
const MAX: usize = 8192;
|
|
|
|
files.take(MAX).collect()
|
|
|
|
};
|
2020-12-18 11:19:50 +01:00
|
|
|
|
|
|
|
Picker::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-03-24 06:28:26 +01:00
|
|
|
let document_id = 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
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
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;
|
|
|
|
use std::borrow::Cow;
|
2021-05-07 10:19:45 +02:00
|
|
|
|
|
|
|
pub type Completer = fn(&str) -> Vec<Completion>;
|
|
|
|
|
2021-03-01 10:02:31 +01:00
|
|
|
// TODO: we could return an iter/lazy thing so it can fetch as many as it needs.
|
2021-03-22 05:16:56 +01:00
|
|
|
pub fn filename(input: &str) -> Vec<Completion> {
|
2021-03-01 10:02:31 +01:00
|
|
|
// Rust's filename handling is really annoying.
|
|
|
|
|
|
|
|
use ignore::WalkBuilder;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
let path = Path::new(input);
|
|
|
|
|
|
|
|
let (dir, file_name) = if input.ends_with('/') {
|
|
|
|
(path.into(), None)
|
|
|
|
} 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-03-21 06:13:49 +01:00
|
|
|
let end = (input.len()..);
|
|
|
|
|
2021-03-01 10:02:31 +01:00
|
|
|
let mut files: Vec<_> = WalkBuilder::new(dir.clone())
|
|
|
|
.max_depth(Some(1))
|
|
|
|
.build()
|
|
|
|
.filter_map(|file| {
|
|
|
|
file.ok().map(|entry| {
|
2021-03-31 16:42:16 +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();
|
|
|
|
let mut path = path.strip_prefix(&dir).unwrap_or(path).to_path_buf();
|
2021-03-01 10:02:31 +01:00
|
|
|
|
|
|
|
if is_dir {
|
|
|
|
path.push("");
|
|
|
|
}
|
2021-04-01 04:01:11 +02:00
|
|
|
let path = path.to_str().unwrap().to_owned();
|
2021-03-21 06:13:49 +01:00
|
|
|
(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 {
|
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
|
|
|
use fuzzy_matcher::FuzzyMatcher;
|
|
|
|
use std::cmp::Reverse;
|
|
|
|
|
|
|
|
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-03-21 06:13:49 +01: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-03-21 06:13:49 +01:00
|
|
|
let range = ((input.len() - file_name.len())..);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|