on master help sign in
Restore buffer_picker.
915fd9eBlaž Hrastnik
helix-term/src/commands.rs+35−6
@@ -11,7 +11,7 @@ use once_cell::sync::Lazy;
11
11
12use crate::{
12use crate::{
13 compositor::{Callback, Compositor},
13 compositor::{Callback, Compositor},
14− ui::{self, Popup, Prompt, PromptEvent},
14+ ui::{self, Picker, Popup, Prompt, PromptEvent},
15};
15};
16
16
17use std::path::PathBuf;
17use std::path::PathBuf;
@@ -19,7 +19,7 @@ use std::path::PathBuf;
19use helix_view::{
19use helix_view::{
20 document::Mode,
20 document::Mode,
21 view::{View, PADDING},
21 view::{View, PADDING},
22− Document, Editor,
22+ Document, DocumentId, Editor,
23};
23};
24
24
25use crossterm::event::{KeyCode, KeyEvent};
25use crossterm::event::{KeyCode, KeyEvent};
@@ -820,10 +820,39 @@ pub fn file_picker(cx: &mut Context) {
820}
820}
821
821
822pub fn buffer_picker(cx: &mut Context) {
822pub fn buffer_picker(cx: &mut Context) {
823− // let documents = cx.editor.documents.iter().map(||).collect();
823+ use std::path::{Path, PathBuf};
824− // (document_id, relative_path/name) mappings
824+ let current = cx.editor.view().doc;
825− // let picker = ui::buffer_picker(&documents, editor.focus);
825+
826+ let picker = Picker::new(
827+ cx.editor
828+ .documents
829+ .iter()
830+ .map(|(id, doc)| (id, doc.relative_path().map(Path::to_path_buf)))
831+ .collect(),
832+ move |(id, path): &(DocumentId, Option<PathBuf>)| {
833+ // format_fn
834+ match path {
835+ Some(path) => {
836+ if *id == current {
837+ format!("{} (*)", path.to_str().unwrap()).into()
838+ } else {
839+ path.to_str().unwrap().into()
840+ }
841+ }
842+ None => "[NEW]".into(),
843+ }
844+ },
845+ |editor: &mut Editor, (_, path): &(DocumentId, Option<PathBuf>)| match path {
846+ Some(path) => {
847+ use helix_view::editor::Action;
848+ editor
849+ .open(path.into(), Action::Replace)
850+ .expect("editor.open failed");
851+ }
852+ None => (),
853+ },
854+ );
826− // cx.push_layer(Box::new(picker));
855+ cx.push_layer(Box::new(picker));
827}
856}
828
857
829// calculate line numbers for each selection range
858// calculate line numbers for each selection range
helix-term/src/ui/mod.rs+2−31
@@ -20,6 +20,8 @@ pub use tui::style::{Color, Modifier, Style};
20use helix_core::regex::Regex;
20use helix_core::regex::Regex;
21use helix_view::{Document, Editor};
21use helix_view::{Document, Editor};
22
22
23+use std::path::{Path, PathBuf};
24+
23// TODO: temp
25// TODO: temp
24#[inline(always)]
26#[inline(always)]
25pub fn text_color() -> Style {
27pub fn text_color() -> Style {
@@ -75,7 +77,6 @@ pub fn regex_prompt(
75 )
77 )
76}
78}
77
79
78−use std::path::{Path, PathBuf};
79pub fn file_picker(root: &str) -> Picker<PathBuf> {
80pub fn file_picker(root: &str) -> Picker<PathBuf> {
80 use ignore::Walk;
81 use ignore::Walk;
81 // TODO: determine root based on git root
82 // TODO: determine root based on git root
@@ -109,36 +110,6 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
109 )
110 )
110}
111}
111
112
112−use helix_view::View;
113−pub fn buffer_picker(buffers: &[Document], current: usize) -> Picker<(Option<PathBuf>, usize)> {
114− use helix_view::Editor;
115− Picker::new(
116− buffers
117− .iter()
118− .enumerate()
119− .map(|(i, doc)| (doc.relative_path().map(Path::to_path_buf), i))
120− .collect(),
121− move |(path, index): &(Option<PathBuf>, usize)| {
122− // format_fn
123− match path {
124− Some(path) => {
125− if *index == current {
126− format!("{} (*)", path.to_str().unwrap()).into()
127− } else {
128− path.to_str().unwrap().into()
129− }
130− }
131− None => "[NEW]".into(),
132− }
133− },
134− |editor: &mut Editor, &(_, index): &(Option<PathBuf>, usize)| {
135− // if index < editor.views.len() {
136− // editor.focus = index;
137− // }
138− },
139− )
140−}
141−
142pub mod completers {
113pub mod completers {
143 use crate::ui::prompt::Completion;
114 use crate::ui::prompt::Completion;
144 use std::borrow::Cow;
115 use std::borrow::Cow;