From 7b0f7dbfbe0f44666daae84f359afb83612b5954 Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Mon, 22 Jul 2024 12:49:37 +0200 Subject: [PATCH 1/9] Add file browser --- helix-term/src/commands.rs | 12 ++++++ helix-term/src/ui/mod.rs | 53 +++++++++++++++++++++++ helix-term/src/ui/picker.rs | 84 +++++++++++++++++++++++++++---------- 3 files changed, 128 insertions(+), 21 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b1c29378..231372a1 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,6 +375,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", + file_browser, "Open file browser at current buffer's directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2948,6 +2949,17 @@ fn file_picker_in_current_directory(cx: &mut Context) { cx.push_layer(Box::new(overlaid(picker))); } +fn file_browser(cx: &mut Context) { + let cwd = helix_stdx::env::current_working_dir(); + if !cwd.exists() { + cx.editor + .set_error("Current working directory does not exist"); + return; + } + let picker = ui::file_browser(cwd, &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); +} + fn buffer_picker(cx: &mut Context) { let current = view!(cx.editor).doc; diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 6a3e198c..49a3d87a 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -30,6 +30,7 @@ pub use text::Text; use helix_view::Editor; +use std::path::Path; use std::{error::Error, path::PathBuf}; pub fn prompt( @@ -265,6 +266,58 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi picker } +pub fn file_browser(root: PathBuf, _config: &helix_view::editor::Config) -> FilePicker { + let directory_content = directory_content(&root); + + let columns = [PickerColumn::new( + "path", + |item: &PathBuf, root: &PathBuf| { + item.strip_prefix(root) + .unwrap_or(item) + .to_string_lossy() + .into() + }, + )]; + let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| { + if let Err(e) = cx.editor.open(path, action) { + let err = if let Some(err) = e.source() { + format!("{}", err) + } else { + format!("unable to open \"{}\"", path.display()) + }; + cx.editor.set_error(err); + } + }) + .with_preview(|_editor, path| Some((path.as_path().into(), None))); + let injector = picker.injector(); + + if let Ok(files) = directory_content { + for file in files { + if injector.push(file).is_err() { + break; + } + } + } + picker +} + +fn directory_content(path: &Path) -> Result, std::io::Error> { + let mut dirs = Vec::new(); + let mut files = Vec::new(); + for entry in std::fs::read_dir(path)?.flatten() { + let entry_path = entry.path(); + if entry.path().is_dir() { + dirs.push(entry_path); + } else { + files.push(entry_path); + } + } + dirs.sort(); + files.sort(); + dirs.extend(files); + Ok(dirs) +} + pub mod completers { use crate::ui::prompt::Completion; use helix_core::fuzzy::fuzzy_match; diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 82fe9689..766bbf35 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -99,6 +99,7 @@ pub type FileLocation<'a> = (PathOrId<'a>, Option<(usize, usize)>); pub enum CachedPreview { Document(Box), + Directory(Vec), Binary, LargeFile, NotFound, @@ -120,12 +121,20 @@ impl Preview<'_, '_> { } } + fn dir_content(&self) -> Option<&Vec> { + match self { + Preview::Cached(CachedPreview::Directory(dir_content)) => Some(dir_content), + _ => None, + } + } + /// Alternate text to show for the preview. fn placeholder(&self) -> &str { match *self { Self::EditorDocument(_) => "", Self::Cached(preview) => match preview { CachedPreview::Document(_) => "", + CachedPreview::Directory(_) => "", CachedPreview::Binary => "", CachedPreview::LargeFile => "", CachedPreview::NotFound => "", @@ -599,33 +608,52 @@ impl Picker { } let path: Arc = path.into(); - let data = std::fs::File::open(&path).and_then(|file| { - let metadata = file.metadata()?; - // Read up to 1kb to detect the content type - let n = file.take(1024).read_to_end(&mut self.read_buffer)?; - let content_type = content_inspector::inspect(&self.read_buffer[..n]); - self.read_buffer.clear(); - Ok((metadata, content_type)) - }); - let preview = data - .map( - |(metadata, content_type)| match (metadata.len(), content_type) { - (_, content_inspector::ContentType::BINARY) => CachedPreview::Binary, - (size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => { - CachedPreview::LargeFile + let preview = std::fs::metadata(&path) + .and_then(|metadata| { + if metadata.is_dir() { + let files = super::directory_content(&path)?; + let file_names: Vec<_> = files + .iter() + .filter_map(|file| file.file_name()) + .map(|name| name.to_string_lossy().into_owned()) + .collect(); + Ok(CachedPreview::Directory(file_names)) + } else if metadata.is_file() { + if metadata.len() > MAX_FILE_SIZE_FOR_PREVIEW { + return Ok(CachedPreview::LargeFile); } - _ => Document::open(&path, None, None, editor.config.clone()) - .map(|doc| { + let content_type = std::fs::File::open(&path).and_then(|file| { + // Read up to 1kb to detect the content type + let n = file.take(1024).read_to_end(&mut self.read_buffer)?; + let content_type = + content_inspector::inspect(&self.read_buffer[..n]); + self.read_buffer.clear(); + Ok(content_type) + })?; + if content_type.is_binary() { + return Ok(CachedPreview::Binary); + } + Document::open(&path, None, None, editor.config.clone()).map_or( + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Cannot open document", + )), + |doc| { // Asynchronously highlight the new document helix_event::send_blocking( &self.preview_highlight_handler, path.clone(), ); - CachedPreview::Document(Box::new(doc)) - }) - .unwrap_or(CachedPreview::NotFound), - }, - ) + Ok(CachedPreview::Document(Box::new(doc))) + }, + ) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Neither a dir, nor a file", + )) + } + }) .unwrap_or(CachedPreview::NotFound); self.preview_cache.insert(path.clone(), preview); Some((Preview::Cached(&self.preview_cache[&path]), range)) @@ -859,6 +887,20 @@ impl Picker { doc } _ => { + if let Some(dir_content) = preview.dir_content() { + for (i, entry) in dir_content.iter().take(inner.height as usize).enumerate() + { + surface.set_stringn( + inner.x, + inner.y + i as u16, + entry, + inner.width as usize, + text, + ); + } + return; + } + let alt_text = preview.placeholder(); let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2; let y = inner.y + inner.height / 2; From 084d1811ea9d82cda9bea032b97957590e923779 Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Tue, 23 Jul 2024 17:18:19 +0200 Subject: [PATCH 2/9] Implement opening of folders --- helix-term/src/commands.rs | 2 +- helix-term/src/ui/mod.rs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 231372a1..9bd1a561 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2956,7 +2956,7 @@ fn file_browser(cx: &mut Context) { .set_error("Current working directory does not exist"); return; } - let picker = ui::file_browser(cwd, &cx.editor.config()); + let picker = ui::file_browser(cwd); cx.push_layer(Box::new(overlaid(picker))); } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 49a3d87a..c9814ca4 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -266,7 +266,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi picker } -pub fn file_browser(root: PathBuf, _config: &helix_view::editor::Config) -> FilePicker { +pub fn file_browser(root: PathBuf) -> FilePicker { let directory_content = directory_content(&root); let columns = [PickerColumn::new( @@ -279,7 +279,18 @@ pub fn file_browser(root: PathBuf, _config: &helix_view::editor::Config) -> File }, )]; let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| { - if let Err(e) = cx.editor.open(path, action) { + if path.is_dir() { + let owned_path = path.clone(); + let callback = Box::pin(async move { + let call: Callback = + Callback::EditorCompositor(Box::new(move |_editor, compositor| { + let picker = file_browser(owned_path); + compositor.push(Box::new(overlay::overlaid(picker))); + })); + Ok(call) + }); + cx.jobs.callback(callback); + } else if let Err(e) = cx.editor.open(path, action) { let err = if let Some(err) = e.source() { format!("{}", err) } else { From 8f5e4dff29516f2776e94b2617abcfe1f4c6408a Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Fri, 9 Aug 2024 20:58:46 +0200 Subject: [PATCH 3/9] Pass directory content into picker directly --- helix-term/src/commands.rs | 7 ++-- helix-term/src/ui/mod.rs | 65 +++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 9bd1a561..8685a47d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,7 +375,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", - file_browser, "Open file browser at current buffer's directory", + file_browser, "Open file browser at current working directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2956,8 +2956,9 @@ fn file_browser(cx: &mut Context) { .set_error("Current working directory does not exist"); return; } - let picker = ui::file_browser(cwd); - cx.push_layer(Box::new(overlaid(picker))); + if let Ok(picker) = ui::file_browser(cwd) { + cx.push_layer(Box::new(overlaid(picker))); + } } fn buffer_picker(cx: &mut Context) { diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index c9814ca4..ae06013b 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -266,8 +266,8 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi picker } -pub fn file_browser(root: PathBuf) -> FilePicker { - let directory_content = directory_content(&root); +pub fn file_browser(root: PathBuf) -> Result { + let directory_content = directory_content(&root)?; let columns = [PickerColumn::new( "path", @@ -278,38 +278,37 @@ pub fn file_browser(root: PathBuf) -> FilePicker { .into() }, )]; - let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| { - if path.is_dir() { - let owned_path = path.clone(); - let callback = Box::pin(async move { - let call: Callback = - Callback::EditorCompositor(Box::new(move |_editor, compositor| { - let picker = file_browser(owned_path); - compositor.push(Box::new(overlay::overlaid(picker))); - })); - Ok(call) - }); - cx.jobs.callback(callback); - } else if let Err(e) = cx.editor.open(path, action) { - let err = if let Some(err) = e.source() { - format!("{}", err) - } else { - format!("unable to open \"{}\"", path.display()) - }; - cx.editor.set_error(err); - } - }) - .with_preview(|_editor, path| Some((path.as_path().into(), None))); - let injector = picker.injector(); - - if let Ok(files) = directory_content { - for file in files { - if injector.push(file).is_err() { - break; + let picker = Picker::new( + columns, + 0, + directory_content, + root, + move |cx, path: &PathBuf, action| { + if path.is_dir() { + let owned_path = path.clone(); + let callback = Box::pin(async move { + let call: Callback = + Callback::EditorCompositor(Box::new(move |_editor, compositor| { + if let Ok(picker) = file_browser(owned_path) { + compositor.push(Box::new(overlay::overlaid(picker))); + } + })); + Ok(call) + }); + cx.jobs.callback(callback); + } else if let Err(e) = cx.editor.open(path, action) { + let err = if let Some(err) = e.source() { + format!("{}", err) + } else { + format!("unable to open \"{}\"", path.display()) + }; + cx.editor.set_error(err); } - } - } - picker + }, + ) + .with_preview(|_editor, path| Some((path.as_path().into(), None))); + + Ok(picker) } fn directory_content(path: &Path) -> Result, std::io::Error> { From f23e98233820c566efe2226d7b8b8986867fa81f Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Sat, 10 Aug 2024 11:26:56 +0200 Subject: [PATCH 4/9] Add parent folder to file browser --- helix-term/src/ui/mod.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index ae06013b..62c9fb91 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -267,6 +267,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi } pub fn file_browser(root: PathBuf) -> Result { + let root = root.canonicalize()?; let directory_content = directory_content(&root)?; let columns = [PickerColumn::new( @@ -324,8 +325,15 @@ fn directory_content(path: &Path) -> Result, std::io::Error> { } dirs.sort(); files.sort(); - dirs.extend(files); - Ok(dirs) + + let mut content = Vec::new(); + if path.parent().is_some() { + log::warn!("{}", path.to_string_lossy()); + content.insert(0, path.join("..")); + } + content.extend(dirs); + content.extend(files); + Ok(content) } pub mod completers { From f4fb8fa4ef778a6bc39a96b06ee44f204fae001b Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Fri, 30 Aug 2024 21:12:47 +0200 Subject: [PATCH 5/9] Open file browser in buffer's directory --- helix-term/src/commands.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 8685a47d..c4f99ee3 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,7 +375,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", - file_browser, "Open file browser at current working directory", + file_browser, "Open file browser at current buffer's directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2950,13 +2950,19 @@ fn file_picker_in_current_directory(cx: &mut Context) { } fn file_browser(cx: &mut Context) { - let cwd = helix_stdx::env::current_working_dir(); - if !cwd.exists() { - cx.editor - .set_error("Current working directory does not exist"); - return; - } - if let Ok(picker) = ui::file_browser(cwd) { + let doc_dir = doc!(cx.editor) + .path() + .and_then(|path| path.parent().map(|path| path.to_path_buf())); + + let path = match doc_dir { + Some(path) => path, + None => { + cx.editor.set_error("Current buffer has no path or parent"); + return; + } + }; + + if let Ok(picker) = ui::file_browser(path) { cx.push_layer(Box::new(overlaid(picker))); } } From 7e58404172cd8ca9c63f986f9551397b67c5e85f Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Fri, 13 Sep 2024 13:44:30 +0200 Subject: [PATCH 6/9] Open file browser in cwd when no buffer path --- helix-term/src/commands.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c4f99ee3..7decc577 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2957,8 +2957,14 @@ fn file_browser(cx: &mut Context) { let path = match doc_dir { Some(path) => path, None => { - cx.editor.set_error("Current buffer has no path or parent"); - return; + let cwd = helix_stdx::env::current_working_dir(); + if !cwd.exists() { + cx.editor.set_error( + "Current buffer has no parent and current working directory does not exist", + ); + return; + } + cwd } }; From adc72f2d875c15cd9a4f895f36c9fd7599eb0c00 Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Thu, 3 Oct 2024 10:20:43 +0200 Subject: [PATCH 7/9] Distinguish dirs visually in file_browser --- helix-term/src/ui/mod.rs | 16 ++++++++-------- helix-term/src/ui/picker.rs | 10 ++++++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 62c9fb91..2c105aba 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -273,10 +273,12 @@ pub fn file_browser(root: PathBuf) -> Result { let columns = [PickerColumn::new( "path", |item: &PathBuf, root: &PathBuf| { - item.strip_prefix(root) - .unwrap_or(item) - .to_string_lossy() - .into() + let name = item.strip_prefix(root).unwrap_or(item).to_string_lossy(); + if item.is_dir() { + format!("{}/", name).into() + } else { + name.into() + } }, )]; let picker = Picker::new( @@ -316,11 +318,10 @@ fn directory_content(path: &Path) -> Result, std::io::Error> { let mut dirs = Vec::new(); let mut files = Vec::new(); for entry in std::fs::read_dir(path)?.flatten() { - let entry_path = entry.path(); if entry.path().is_dir() { - dirs.push(entry_path); + dirs.push(entry.path()); } else { - files.push(entry_path); + files.push(entry.path()); } } dirs.sort(); @@ -328,7 +329,6 @@ fn directory_content(path: &Path) -> Result, std::io::Error> { let mut content = Vec::new(); if path.parent().is_some() { - log::warn!("{}", path.to_string_lossy()); content.insert(0, path.join("..")); } content.extend(dirs); diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 766bbf35..d6ee760f 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -614,8 +614,14 @@ impl Picker { let files = super::directory_content(&path)?; let file_names: Vec<_> = files .iter() - .filter_map(|file| file.file_name()) - .map(|name| name.to_string_lossy().into_owned()) + .filter_map(|file| { + let name = file.file_name()?.to_string_lossy(); + if file.is_dir() { + Some(format!("{}/", name)) + } else { + Some(name.into_owned()) + } + }) .collect(); Ok(CachedPreview::Directory(file_names)) } else if metadata.is_file() { From 68b252a5fd14bda964293f1ba95c73b859eb703c Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Fri, 4 Oct 2024 11:47:40 +0200 Subject: [PATCH 8/9] Do not resolve symlinks in file browser --- helix-term/src/ui/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 2c105aba..1bc6ef4f 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -267,7 +267,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi } pub fn file_browser(root: PathBuf) -> Result { - let root = root.canonicalize()?; + let root = helix_stdx::path::canonicalize(root); let directory_content = directory_content(&root)?; let columns = [PickerColumn::new( From 127a588e7f2d61fb787d52f3bc686f29d53b1245 Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Wed, 27 Nov 2024 08:53:23 +0100 Subject: [PATCH 9/9] Add file_browser for cwd and workspace root --- helix-term/src/commands.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7decc577..f0b1562e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,7 +375,9 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", - file_browser, "Open file browser at current buffer's directory", + file_browser, "Open file browser in workspace root", + file_browser_in_current_buffer_directory, "Open file browser at current buffer's directory", + file_browser_in_current_directory, "Open file browser at current working directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2950,6 +2952,18 @@ fn file_picker_in_current_directory(cx: &mut Context) { } fn file_browser(cx: &mut Context) { + let root = find_workspace().0; + if !root.exists() { + cx.editor.set_error("Workspace directory does not exist"); + return; + } + + if let Ok(picker) = ui::file_browser(root) { + cx.push_layer(Box::new(overlaid(picker))); + } +} + +fn file_browser_in_current_buffer_directory(cx: &mut Context) { let doc_dir = doc!(cx.editor) .path() .and_then(|path| path.parent().map(|path| path.to_path_buf())); @@ -2964,6 +2978,9 @@ fn file_browser(cx: &mut Context) { ); return; } + cx.editor.set_error( + "Current buffer has no parent, opening file browser in current working directory", + ); cwd } }; @@ -2973,6 +2990,19 @@ fn file_browser(cx: &mut Context) { } } +fn file_browser_in_current_directory(cx: &mut Context) { + let cwd = helix_stdx::env::current_working_dir(); + if !cwd.exists() { + cx.editor + .set_error("Current working directory does not exist"); + return; + } + + if let Ok(picker) = ui::file_browser(cwd) { + cx.push_layer(Box::new(overlaid(picker))); + } +} + fn buffer_picker(cx: &mut Context) { let current = view!(cx.editor).doc;