diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 12d80a0f8..7a1643fed 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -11,7 +11,7 @@ use once_cell::sync::Lazy; use crate::{ compositor::{Callback, Compositor}, - ui::{self, Popup, Prompt, PromptEvent}, + ui::{self, Picker, Popup, Prompt, PromptEvent}, }; use std::path::PathBuf; @@ -19,7 +19,7 @@ use std::path::PathBuf; use helix_view::{ document::Mode, view::{View, PADDING}, - Document, Editor, + Document, DocumentId, Editor, }; use crossterm::event::{KeyCode, KeyEvent}; @@ -820,10 +820,39 @@ pub fn file_picker(cx: &mut Context) { } pub fn buffer_picker(cx: &mut Context) { - // let documents = cx.editor.documents.iter().map(||).collect(); - // (document_id, relative_path/name) mappings - // let picker = ui::buffer_picker(&documents, editor.focus); - // cx.push_layer(Box::new(picker)); + use std::path::{Path, PathBuf}; + let current = cx.editor.view().doc; + + let picker = Picker::new( + cx.editor + .documents + .iter() + .map(|(id, doc)| (id, doc.relative_path().map(Path::to_path_buf))) + .collect(), + move |(id, path): &(DocumentId, Option)| { + // format_fn + match path { + Some(path) => { + if *id == current { + format!("{} (*)", path.to_str().unwrap()).into() + } else { + path.to_str().unwrap().into() + } + } + None => "[NEW]".into(), + } + }, + |editor: &mut Editor, (_, path): &(DocumentId, Option)| match path { + Some(path) => { + use helix_view::editor::Action; + editor + .open(path.into(), Action::Replace) + .expect("editor.open failed"); + } + None => (), + }, + ); + cx.push_layer(Box::new(picker)); } // calculate line numbers for each selection range diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index a625aa14c..f7f77d0c8 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -20,6 +20,8 @@ pub use tui::style::{Color, Modifier, Style}; use helix_core::regex::Regex; use helix_view::{Document, Editor}; +use std::path::{Path, PathBuf}; + // TODO: temp #[inline(always)] pub fn text_color() -> Style { @@ -75,7 +77,6 @@ pub fn regex_prompt( ) } -use std::path::{Path, PathBuf}; pub fn file_picker(root: &str) -> Picker { use ignore::Walk; // TODO: determine root based on git root @@ -109,36 +110,6 @@ pub fn file_picker(root: &str) -> Picker { ) } -use helix_view::View; -pub fn buffer_picker(buffers: &[Document], current: usize) -> Picker<(Option, usize)> { - use helix_view::Editor; - Picker::new( - buffers - .iter() - .enumerate() - .map(|(i, doc)| (doc.relative_path().map(Path::to_path_buf), i)) - .collect(), - move |(path, index): &(Option, usize)| { - // format_fn - match path { - Some(path) => { - if *index == current { - format!("{} (*)", path.to_str().unwrap()).into() - } else { - path.to_str().unwrap().into() - } - } - None => "[NEW]".into(), - } - }, - |editor: &mut Editor, &(_, index): &(Option, usize)| { - // if index < editor.views.len() { - // editor.focus = index; - // } - }, - ) -} - pub mod completers { use crate::ui::prompt::Completion; use std::borrow::Cow;