mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-25 02:46:17 +04:00
Merge 68b252a5fd
into dc941d6d24
This commit is contained in:
commit
353105567b
@ -375,6 +375,7 @@ pub fn doc(&self) -> &str {
|
|||||||
file_picker, "Open file picker",
|
file_picker, "Open file picker",
|
||||||
file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory",
|
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_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",
|
code_action, "Perform code action",
|
||||||
buffer_picker, "Open buffer picker",
|
buffer_picker, "Open buffer picker",
|
||||||
jumplist_picker, "Open jumplist 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)));
|
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) {
|
fn buffer_picker(cx: &mut Context) {
|
||||||
let current = view!(cx.editor).doc;
|
let current = view!(cx.editor).doc;
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
use helix_view::Editor;
|
use helix_view::Editor;
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
use std::{error::Error, path::PathBuf};
|
use std::{error::Error, path::PathBuf};
|
||||||
|
|
||||||
pub fn prompt(
|
pub fn prompt(
|
||||||
@ -265,6 +266,76 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
|
|||||||
picker
|
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 {
|
pub mod completers {
|
||||||
use crate::ui::prompt::Completion;
|
use crate::ui::prompt::Completion;
|
||||||
use helix_core::fuzzy::fuzzy_match;
|
use helix_core::fuzzy::fuzzy_match;
|
||||||
|
@ -85,6 +85,7 @@ fn from(v: DocumentId) -> Self {
|
|||||||
|
|
||||||
pub enum CachedPreview {
|
pub enum CachedPreview {
|
||||||
Document(Box<Document>),
|
Document(Box<Document>),
|
||||||
|
Directory(Vec<String>),
|
||||||
Binary,
|
Binary,
|
||||||
LargeFile,
|
LargeFile,
|
||||||
NotFound,
|
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.
|
/// Alternate text to show for the preview.
|
||||||
fn placeholder(&self) -> &str {
|
fn placeholder(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
Self::EditorDocument(_) => "<Invalid file location>",
|
Self::EditorDocument(_) => "<Invalid file location>",
|
||||||
Self::Cached(preview) => match preview {
|
Self::Cached(preview) => match preview {
|
||||||
CachedPreview::Document(_) => "<Invalid file location>",
|
CachedPreview::Document(_) => "<Invalid file location>",
|
||||||
|
CachedPreview::Directory(_) => "<Invalid directory location>",
|
||||||
CachedPreview::Binary => "<Binary file>",
|
CachedPreview::Binary => "<Binary file>",
|
||||||
CachedPreview::LargeFile => "<File too large to preview>",
|
CachedPreview::LargeFile => "<File too large to preview>",
|
||||||
CachedPreview::NotFound => "<File not found>",
|
CachedPreview::NotFound => "<File not found>",
|
||||||
@ -584,33 +593,58 @@ fn get_preview<'picker, 'editor>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let path: Arc<Path> = path.into();
|
let path: Arc<Path> = path.into();
|
||||||
let data = std::fs::File::open(&path).and_then(|file| {
|
let preview = std::fs::metadata(&path)
|
||||||
let metadata = file.metadata()?;
|
.and_then(|metadata| {
|
||||||
// Read up to 1kb to detect the content type
|
if metadata.is_dir() {
|
||||||
let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
|
let files = super::directory_content(&path)?;
|
||||||
let content_type = content_inspector::inspect(&self.read_buffer[..n]);
|
let file_names: Vec<_> = files
|
||||||
self.read_buffer.clear();
|
.iter()
|
||||||
Ok((metadata, content_type))
|
.filter_map(|file| {
|
||||||
});
|
let name = file.file_name()?.to_string_lossy();
|
||||||
let preview = data
|
if file.is_dir() {
|
||||||
.map(
|
Some(format!("{}/", name))
|
||||||
|(metadata, content_type)| match (metadata.len(), content_type) {
|
} else {
|
||||||
(_, content_inspector::ContentType::BINARY) => CachedPreview::Binary,
|
Some(name.into_owned())
|
||||||
(size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => {
|
}
|
||||||
CachedPreview::LargeFile
|
})
|
||||||
|
.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())
|
let content_type = std::fs::File::open(&path).and_then(|file| {
|
||||||
.map(|doc| {
|
// 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
|
// Asynchronously highlight the new document
|
||||||
helix_event::send_blocking(
|
helix_event::send_blocking(
|
||||||
&self.preview_highlight_handler,
|
&self.preview_highlight_handler,
|
||||||
path.clone(),
|
path.clone(),
|
||||||
);
|
);
|
||||||
CachedPreview::Document(Box::new(doc))
|
Ok(CachedPreview::Document(Box::new(doc)))
|
||||||
})
|
},
|
||||||
.unwrap_or(CachedPreview::NotFound),
|
)
|
||||||
},
|
} else {
|
||||||
)
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::NotFound,
|
||||||
|
"Neither a dir, nor a file",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
})
|
||||||
.unwrap_or(CachedPreview::NotFound);
|
.unwrap_or(CachedPreview::NotFound);
|
||||||
self.preview_cache.insert(path.clone(), preview);
|
self.preview_cache.insert(path.clone(), preview);
|
||||||
Some((Preview::Cached(&self.preview_cache[&path]), range))
|
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
|
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 alt_text = preview.placeholder();
|
||||||
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
|
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
|
||||||
let y = inner.y + inner.height / 2;
|
let y = inner.y + inner.height / 2;
|
||||||
|
Loading…
Reference in New Issue
Block a user