on master help sign in
Teach file picker how to find the project root (.git).
e833d65Blaž Hrastnik
helix-term/src/commands.rs+28−2
@@ -14,7 +14,7 @@ use crate::{
14 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
14 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
15};
15};
16
16
17−use std::path::PathBuf;
17+use std::path::{Path, PathBuf};
18
18
19use helix_view::{
19use helix_view::{
20 document::Mode,
20 document::Mode,
@@ -840,8 +840,34 @@ pub fn command_mode(cx: &mut Context) {
840 );
840 );
841 cx.push_layer(Box::new(prompt));
841 cx.push_layer(Box::new(prompt));
842}
842}
843+
844+fn find_root(root: Option<&str>) -> Option<PathBuf> {
845+ let current_dir = std::env::current_dir().expect("unable to determine current directory");
846+
847+ let root = match root {
848+ Some(root) => {
849+ let root = Path::new(root);
850+ if root.is_absolute() {
851+ root.to_path_buf()
852+ } else {
853+ current_dir.join(root)
854+ }
855+ }
856+ None => current_dir,
857+ };
858+
859+ for ancestor in root.ancestors() {
860+ // TODO: also use defined roots if git isn't found
861+ if ancestor.join(".git").is_dir() {
862+ return Some(ancestor.to_path_buf());
863+ }
864+ }
865+ None
866+}
867+
843pub fn file_picker(cx: &mut Context) {
868pub fn file_picker(cx: &mut Context) {
869+ let root = find_root(None).unwrap_or_else(|| PathBuf::from("./"));
844− let picker = ui::file_picker("./");
870+ let picker = ui::file_picker(root);
845 cx.push_layer(Box::new(picker));
871 cx.push_layer(Box::new(picker));
846}
872}
847
873
helix-term/src/ui/mod.rs+4−6
@@ -79,10 +79,9 @@ pub fn regex_prompt(
79 )
79 )
80}
80}
81
81
82−pub fn file_picker(root: &str) -> Picker<PathBuf> {
82+pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
83 use ignore::Walk;
83 use ignore::Walk;
84− // TODO: determine root based on git root
85− let files = Walk::new(root).filter_map(|entry| match entry {
84+ let files = Walk::new(root.clone()).filter_map(|entry| match entry {
86 Ok(entry) => {
85 Ok(entry) => {
87 // filter dirs, but we might need special handling for symlinks!
86 // filter dirs, but we might need special handling for symlinks!
88 if !entry.file_type().unwrap().is_dir() {
87 if !entry.file_type().unwrap().is_dir() {
@@ -96,12 +95,11 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
96
95
97 const MAX: usize = 1024;
96 const MAX: usize = 1024;
98
97
99− use helix_view::Editor;
100 Picker::new(
98 Picker::new(
101 files.take(MAX).collect(),
99 files.take(MAX).collect(),
102− |path: &PathBuf| {
100+ move |path: &PathBuf| {
103 // format_fn
101 // format_fn
104− path.strip_prefix("./").unwrap().to_str().unwrap().into()
102+ path.strip_prefix(&root).unwrap().to_str().unwrap().into()
105 },
103 },
106 move |editor: &mut Editor, path: &PathBuf, action| {
104 move |editor: &mut Editor, path: &PathBuf, action| {
107 let document_id = editor
105 let document_id = editor