on master help sign in
Teach file picker how to find the project root (.git).
commite833d65b77211c76cd1eef1ac723efb3f61d38ba authorBlaž Hrastnik <blaz@mxxn.io> dateMon 29 Mar 2021, 17:04 (6 years ago) parent8098e9b
2 files changed+32−8
helix-term/src/commands.rs+28−2 helix-term/src/ui/mod.rs+4−6
helix-term/src/commands.rs+28−2
@@ -14,7 +14,7 @@ use crate::{
1414 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
1515};
1616
17−use std::path::PathBuf;
17+use std::path::{Path, PathBuf};
1818
1919use helix_view::{
2020 document::Mode,
@@ -840,8 +840,34 @@ pub fn command_mode(cx: &mut Context) {
840840 );
841841 cx.push_layer(Box::new(prompt));
842842}
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+
843868pub fn file_picker(cx: &mut Context) {
844− let picker = ui::file_picker("./");
869+ let root = find_root(None).unwrap_or_else(|| PathBuf::from("./"));
870+ let picker = ui::file_picker(root);
845871 cx.push_layer(Box::new(picker));
846872}
847873
helix-term/src/ui/mod.rs+4−6
@@ -79,10 +79,9 @@ pub fn regex_prompt(
7979 )
8080}
8181
82−pub fn file_picker(root: &str) -> Picker<PathBuf> {
82+pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
8383 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 {
8685 Ok(entry) => {
8786 // filter dirs, but we might need special handling for symlinks!
8887 if !entry.file_type().unwrap().is_dir() {
@@ -96,12 +95,11 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
9695
9796 const MAX: usize = 1024;
9897
99− use helix_view::Editor;
10098 Picker::new(
10199 files.take(MAX).collect(),
102− |path: &PathBuf| {
100+ move |path: &PathBuf| {
103101 // format_fn
104− path.strip_prefix("./").unwrap().to_str().unwrap().into()
102+ path.strip_prefix(&root).unwrap().to_str().unwrap().into()
105103 },
106104 move |editor: &mut Editor, path: &PathBuf, action| {
107105 let document_id = editor