ZIm/crates/toolchain_selector/src/active_toolchain.rs
Nathan Sobo 6fca1d2b0b
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>
2025-01-26 03:02:45 +00:00

187 lines
6.9 KiB
Rust

use editor::Editor;
use gpui::{
div, AsyncWindowContext, Context, Entity, IntoElement, ParentElement, Render, Subscription,
Task, WeakEntity, Window,
};
use language::{Buffer, BufferEvent, LanguageName, Toolchain};
use project::{Project, WorktreeId};
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, SharedString, Tooltip};
use workspace::{item::ItemHandle, StatusItemView, Workspace};
use crate::ToolchainSelector;
pub struct ActiveToolchain {
active_toolchain: Option<Toolchain>,
term: SharedString,
workspace: WeakEntity<Workspace>,
active_buffer: Option<(WorktreeId, WeakEntity<Buffer>, Subscription)>,
_update_toolchain_task: Task<Option<()>>,
}
impl ActiveToolchain {
pub fn new(workspace: &Workspace, window: &mut Window, cx: &mut Context<Self>) -> Self {
Self {
active_toolchain: None,
active_buffer: None,
term: SharedString::new_static("Toolchain"),
workspace: workspace.weak_handle(),
_update_toolchain_task: Self::spawn_tracker_task(window, cx),
}
}
fn spawn_tracker_task(window: &mut Window, cx: &mut Context<Self>) -> Task<Option<()>> {
cx.spawn_in(window, |this, mut cx| async move {
let active_file = this
.update(&mut cx, |this, _| {
this.active_buffer
.as_ref()
.map(|(_, buffer, _)| buffer.clone())
})
.ok()
.flatten()?;
let workspace = this
.update(&mut cx, |this, _| this.workspace.clone())
.ok()?;
let language_name = active_file
.update(&mut cx, |this, _| Some(this.language()?.name()))
.ok()
.flatten()?;
let term = workspace
.update(&mut cx, |workspace, cx| {
let languages = workspace.project().read(cx).languages();
Project::toolchain_term(languages.clone(), language_name.clone())
})
.ok()?
.await?;
let _ = this.update(&mut cx, |this, cx| {
this.term = term;
cx.notify();
});
let worktree_id = active_file
.update(&mut cx, |this, cx| Some(this.file()?.worktree_id(cx)))
.ok()
.flatten()?;
let toolchain =
Self::active_toolchain(workspace, worktree_id, language_name, &mut cx).await?;
let _ = this.update(&mut cx, |this, cx| {
this.active_toolchain = Some(toolchain);
cx.notify();
});
Some(())
})
}
fn update_lister(
&mut self,
editor: Entity<Editor>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let editor = editor.read(cx);
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
if let Some(worktree_id) = buffer.read(cx).file().map(|file| file.worktree_id(cx)) {
let subscription = cx.subscribe_in(
&buffer,
window,
|this, _, event: &BufferEvent, window, cx| {
if matches!(event, BufferEvent::LanguageChanged) {
this._update_toolchain_task = Self::spawn_tracker_task(window, cx);
}
},
);
self.active_buffer = Some((worktree_id, buffer.downgrade(), subscription));
self._update_toolchain_task = Self::spawn_tracker_task(window, cx);
}
}
cx.notify();
}
fn active_toolchain(
workspace: WeakEntity<Workspace>,
worktree_id: WorktreeId,
language_name: LanguageName,
cx: &mut AsyncWindowContext,
) -> Task<Option<Toolchain>> {
cx.spawn(move |mut cx| async move {
let workspace_id = workspace
.update(&mut cx, |this, _| this.database_id())
.ok()
.flatten()?;
let selected_toolchain = workspace
.update(&mut cx, |this, cx| {
this.project()
.read(cx)
.active_toolchain(worktree_id, language_name.clone(), cx)
})
.ok()?
.await;
if let Some(toolchain) = selected_toolchain {
Some(toolchain)
} else {
let project = workspace
.update(&mut cx, |this, _| this.project().clone())
.ok()?;
let toolchains = cx
.update(|_, cx| {
project
.read(cx)
.available_toolchains(worktree_id, language_name, cx)
})
.ok()?
.await?;
if let Some(toolchain) = toolchains.toolchains.first() {
// Since we don't have a selected toolchain, pick one for user here.
workspace::WORKSPACE_DB
.set_toolchain(workspace_id, worktree_id, toolchain.clone())
.await
.ok()?;
project
.update(&mut cx, |this, cx| {
this.activate_toolchain(worktree_id, toolchain.clone(), cx)
})
.ok()?
.await;
}
toolchains.toolchains.first().cloned()
}
})
}
}
impl Render for ActiveToolchain {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().when_some(self.active_toolchain.as_ref(), |el, active_toolchain| {
let term = self.term.clone();
el.child(
Button::new("change-toolchain", active_toolchain.name.clone())
.label_size(LabelSize::Small)
.on_click(cx.listener(|this, _, window, cx| {
if let Some(workspace) = this.workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
ToolchainSelector::toggle(workspace, window, cx)
});
}
}))
.tooltip(Tooltip::text(format!("Select {}", &term))),
)
})
}
}
impl StatusItemView for ActiveToolchain {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
self.active_toolchain.take();
self.update_lister(editor, window, cx);
}
cx.notify();
}
}