Eliminate GPUI View, ViewContext, and WindowContext types (#22632)

There's still a bit more work to do on this, but this PR is compiling
(with warnings) after eliminating the key types. When the tasks below
are complete, this will be the new narrative for GPUI:

- `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit
of state, and if `T` implements `Render`, then `Entity<T>` implements
`Element`.
- `&mut App` This replaces `AppContext` and represents the app.
- `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It
is provided by the framework when updating an entity.
- `&mut Window` Broken out of `&mut WindowContext` which no longer
exists. Every method that once took `&mut WindowContext` now takes `&mut
Window, &mut App` and every method that took `&mut ViewContext<T>` now
takes `&mut Window, &mut Context<T>`

Not pictured here are the two other failed attempts. It's been quite a
month!

Tasks:

- [x] Remove `View`, `ViewContext`, `WindowContext` and thread through
`Window`
- [x] [@cole-miller @mikayla-maki] Redraw window when entities change
- [x] [@cole-miller @mikayla-maki] Get examples and Zed running
- [x] [@cole-miller @mikayla-maki] Fix Zed rendering
- [x] [@mikayla-maki] Fix todo! macros and comments
- [x] Fix a bug where the editor would not be redrawn because of view
caching
- [x] remove publicness window.notify() and replace with
`AppContext::notify`
- [x] remove `observe_new_window_models`, replace with
`observe_new_models` with an optional window
- [x] Fix a bug where the project panel would not be redrawn because of
the wrong refresh() call being used
- [x] Fix the tests
- [x] Fix warnings by eliminating `Window` params or using `_`
- [x] Fix conflicts
- [x] Simplify generic code where possible
- [x] Rename types
- [ ] Update docs

### issues post merge

- [x] Issues switching between normal and insert mode
- [x] Assistant re-rendering failure
- [x] Vim test failures
- [x] Mac build issue



Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
Co-authored-by: Cole Miller <cole@zed.dev>
Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Joseph <joseph@zed.dev>
Co-authored-by: max <max@zed.dev>
Co-authored-by: Michael Sloan <michael@zed.dev>
Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local>
Co-authored-by: Mikayla <mikayla.c.maki@gmail.com>
Co-authored-by: joão <joao@zed.dev>
This commit is contained in:
Nathan Sobo 2025-01-25 20:02:45 -07:00 committed by GitHub
parent 21b4a0d50e
commit 6fca1d2b0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
648 changed files with 36248 additions and 28208 deletions

View file

@ -11,9 +11,8 @@ use file_icons::FileIcons;
use futures::future::try_join_all;
use git::status::GitSummary;
use gpui::{
point, AnyElement, AppContext, AsyncWindowContext, Context, Entity, EntityId, EventEmitter,
IntoElement, Model, ParentElement, Pixels, SharedString, Styled, Task, View, ViewContext,
VisualContext, WeakView, WindowContext,
point, AnyElement, App, AsyncWindowContext, Context, Entity, EntityId, EventEmitter,
IntoElement, ParentElement, Pixels, SharedString, Styled, Task, WeakEntity, Window,
};
use language::{
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, CharKind, DiskState, Point,
@ -55,11 +54,12 @@ impl FollowableItem for Editor {
}
fn from_state_proto(
workspace: View<Workspace>,
workspace: Entity<Workspace>,
remote_id: ViewId,
state: &mut Option<proto::view::Variant>,
cx: &mut WindowContext,
) -> Option<Task<Result<View<Self>>>> {
window: &mut Window,
cx: &mut App,
) -> Option<Task<Result<Entity<Self>>>> {
let project = workspace.read(cx).project().to_owned();
let Some(proto::view::Variant::Editor(_)) = state else {
return None;
@ -80,13 +80,13 @@ impl FollowableItem for Editor {
.collect::<Result<Vec<_>>>()
});
Some(cx.spawn(|mut cx| async move {
Some(window.spawn(cx, |mut cx| async move {
let mut buffers = futures::future::try_join_all(buffers?)
.await
.debug_assert_ok("leaders don't share views for unshared buffers")?;
let editor = cx.update(|cx| {
let multibuffer = cx.new_model(|cx| {
let editor = cx.update(|window, cx| {
let multibuffer = cx.new(|cx| {
let mut multibuffer;
if state.singleton && buffers.len() == 1 {
multibuffer = MultiBuffer::singleton(buffers.pop().unwrap(), cx)
@ -121,9 +121,14 @@ impl FollowableItem for Editor {
multibuffer
});
cx.new_view(|cx| {
let mut editor =
Editor::for_multibuffer(multibuffer, Some(project.clone()), true, cx);
cx.new(|cx| {
let mut editor = Editor::for_multibuffer(
multibuffer,
Some(project.clone()),
true,
window,
cx,
);
editor.remote_id = Some(remote_id);
editor
})
@ -148,13 +153,18 @@ impl FollowableItem for Editor {
}))
}
fn set_leader_peer_id(&mut self, leader_peer_id: Option<PeerId>, cx: &mut ViewContext<Self>) {
fn set_leader_peer_id(
&mut self,
leader_peer_id: Option<PeerId>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.leader_peer_id = leader_peer_id;
if self.leader_peer_id.is_some() {
self.buffer.update(cx, |buffer, cx| {
buffer.remove_active_selections(cx);
});
} else if self.focus_handle.is_focused(cx) {
} else if self.focus_handle.is_focused(window) {
self.buffer.update(cx, |buffer, cx| {
buffer.set_active_selections(
&self.selections.disjoint_anchors(),
@ -167,7 +177,7 @@ impl FollowableItem for Editor {
cx.notify();
}
fn to_state_proto(&self, cx: &WindowContext) -> Option<proto::view::Variant> {
fn to_state_proto(&self, _: &Window, cx: &App) -> Option<proto::view::Variant> {
let buffer = self.buffer.read(cx);
if buffer
.as_singleton()
@ -237,7 +247,8 @@ impl FollowableItem for Editor {
&self,
event: &EditorEvent,
update: &mut Option<proto::update_view::Variant>,
cx: &WindowContext,
_: &Window,
cx: &App,
) -> bool {
let update =
update.get_or_insert_with(|| proto::update_view::Variant::Editor(Default::default()));
@ -299,22 +310,23 @@ impl FollowableItem for Editor {
fn apply_update_proto(
&mut self,
project: &Model<Project>,
project: &Entity<Project>,
message: update_view::Variant,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<Result<()>> {
let update_view::Variant::Editor(message) = message;
let project = project.clone();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
update_editor_from_message(this, project, message, &mut cx).await
})
}
fn is_project_item(&self, _cx: &WindowContext) -> bool {
fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
true
}
fn dedup(&self, existing: &Self, cx: &WindowContext) -> Option<Dedup> {
fn dedup(&self, existing: &Self, _: &Window, cx: &App) -> Option<Dedup> {
let self_singleton = self.buffer.read(cx).as_singleton()?;
let other_singleton = existing.buffer.read(cx).as_singleton()?;
if self_singleton == other_singleton {
@ -326,8 +338,8 @@ impl FollowableItem for Editor {
}
async fn update_editor_from_message(
this: WeakView<Editor>,
project: Model<Project>,
this: WeakEntity<Editor>,
project: Entity<Project>,
message: proto::update_view::Editor,
cx: &mut AsyncWindowContext,
) -> Result<()> {
@ -437,9 +449,9 @@ async fn update_editor_from_message(
.await?;
// Update the editor's state.
this.update(cx, |editor, cx| {
this.update_in(cx, |editor, window, cx| {
if !selections.is_empty() || pending_selection.is_some() {
editor.set_selections_from_remote(selections, pending_selection, cx);
editor.set_selections_from_remote(selections, pending_selection, window, cx);
editor.request_autoscroll_remotely(Autoscroll::newest(), cx);
} else if let Some(scroll_top_anchor) = scroll_top_anchor {
editor.set_scroll_anchor_remote(
@ -447,6 +459,7 @@ async fn update_editor_from_message(
anchor: scroll_top_anchor,
offset: point(message.scroll_x, message.scroll_y),
},
window,
cx,
);
}
@ -534,7 +547,12 @@ fn deserialize_anchor(buffer: &MultiBufferSnapshot, anchor: proto::EditorAnchor)
impl Item for Editor {
type Event = EditorEvent;
fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) -> bool {
fn navigate(
&mut self,
data: Box<dyn std::any::Any>,
window: &mut Window,
cx: &mut Context<Self>,
) -> bool {
if let Ok(data) = data.downcast::<NavigationData>() {
let newest_selection = self.selections.newest::<Point>(cx);
let buffer = self.buffer.read(cx).read(cx);
@ -557,8 +575,8 @@ impl Item for Editor {
false
} else {
let nav_history = self.nav_history.take();
self.set_scroll_anchor(scroll_anchor, cx);
self.change_selections(Some(Autoscroll::fit()), cx, |s| {
self.set_scroll_anchor(scroll_anchor, window, cx);
self.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.select_ranges([offset..offset])
});
self.nav_history = nav_history;
@ -569,7 +587,7 @@ impl Item for Editor {
}
}
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
let file_path = self
.buffer()
.read(cx)
@ -588,12 +606,12 @@ impl Item for Editor {
None
}
fn tab_description(&self, detail: usize, cx: &AppContext) -> Option<SharedString> {
fn tab_description(&self, detail: usize, cx: &App) -> Option<SharedString> {
let path = path_for_buffer(&self.buffer, detail, true, cx)?;
Some(path.to_string_lossy().to_string().into())
}
fn tab_icon(&self, cx: &WindowContext) -> Option<Icon> {
fn tab_icon(&self, _: &Window, cx: &App) -> Option<Icon> {
ItemSettings::get_global(cx)
.file_icons
.then(|| {
@ -607,7 +625,7 @@ impl Item for Editor {
.map(Icon::from_path)
}
fn tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement {
fn tab_content(&self, params: TabContentParams, _: &Window, cx: &App) -> AnyElement {
let label_color = if ItemSettings::get_global(cx).git_status {
self.buffer()
.read(cx)
@ -673,7 +691,7 @@ impl Item for Editor {
fn for_each_project_item(
&self,
cx: &AppContext,
cx: &App,
f: &mut dyn FnMut(EntityId, &dyn project::ProjectItem),
) {
self.buffer
@ -681,53 +699,59 @@ impl Item for Editor {
.for_each_buffer(|buffer| f(buffer.entity_id(), buffer.read(cx)));
}
fn is_singleton(&self, cx: &AppContext) -> bool {
fn is_singleton(&self, cx: &App) -> bool {
self.buffer.read(cx).is_singleton()
}
fn clone_on_split(
&self,
_workspace_id: Option<WorkspaceId>,
cx: &mut ViewContext<Self>,
) -> Option<View<Editor>>
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<Entity<Editor>>
where
Self: Sized,
{
Some(cx.new_view(|cx| self.clone(cx)))
Some(cx.new(|cx| self.clone(window, cx)))
}
fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
fn set_nav_history(
&mut self,
history: ItemNavHistory,
_window: &mut Window,
_: &mut Context<Self>,
) {
self.nav_history = Some(history);
}
fn discarded(&self, _project: Model<Project>, cx: &mut ViewContext<Self>) {
fn discarded(&self, _project: Entity<Project>, _: &mut Window, cx: &mut Context<Self>) {
for buffer in self.buffer().clone().read(cx).all_buffers() {
buffer.update(cx, |buffer, cx| buffer.discarded(cx))
}
}
fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
fn deactivated(&mut self, _: &mut Window, cx: &mut Context<Self>) {
let selection = self.selections.newest_anchor();
self.push_to_nav_history(selection.head(), None, cx);
}
fn workspace_deactivated(&mut self, cx: &mut ViewContext<Self>) {
fn workspace_deactivated(&mut self, _: &mut Window, cx: &mut Context<Self>) {
self.hide_hovered_link(cx);
}
fn is_dirty(&self, cx: &AppContext) -> bool {
fn is_dirty(&self, cx: &App) -> bool {
self.buffer().read(cx).read(cx).is_dirty()
}
fn has_deleted_file(&self, cx: &AppContext) -> bool {
fn has_deleted_file(&self, cx: &App) -> bool {
self.buffer().read(cx).read(cx).has_deleted_file()
}
fn has_conflict(&self, cx: &AppContext) -> bool {
fn has_conflict(&self, cx: &App) -> bool {
self.buffer().read(cx).read(cx).has_conflict()
}
fn can_save(&self, cx: &AppContext) -> bool {
fn can_save(&self, cx: &App) -> bool {
let buffer = &self.buffer().read(cx);
if let Some(buffer) = buffer.as_singleton() {
buffer.read(cx).project_path(cx).is_some()
@ -739,8 +763,9 @@ impl Item for Editor {
fn save(
&mut self,
format: bool,
project: Model<Project>,
cx: &mut ViewContext<Self>,
project: Entity<Project>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<Result<()>> {
self.report_editor_event("Editor Saved", None, cx);
let buffers = self.buffer().clone().read(cx).all_buffers();
@ -748,13 +773,14 @@ impl Item for Editor {
.into_iter()
.map(|handle| handle.read(cx).base_buffer().unwrap_or(handle.clone()))
.collect::<HashSet<_>>();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
if format {
this.update(&mut cx, |editor, cx| {
this.update_in(&mut cx, |editor, window, cx| {
editor.perform_format(
project.clone(),
FormatTrigger::Save,
FormatTarget::Buffers,
window,
cx,
)
})?
@ -800,9 +826,10 @@ impl Item for Editor {
fn save_as(
&mut self,
project: Model<Project>,
project: Entity<Project>,
path: ProjectPath,
cx: &mut ViewContext<Self>,
_: &mut Window,
cx: &mut Context<Self>,
) -> Task<Result<()>> {
let buffer = self
.buffer()
@ -819,12 +846,17 @@ impl Item for Editor {
project.update(cx, |project, cx| project.save_buffer_as(buffer, path, cx))
}
fn reload(&mut self, project: Model<Project>, cx: &mut ViewContext<Self>) -> Task<Result<()>> {
fn reload(
&mut self,
project: Entity<Project>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<Result<()>> {
let buffer = self.buffer().clone();
let buffers = self.buffer.read(cx).all_buffers();
let reload_buffers =
project.update(cx, |project, cx| project.reload_buffers(buffers, true, cx));
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
let transaction = reload_buffers.log_err().await;
this.update(&mut cx, |editor, cx| {
editor.request_autoscroll(Autoscroll::fit(), cx)
@ -842,15 +874,15 @@ impl Item for Editor {
})
}
fn as_searchable(&self, handle: &View<Self>) -> Option<Box<dyn SearchableItemHandle>> {
fn as_searchable(&self, handle: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
Some(Box::new(handle.clone()))
}
fn pixel_position_of_cursor(&self, _: &AppContext) -> Option<gpui::Point<Pixels>> {
fn pixel_position_of_cursor(&self, _: &App) -> Option<gpui::Point<Pixels>> {
self.pixel_position_of_newest_cursor
}
fn breadcrumb_location(&self, _: &AppContext) -> ToolbarItemLocation {
fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
if self.show_breadcrumbs {
ToolbarItemLocation::PrimaryLeft
} else {
@ -858,7 +890,7 @@ impl Item for Editor {
}
}
fn breadcrumbs(&self, variant: &Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
fn breadcrumbs(&self, variant: &Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
let cursor = self.selections.newest_anchor().head();
let multibuffer = &self.buffer().read(cx);
let (buffer_id, symbols) =
@ -902,7 +934,12 @@ impl Item for Editor {
Some(breadcrumbs)
}
fn added_to_workspace(&mut self, workspace: &mut Workspace, _: &mut ViewContext<Self>) {
fn added_to_workspace(
&mut self,
workspace: &mut Workspace,
_window: &mut Window,
_: &mut Context<Self>,
) {
self.workspace = Some((workspace.weak_handle(), workspace.database_id()));
}
@ -940,7 +977,7 @@ impl Item for Editor {
}
}
fn preserve_preview(&self, cx: &AppContext) -> bool {
fn preserve_preview(&self, cx: &App) -> bool {
self.buffer.read(cx).preserve_preview(cx)
}
}
@ -953,18 +990,20 @@ impl SerializableItem for Editor {
fn cleanup(
workspace_id: WorkspaceId,
alive_items: Vec<ItemId>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> Task<Result<()>> {
cx.spawn(|_| DB.delete_unloaded_items(workspace_id, alive_items))
window.spawn(cx, |_| DB.delete_unloaded_items(workspace_id, alive_items))
}
fn deserialize(
project: Model<Project>,
workspace: WeakView<Workspace>,
project: Entity<Project>,
workspace: WeakEntity<Workspace>,
workspace_id: workspace::WorkspaceId,
item_id: ItemId,
cx: &mut WindowContext,
) -> Task<Result<View<Self>>> {
window: &mut Window,
cx: &mut App,
) -> Task<Result<Entity<Self>>> {
let serialized_editor = match DB
.get_serialized_editor(item_id, workspace_id)
.context("Failed to query editor state")
@ -998,7 +1037,7 @@ impl SerializableItem for Editor {
contents: Some(contents),
language,
..
} => cx.spawn(|mut cx| {
} => window.spawn(cx, |mut cx| {
let project = project.clone();
async move {
let language = if let Some(language_name) = language {
@ -1028,11 +1067,11 @@ impl SerializableItem for Editor {
buffer.set_text(contents, cx);
})?;
cx.update(|cx| {
cx.new_view(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project), cx);
cx.update(|window, cx| {
cx.new(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project), window, cx);
editor.read_scroll_position_from_db(item_id, workspace_id, cx);
editor.read_scroll_position_from_db(item_id, workspace_id, window, cx);
editor
})
})
@ -1055,7 +1094,7 @@ impl SerializableItem for Editor {
match project_item {
Some(project_item) => {
cx.spawn(|mut cx| async move {
window.spawn(cx, |mut cx| async move {
let (_, project_item) = project_item.await?;
let buffer = project_item.downcast::<Buffer>().map_err(|_| {
anyhow!("Project item at stored path was not a buffer")
@ -1082,11 +1121,17 @@ impl SerializableItem for Editor {
})?;
}
cx.update(|cx| {
cx.new_view(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project), cx);
cx.update(|window, cx| {
cx.new(|cx| {
let mut editor =
Editor::for_buffer(buffer, Some(project), window, cx);
editor.read_scroll_position_from_db(item_id, workspace_id, cx);
editor.read_scroll_position_from_db(
item_id,
workspace_id,
window,
cx,
);
editor
})
})
@ -1094,12 +1139,12 @@ impl SerializableItem for Editor {
}
None => {
let open_by_abs_path = workspace.update(cx, |workspace, cx| {
workspace.open_abs_path(abs_path.clone(), false, cx)
workspace.open_abs_path(abs_path.clone(), false, window, cx)
});
cx.spawn(|mut cx| async move {
window.spawn(cx, |mut cx| async move {
let editor = open_by_abs_path?.await?.downcast::<Editor>().with_context(|| format!("Failed to downcast to Editor after opening abs path {abs_path:?}"))?;
editor.update(&mut cx, |editor, cx| {
editor.read_scroll_position_from_db(item_id, workspace_id, cx);
editor.update_in(&mut cx, |editor, window, cx| {
editor.read_scroll_position_from_db(item_id, workspace_id, window, cx);
})?;
Ok(editor)
})
@ -1119,7 +1164,8 @@ impl SerializableItem for Editor {
workspace: &mut Workspace,
item_id: ItemId,
closing: bool,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<Task<Result<()>>> {
let mut serialize_dirty_buffers = self.serialize_dirty_buffers;
@ -1156,7 +1202,7 @@ impl SerializableItem for Editor {
let snapshot = buffer.read(cx).snapshot();
Some(cx.spawn(|_this, cx| async move {
Some(cx.spawn_in(window, |_this, cx| async move {
cx.background_executor()
.spawn(async move {
let (contents, language) = if serialize_dirty_buffers && is_dirty {
@ -1173,7 +1219,6 @@ impl SerializableItem for Editor {
language,
mtime,
};
DB.save_serialized_editor(item_id, workspace_id, editor)
.await
.context("failed to save serialized editor")
@ -1197,11 +1242,12 @@ impl ProjectItem for Editor {
type Item = Buffer;
fn for_project_item(
project: Model<Project>,
buffer: Model<Buffer>,
cx: &mut ViewContext<Self>,
project: Entity<Project>,
buffer: Entity<Buffer>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
Self::for_buffer(buffer, Some(project), cx)
Self::for_buffer(buffer, Some(project), window, cx)
}
}
@ -1211,7 +1257,7 @@ pub(crate) enum BufferSearchHighlights {}
impl SearchableItem for Editor {
type Match = Range<Anchor>;
fn get_matches(&self, _: &mut WindowContext) -> Vec<Range<Anchor>> {
fn get_matches(&self, _window: &mut Window, _: &mut App) -> Vec<Range<Anchor>> {
self.background_highlights
.get(&TypeId::of::<BufferSearchHighlights>())
.map_or(Vec::new(), |(_color, ranges)| {
@ -1219,7 +1265,7 @@ impl SearchableItem for Editor {
})
}
fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
fn clear_matches(&mut self, _: &mut Window, cx: &mut Context<Self>) {
if self
.clear_background_highlights::<BufferSearchHighlights>(cx)
.is_some()
@ -1228,7 +1274,12 @@ impl SearchableItem for Editor {
}
}
fn update_matches(&mut self, matches: &[Range<Anchor>], cx: &mut ViewContext<Self>) {
fn update_matches(
&mut self,
matches: &[Range<Anchor>],
_: &mut Window,
cx: &mut Context<Self>,
) {
let existing_range = self
.background_highlights
.get(&TypeId::of::<BufferSearchHighlights>())
@ -1248,7 +1299,12 @@ impl SearchableItem for Editor {
self.has_background_highlights::<SearchWithinRange>()
}
fn toggle_filtered_search_ranges(&mut self, enabled: bool, cx: &mut ViewContext<Self>) {
fn toggle_filtered_search_ranges(
&mut self,
enabled: bool,
_: &mut Window,
cx: &mut Context<Self>,
) {
if self.has_filtered_search_ranges() {
self.previous_search_ranges = self
.clear_background_highlights::<SearchWithinRange>(cx)
@ -1267,9 +1323,9 @@ impl SearchableItem for Editor {
}
}
fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
fn query_suggestion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> String {
let setting = EditorSettings::get_global(cx).seed_search_query_from_cursor;
let snapshot = &self.snapshot(cx).buffer_snapshot;
let snapshot = &self.snapshot(window, cx).buffer_snapshot;
let selection = self.selections.newest::<usize>(cx);
match setting {
@ -1302,28 +1358,35 @@ impl SearchableItem for Editor {
&mut self,
index: usize,
matches: &[Range<Anchor>],
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.unfold_ranges(&[matches[index].clone()], false, true, cx);
let range = self.range_for_match(&matches[index]);
self.change_selections(Some(Autoscroll::fit()), cx, |s| {
self.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.select_ranges([range]);
})
}
fn select_matches(&mut self, matches: &[Self::Match], cx: &mut ViewContext<Self>) {
fn select_matches(
&mut self,
matches: &[Self::Match],
window: &mut Window,
cx: &mut Context<Self>,
) {
self.unfold_ranges(matches, false, false, cx);
let mut ranges = Vec::new();
for m in matches {
ranges.push(self.range_for_match(m))
}
self.change_selections(None, cx, |s| s.select_ranges(ranges));
self.change_selections(None, window, cx, |s| s.select_ranges(ranges));
}
fn replace(
&mut self,
identifier: &Self::Match,
query: &SearchQuery,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let text = self.buffer.read(cx);
let text = text.snapshot(cx);
@ -1336,7 +1399,7 @@ impl SearchableItem for Editor {
};
if let Some(replacement) = query.replacement_for(&text) {
self.transact(cx, |this, cx| {
self.transact(window, cx, |this, _, cx| {
this.edit([(identifier.clone(), Arc::from(&*replacement))], cx);
});
}
@ -1345,7 +1408,8 @@ impl SearchableItem for Editor {
&mut self,
matches: &mut dyn Iterator<Item = &Self::Match>,
query: &SearchQuery,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let text = self.buffer.read(cx);
let text = text.snapshot(cx);
@ -1365,7 +1429,7 @@ impl SearchableItem for Editor {
}
if !edits.is_empty() {
self.transact(cx, |this, cx| {
self.transact(window, cx, |this, _, cx| {
this.edit(edits, cx);
});
}
@ -1376,7 +1440,8 @@ impl SearchableItem for Editor {
current_index: usize,
direction: Direction,
count: usize,
cx: &mut ViewContext<Self>,
_: &mut Window,
cx: &mut Context<Self>,
) -> usize {
let buffer = self.buffer().read(cx).snapshot(cx);
let current_index_position = if self.selections.disjoint_anchors().len() == 1 {
@ -1422,7 +1487,8 @@ impl SearchableItem for Editor {
fn find_matches(
&mut self,
query: Arc<project::search::SearchQuery>,
cx: &mut ViewContext<Self>,
_: &mut Window,
cx: &mut Context<Self>,
) -> Task<Vec<Range<Anchor>>> {
let buffer = self.buffer().read(cx).snapshot(cx);
let search_within_ranges = self
@ -1470,7 +1536,8 @@ impl SearchableItem for Editor {
fn active_match_index(
&mut self,
matches: &[Range<Anchor>],
cx: &mut ViewContext<Self>,
_: &mut Window,
cx: &mut Context<Self>,
) -> Option<usize> {
active_match_index(
matches,
@ -1479,7 +1546,7 @@ impl SearchableItem for Editor {
)
}
fn search_bar_visibility_changed(&mut self, _visible: bool, _cx: &mut ViewContext<Self>) {
fn search_bar_visibility_changed(&mut self, _: bool, _: &mut Window, _: &mut Context<Self>) {
self.expect_bounds_change = self.last_bounds;
}
}
@ -1550,10 +1617,10 @@ pub fn entry_git_aware_label_color(git_status: GitSummary, ignored: bool, select
}
fn path_for_buffer<'a>(
buffer: &Model<MultiBuffer>,
buffer: &Entity<MultiBuffer>,
height: usize,
include_filename: bool,
cx: &'a AppContext,
cx: &'a App,
) -> Option<Cow<'a, Path>> {
let file = buffer.read(cx).as_singleton()?.read(cx).file()?;
path_for_file(file.as_ref(), height, include_filename, cx)
@ -1563,7 +1630,7 @@ fn path_for_file<'a>(
file: &'a dyn language::File,
mut height: usize,
include_filename: bool,
cx: &'a AppContext,
cx: &'a App,
) -> Option<Cow<'a, Path>> {
// Ensure we always render at least the filename.
height += 1;
@ -1604,13 +1671,13 @@ mod tests {
use super::*;
use fs::MTime;
use gpui::{AppContext, VisualTestContext};
use gpui::{App, VisualTestContext};
use language::{LanguageMatcher, TestFile};
use project::FakeFs;
use std::path::{Path, PathBuf};
#[gpui::test]
fn test_path_for_file(cx: &mut AppContext) {
fn test_path_for_file(cx: &mut App) {
let file = TestFile {
path: Path::new("").into(),
root_name: String::new(),
@ -1621,12 +1688,12 @@ mod tests {
async fn deserialize_editor(
item_id: ItemId,
workspace_id: WorkspaceId,
workspace: View<Workspace>,
project: Model<Project>,
workspace: Entity<Workspace>,
project: Entity<Project>,
cx: &mut VisualTestContext,
) -> View<Editor> {
) -> Entity<Editor> {
workspace
.update(cx, |workspace, cx| {
.update_in(cx, |workspace, window, cx| {
let pane = workspace.active_pane();
pane.update(cx, |_, cx| {
Editor::deserialize(
@ -1634,6 +1701,7 @@ mod tests {
workspace.weak_handle(),
workspace_id,
item_id,
window,
cx,
)
})
@ -1666,7 +1734,8 @@ mod tests {
// Test case 1: Deserialize with path and contents
{
let project = Project::test(fs.clone(), ["/file.rs".as_ref()], cx).await;
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
let (workspace, cx) =
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
let workspace_id = workspace::WORKSPACE_DB.next_id().await.unwrap();
let item_id = 1234 as ItemId;
let mtime = fs
@ -1702,7 +1771,8 @@ mod tests {
// Test case 2: Deserialize with only path
{
let project = Project::test(fs.clone(), ["/file.rs".as_ref()], cx).await;
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
let (workspace, cx) =
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
let workspace_id = workspace::WORKSPACE_DB.next_id().await.unwrap();
@ -1737,7 +1807,8 @@ mod tests {
// Add Rust to the language, so that we can restore the language of the buffer
project.update(cx, |project, _| project.languages().add(rust_language()));
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
let (workspace, cx) =
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
let workspace_id = workspace::WORKSPACE_DB.next_id().await.unwrap();
@ -1772,7 +1843,8 @@ mod tests {
// Test case 4: Deserialize with path, content, and old mtime
{
let project = Project::test(fs.clone(), ["/file.rs".as_ref()], cx).await;
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
let (workspace, cx) =
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
let workspace_id = workspace::WORKSPACE_DB.next_id().await.unwrap();