Skip to content

Commit

Permalink
Find file (space .)
Browse files Browse the repository at this point in the history
Based on doom emacs find-file but also wanted to show an alternative to
file explorer helix-editor#2377.
  • Loading branch information
pickfire committed May 5, 2022
1 parent 5ab669f commit 8e1a012
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
7 changes: 7 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ impl MappableCommand {
command_mode, "Enter command mode",
file_picker, "Open file picker",
file_picker_in_current_directory, "Open file picker at current working directory",
find_file_picker, "Find file",
code_action, "Perform code action",
buffer_picker, "Open buffer picker",
symbol_picker, "Open symbol picker",
Expand Down Expand Up @@ -2095,6 +2096,12 @@ fn file_picker_in_current_directory(cx: &mut Context) {
cx.push_layer(Box::new(overlayed(picker)));
}

fn find_file_picker(cx: &mut Context) {
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("./"));
let picker = ui::find_file_picker(cwd, &cx.editor.config());
cx.push_layer(Box::new(overlayed(picker)));
}

fn buffer_picker(cx: &mut Context) {
let current = view!(cx.editor).doc;

Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"space" => { "Space"
"f" => file_picker,
"F" => file_picker_in_current_directory,
"." => find_file_picker,
"b" => buffer_picker,
"s" => symbol_picker,
"S" => workspace_symbol_picker,
Expand Down
31 changes: 31 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use helix_core::regex::Regex;
use helix_core::regex::RegexBuilder;
use helix_view::{Document, Editor, View};

use std::fs;
use std::path::PathBuf;

pub fn prompt(
Expand Down Expand Up @@ -188,6 +189,36 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
)
}

/// Based on find-file on doom emacs (SPC . or SPC f f).
pub fn find_file_picker(dir: PathBuf, config: &helix_view::editor::Config) -> FilePicker<PathBuf> {
// switch to Result::flatten later
let files: Vec<_> = match fs::read_dir(&dir) {
Ok(dir) => dir
.flat_map(|entry| entry.map(|entry| entry.path()))
.collect(),
Err(_) => Vec::new(),
};
let config = config.clone();
FilePicker::new(
files,
move |path| {
let suffix = if path.is_dir() { "/" } else { "" };
path.strip_prefix(&dir).unwrap_or(path).to_string_lossy() + suffix
},
move |cx, path, action| {
if path.is_dir() {
let _picker = find_file_picker(path.to_path_buf(), &config);
todo!("recurse picker, probably need to change how handle_event close_fn works");
} else {
cx.editor
.open(path.into(), action)
.expect("editor.open failed");
}
},
|_editor, path| Some((path.clone(), None)),
)
}

pub mod completers {
use crate::ui::prompt::Completion;
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
Expand Down
26 changes: 13 additions & 13 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
/// Biggest file size to preview in bytes
pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024;

/// File path and range of lines (used to align and highlight lines)
pub type FileLocation = (PathBuf, Option<(usize, usize)>);

pub struct FilePicker<T> {
picker: Picker<T>,
pub truncate_start: bool,
/// Caches paths to documents
preview_cache: HashMap<PathBuf, CachedPreview>,
read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display.
file_fn: Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>,
}

pub enum CachedPreview {
Document(Box<Document>),
Binary,
Expand Down Expand Up @@ -84,6 +71,19 @@ impl Preview<'_, '_> {
}
}

/// File path and range of lines (used to align and highlight lines)
pub type FileLocation = (PathBuf, Option<(usize, usize)>);

pub struct FilePicker<T> {
picker: Picker<T>,
pub truncate_start: bool,
/// Caches paths to documents
preview_cache: HashMap<PathBuf, CachedPreview>,
read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display.
file_fn: Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>,
}

impl<T> FilePicker<T> {
pub fn new(
options: Vec<T>,
Expand Down

0 comments on commit 8e1a012

Please sign in to comment.