From 0a2d2aa6845e5866e9e4304417b670c378f62485 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 13 Apr 2021 20:07:25 -0600 Subject: [PATCH] Replace app/app_mut on contexts with AsRef/AsMut impls Co-Authored-By: Brooks Swinnerton <934497+bswinnerton@users.noreply.github.com> --- gpui/src/app.rs | 40 +++++++------ zed/src/editor/buffer/mod.rs | 2 +- zed/src/editor/buffer_view.rs | 87 ++++++++++++++++------------- zed/src/editor/display_map/mod.rs | 8 +-- zed/src/file_finder.rs | 6 +- zed/src/workspace/mod.rs | 4 +- zed/src/workspace/pane.rs | 9 ++- zed/src/workspace/workspace.rs | 4 +- zed/src/workspace/workspace_view.rs | 10 ++-- zed/src/worktree/worktree.rs | 2 +- 10 files changed, 95 insertions(+), 77 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index 4f5fb7b905..b35c5ba0b4 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -1495,14 +1495,6 @@ impl<'a, T: Entity> ModelContext<'a, T> { } } - pub fn app(&self) -> &AppContext { - &self.app.ctx - } - - pub fn app_mut(&mut self) -> &mut MutableAppContext { - self.app - } - pub fn background_executor(&self) -> &Arc { &self.app.ctx.background } @@ -1640,6 +1632,18 @@ impl<'a, T: Entity> ModelContext<'a, T> { } } +impl AsRef for ModelContext<'_, M> { + fn as_ref(&self) -> &AppContext { + &self.app.ctx + } +} + +impl AsMut for ModelContext<'_, M> { + fn as_mut(&mut self) -> &mut MutableAppContext { + self.app + } +} + impl ReadModel for ModelContext<'_, M> { fn read_model(&self, handle: &ModelHandle) -> &T { self.app.read_model(handle) @@ -1685,14 +1689,6 @@ impl<'a, T: View> ViewContext<'a, T> { self.window_id } - pub fn app(&self) -> &AppContext { - &self.app.ctx - } - - pub fn app_mut(&mut self) -> &mut MutableAppContext { - self.app - } - pub fn background_executor(&self) -> &Arc { &self.app.ctx.background } @@ -1895,6 +1891,18 @@ impl<'a, T: View> ViewContext<'a, T> { } } +impl AsRef for ViewContext<'_, M> { + fn as_ref(&self) -> &AppContext { + &self.app.ctx + } +} + +impl AsMut for ViewContext<'_, M> { + fn as_mut(&mut self) -> &mut MutableAppContext { + self.app + } +} + impl ReadModel for ViewContext<'_, V> { fn read_model(&self, handle: &ModelHandle) -> &T { self.app.read_model(handle) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index 7f06e1700f..d69e1d96e2 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -447,7 +447,7 @@ impl Buffer { if let Some(file) = &self.file { let snapshot = self.snapshot(); let version = self.version.clone(); - let save_task = file.save(snapshot, ctx.app()); + 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); diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index aa91e39ec5..f1855c9128 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -297,7 +297,7 @@ impl BufferView { let display_map = self.display_map.read(ctx); let cursor = display_map - .anchor_before(position, Bias::Left, ctx.app()) + .anchor_before(position, Bias::Left, ctx.as_ref()) .unwrap(); let selection = Selection { start: cursor.clone(), @@ -322,7 +322,9 @@ impl BufferView { ) { let buffer = self.buffer.read(ctx); let map = self.display_map.read(ctx); - let cursor = map.anchor_before(position, Bias::Left, ctx.app()).unwrap(); + let cursor = map + .anchor_before(position, Bias::Left, ctx.as_ref()) + .unwrap(); if let Some(selection) = self.pending_selection.as_mut() { selection.set_head(buffer, cursor); } else { @@ -337,8 +339,8 @@ impl BufferView { fn end_selection(&mut self, ctx: &mut ViewContext) { if let Some(selection) = self.pending_selection.take() { - let ix = self.selection_insertion_index(&selection.start, ctx.app()); - let mut selections = self.selections(ctx.app()).to_vec(); + let ix = self.selection_insertion_index(&selection.start, ctx.as_ref()); + let mut selections = self.selections(ctx.as_ref()).to_vec(); selections.insert(ix, selection); self.update_selections(selections, ctx); } else { @@ -359,8 +361,8 @@ impl BufferView { let mut selections = Vec::new(); for range in ranges { selections.push(Selection { - start: map.anchor_after(range.start, Bias::Left, ctx.app())?, - end: map.anchor_before(range.end, Bias::Left, ctx.app())?, + start: map.anchor_after(range.start, Bias::Left, ctx.as_ref())?, + end: map.anchor_before(range.end, Bias::Left, ctx.as_ref())?, reversed: false, goal_column: None, }); @@ -373,7 +375,7 @@ impl BufferView { let mut offset_ranges = SmallVec::<[Range; 32]>::new(); { let buffer = self.buffer.read(ctx); - for selection in self.selections(ctx.app()) { + for selection in self.selections(ctx.as_ref()) { let start = selection.start.to_offset(buffer).unwrap(); let end = selection.end.to_offset(buffer).unwrap(); offset_ranges.push(start..end); @@ -423,18 +425,21 @@ impl BufferView { pub fn backspace(&mut self, _: &(), ctx: &mut ViewContext) { self.start_transaction(ctx); - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { let buffer = self.buffer.read(ctx); let map = self.display_map.read(ctx); for selection in &mut selections { if selection.range(buffer).is_empty() { - let head = selection.head().to_display_point(map, ctx.app()).unwrap(); + let head = selection + .head() + .to_display_point(map, ctx.as_ref()) + .unwrap(); let cursor = map .anchor_before( - movement::left(map, head, ctx.app()).unwrap(), + movement::left(map, head, ctx.as_ref()).unwrap(), Bias::Left, - ctx.app(), + ctx.as_ref(), ) .unwrap(); selection.set_head(&buffer, cursor); @@ -460,7 +465,7 @@ impl BufferView { } pub fn move_left(&mut self, _: &(), ctx: &mut ViewContext) { - let app = ctx.app(); + let app = ctx.as_ref(); let mut selections = self.selections(app).to_vec(); { let map = self.display_map.read(app); @@ -486,17 +491,20 @@ impl BufferView { } pub fn select_left(&mut self, _: &(), ctx: &mut ViewContext) { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { let buffer = self.buffer.read(ctx); let map = self.display_map.read(ctx); for selection in &mut selections { - let head = selection.head().to_display_point(map, ctx.app()).unwrap(); + let head = selection + .head() + .to_display_point(map, ctx.as_ref()) + .unwrap(); let cursor = map .anchor_before( - movement::left(map, head, ctx.app()).unwrap(), + movement::left(map, head, ctx.as_ref()).unwrap(), Bias::Left, - ctx.app(), + ctx.as_ref(), ) .unwrap(); selection.set_head(&buffer, cursor); @@ -508,9 +516,9 @@ impl BufferView { } pub fn move_right(&mut self, _: &(), ctx: &mut ViewContext) { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { - let app = ctx.app(); + let app = ctx.as_ref(); let map = self.display_map.read(app); for selection in &mut selections { let start = selection.start.to_display_point(map, app).unwrap(); @@ -534,13 +542,16 @@ impl BufferView { } pub fn select_right(&mut self, _: &(), ctx: &mut ViewContext) { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { - let app = ctx.app(); + let app = ctx.as_ref(); let buffer = self.buffer.read(app); let map = self.display_map.read(app); for selection in &mut selections { - let head = selection.head().to_display_point(map, ctx.app()).unwrap(); + let head = selection + .head() + .to_display_point(map, ctx.as_ref()) + .unwrap(); let cursor = map .anchor_before(movement::right(map, head, app).unwrap(), Bias::Right, app) .unwrap(); @@ -556,9 +567,9 @@ impl BufferView { if self.single_line { ctx.propagate_action(); } else { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { - let app = ctx.app(); + let app = ctx.as_ref(); let map = self.display_map.read(app); for selection in &mut selections { let start = selection.start.to_display_point(map, app).unwrap(); @@ -585,9 +596,9 @@ impl BufferView { if self.single_line { ctx.propagate_action(); } else { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { - let app = ctx.app(); + let app = ctx.as_ref(); let buffer = self.buffer.read(app); let map = self.display_map.read(app); for selection in &mut selections { @@ -607,9 +618,9 @@ impl BufferView { if self.single_line { ctx.propagate_action(); } else { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { - let app = ctx.app(); + let app = ctx.as_ref(); let map = self.display_map.read(app); for selection in &mut selections { let start = selection.start.to_display_point(map, app).unwrap(); @@ -636,9 +647,9 @@ impl BufferView { if self.single_line { ctx.propagate_action(); } else { - let mut selections = self.selections(ctx.app()).to_vec(); + let mut selections = self.selections(ctx.as_ref()).to_vec(); { - let app = ctx.app(); + let app = ctx.as_ref(); let buffer = self.buffer.read(app); let map = self.display_map.read(app); for selection in &mut selections { @@ -781,7 +792,7 @@ impl BufferView { let mut fold_ranges = Vec::new(); - let app = ctx.app(); + let app = ctx.as_ref(); let map = self.display_map.read(app); for selection in self.selections(app) { let (start, end) = selection.display_range(map, app).sorted(); @@ -811,7 +822,7 @@ impl BufferView { pub fn unfold(&mut self, _: &(), ctx: &mut ViewContext) { use super::RangeExt; - let app = ctx.app(); + let app = ctx.as_ref(); let map = self.display_map.read(app); let buffer = self.buffer.read(app); let ranges = self @@ -895,7 +906,7 @@ impl BufferView { self.display_map.update(ctx, |map, ctx| { let buffer = self.buffer.read(ctx); let ranges = self - .selections(ctx.app()) + .selections(ctx.as_ref()) .iter() .map(|s| s.range(buffer)) .collect::>(); @@ -1410,7 +1421,7 @@ mod tests { .unwrap(); view.fold(&(), ctx); assert_eq!( - view.text(ctx.app()), + view.text(ctx.as_ref()), " impl Foo { // Hello! @@ -1431,7 +1442,7 @@ mod tests { view.fold(&(), ctx); assert_eq!( - view.text(ctx.app()), + view.text(ctx.as_ref()), " impl Foo {… } @@ -1441,7 +1452,7 @@ mod tests { view.unfold(&(), ctx); assert_eq!( - view.text(ctx.app()), + view.text(ctx.as_ref()), " impl Foo { // Hello! @@ -1461,7 +1472,7 @@ mod tests { ); view.unfold(&(), ctx); - assert_eq!(view.text(ctx.app()), buffer.read(ctx).text()); + assert_eq!(view.text(ctx.as_ref()), buffer.read(ctx).text()); }); }); } @@ -1488,12 +1499,12 @@ mod tests { view.update(app, |view, ctx| { view.move_down(&(), ctx); assert_eq!( - view.selection_ranges(ctx.app()), + view.selection_ranges(ctx.as_ref()), &[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)] ); view.move_right(&(), ctx); assert_eq!( - view.selection_ranges(ctx.app()), + view.selection_ranges(ctx.as_ref()), &[DisplayPoint::new(1, 4)..DisplayPoint::new(1, 4)] ); Ok::<(), Error>(()) diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index e44193104e..8142a8925c 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -29,7 +29,7 @@ impl DisplayMap { DisplayMap { buffer: buffer.clone(), - fold_map: FoldMap::new(buffer, ctx.app()), + fold_map: FoldMap::new(buffer, ctx.as_ref()), tab_size, } } @@ -39,7 +39,7 @@ impl DisplayMap { ranges: impl IntoIterator>, ctx: &mut ModelContext, ) -> Result<()> { - self.fold_map.fold(ranges, ctx.app())?; + self.fold_map.fold(ranges, ctx.as_ref())?; ctx.notify(); Ok(()) } @@ -49,7 +49,7 @@ impl DisplayMap { ranges: impl IntoIterator>, ctx: &mut ModelContext, ) -> Result<()> { - self.fold_map.unfold(ranges, ctx.app())?; + self.fold_map.unfold(ranges, ctx.as_ref())?; ctx.notify(); Ok(()) } @@ -125,7 +125,7 @@ impl DisplayMap { fn handle_buffer_event(&mut self, event: &buffer::Event, ctx: &mut ModelContext) { match event { - buffer::Event::Edited(edits) => self.fold_map.apply_edits(edits, ctx.app()).unwrap(), + buffer::Event::Edited(edits) => self.fold_map.apply_edits(edits, ctx.as_ref()).unwrap(), _ => {} } } diff --git a/zed/src/file_finder.rs b/zed/src/file_finder.rs index 169d2243a4..b27189814c 100644 --- a/zed/src/file_finder.rs +++ b/zed/src/file_finder.rs @@ -287,7 +287,7 @@ impl FileFinder { } fn workspace_updated(&mut self, _: ModelHandle, ctx: &mut ViewContext) { - self.spawn_search(self.query_buffer.read(ctx).text(ctx.app()), ctx); + self.spawn_search(self.query_buffer.read(ctx).text(ctx.as_ref()), ctx); } fn on_query_buffer_event( @@ -299,7 +299,7 @@ impl FileFinder { use buffer_view::Event::*; match event { Edited => { - let query = self.query_buffer.read(ctx).text(ctx.app()); + let query = self.query_buffer.read(ctx).text(ctx.as_ref()); if query.is_empty() { self.latest_search_id = util::post_inc(&mut self.search_count); self.matches.clear(); @@ -345,7 +345,7 @@ impl FileFinder { } fn spawn_search(&mut self, query: String, ctx: &mut ViewContext) { - let worktrees = self.worktrees(ctx.app()); + let worktrees = self.worktrees(ctx.as_ref()); let search_id = util::post_inc(&mut self.search_count); let task = ctx.background_executor().spawn(async move { let matches = match_paths(worktrees.as_slice(), &query, false, false, 100); diff --git a/zed/src/workspace/mod.rs b/zed/src/workspace/mod.rs index 4cf765b0aa..d9e9039994 100644 --- a/zed/src/workspace/mod.rs +++ b/zed/src/workspace/mod.rs @@ -51,8 +51,8 @@ fn open_paths(params: &OpenParams, app: &mut MutableAppContext) { for window_id in app.window_ids().collect::>() { if let Some(handle) = app.root_view::(window_id) { if handle.update(app, |view, ctx| { - if view.contains_paths(¶ms.paths, ctx.app()) { - view.open_paths(¶ms.paths, ctx.app_mut()); + if view.contains_paths(¶ms.paths, ctx.as_ref()) { + view.open_paths(¶ms.paths, ctx.as_mut()); log::info!("open paths on existing workspace"); true } else { diff --git a/zed/src/workspace/pane.rs b/zed/src/workspace/pane.rs index 58acb86abb..e0b0109ea4 100644 --- a/zed/src/workspace/pane.rs +++ b/zed/src/workspace/pane.rs @@ -110,11 +110,10 @@ impl Pane { entry_id: (usize, usize), ctx: &mut ViewContext, ) -> bool { - if let Some(index) = self - .items - .iter() - .position(|item| item.entry_id(ctx.app()).map_or(false, |id| id == entry_id)) - { + if let Some(index) = self.items.iter().position(|item| { + item.entry_id(ctx.as_ref()) + .map_or(false, |id| id == entry_id) + }) { self.activate_item(index, ctx); true } else { diff --git a/zed/src/workspace/workspace.rs b/zed/src/workspace/workspace.rs index b4e5a85e58..a3a2d79a0e 100644 --- a/zed/src/workspace/workspace.rs +++ b/zed/src/workspace/workspace.rs @@ -153,8 +153,8 @@ impl Workspace { .ok_or(anyhow!("worktree {} does not exist", entry.0,))?; let replica_id = self.replica_id; - let file = worktree.file(entry.1, ctx.app())?; - let history = file.load_history(ctx.app()); + let file = worktree.file(entry.1, ctx.as_ref())?; + let history = file.load_history(ctx.as_ref()); let buffer = async move { Ok(Buffer::from_history(replica_id, file, history.await?)) }; let (mut tx, rx) = watch::channel(None); diff --git a/zed/src/workspace/workspace_view.rs b/zed/src/workspace/workspace_view.rs index 6a9b3ca320..23612116c3 100644 --- a/zed/src/workspace/workspace_view.rs +++ b/zed/src/workspace/workspace_view.rs @@ -214,7 +214,7 @@ impl WorkspaceView { me.loading_entries.remove(&entry); match item { Ok(item) => { - let item_view = item.add_view(ctx.window_id(), settings, ctx.app_mut()); + let item_view = item.add_view(ctx.window_id(), settings, ctx.as_mut()); me.add_item(item_view, ctx); } Err(error) => { @@ -243,7 +243,7 @@ impl WorkspaceView { pub fn save_active_item(&mut self, _: &(), ctx: &mut ViewContext) { self.active_pane.update(ctx, |pane, ctx| { if let Some(item) = pane.active_item() { - let task = item.save(ctx.app_mut()); + let task = item.save(ctx.as_mut()); ctx.spawn(task, |_, result, _| { if let Err(e) = result { // TODO - present this error to the user @@ -258,7 +258,7 @@ impl WorkspaceView { pub fn debug_elements(&mut self, _: &(), ctx: &mut ViewContext) { match to_string_pretty(&ctx.debug_elements()) { Ok(json) => { - ctx.app_mut().copy(&json); + ctx.as_mut().copy(&json); log::info!( "copied {:.1} KiB of element debug JSON to the clipboard", json.len() as f32 / 1024. @@ -323,7 +323,7 @@ impl WorkspaceView { let new_pane = self.add_pane(ctx); self.activate_pane(new_pane.clone(), ctx); if let Some(item) = pane.read(ctx).active_item() { - if let Some(clone) = item.clone_on_split(ctx.app_mut()) { + if let Some(clone) = item.clone_on_split(ctx.as_mut()) { self.add_item(clone, ctx); } } @@ -351,7 +351,7 @@ impl WorkspaceView { fn add_item(&self, item: Box, ctx: &mut ViewContext) { let active_pane = self.active_pane(); - item.set_parent_pane(&active_pane, ctx.app_mut()); + item.set_parent_pane(&active_pane, ctx.as_mut()); active_pane.update(ctx, |pane, ctx| { let item_idx = pane.add_item(item, ctx); pane.activate_item(item_idx, ctx); diff --git a/zed/src/worktree/worktree.rs b/zed/src/worktree/worktree.rs index 2e685261ce..2e63ad117f 100644 --- a/zed/src/worktree/worktree.rs +++ b/zed/src/worktree/worktree.rs @@ -710,7 +710,7 @@ mod test { let file_id = entry.entry_id; tree.update(&mut app, |tree, ctx| { - smol::block_on(tree.save(file_id, buffer.snapshot(), ctx.app())).unwrap() + smol::block_on(tree.save(file_id, buffer.snapshot(), ctx.as_ref())).unwrap() }); let history = app