Allow editor to be saved when in multi-buffer mode

Also, this commit enables the customization of the title in a multi-buffer.
When specified, it will take precedence over a filename (or "untitled").
This commit is contained in:
Antonio Scandurra 2022-02-10 09:35:19 +01:00
parent 9ea535986f
commit dd223f93ec
4 changed files with 35 additions and 17 deletions

View file

@ -39,6 +39,7 @@ pub struct MultiBuffer {
singleton: bool,
replica_id: ReplicaId,
history: History,
title: Option<String>,
}
struct History {
@ -167,9 +168,15 @@ impl MultiBuffer {
transaction_depth: 0,
group_interval: Duration::from_millis(300),
},
title: Default::default(),
}
}
pub fn with_title(mut self, title: String) -> Self {
self.title = Some(title);
self
}
pub fn singleton(buffer: ModelHandle<Buffer>, cx: &mut ModelContext<Self>) -> Self {
let mut this = Self::new(buffer.read(cx).replica_id());
this.singleton = true;
@ -227,6 +234,10 @@ impl MultiBuffer {
}
}
pub fn is_singleton(&self) -> bool {
self.singleton
}
pub fn subscribe(&mut self) -> Subscription {
self.subscriptions.subscribe()
}
@ -945,6 +956,16 @@ impl MultiBuffer {
self.as_singleton()?.read(cx).file()
}
pub fn title(&self, cx: &AppContext) -> String {
if let Some(title) = self.title.clone() {
title
} else if let Some(file) = self.file(cx) {
file.file_name(cx).to_string_lossy().into()
} else {
"untitled".into()
}
}
#[cfg(test)]
pub fn is_parsing(&self, cx: &AppContext) -> bool {
self.as_singleton().unwrap().read(cx).is_parsing()