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

@ -4,7 +4,7 @@ use super::{
};
use crate::{EditorStyle, GutterDimensions};
use collections::{Bound, HashMap, HashSet};
use gpui::{AnyElement, AppContext, EntityId, Pixels, WindowContext};
use gpui::{AnyElement, App, EntityId, Pixels, Window};
use language::{Chunk, Patch, Point};
use multi_buffer::{
Anchor, ExcerptId, ExcerptInfo, MultiBuffer, MultiBufferRow, MultiBufferSnapshot, RowInfo,
@ -226,7 +226,8 @@ pub enum BlockStyle {
}
pub struct BlockContext<'a, 'b> {
pub context: &'b mut WindowContext<'a>,
pub window: &'a mut Window,
pub app: &'b mut App,
pub anchor_x: Pixels,
pub max_width: Pixels,
pub gutter_dimensions: &'b GutterDimensions,
@ -1232,22 +1233,12 @@ impl<'a> BlockMapWriter<'a> {
self.remove(blocks_to_remove);
}
pub fn fold_buffer(
&mut self,
buffer_id: BufferId,
multi_buffer: &MultiBuffer,
cx: &AppContext,
) {
pub fn fold_buffer(&mut self, buffer_id: BufferId, multi_buffer: &MultiBuffer, cx: &App) {
self.0.folded_buffers.insert(buffer_id);
self.recompute_blocks_for_buffer(buffer_id, multi_buffer, cx);
}
pub fn unfold_buffer(
&mut self,
buffer_id: BufferId,
multi_buffer: &MultiBuffer,
cx: &AppContext,
) {
pub fn unfold_buffer(&mut self, buffer_id: BufferId, multi_buffer: &MultiBuffer, cx: &App) {
self.0.folded_buffers.remove(&buffer_id);
self.recompute_blocks_for_buffer(buffer_id, multi_buffer, cx);
}
@ -1256,7 +1247,7 @@ impl<'a> BlockMapWriter<'a> {
&mut self,
buffer_id: BufferId,
multi_buffer: &MultiBuffer,
cx: &AppContext,
cx: &App,
) {
let wrap_snapshot = self.0.wrap_snapshot.borrow().clone();
@ -1934,16 +1925,16 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for BlockRow {
}
impl<'a> Deref for BlockContext<'a, '_> {
type Target = WindowContext<'a>;
type Target = App;
fn deref(&self) -> &Self::Target {
self.context
self.app
}
}
impl DerefMut for BlockContext<'_, '_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.context
self.app
}
}
@ -2001,7 +1992,7 @@ mod tests {
use crate::display_map::{
fold_map::FoldMap, inlay_map::InlayMap, tab_map::TabMap, wrap_map::WrapMap,
};
use gpui::{div, font, px, AppContext, Context as _, Element};
use gpui::{div, font, px, App, AppContext as _, Element};
use itertools::Itertools;
use language::{Buffer, Capability};
use multi_buffer::{ExcerptRange, MultiBuffer};
@ -2195,15 +2186,15 @@ mod tests {
}
#[gpui::test]
fn test_multibuffer_headers_and_footers(cx: &mut AppContext) {
fn test_multibuffer_headers_and_footers(cx: &mut App) {
init_test(cx);
let buffer1 = cx.new_model(|cx| Buffer::local("Buffer 1", cx));
let buffer2 = cx.new_model(|cx| Buffer::local("Buffer 2", cx));
let buffer3 = cx.new_model(|cx| Buffer::local("Buffer 3", cx));
let buffer1 = cx.new(|cx| Buffer::local("Buffer 1", cx));
let buffer2 = cx.new(|cx| Buffer::local("Buffer 2", cx));
let buffer3 = cx.new(|cx| Buffer::local("Buffer 3", cx));
let mut excerpt_ids = Vec::new();
let multi_buffer = cx.new_model(|cx| {
let multi_buffer = cx.new(|cx| {
let mut multi_buffer = MultiBuffer::new(Capability::ReadWrite);
excerpt_ids.extend(multi_buffer.push_excerpts(
buffer1.clone(),
@ -3652,7 +3643,7 @@ mod tests {
}
}
fn init_test(cx: &mut gpui::AppContext) {
fn init_test(cx: &mut gpui::App) {
let settings = SettingsStore::test(cx);
cx.set_global(settings);
theme::init(theme::LoadThemes::JustBase, cx);

View file

@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use std::{cmp::Ordering, fmt::Debug, ops::Range, sync::Arc};
use sum_tree::{Bias, SeekTarget, SumTree};
use text::Point;
use ui::{IconName, SharedString, WindowContext};
use ui::{App, IconName, SharedString, Window};
use crate::{BlockStyle, FoldPlaceholder, RenderBlock};
@ -117,12 +117,13 @@ type RenderToggleFn = Arc<
+ Fn(
MultiBufferRow,
bool,
Arc<dyn Send + Sync + Fn(bool, &mut WindowContext)>,
&mut WindowContext,
Arc<dyn Send + Sync + Fn(bool, &mut Window, &mut App)>,
&mut Window,
&mut App,
) -> AnyElement,
>;
type RenderTrailerFn =
Arc<dyn Send + Sync + Fn(MultiBufferRow, bool, &mut WindowContext) -> AnyElement>;
Arc<dyn Send + Sync + Fn(MultiBufferRow, bool, &mut Window, &mut App) -> AnyElement>;
#[derive(Clone)]
pub enum Crease<T> {
@ -185,26 +186,27 @@ impl<T> Crease<T> {
+ Fn(
MultiBufferRow,
bool,
Arc<dyn Send + Sync + Fn(bool, &mut WindowContext)>,
&mut WindowContext,
Arc<dyn Send + Sync + Fn(bool, &mut Window, &mut App)>,
&mut Window,
&mut App,
) -> ToggleElement
+ 'static,
ToggleElement: IntoElement,
RenderTrailer: 'static
+ Send
+ Sync
+ Fn(MultiBufferRow, bool, &mut WindowContext) -> TrailerElement
+ Fn(MultiBufferRow, bool, &mut Window, &mut App) -> TrailerElement
+ 'static,
TrailerElement: IntoElement,
{
Crease::Inline {
range,
placeholder,
render_toggle: Some(Arc::new(move |row, folded, toggle, cx| {
render_toggle(row, folded, toggle, cx).into_any_element()
render_toggle: Some(Arc::new(move |row, folded, toggle, window, cx| {
render_toggle(row, folded, toggle, window, cx).into_any_element()
})),
render_trailer: Some(Arc::new(move |row, folded, cx| {
render_trailer(row, folded, cx).into_any_element()
render_trailer: Some(Arc::new(move |row, folded, window, cx| {
render_trailer(row, folded, window, cx).into_any_element()
})),
metadata: None,
}
@ -387,11 +389,11 @@ impl SeekTarget<'_, ItemSummary, ItemSummary> for Anchor {
#[cfg(test)]
mod test {
use super::*;
use gpui::{div, AppContext};
use gpui::{div, App};
use multi_buffer::MultiBuffer;
#[gpui::test]
fn test_insert_and_remove_creases(cx: &mut AppContext) {
fn test_insert_and_remove_creases(cx: &mut App) {
let text = "line1\nline2\nline3\nline4\nline5";
let buffer = MultiBuffer::build_simple(text, cx);
let snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx));
@ -402,14 +404,14 @@ mod test {
Crease::inline(
snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 5)),
FoldPlaceholder::test(),
|_row, _folded, _toggle, _cx| div(),
|_row, _folded, _cx| div(),
|_row, _folded, _toggle, _window, _cx| div(),
|_row, _folded, _window, _cx| div(),
),
Crease::inline(
snapshot.anchor_before(Point::new(3, 0))..snapshot.anchor_after(Point::new(3, 5)),
FoldPlaceholder::test(),
|_row, _folded, _toggle, _cx| div(),
|_row, _folded, _cx| div(),
|_row, _folded, _toggle, _window, _cx| div(),
|_row, _folded, _window, _cx| div(),
),
];
let crease_ids = crease_map.insert(creases, &snapshot);
@ -438,7 +440,7 @@ mod test {
}
#[gpui::test]
fn test_creases_in_range(cx: &mut AppContext) {
fn test_creases_in_range(cx: &mut App) {
let text = "line1\nline2\nline3\nline4\nline5\nline6\nline7";
let buffer = MultiBuffer::build_simple(text, cx);
let snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx));
@ -448,20 +450,20 @@ mod test {
Crease::inline(
snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 5)),
FoldPlaceholder::test(),
|_row, _folded, _toggle, _cx| div(),
|_row, _folded, _cx| div(),
|_row, _folded, _toggle, _window, _cx| div(),
|_row, _folded, _window, _cx| div(),
),
Crease::inline(
snapshot.anchor_before(Point::new(3, 0))..snapshot.anchor_after(Point::new(3, 5)),
FoldPlaceholder::test(),
|_row, _folded, _toggle, _cx| div(),
|_row, _folded, _cx| div(),
|_row, _folded, _toggle, _window, _cx| div(),
|_row, _folded, _window, _cx| div(),
),
Crease::inline(
snapshot.anchor_before(Point::new(5, 0))..snapshot.anchor_after(Point::new(5, 5)),
FoldPlaceholder::test(),
|_row, _folded, _toggle, _cx| div(),
|_row, _folded, _cx| div(),
|_row, _folded, _toggle, _window, _cx| div(),
|_row, _folded, _window, _cx| div(),
),
];
crease_map.insert(creases, &snapshot);

View file

@ -2,7 +2,7 @@ use super::{
inlay_map::{InlayBufferRows, InlayChunks, InlayEdit, InlayOffset, InlayPoint, InlaySnapshot},
Highlights,
};
use gpui::{AnyElement, ElementId, WindowContext};
use gpui::{AnyElement, App, ElementId, Window};
use language::{Chunk, ChunkRenderer, Edit, Point, TextSummary};
use multi_buffer::{
Anchor, AnchorRangeExt, MultiBufferRow, MultiBufferSnapshot, RowInfo, ToOffset,
@ -21,7 +21,8 @@ use util::post_inc;
#[derive(Clone)]
pub struct FoldPlaceholder {
/// Creates an element to represent this fold's placeholder.
pub render: Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut WindowContext) -> AnyElement>,
pub render:
Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut Window, &mut App) -> AnyElement>,
/// If true, the element is constrained to the shaped width of an ellipsis.
pub constrain_width: bool,
/// If true, merges the fold with an adjacent one.
@ -33,7 +34,7 @@ pub struct FoldPlaceholder {
impl Default for FoldPlaceholder {
fn default() -> Self {
Self {
render: Arc::new(|_, _, _| gpui::Empty.into_any_element()),
render: Arc::new(|_, _, _, _| gpui::Empty.into_any_element()),
constrain_width: true,
merge_adjacent: true,
type_tag: None,
@ -45,7 +46,7 @@ impl FoldPlaceholder {
#[cfg(any(test, feature = "test-support"))]
pub fn test() -> Self {
Self {
render: Arc::new(|_id, _range, _cx| gpui::Empty.into_any_element()),
render: Arc::new(|_id, _range, _window, _cx| gpui::Empty.into_any_element()),
constrain_width: true,
merge_adjacent: true,
type_tag: None,
@ -485,7 +486,8 @@ impl FoldMap {
(fold.placeholder.render)(
fold_id,
fold.range.0.clone(),
cx,
cx.window,
cx.context,
)
}),
constrain_width: fold.placeholder.constrain_width,
@ -1395,7 +1397,7 @@ mod tests {
use Bias::{Left, Right};
#[gpui::test]
fn test_basic_folds(cx: &mut gpui::AppContext) {
fn test_basic_folds(cx: &mut gpui::App) {
init_test(cx);
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
@ -1474,7 +1476,7 @@ mod tests {
}
#[gpui::test]
fn test_adjacent_folds(cx: &mut gpui::AppContext) {
fn test_adjacent_folds(cx: &mut gpui::App) {
init_test(cx);
let buffer = MultiBuffer::build_simple("abcdefghijkl", cx);
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
@ -1533,7 +1535,7 @@ mod tests {
}
#[gpui::test]
fn test_overlapping_folds(cx: &mut gpui::AppContext) {
fn test_overlapping_folds(cx: &mut gpui::App) {
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
let buffer_snapshot = buffer.read(cx).snapshot(cx);
let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot);
@ -1550,7 +1552,7 @@ mod tests {
}
#[gpui::test]
fn test_merging_folds_via_edit(cx: &mut gpui::AppContext) {
fn test_merging_folds_via_edit(cx: &mut gpui::App) {
init_test(cx);
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
@ -1577,7 +1579,7 @@ mod tests {
}
#[gpui::test]
fn test_folds_in_range(cx: &mut gpui::AppContext) {
fn test_folds_in_range(cx: &mut gpui::App) {
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
let buffer_snapshot = buffer.read(cx).snapshot(cx);
let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
@ -1608,7 +1610,7 @@ mod tests {
}
#[gpui::test(iterations = 100)]
fn test_random_folds(cx: &mut gpui::AppContext, mut rng: StdRng) {
fn test_random_folds(cx: &mut gpui::App, mut rng: StdRng) {
init_test(cx);
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
@ -1879,7 +1881,7 @@ mod tests {
}
#[gpui::test]
fn test_buffer_rows(cx: &mut gpui::AppContext) {
fn test_buffer_rows(cx: &mut gpui::App) {
let text = sample_text(6, 6, 'a') + "\n";
let buffer = MultiBuffer::build_simple(&text, cx);
@ -1911,7 +1913,7 @@ mod tests {
);
}
fn init_test(cx: &mut gpui::AppContext) {
fn init_test(cx: &mut gpui::App) {
let store = SettingsStore::test(cx);
cx.set_global(store);
}

View file

@ -1070,7 +1070,7 @@ mod tests {
hover_links::InlayHighlight,
InlayId, MultiBuffer,
};
use gpui::{AppContext, HighlightStyle};
use gpui::{App, HighlightStyle};
use project::{InlayHint, InlayHintLabel, ResolveState};
use rand::prelude::*;
use settings::SettingsStore;
@ -1163,7 +1163,7 @@ mod tests {
}
#[gpui::test]
fn test_basic_inlays(cx: &mut AppContext) {
fn test_basic_inlays(cx: &mut App) {
let buffer = MultiBuffer::build_simple("abcdefghi", cx);
let buffer_edits = buffer.update(cx, |buffer, _| buffer.subscribe());
let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer.read(cx).snapshot(cx));
@ -1451,7 +1451,7 @@ mod tests {
}
#[gpui::test]
fn test_inlay_buffer_rows(cx: &mut AppContext) {
fn test_inlay_buffer_rows(cx: &mut App) {
let buffer = MultiBuffer::build_simple("abc\ndef\nghi", cx);
let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer.read(cx).snapshot(cx));
assert_eq!(inlay_snapshot.text(), "abc\ndef\nghi");
@ -1488,7 +1488,7 @@ mod tests {
}
#[gpui::test(iterations = 100)]
fn test_random_inlays(cx: &mut AppContext, mut rng: StdRng) {
fn test_random_inlays(cx: &mut App, mut rng: StdRng) {
init_test(cx);
let operations = env::var("OPERATIONS")
@ -1792,7 +1792,7 @@ mod tests {
}
}
fn init_test(cx: &mut AppContext) {
fn init_test(cx: &mut App) {
let store = SettingsStore::test(cx);
cx.set_global(store);
theme::init(theme::LoadThemes::JustBase, cx);

View file

@ -608,7 +608,7 @@ mod tests {
use rand::{prelude::StdRng, Rng};
#[gpui::test]
fn test_expand_tabs(cx: &mut gpui::AppContext) {
fn test_expand_tabs(cx: &mut gpui::App) {
let buffer = MultiBuffer::build_simple("", cx);
let buffer_snapshot = buffer.read(cx).snapshot(cx);
let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
@ -621,7 +621,7 @@ mod tests {
}
#[gpui::test]
fn test_long_lines(cx: &mut gpui::AppContext) {
fn test_long_lines(cx: &mut gpui::App) {
let max_expansion_column = 12;
let input = "A\tBC\tDEF\tG\tHI\tJ\tK\tL\tM";
let output = "A BC DEF G HI J K L M";
@ -669,7 +669,7 @@ mod tests {
}
#[gpui::test]
fn test_long_lines_with_character_spanning_max_expansion_column(cx: &mut gpui::AppContext) {
fn test_long_lines_with_character_spanning_max_expansion_column(cx: &mut gpui::App) {
let max_expansion_column = 8;
let input = "abcdefg⋯hij";
@ -684,7 +684,7 @@ mod tests {
}
#[gpui::test]
fn test_marking_tabs(cx: &mut gpui::AppContext) {
fn test_marking_tabs(cx: &mut gpui::App) {
let input = "\t \thello";
let buffer = MultiBuffer::build_simple(input, cx);
@ -735,7 +735,7 @@ mod tests {
}
#[gpui::test(iterations = 100)]
fn test_random_tabs(cx: &mut gpui::AppContext, mut rng: StdRng) {
fn test_random_tabs(cx: &mut gpui::App, mut rng: StdRng) {
let tab_size = NonZeroU32::new(rng.gen_range(1..=4)).unwrap();
let len = rng.gen_range(0..30);
let buffer = if rng.gen() {

View file

@ -3,7 +3,7 @@ use super::{
tab_map::{self, TabEdit, TabPoint, TabSnapshot},
Highlights,
};
use gpui::{AppContext, Context, Font, LineWrapper, Model, ModelContext, Pixels, Task};
use gpui::{App, AppContext as _, Context, Entity, Font, LineWrapper, Pixels, Task};
use language::{Chunk, Point};
use multi_buffer::{MultiBufferSnapshot, RowInfo};
use smol::future::yield_now;
@ -90,9 +90,9 @@ impl WrapMap {
font: Font,
font_size: Pixels,
wrap_width: Option<Pixels>,
cx: &mut AppContext,
) -> (Model<Self>, WrapSnapshot) {
let handle = cx.new_model(|cx| {
cx: &mut App,
) -> (Entity<Self>, WrapSnapshot) {
let handle = cx.new(|cx| {
let mut this = Self {
font_with_size: (font, font_size),
wrap_width: None,
@ -119,7 +119,7 @@ impl WrapMap {
&mut self,
tab_snapshot: TabSnapshot,
edits: Vec<TabEdit>,
cx: &mut ModelContext<Self>,
cx: &mut Context<Self>,
) -> (WrapSnapshot, Patch<u32>) {
if self.wrap_width.is_some() {
self.pending_edits.push_back((tab_snapshot, edits));
@ -138,7 +138,7 @@ impl WrapMap {
&mut self,
font: Font,
font_size: Pixels,
cx: &mut ModelContext<Self>,
cx: &mut Context<Self>,
) -> bool {
let font_with_size = (font, font_size);
@ -151,11 +151,7 @@ impl WrapMap {
}
}
pub fn set_wrap_width(
&mut self,
wrap_width: Option<Pixels>,
cx: &mut ModelContext<Self>,
) -> bool {
pub fn set_wrap_width(&mut self, wrap_width: Option<Pixels>, cx: &mut Context<Self>) -> bool {
if wrap_width == self.wrap_width {
return false;
}
@ -165,7 +161,7 @@ impl WrapMap {
true
}
fn rewrap(&mut self, cx: &mut ModelContext<Self>) {
fn rewrap(&mut self, cx: &mut Context<Self>) {
self.background_task.take();
self.interpolated_edits.clear();
self.pending_edits.clear();
@ -236,7 +232,7 @@ impl WrapMap {
}
}
fn flush_edits(&mut self, cx: &mut ModelContext<Self>) {
fn flush_edits(&mut self, cx: &mut Context<Self>) {
if !self.snapshot.interpolated {
let mut to_remove_len = 0;
for (tab_snapshot, _) in &self.pending_edits {