Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into implicit-ancestry

Antonio Scandurrada19edc3e361
diff --cc crates/gpui/src/app.rs
index 862374f3127,caebc2714a5..23d9662b570
--- a/crates/gpui/src/app.rs
+++ b/crates/gpui/src/app.rs
@@@ -450,6 -441,8 +451,7 @@@ type WindowShouldCloseSubscriptionCallb
pub struct AppContext {
models: HashMap<usize, Box<dyn AnyModel>>,
views: HashMap<(usize, usize), Box<dyn AnyView>>,
+ views_metadata: HashMap<(usize, usize), ViewMetadata>,
- pub(crate) parents: HashMap<(usize, usize), ParentId>,
windows: HashMap<usize, Window>,
globals: HashMap<TypeId, Box<dyn Any>>,
element_states: HashMap<ElementStateId, Box<dyn Any>>,
@@@ -511,6 -504,8 +513,7 @@@ impl AppContext
Self {
models: Default::default(),
views: Default::default(),
+ views_metadata: Default::default(),
- parents: Default::default(),
windows: Default::default(),
globals: Default::default(),
element_states: Default::default(),
@@@ -3205,125 -3269,6 +3219,116 @@@ impl<V> BorrowWindowContext for ViewCon
}
}
+pub struct LayoutContext<'a, 'b, 'c, V: View> {
+ view_context: &'c mut ViewContext<'a, 'b, V>,
+ new_parents: &'c mut HashMap<usize, usize>,
+ views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>,
+ pub refreshing: bool,
+}
+
+impl<'a, 'b, 'c, V: View> LayoutContext<'a, 'b, 'c, V> {
+ pub fn new(
+ view_context: &'c mut ViewContext<'a, 'b, V>,
+ new_parents: &'c mut HashMap<usize, usize>,
+ views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>,
+ refreshing: bool,
+ ) -> Self {
+ Self {
+ view_context,
+ new_parents,
+ views_to_notify_if_ancestors_change,
+ refreshing,
+ }
+ }
+
+ pub fn view_context(&mut self) -> &mut ViewContext<'a, 'b, V> {
+ self.view_context
+ }
+
+ /// Return keystrokes that would dispatch the given action on the given view.
+ pub(crate) fn keystrokes_for_action(
+ &mut self,
- view: &V,
+ view_id: usize,
+ action: &dyn Action,
+ ) -> Option<SmallVec<[Keystroke; 2]>> {
+ self.notify_if_view_ancestors_change(view_id);
+
+ let window_id = self.window_id;
+ let mut contexts = Vec::new();
+ let mut handler_depth = None;
+ for (i, view_id) in self.ancestors(view_id).enumerate() {
- let view = if view_id == self.view_id {
- Some(view as _)
- } else {
- self.views
- .get(&(window_id, view_id))
- .map(|view| view.as_ref())
- };
-
- if let Some(view) = view {
- if let Some(actions) = self.actions.get(&view.as_any().type_id()) {
++ if let Some(view_metadata) = self.views_metadata.get(&(window_id, view_id)) {
++ if let Some(actions) = self.actions.get(&view_metadata.type_id) {
+ if actions.contains_key(&action.as_any().type_id()) {
+ handler_depth = Some(i);
+ }
+ }
- contexts.push(view.keymap_context(self));
++ contexts.push(view_metadata.keymap_context.clone());
+ }
+ }
+
+ if self.global_actions.contains_key(&action.as_any().type_id()) {
+ handler_depth = Some(contexts.len())
+ }
+
+ self.keystroke_matcher
+ .bindings_for_action_type(action.as_any().type_id())
+ .find_map(|b| {
+ handler_depth
+ .map(|highest_handler| {
+ if (0..=highest_handler).any(|depth| b.match_context(&contexts[depth..])) {
+ Some(b.keystrokes().into())
+ } else {
+ None
+ }
+ })
+ .flatten()
+ })
+ }
+
+ fn notify_if_view_ancestors_change(&mut self, view_id: usize) {
+ let self_view_id = self.view_id;
+ self.views_to_notify_if_ancestors_change
+ .entry(view_id)
+ .or_default()
+ .push(self_view_id);
+ }
+}
+
+impl<'a, 'b, 'c, V: View> Deref for LayoutContext<'a, 'b, 'c, V> {
+ type Target = ViewContext<'a, 'b, V>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.view_context
+ }
+}
+
+impl<V: View> DerefMut for LayoutContext<'_, '_, '_, V> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.view_context
+ }
+}
+
+impl<V: View> BorrowAppContext for LayoutContext<'_, '_, '_, V> {
+ fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
+ BorrowAppContext::read_with(&*self.view_context, f)
+ }
+
+ fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
+ BorrowAppContext::update(&mut *self.view_context, f)
+ }
+}
+
+impl<V: View> BorrowWindowContext for LayoutContext<'_, '_, '_, V> {
+ fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
+ BorrowWindowContext::read_with(&*self.view_context, window_id, f)
+ }
+
+ fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
+ BorrowWindowContext::update(&mut *self.view_context, window_id, f)
+ }
+}
+
pub struct EventContext<'a, 'b, 'c, V: View> {
view_context: &'c mut ViewContext<'a, 'b, V>,
pub(crate) handled: bool,
@@@ -5851,82 -5726,66 +5856,81 @@@ mod tests
]);
});
- // Here we update the views to ensure that, even if they are on the stack,
- // we can still retrieve keystrokes correctly.
+ let view_1_id = view_1.id();
- view_1.update(cx, |view_1, cx| {
- // Sanity check
- let mut new_parents = Default::default();
- let mut notify_views_if_parents_change = Default::default();
- let mut layout_cx = LayoutContext::new(
- cx,
- &mut new_parents,
- &mut notify_views_if_parents_change,
- false,
- );
- assert_eq!(
- layout_cx
- .keystrokes_for_action(view_1, view_1_id, &Action1)
- .unwrap()
- .as_slice(),
- &[Keystroke::parse("a").unwrap()]
- );
- assert_eq!(
- layout_cx
- .keystrokes_for_action(view_1, view_2.id(), &Action2)
- .unwrap()
- .as_slice(),
- &[Keystroke::parse("b").unwrap()]
- );
+ view_1.update(cx, |_, cx| {
+ view_2.update(cx, |_, cx| {
+ // Sanity check
++ let mut new_parents = Default::default();
++ let mut notify_views_if_parents_change = Default::default();
++ let mut layout_cx = LayoutContext::new(
++ cx,
++ &mut new_parents,
++ &mut notify_views_if_parents_change,
++ false,
++ );
+ assert_eq!(
- cx.keystrokes_for_action(view_1.id(), &Action1)
++ layout_cx
++ .keystrokes_for_action(view_1_id, &Action1)
+ .unwrap()
+ .as_slice(),
+ &[Keystroke::parse("a").unwrap()]
+ );
+ assert_eq!(
- cx.keystrokes_for_action(view_2.id(), &Action2)
++ layout_cx
++ .keystrokes_for_action(view_2.id(), &Action2)
+ .unwrap()
+ .as_slice(),
+ &[Keystroke::parse("b").unwrap()]
+ );
- // The 'a' keystroke propagates up the view tree from view_2
- // to view_1. The action, Action1, is handled by view_1.
- assert_eq!(
- layout_cx
- .keystrokes_for_action(view_1, view_2.id(), &Action1)
- .unwrap()
- .as_slice(),
- &[Keystroke::parse("a").unwrap()]
- );
+ // The 'a' keystroke propagates up the view tree from view_2
+ // to view_1. The action, Action1, is handled by view_1.
+ assert_eq!(
- cx.keystrokes_for_action(view_2.id(), &Action1)
++ layout_cx
++ .keystrokes_for_action(view_2.id(), &Action1)
+ .unwrap()
+ .as_slice(),
+ &[Keystroke::parse("a").unwrap()]
+ );
- // Actions that are handled below the current view don't have bindings
- assert_eq!(
- layout_cx.keystrokes_for_action(view_1, view_1_id, &Action2),
- None
- );
+ // Actions that are handled below the current view don't have bindings
- assert_eq!(cx.keystrokes_for_action(view_1.id(), &Action2), None);
++ assert_eq!(layout_cx.keystrokes_for_action(view_1_id, &Action2), None);
- // Actions that are handled in other branches of the tree should not have a binding
- assert_eq!(
- layout_cx.keystrokes_for_action(view_1, view_2.id(), &GlobalAction),
- None
- );
+ // Actions that are handled in other branches of the tree should not have a binding
- assert_eq!(cx.keystrokes_for_action(view_2.id(), &GlobalAction), None);
-
- // Check that global actions do not have a binding, even if a binding does exist in another view
- assert_eq!(
- &available_actions(view_1.id(), cx),
- &[
- ("test::Action1", vec![Keystroke::parse("a").unwrap()]),
- ("test::GlobalAction", vec![])
- ],
- );
-
- // Check that view 1 actions and bindings are available even when called from view 2
+ assert_eq!(
- &available_actions(view_2.id(), cx),
- &[
- ("test::Action1", vec![Keystroke::parse("a").unwrap()]),
- ("test::Action2", vec![Keystroke::parse("b").unwrap()]),
- ("test::GlobalAction", vec![]),
- ],
++ layout_cx.keystrokes_for_action(view_2.id(), &GlobalAction),
++ None
+ );
+ });
});
+ // Check that global actions do not have a binding, even if a binding does exist in another view
+ assert_eq!(
+ &available_actions(window_id, view_1.id(), cx),
+ &[
+ ("test::Action1", vec![Keystroke::parse("a").unwrap()]),
+ ("test::GlobalAction", vec![])
+ ],
+ );
+
+ // Check that view 1 actions and bindings are available even when called from view 2
+ assert_eq!(
+ &available_actions(window_id, view_2.id(), cx),
+ &[
+ ("test::Action1", vec![Keystroke::parse("a").unwrap()]),
+ ("test::Action2", vec![Keystroke::parse("b").unwrap()]),
+ ("test::GlobalAction", vec![]),
+ ],
+ );
+
// Produces a list of actions and key bindings
fn available_actions(
+ window_id: usize,
view_id: usize,
- cx: &WindowContext,
+ cx: &TestAppContext,
) -> Vec<(&'static str, Vec<Keystroke>)> {
- cx.available_actions(view_id)
+ cx.available_actions(window_id, view_id)
+ .into_iter()
.map(|(action_name, _, bindings)| {
(
action_name,
diff --cc crates/gpui/src/app/window.rs
index 9801119fb46,94c4df23850..bd9bd6d2db0
--- a/crates/gpui/src/app/window.rs
+++ b/crates/gpui/src/app/window.rs
@@@ -1147,8 -1172,19 +1146,17 @@@ impl<'a> WindowContext<'a>
{
let window_id = self.window_id;
let view_id = post_inc(&mut self.next_entity_id);
- // Make sure we can tell child views about their parentu
- self.parents.insert((window_id, view_id), parent_id);
let mut cx = ViewContext::mutable(self, view_id);
let handle = if let Some(view) = build_view(&mut cx) {
+ let mut keymap_context = KeymapContext::default();
+ view.update_keymap_context(&mut keymap_context, cx.app_context());
+ self.views_metadata.insert(
+ (window_id, view_id),
+ ViewMetadata {
+ type_id: TypeId::of::<T>(),
+ keymap_context,
+ },
+ );
self.views.insert((window_id, view_id), Box::new(view));
self.window
.invalidation
diff --cc crates/gpui/src/elements/keystroke_label.rs
index 05b6864d568,b179d781275..c011649b2e9
--- a/crates/gpui/src/elements/keystroke_label.rs
+++ b/crates/gpui/src/elements/keystroke_label.rs
@@@ -39,10 -39,10 +39,10 @@@ impl<V: View> Element<V> for KeystrokeL
&mut self,
constraint: SizeConstraint,
view: &mut V,
- cx: &mut ViewContext<V>,
+ cx: &mut LayoutContext<V>,
) -> (Vector2F, AnyElement<V>) {
let mut element = if let Some(keystrokes) =
- cx.keystrokes_for_action(view, self.view_id, self.action.as_ref())
+ cx.keystrokes_for_action(self.view_id, self.action.as_ref())
{
Flex::row()
.with_children(keystrokes.iter().map(|keystroke| {
diff --cc crates/workspace/src/workspace.rs
index f86d054a5ca,e1007043dcf..6b0eaf04a82
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@@ -2765,15 -2795,19 +2777,11 @@@ impl View for Workspace
.into_any_named("workspace")
}
- fn focus_in(&mut self, view: AnyViewHandle, cx: &mut ViewContext<Self>) {
+ fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
if cx.is_self_focused() {
cx.focus(&self.active_pane);
- } else {
- for pane in self.panes() {
- let view = view.clone();
- if pane.update(cx, |_, cx| view.id() == cx.view_id() || cx.is_child(view)) {
- self.handle_pane_focused(pane.clone(), cx);
- break;
- }
- }
}
}
-
- fn keymap_context(&self, _: &AppContext) -> KeymapContext {
- Self::default_keymap_context()
- }
}
impl ViewId {