on master help sign in
buffer picker add is_modifier flag (#1020)
commit68224232af1126daa5b043a096269fbd6cb53551 authorCossonLeo <20379044+cossonleo@users.noreply.github.com> dateWed 10 Nov 2021, 09:52 (5 years ago) parent92d2343
1 file changed+49−19
helix-term/src/commands.rs+49−19
helix-term/src/commands.rs+49−19
@@ -2600,36 +2600,66 @@ fn file_picker(cx: &mut Context) {
26002600fn buffer_picker(cx: &mut Context) {
26012601 let current = view!(cx.editor).doc;
26022602
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+
26032645 let picker = FilePicker::new(
26042646 cx.editor
26052647 .documents
26062648 .iter()
2607− .map(|(id, doc)| (*id, doc.path().cloned()))
2649+ .map(|(_, doc)| new_meta(doc))
26082650 .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()
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| {
2623− editor.switch(*id, Action::Replace);
2651+ BufferMeta::format,
2652+ |editor: &mut Editor, meta, _action| {
2653+ editor.switch(meta.id, Action::Replace);
26242654 },
2625− |editor, (id, path)| {
2626− let doc = &editor.documents.get(id)?;
2655+ |editor, meta| {
2656+ let doc = &editor.documents.get(&meta.id)?;
26272657 let &view_id = doc.selections().keys().next()?;
26282658 let line = doc
26292659 .selection(view_id)
26302660 .primary()
26312661 .cursor_line(doc.text().slice(..));
2632− Some((path.clone()?, Some((line, line))))
2662+ Some((meta.path.clone()?, Some((line, line))))
26332663 },
26342664 );
26352665 cx.push_layer(Box::new(picker));