Move file handle from buffer to buffer view

This commit is contained in:
Max Brunsfeld
2021-04-29 17:47:06 -07:00
parent d2f309d10d
commit 8cffa8bdb2
5 changed files with 136 additions and 81 deletions
+19 -48
View File
@@ -18,16 +18,14 @@ use crate::{
worktree::FileHandle,
};
use anyhow::{anyhow, Result};
use gpui::{AppContext, Entity, ModelContext};
use gpui::{Entity, ModelContext};
use lazy_static::lazy_static;
use rand::prelude::*;
use std::{
cmp,
ffi::OsString,
hash::BuildHasher,
iter::{self, Iterator},
ops::{AddAssign, Range},
path::Path,
str,
sync::Arc,
time::{Duration, Instant},
@@ -59,7 +57,6 @@ type HashMap<K, V> = std::collections::HashMap<K, V>;
type HashSet<T> = std::collections::HashSet<T>;
pub struct Buffer {
file: Option<FileHandle>,
fragments: SumTree<Fragment>,
insertion_splits: HashMap<time::Local, SumTree<InsertionSplit>>,
pub version: time::Global,
@@ -359,24 +356,18 @@ impl Buffer {
base_text: T,
ctx: &mut ModelContext<Self>,
) -> Self {
Self::build(replica_id, None, History::new(base_text.into()), ctx)
Self::build(replica_id, History::new(base_text.into()), ctx)
}
pub fn from_history(
replica_id: ReplicaId,
file: FileHandle,
history: History,
ctx: &mut ModelContext<Self>,
) -> Self {
Self::build(replica_id, Some(file), history, ctx)
Self::build(replica_id, history, ctx)
}
fn build(
replica_id: ReplicaId,
file: Option<FileHandle>,
history: History,
ctx: &mut ModelContext<Self>,
) -> Self {
fn build(replica_id: ReplicaId, history: History, _: &mut ModelContext<Self>) -> Self {
let mut insertion_splits = HashMap::default();
let mut fragments = SumTree::new();
@@ -425,12 +416,7 @@ impl Buffer {
});
}
if let Some(file) = file.as_ref() {
file.observe_from_model(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged));
}
Self {
file,
fragments,
insertion_splits,
version: time::Global::new(),
@@ -448,41 +434,27 @@ impl Buffer {
}
}
pub fn file_name(&self, ctx: &AppContext) -> Option<OsString> {
self.file.as_ref().and_then(|file| file.file_name(ctx))
}
pub fn path(&self) -> Option<Arc<Path>> {
self.file.as_ref().map(|file| file.path())
}
pub fn entry_id(&self) -> Option<(usize, Arc<Path>)> {
self.file.as_ref().map(|file| file.entry_id())
}
pub fn snapshot(&self) -> Snapshot {
Snapshot {
fragments: self.fragments.clone(),
}
}
pub fn save(&mut self, ctx: &mut ModelContext<Self>) -> LocalBoxFuture<'static, Result<()>> {
if let Some(file) = &self.file {
dbg!(file.path());
let snapshot = self.snapshot();
let version = self.version.clone();
let save_task = file.save(snapshot, ctx.as_ref());
let task = ctx.spawn(save_task, |me, save_result, ctx| {
if save_result.is_ok() {
me.did_save(version, ctx);
}
save_result
});
Box::pin(task)
} else {
Box::pin(async { Ok(()) })
}
pub fn save(
&mut self,
file: &FileHandle,
ctx: &mut ModelContext<Self>,
) -> LocalBoxFuture<'static, Result<()>> {
let snapshot = self.snapshot();
let version = self.version.clone();
let save_task = file.save(snapshot, ctx.as_ref());
let task = ctx.spawn(save_task, |me, save_result, ctx| {
if save_result.is_ok() {
me.did_save(version, ctx);
}
save_result
});
Box::pin(task)
}
fn did_save(&mut self, version: time::Global, ctx: &mut ModelContext<Buffer>) {
@@ -1763,7 +1735,6 @@ impl Buffer {
impl Clone for Buffer {
fn clone(&self) -> Self {
Self {
file: self.file.clone(),
fragments: self.fragments.clone(),
insertion_splits: self.insertion_splits.clone(),
version: self.version.clone(),
+34 -15
View File
@@ -2,7 +2,7 @@ use super::{
buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point,
Selection, SelectionSetId, ToOffset, ToPoint,
};
use crate::{settings::Settings, watch, workspace};
use crate::{settings::Settings, watch, workspace, worktree::FileHandle};
use anyhow::Result;
use futures_core::future::LocalBoxFuture;
use gpui::{
@@ -98,6 +98,7 @@ pub enum SelectAction {
pub struct BufferView {
handle: WeakViewHandle<Self>,
buffer: ModelHandle<Buffer>,
file: Option<FileHandle>,
display_map: ModelHandle<DisplayMap>,
selection_set_id: SelectionSetId,
pending_selection: Option<Selection>,
@@ -120,18 +121,23 @@ struct ClipboardSelection {
impl BufferView {
pub fn single_line(settings: watch::Receiver<Settings>, ctx: &mut ViewContext<Self>) -> Self {
let buffer = ctx.add_model(|ctx| Buffer::new(0, String::new(), ctx));
let mut view = Self::for_buffer(buffer, settings, ctx);
let mut view = Self::for_buffer(buffer, None, settings, ctx);
view.single_line = true;
view
}
pub fn for_buffer(
buffer: ModelHandle<Buffer>,
file: Option<FileHandle>,
settings: watch::Receiver<Settings>,
ctx: &mut ViewContext<Self>,
) -> Self {
settings.notify_view_on_change(ctx);
if let Some(file) = file.as_ref() {
file.observe_from_view(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged));
}
ctx.observe(&buffer, Self::on_buffer_changed);
ctx.subscribe_to_model(&buffer, Self::on_buffer_event);
let display_map = ctx.add_model(|ctx| {
@@ -157,6 +163,7 @@ impl BufferView {
Self {
handle: ctx.handle().downgrade(),
buffer,
file,
display_map,
selection_set_id,
pending_selection: None,
@@ -405,7 +412,7 @@ impl BufferView {
Ok(())
}
fn insert(&mut self, text: &String, ctx: &mut ViewContext<Self>) {
pub fn insert(&mut self, text: &String, ctx: &mut ViewContext<Self>) {
let mut offset_ranges = SmallVec::<[Range<usize>; 32]>::new();
{
let buffer = self.buffer.read(ctx);
@@ -1343,9 +1350,10 @@ impl workspace::Item for Buffer {
fn build_view(
buffer: ModelHandle<Self>,
settings: watch::Receiver<Settings>,
file: Option<FileHandle>,
ctx: &mut ViewContext<Self::View>,
) -> Self::View {
BufferView::for_buffer(buffer, settings, ctx)
BufferView::for_buffer(buffer, file, settings, ctx)
}
}
@@ -1362,28 +1370,39 @@ impl workspace::ItemView for BufferView {
}
fn title(&self, app: &AppContext) -> std::string::String {
if let Some(name) = self.buffer.read(app).file_name(app) {
let filename = self.file.as_ref().and_then(|file| file.file_name(app));
if let Some(name) = filename {
name.to_string_lossy().into()
} else {
"untitled".into()
}
}
fn entry_id(&self, app: &AppContext) -> Option<(usize, Arc<Path>)> {
self.buffer.read(app).entry_id()
fn entry_id(&self, _: &AppContext) -> Option<(usize, Arc<Path>)> {
self.file.as_ref().map(|file| file.entry_id())
}
fn clone_on_split(&self, ctx: &mut ViewContext<Self>) -> Option<Self>
where
Self: Sized,
{
let clone = BufferView::for_buffer(self.buffer.clone(), self.settings.clone(), ctx);
let clone = BufferView::for_buffer(
self.buffer.clone(),
self.file.clone(),
self.settings.clone(),
ctx,
);
*clone.scroll_position.lock() = *self.scroll_position.lock();
Some(clone)
}
fn save(&self, ctx: &mut ViewContext<Self>) -> LocalBoxFuture<'static, Result<()>> {
self.buffer.update(ctx, |buffer, ctx| buffer.save(ctx))
if let Some(file) = self.file.as_ref() {
self.buffer
.update(ctx, |buffer, ctx| buffer.save(file, ctx))
} else {
Box::pin(async { Ok(()) })
}
}
fn is_dirty(&self, ctx: &AppContext) -> bool {
@@ -1406,7 +1425,7 @@ mod tests {
app.add_model(|ctx| Buffer::new(0, "aaaaaa\nbbbbbb\ncccccc\ndddddd\n", ctx));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, buffer_view) =
app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx));
app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx));
buffer_view.update(app, |view, ctx| {
view.begin_selection(DisplayPoint::new(2, 2), false, ctx);
@@ -1521,7 +1540,7 @@ mod tests {
let settings = settings::channel(&font_cache).unwrap().1;
let (_, view) =
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx));
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx));
let layouts = view
.read(app)
@@ -1560,7 +1579,7 @@ mod tests {
});
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, view) =
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx));
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx));
view.update(app, |view, ctx| {
view.select_display_ranges(
@@ -1632,7 +1651,7 @@ mod tests {
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(6, 6), ctx));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, view) =
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx));
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx));
buffer.update(app, |buffer, ctx| {
buffer.edit(
@@ -1675,7 +1694,7 @@ mod tests {
});
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, view) =
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx));
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx));
view.update(app, |view, ctx| {
view.select_display_ranges(
@@ -1706,7 +1725,7 @@ mod tests {
let buffer = app.add_model(|ctx| Buffer::new(0, "one two three four five six ", ctx));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let view = app
.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx))
.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx))
.1;
// Cut with three selections. Clipboard text is divided into three slices.