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

@ -3,11 +3,11 @@ use std::sync::Arc;
use anyhow::Result;
use extension::{ExtensionHostProxy, ExtensionSnippetProxy};
use gpui::AppContext;
use gpui::App;
use crate::SnippetRegistry;
pub fn init(cx: &mut AppContext) {
pub fn init(cx: &mut App) {
let proxy = ExtensionHostProxy::default_global(cx);
proxy.register_snippet_proxy(SnippetRegistryProxy {
snippet_registry: SnippetRegistry::global(cx),

View file

@ -13,11 +13,11 @@ use collections::{BTreeMap, BTreeSet, HashMap};
use format::VSSnippetsFile;
use fs::Fs;
use futures::stream::StreamExt;
use gpui::{AppContext, AsyncAppContext, Context, Model, ModelContext, Task, WeakModel};
use gpui::{App, AppContext as _, AsyncAppContext, Context, Entity, Task, WeakEntity};
pub use registry::*;
use util::ResultExt;
pub fn init(cx: &mut AppContext) {
pub fn init(cx: &mut App) {
SnippetRegistry::init_global(cx);
extension_snippet::init(cx);
}
@ -62,7 +62,7 @@ pub struct Snippet {
}
async fn process_updates(
this: WeakModel<SnippetProvider>,
this: WeakEntity<SnippetProvider>,
entries: Vec<PathBuf>,
mut cx: AsyncAppContext,
) -> Result<()> {
@ -112,7 +112,7 @@ async fn process_updates(
}
async fn initial_scan(
this: WeakModel<SnippetProvider>,
this: WeakEntity<SnippetProvider>,
path: Arc<Path>,
mut cx: AsyncAppContext,
) -> Result<()> {
@ -136,12 +136,12 @@ pub struct SnippetProvider {
}
// Watches global snippet directory, is created just once and reused across multiple projects
struct GlobalSnippetWatcher(Model<SnippetProvider>);
struct GlobalSnippetWatcher(Entity<SnippetProvider>);
impl GlobalSnippetWatcher {
fn new(fs: Arc<dyn Fs>, cx: &mut AppContext) -> Self {
fn new(fs: Arc<dyn Fs>, cx: &mut App) -> Self {
let global_snippets_dir = paths::config_dir().join("snippets");
let provider = cx.new_model(|_cx| SnippetProvider {
let provider = cx.new(|_cx| SnippetProvider {
fs,
snippets: Default::default(),
watch_tasks: vec![],
@ -156,12 +156,8 @@ impl GlobalSnippetWatcher {
impl gpui::Global for GlobalSnippetWatcher {}
impl SnippetProvider {
pub fn new(
fs: Arc<dyn Fs>,
dirs_to_watch: BTreeSet<PathBuf>,
cx: &mut AppContext,
) -> Model<Self> {
cx.new_model(move |cx| {
pub fn new(fs: Arc<dyn Fs>, dirs_to_watch: BTreeSet<PathBuf>, cx: &mut App) -> Entity<Self> {
cx.new(move |cx| {
if !cx.has_global::<GlobalSnippetWatcher>() {
let global_watcher = GlobalSnippetWatcher::new(fs.clone(), cx);
cx.set_global(global_watcher);
@ -181,7 +177,7 @@ impl SnippetProvider {
}
/// Add directory to be watched for content changes
fn watch_directory(&mut self, path: &Path, cx: &ModelContext<Self>) {
fn watch_directory(&mut self, path: &Path, cx: &Context<Self>) {
let path: Arc<Path> = Arc::from(path);
self.watch_tasks.push(cx.spawn(|this, mut cx| async move {
@ -206,7 +202,7 @@ impl SnippetProvider {
fn lookup_snippets<'a, const LOOKUP_GLOBALS: bool>(
&'a self,
language: &'a SnippetKind,
cx: &AppContext,
cx: &App,
) -> Vec<Arc<Snippet>> {
let mut user_snippets: Vec<_> = self
.snippets
@ -237,7 +233,7 @@ impl SnippetProvider {
user_snippets
}
pub fn snippets_for(&self, language: SnippetKind, cx: &AppContext) -> Vec<Arc<Snippet>> {
pub fn snippets_for(&self, language: SnippetKind, cx: &App) -> Vec<Arc<Snippet>> {
let mut requested_snippets = self.lookup_snippets::<true>(&language, cx);
if language.is_some() {

View file

@ -2,7 +2,7 @@ use std::{path::Path, sync::Arc};
use anyhow::Result;
use collections::HashMap;
use gpui::{AppContext, Global, ReadGlobal, UpdateGlobal};
use gpui::{App, Global, ReadGlobal, UpdateGlobal};
use parking_lot::RwLock;
use crate::{file_stem_to_key, Snippet, SnippetKind};
@ -17,16 +17,16 @@ pub struct SnippetRegistry {
}
impl SnippetRegistry {
pub fn global(cx: &AppContext) -> Arc<Self> {
pub fn global(cx: &App) -> Arc<Self> {
GlobalSnippetRegistry::global(cx).0.clone()
}
pub fn try_global(cx: &AppContext) -> Option<Arc<Self>> {
pub fn try_global(cx: &App) -> Option<Arc<Self>> {
cx.try_global::<GlobalSnippetRegistry>()
.map(|registry| registry.0.clone())
}
pub fn init_global(cx: &mut AppContext) {
pub fn init_global(cx: &mut App) {
GlobalSnippetRegistry::set_global(cx, GlobalSnippetRegistry(Arc::new(Self::new())))
}