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 std::{
sync::Arc,
};
use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, Context as _, Result};
use collections::{HashMap, HashSet};
use fs::Fs;
use futures::{
@ -12,7 +12,7 @@ use futures::{
stream::FuturesUnordered,
FutureExt,
};
use gpui::{AsyncAppContext, EventEmitter, Model, ModelContext, Task, WeakModel};
use gpui::{AsyncAppContext, Context, Entity, EventEmitter, Task, WeakEntity};
use language::{
language_settings::{Formatter, LanguageSettings, SelectedFormatter},
Buffer, LanguageRegistry, LocalFile,
@ -33,7 +33,7 @@ pub struct PrettierStore {
node: NodeRuntime,
fs: Arc<dyn Fs>,
languages: Arc<LanguageRegistry>,
worktree_store: Model<WorktreeStore>,
worktree_store: Entity<WorktreeStore>,
default_prettier: DefaultPrettier,
prettiers_per_worktree: HashMap<WorktreeId, HashSet<Option<PathBuf>>>,
prettier_ignores_per_worktree: HashMap<WorktreeId, HashSet<PathBuf>>,
@ -56,8 +56,8 @@ impl PrettierStore {
node: NodeRuntime,
fs: Arc<dyn Fs>,
languages: Arc<LanguageRegistry>,
worktree_store: Model<WorktreeStore>,
_: &mut ModelContext<Self>,
worktree_store: Entity<WorktreeStore>,
_: &mut Context<Self>,
) -> Self {
Self {
node,
@ -71,7 +71,7 @@ impl PrettierStore {
}
}
pub fn remove_worktree(&mut self, id_to_remove: WorktreeId, cx: &mut ModelContext<Self>) {
pub fn remove_worktree(&mut self, id_to_remove: WorktreeId, cx: &mut Context<Self>) {
self.prettier_ignores_per_worktree.remove(&id_to_remove);
let mut prettier_instances_to_clean = FuturesUnordered::new();
if let Some(prettier_paths) = self.prettiers_per_worktree.remove(&id_to_remove) {
@ -104,8 +104,8 @@ impl PrettierStore {
fn prettier_instance_for_buffer(
&mut self,
buffer: &Model<Buffer>,
cx: &mut ModelContext<Self>,
buffer: &Entity<Buffer>,
cx: &mut Context<Self>,
) -> Task<Option<(Option<PathBuf>, PrettierTask)>> {
let buffer = buffer.read(cx);
let buffer_file = buffer.file();
@ -216,8 +216,8 @@ impl PrettierStore {
fn prettier_ignore_for_buffer(
&mut self,
buffer: &Model<Buffer>,
cx: &mut ModelContext<Self>,
buffer: &Entity<Buffer>,
cx: &mut Context<Self>,
) -> Task<Option<PathBuf>> {
let buffer = buffer.read(cx);
let buffer_file = buffer.file();
@ -277,7 +277,7 @@ impl PrettierStore {
node: NodeRuntime,
prettier_dir: PathBuf,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<Self>,
cx: &mut Context<Self>,
) -> PrettierTask {
cx.spawn(|prettier_store, mut cx| async move {
log::info!("Starting prettier at path {prettier_dir:?}");
@ -305,7 +305,7 @@ impl PrettierStore {
fn start_default_prettier(
node: NodeRuntime,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<PrettierStore>,
cx: &mut Context<PrettierStore>,
) -> Task<anyhow::Result<PrettierTask>> {
cx.spawn(|prettier_store, mut cx| async move {
let installation_task = prettier_store.update(&mut cx, |prettier_store, _| {
@ -383,7 +383,7 @@ impl PrettierStore {
}
fn register_new_prettier(
prettier_store: &WeakModel<Self>,
prettier_store: &WeakEntity<Self>,
prettier: &Prettier,
worktree_id: Option<WorktreeId>,
new_server_id: LanguageServerId,
@ -442,9 +442,9 @@ impl PrettierStore {
pub fn update_prettier_settings(
&self,
worktree: &Model<Worktree>,
worktree: &Entity<Worktree>,
changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
cx: &mut ModelContext<Self>,
cx: &mut Context<Self>,
) {
let prettier_config_files = Prettier::CONFIG_FILE_NAMES
.iter()
@ -516,7 +516,7 @@ impl PrettierStore {
&mut self,
worktree: Option<WorktreeId>,
plugins: impl Iterator<Item = Arc<str>>,
cx: &mut ModelContext<Self>,
cx: &mut Context<Self>,
) {
if cfg!(any(test, feature = "test-support")) {
self.default_prettier.installed_plugins.extend(plugins);
@ -668,7 +668,7 @@ impl PrettierStore {
pub fn on_settings_changed(
&mut self,
language_formatters_to_check: Vec<(Option<WorktreeId>, LanguageSettings)>,
cx: &mut ModelContext<Self>,
cx: &mut Context<Self>,
) {
let mut prettier_plugins_by_worktree = HashMap::default();
for (worktree, language_settings) in language_formatters_to_check {
@ -705,8 +705,8 @@ pub fn prettier_plugins_for_language(
}
pub(super) async fn format_with_prettier(
prettier_store: &WeakModel<PrettierStore>,
buffer: &Model<Buffer>,
prettier_store: &WeakEntity<PrettierStore>,
buffer: &Entity<Buffer>,
cx: &mut AsyncAppContext,
) -> Option<Result<crate::lsp_store::FormatOperation>> {
let prettier_instance = prettier_store
@ -822,7 +822,7 @@ impl DefaultPrettier {
&mut self,
node: &NodeRuntime,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<PrettierStore>,
cx: &mut Context<PrettierStore>,
) -> Option<Task<anyhow::Result<PrettierTask>>> {
match &mut self.prettier {
PrettierInstallation::NotInstalled { .. } => Some(
@ -841,7 +841,7 @@ impl PrettierInstance {
node: &NodeRuntime,
prettier_dir: Option<&Path>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<PrettierStore>,
cx: &mut Context<PrettierStore>,
) -> Option<Task<anyhow::Result<PrettierTask>>> {
if self.attempt > prettier::FAIL_THRESHOLD {
match prettier_dir {