on master help sign in
Restore buffer_picker.
commit915fd9ebafe4f5ee86421697725176edbb961c43 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 24 Mar 2021, 16:26 (6 years ago) parent376232c
2 files changed+37−37
helix-term/src/commands.rs+35−6 helix-term/src/ui/mod.rs+2−31
helix-term/src/commands.rs+35−6
@@ -11,7 +11,7 @@ use once_cell::sync::Lazy;
1111
1212use crate::{
1313 compositor::{Callback, Compositor},
14− ui::{self, Popup, Prompt, PromptEvent},
14+ ui::{self, Picker, Popup, Prompt, PromptEvent},
1515};
1616
1717use std::path::PathBuf;
@@ -19,7 +19,7 @@ use std::path::PathBuf;
1919use helix_view::{
2020 document::Mode,
2121 view::{View, PADDING},
22− Document, Editor,
22+ Document, DocumentId, Editor,
2323};
2424
2525use crossterm::event::{KeyCode, KeyEvent};
@@ -820,10 +820,39 @@ pub fn file_picker(cx: &mut Context) {
820820}
821821
822822pub fn buffer_picker(cx: &mut Context) {
823− // let documents = cx.editor.documents.iter().map(||).collect();
824− // (document_id, relative_path/name) mappings
825− // let picker = ui::buffer_picker(&documents, editor.focus);
826− // cx.push_layer(Box::new(picker));
823+ use std::path::{Path, PathBuf};
824+ let current = cx.editor.view().doc;
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+ );
855+ cx.push_layer(Box::new(picker));
827856}
828857
829858// 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};
2020use helix_core::regex::Regex;
2121use helix_view::{Document, Editor};
2222
23+use std::path::{Path, PathBuf};
24+
2325// TODO: temp
2426#[inline(always)]
2527pub fn text_color() -> Style {
@@ -75,7 +77,6 @@ pub fn regex_prompt(
7577 )
7678}
7779
78−use std::path::{Path, PathBuf};
7980pub fn file_picker(root: &str) -> Picker<PathBuf> {
8081 use ignore::Walk;
8182 // TODO: determine root based on git root
@@ -109,36 +110,6 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
109110 )
110111}
111112
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−
142113pub mod completers {
143114 use crate::ui::prompt::Completion;
144115 use std::borrow::Cow;