on master help sign in
buffer picker add is_modifier flag (#1020)
6822423CossonLeo
helix-term/src/commands.rs+49−19
@@ -2600,36 +2600,66 @@ fn file_picker(cx: &mut Context) {
2600fn buffer_picker(cx: &mut Context) {
2600fn buffer_picker(cx: &mut Context) {
2601 let current = view!(cx.editor).doc;
2601 let current = view!(cx.editor).doc;
2602
2602
2603+ struct BufferMeta {
2604+ id: DocumentId,
2605+ path: Option<PathBuf>,
2606+ is_modified: bool,
2607+ is_current: bool,
2608+ }
2609+
2610+ impl BufferMeta {
2611+ fn format(&self) -> Cow<str> {
2612+ let path = self
2613+ .path
2614+ .as_deref()
2615+ .map(helix_core::path::get_relative_path);
2616+ let path = match path.as_deref().and_then(Path::to_str) {
2617+ Some(path) => path,
2618+ None => return Cow::Borrowed("[scratch buffer]"),
2619+ };
2620+
2621+ let mut flags = Vec::new();
2622+ if self.is_modified {
2623+ flags.push("+");
2624+ }
2625+ if self.is_current {
2626+ flags.push("*");
2627+ }
2628+
2629+ let flag = if flags.is_empty() {
2630+ "".into()
2631+ } else {
2632+ format!(" ({})", flags.join(""))
2633+ };
2634+ Cow::Owned(format!("{}{}", path, flag))
2635+ }
2636+ }
2637+
2638+ let new_meta = |doc: &Document| BufferMeta {
2639+ id: doc.id(),
2640+ path: doc.path().cloned(),
2641+ is_modified: doc.is_modified(),
2642+ is_current: doc.id() == current,
2643+ };
2644+
2603 let picker = FilePicker::new(
2645 let picker = FilePicker::new(
2604 cx.editor
2646 cx.editor
2605 .documents
2647 .documents
2606 .iter()
2648 .iter()
2607− .map(|(id, doc)| (*id, doc.path().cloned()))
2649+ .map(|(_, doc)| new_meta(doc))
2608 .collect(),
2650 .collect(),
2609− move |(id, path): &(DocumentId, Option<PathBuf>)| {
2610− let path = path.as_deref().map(helix_core::path::get_relative_path);
2611− match path.as_ref().and_then(|path| path.to_str()) {
2612− Some(path) => {
2613− if *id == current {
2614− format!("{} (*)", &path).into()
2651+ BufferMeta::format,
2615− } else {
2616− path.to_owned().into()
2617− }
2618− }
2619− None => "[scratch buffer]".into(),
2620− }
2621− },
2622− |editor: &mut Editor, (id, _path): &(DocumentId, Option<PathBuf>), _action| {
2652+ |editor: &mut Editor, meta, _action| {
2623− editor.switch(*id, Action::Replace);
2653+ editor.switch(meta.id, Action::Replace);
2624 },
2654 },
2625− |editor, (id, path)| {
2655+ |editor, meta| {
2626− let doc = &editor.documents.get(id)?;
2656+ let doc = &editor.documents.get(&meta.id)?;
2627 let &view_id = doc.selections().keys().next()?;
2657 let &view_id = doc.selections().keys().next()?;
2628 let line = doc
2658 let line = doc
2629 .selection(view_id)
2659 .selection(view_id)
2630 .primary()
2660 .primary()
2631 .cursor_line(doc.text().slice(..));
2661 .cursor_line(doc.text().slice(..));
2632− Some((path.clone()?, Some((line, line))))
2662+ Some((meta.path.clone()?, Some((line, line))))
2633 },
2663 },
2634 );
2664 );
2635 cx.push_layer(Box::new(picker));
2665 cx.push_layer(Box::new(picker));