This commit is contained in:
Denys Rybalka 2024-11-21 21:36:25 -06:00 committed by GitHub
commit 353105567b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 165 additions and 21 deletions

View File

@ -375,6 +375,7 @@ pub fn doc(&self) -> &str {
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",
@ -2928,6 +2929,30 @@ fn file_picker_in_current_directory(cx: &mut Context) {
cx.push_layer(Box::new(overlaid(picker)));
}
fn file_browser(cx: &mut Context) {
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 => {
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
}
};
if let Ok(picker) = ui::file_browser(path) {
cx.push_layer(Box::new(overlaid(picker)));
}
}
fn buffer_picker(cx: &mut Context) {
let current = view!(cx.editor).doc;

View File

@ -30,6 +30,7 @@
use helix_view::Editor;
use std::path::Path;
use std::{error::Error, path::PathBuf};
pub fn prompt(
@ -265,6 +266,76 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
picker
}
pub fn file_browser(root: PathBuf) -> Result<FilePicker, std::io::Error> {
let root = helix_stdx::path::canonicalize(root);
let directory_content = directory_content(&root)?;
let columns = [PickerColumn::new(
"path",
|item: &PathBuf, root: &PathBuf| {
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(
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);
}
},
)
.with_preview(|_editor, path| Some((path.as_path().into(), None)));
Ok(picker)
}
fn directory_content(path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
let mut dirs = Vec::new();
let mut files = Vec::new();
for entry in std::fs::read_dir(path)?.flatten() {
if entry.path().is_dir() {
dirs.push(entry.path());
} else {
files.push(entry.path());
}
}
dirs.sort();
files.sort();
let mut content = Vec::new();
if path.parent().is_some() {
content.insert(0, path.join(".."));
}
content.extend(dirs);
content.extend(files);
Ok(content)
}
pub mod completers {
use crate::ui::prompt::Completion;
use helix_core::fuzzy::fuzzy_match;

View File

@ -85,6 +85,7 @@ fn from(v: DocumentId) -> Self {
pub enum CachedPreview {
Document(Box<Document>),
Directory(Vec<String>),
Binary,
LargeFile,
NotFound,
@ -106,12 +107,20 @@ fn document(&self) -> Option<&Document> {
}
}
fn dir_content(&self) -> Option<&Vec<String>> {
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(_) => "<Invalid file location>",
Self::Cached(preview) => match preview {
CachedPreview::Document(_) => "<Invalid file location>",
CachedPreview::Directory(_) => "<Invalid directory location>",
CachedPreview::Binary => "<Binary file>",
CachedPreview::LargeFile => "<File too large to preview>",
CachedPreview::NotFound => "<File not found>",
@ -584,33 +593,58 @@ fn get_preview<'picker, 'editor>(
}
let path: Arc<Path> = path.into();
let data = std::fs::File::open(&path).and_then(|file| {
let metadata = file.metadata()?;
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| {
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() {
if metadata.len() > MAX_FILE_SIZE_FOR_PREVIEW {
return Ok(CachedPreview::LargeFile);
}
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]);
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
Ok(content_type)
})?;
if content_type.is_binary() {
return Ok(CachedPreview::Binary);
}
_ => Document::open(&path, None, None, editor.config.clone())
.map(|doc| {
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))
@ -844,6 +878,20 @@ fn render_preview(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context
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;