buffer picker add is_modifier flag (#1020)

imgbot
CossonLeo 3 years ago committed by GitHub
parent 92d23430c0
commit 68224232af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2600,36 +2600,66 @@ fn file_picker(cx: &mut Context) {
fn buffer_picker(cx: &mut Context) { fn buffer_picker(cx: &mut Context) {
let current = view!(cx.editor).doc; let current = view!(cx.editor).doc;
struct BufferMeta {
id: DocumentId,
path: Option<PathBuf>,
is_modified: bool,
is_current: bool,
}
impl BufferMeta {
fn format(&self) -> Cow<str> {
let path = self
.path
.as_deref()
.map(helix_core::path::get_relative_path);
let path = match path.as_deref().and_then(Path::to_str) {
Some(path) => path,
None => return Cow::Borrowed("[scratch buffer]"),
};
let mut flags = Vec::new();
if self.is_modified {
flags.push("+");
}
if self.is_current {
flags.push("*");
}
let flag = if flags.is_empty() {
"".into()
} else {
format!(" ({})", flags.join(""))
};
Cow::Owned(format!("{}{}", path, flag))
}
}
let new_meta = |doc: &Document| BufferMeta {
id: doc.id(),
path: doc.path().cloned(),
is_modified: doc.is_modified(),
is_current: doc.id() == current,
};
let picker = FilePicker::new( let picker = FilePicker::new(
cx.editor cx.editor
.documents .documents
.iter() .iter()
.map(|(id, doc)| (*id, doc.path().cloned())) .map(|(_, doc)| new_meta(doc))
.collect(), .collect(),
move |(id, path): &(DocumentId, Option<PathBuf>)| { BufferMeta::format,
let path = path.as_deref().map(helix_core::path::get_relative_path); |editor: &mut Editor, meta, _action| {
match path.as_ref().and_then(|path| path.to_str()) { editor.switch(meta.id, Action::Replace);
Some(path) => {
if *id == current {
format!("{} (*)", &path).into()
} else {
path.to_owned().into()
}
}
None => "[scratch buffer]".into(),
}
},
|editor: &mut Editor, (id, _path): &(DocumentId, Option<PathBuf>), _action| {
editor.switch(*id, Action::Replace);
}, },
|editor, (id, path)| { |editor, meta| {
let doc = &editor.documents.get(id)?; let doc = &editor.documents.get(&meta.id)?;
let &view_id = doc.selections().keys().next()?; let &view_id = doc.selections().keys().next()?;
let line = doc let line = doc
.selection(view_id) .selection(view_id)
.primary() .primary()
.cursor_line(doc.text().slice(..)); .cursor_line(doc.text().slice(..));
Some((path.clone()?, Some((line, line)))) Some((meta.path.clone()?, Some((line, line))))
}, },
); );
cx.push_layer(Box::new(picker)); cx.push_layer(Box::new(picker));

Loading…
Cancel
Save