Merge pull request #1027 from zed-industries/missing-menu-commands

Add missing File menu commands, improve handling of unsaved multibuffers
This commit is contained in:
Max Brunsfeld
2022-05-23 21:29:32 -07:00
committed by GitHub
17 changed files with 784 additions and 443 deletions
+3 -3
View File
@@ -216,7 +216,7 @@ pub enum Direction {
}
pub fn init(cx: &mut MutableAppContext) {
cx.add_action(Editor::open_new);
cx.add_action(Editor::new_file);
cx.add_action(|this: &mut Editor, action: &Scroll, cx| this.set_scroll_position(action.0, cx));
cx.add_action(Editor::select);
cx.add_action(Editor::cancel);
@@ -1002,9 +1002,9 @@ impl Editor {
this
}
pub fn open_new(
pub fn new_file(
workspace: &mut Workspace,
_: &workspace::OpenNew,
_: &workspace::NewFile,
cx: &mut ViewContext<Workspace>,
) {
let project = workspace.project().clone();
+15 -7
View File
@@ -9,6 +9,7 @@ use language::{Bias, Buffer, File as _, SelectionGoal};
use project::{File, Project, ProjectEntryId, ProjectPath};
use rpc::proto::{self, update_view};
use settings::Settings;
use smallvec::SmallVec;
use std::{fmt::Write, path::PathBuf, time::Duration};
use text::{Point, Selection};
use util::TryFutureExt;
@@ -293,14 +294,25 @@ impl Item for Editor {
}
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
File::from_dyn(self.buffer().read(cx).file(cx)).map(|file| ProjectPath {
let buffer = self.buffer.read(cx).as_singleton()?;
let file = buffer.read(cx).file();
File::from_dyn(file).map(|file| ProjectPath {
worktree_id: file.worktree_id(cx),
path: file.path().clone(),
})
}
fn project_entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId> {
File::from_dyn(self.buffer().read(cx).file(cx)).and_then(|file| file.project_entry_id(cx))
fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[ProjectEntryId; 3]> {
self.buffer
.read(cx)
.files(cx)
.into_iter()
.filter_map(|file| File::from_dyn(Some(file))?.project_entry_id(cx))
.collect()
}
fn is_singleton(&self, cx: &AppContext) -> bool {
self.buffer.read(cx).is_singleton()
}
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
@@ -372,10 +384,6 @@ impl Item for Editor {
})
}
fn can_save_as(&self, cx: &AppContext) -> bool {
self.buffer().read(cx).is_singleton()
}
fn save_as(
&mut self,
project: ModelHandle<Project>,
+16 -7
View File
@@ -12,6 +12,7 @@ use language::{
ToPointUtf16 as _, TransactionId,
};
use settings::Settings;
use smallvec::SmallVec;
use std::{
cell::{Ref, RefCell},
cmp, fmt, io,
@@ -1126,18 +1127,26 @@ impl MultiBuffer {
.and_then(|(buffer, _)| buffer.read(cx).language())
}
pub fn file<'a>(&self, cx: &'a AppContext) -> Option<&'a dyn File> {
self.as_singleton()?.read(cx).file()
pub fn files<'a>(&'a self, cx: &'a AppContext) -> SmallVec<[&'a dyn File; 2]> {
let buffers = self.buffers.borrow();
buffers
.values()
.filter_map(|buffer| buffer.buffer.read(cx).file())
.collect()
}
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()
return title;
}
if let Some(buffer) = self.as_singleton() {
if let Some(file) = buffer.read(cx).file() {
return file.file_name(cx).to_string_lossy().into();
}
}
"untitled".into()
}
#[cfg(test)]