From e833d65b77211c76cd1eef1ac723efb3f61d38ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 29 Mar 2021 17:04:12 +0900 Subject: [PATCH] Teach file picker how to find the project root (.git). --- helix-term/src/commands.rs | 30 ++++++++++++++++++++++++++++-- helix-term/src/ui/mod.rs | 10 ++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index fa7a4162..166325b9 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -14,7 +14,7 @@ use crate::{ ui::{self, Completion, Picker, Popup, Prompt, PromptEvent}, }; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use helix_view::{ document::Mode, @@ -840,8 +840,34 @@ pub fn command_mode(cx: &mut Context) { ); cx.push_layer(Box::new(prompt)); } + +fn find_root(root: Option<&str>) -> Option { + let current_dir = std::env::current_dir().expect("unable to determine current directory"); + + let root = match root { + Some(root) => { + let root = Path::new(root); + if root.is_absolute() { + root.to_path_buf() + } else { + current_dir.join(root) + } + } + None => current_dir, + }; + + for ancestor in root.ancestors() { + // TODO: also use defined roots if git isn't found + if ancestor.join(".git").is_dir() { + return Some(ancestor.to_path_buf()); + } + } + None +} + pub fn file_picker(cx: &mut Context) { - let picker = ui::file_picker("./"); + let root = find_root(None).unwrap_or_else(|| PathBuf::from("./")); + let picker = ui::file_picker(root); cx.push_layer(Box::new(picker)); } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 0e38e598..47c75d2f 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -79,10 +79,9 @@ pub fn regex_prompt( ) } -pub fn file_picker(root: &str) -> Picker { +pub fn file_picker(root: PathBuf) -> Picker { use ignore::Walk; - // TODO: determine root based on git root - let files = Walk::new(root).filter_map(|entry| match entry { + let files = Walk::new(root.clone()).filter_map(|entry| match entry { Ok(entry) => { // filter dirs, but we might need special handling for symlinks! if !entry.file_type().unwrap().is_dir() { @@ -96,12 +95,11 @@ pub fn file_picker(root: &str) -> Picker { const MAX: usize = 1024; - use helix_view::Editor; Picker::new( files.take(MAX).collect(), - |path: &PathBuf| { + move |path: &PathBuf| { // format_fn - path.strip_prefix("./").unwrap().to_str().unwrap().into() + path.strip_prefix(&root).unwrap().to_str().unwrap().into() }, move |editor: &mut Editor, path: &PathBuf, action| { let document_id = editor