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

@ -28,7 +28,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use util::TryFutureExt; use util::TryFutureExt;
use workspace::{ItemNavHistory, Workspace}; use workspace::{ItemNavHistory, ItemViewHandle as _, Workspace};
action!(Deploy); action!(Deploy);
action!(OpenExcerpts); action!(OpenExcerpts);
@ -573,7 +573,7 @@ impl workspace::ItemView for ProjectDiagnosticsEditor {
} }
fn save(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> { fn save(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> {
self.excerpts.update(cx, |excerpts, cx| excerpts.save(cx)) self.editor.save(cx)
} }
fn can_save_as(&self, _: &AppContext) -> bool { fn can_save_as(&self, _: &AppContext) -> bool {

View file

@ -883,16 +883,7 @@ impl Editor {
} }
pub fn title(&self, cx: &AppContext) -> String { pub fn title(&self, cx: &AppContext) -> String {
let filename = self self.buffer().read(cx).title(cx)
.buffer()
.read(cx)
.file(cx)
.map(|file| file.file_name(cx));
if let Some(name) = filename {
name.to_string_lossy().into()
} else {
"untitled".into()
}
} }
pub fn snapshot(&mut self, cx: &mut MutableAppContext) -> EditorSnapshot { pub fn snapshot(&mut self, cx: &mut MutableAppContext) -> EditorSnapshot {
@ -2120,6 +2111,7 @@ impl Editor {
}; };
let action_ix = action_ix.unwrap_or(actions_menu.selected_item); let action_ix = action_ix.unwrap_or(actions_menu.selected_item);
let action = actions_menu.actions.get(action_ix)?.clone(); let action = actions_menu.actions.get(action_ix)?.clone();
let title = action.lsp_action.title.clone();
let buffer = actions_menu.buffer; let buffer = actions_menu.buffer;
let replica_id = editor.read(cx).replica_id(cx); let replica_id = editor.read(cx).replica_id(cx);
@ -2131,7 +2123,7 @@ impl Editor {
let mut ranges_to_highlight = Vec::new(); let mut ranges_to_highlight = Vec::new();
let excerpt_buffer = cx.add_model(|cx| { let excerpt_buffer = cx.add_model(|cx| {
let mut multibuffer = MultiBuffer::new(replica_id); let mut multibuffer = MultiBuffer::new(replica_id).with_title(title);
for (buffer, transaction) in &project_transaction.0 { for (buffer, transaction) in &project_transaction.0 {
let snapshot = buffer.read(cx).snapshot(); let snapshot = buffer.read(cx).snapshot();
ranges_to_highlight.extend( ranges_to_highlight.extend(
@ -2152,7 +2144,12 @@ impl Editor {
let editor = workspace.open_item(MultiBufferItemHandle(excerpt_buffer), cx); let editor = workspace.open_item(MultiBufferItemHandle(excerpt_buffer), cx);
if let Some(editor) = editor.act_as::<Self>(cx) { if let Some(editor) = editor.act_as::<Self>(cx) {
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
editor.highlight_ranges::<Self>(ranges_to_highlight, Color::blue(), cx); let settings = (editor.build_settings)(cx);
editor.highlight_ranges::<Self>(
ranges_to_highlight,
settings.style.highlighted_line_background,
cx,
);
}); });
} }
}); });

View file

@ -218,7 +218,7 @@ impl ItemView for Editor {
} }
fn can_save(&self, cx: &AppContext) -> bool { fn can_save(&self, cx: &AppContext) -> bool {
self.project_path(cx).is_some() !self.buffer().read(cx).is_singleton() || self.project_path(cx).is_some()
} }
fn save(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> { fn save(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> {
@ -235,8 +235,8 @@ impl ItemView for Editor {
}) })
} }
fn can_save_as(&self, _: &AppContext) -> bool { fn can_save_as(&self, cx: &AppContext) -> bool {
true self.buffer().read(cx).is_singleton()
} }
fn save_as( fn save_as(

View file

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