
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>
215 lines
6.2 KiB
Rust
215 lines
6.2 KiB
Rust
use editor::{display_map::ToDisplayPoint, movement, scroll::Autoscroll, Bias, Direction, Editor};
|
|
use gpui::{actions, Context, Window};
|
|
|
|
use crate::{state::Mode, Vim};
|
|
|
|
actions!(vim, [ChangeListOlder, ChangeListNewer]);
|
|
|
|
pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
|
|
Vim::action(editor, cx, |vim, _: &ChangeListOlder, window, cx| {
|
|
vim.move_to_change(Direction::Prev, window, cx);
|
|
});
|
|
Vim::action(editor, cx, |vim, _: &ChangeListNewer, window, cx| {
|
|
vim.move_to_change(Direction::Next, window, cx);
|
|
});
|
|
}
|
|
|
|
impl Vim {
|
|
fn move_to_change(
|
|
&mut self,
|
|
direction: Direction,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
let count = Vim::take_count(cx).unwrap_or(1);
|
|
if self.change_list.is_empty() {
|
|
return;
|
|
}
|
|
|
|
let prev = self.change_list_position.unwrap_or(self.change_list.len());
|
|
let next = if direction == Direction::Prev {
|
|
prev.saturating_sub(count)
|
|
} else {
|
|
(prev + count).min(self.change_list.len() - 1)
|
|
};
|
|
self.change_list_position = Some(next);
|
|
let Some(selections) = self.change_list.get(next).cloned() else {
|
|
return;
|
|
};
|
|
self.update_editor(window, cx, |_, editor, window, cx| {
|
|
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
|
|
let map = s.display_map();
|
|
s.select_display_ranges(selections.into_iter().map(|a| {
|
|
let point = a.to_display_point(&map);
|
|
point..point
|
|
}))
|
|
})
|
|
});
|
|
}
|
|
|
|
pub(crate) fn push_to_change_list(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
let Some((map, selections)) = self.update_editor(window, cx, |_, editor, _, cx| {
|
|
editor.selections.all_adjusted_display(cx)
|
|
}) else {
|
|
return;
|
|
};
|
|
|
|
let pop_state = self
|
|
.change_list
|
|
.last()
|
|
.map(|previous| {
|
|
previous.len() == selections.len()
|
|
&& previous.iter().enumerate().all(|(ix, p)| {
|
|
p.to_display_point(&map).row() == selections[ix].head().row()
|
|
})
|
|
})
|
|
.unwrap_or(false);
|
|
|
|
let new_positions = selections
|
|
.into_iter()
|
|
.map(|s| {
|
|
let point = if self.mode == Mode::Insert {
|
|
movement::saturating_left(&map, s.head())
|
|
} else {
|
|
s.head()
|
|
};
|
|
map.display_point_to_anchor(point, Bias::Left)
|
|
})
|
|
.collect();
|
|
|
|
self.change_list_position.take();
|
|
if pop_state {
|
|
self.change_list.pop();
|
|
}
|
|
self.change_list.push(new_positions);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use indoc::indoc;
|
|
|
|
use crate::{state::Mode, test::NeovimBackedTestContext};
|
|
|
|
#[gpui::test]
|
|
async fn test_change_list_insert(cx: &mut gpui::TestAppContext) {
|
|
let mut cx = NeovimBackedTestContext::new(cx).await;
|
|
|
|
cx.set_shared_state("ˇ").await;
|
|
|
|
cx.simulate_shared_keystrokes("i 1 1 escape shift-o 2 2 escape shift-g o 3 3 escape")
|
|
.await;
|
|
|
|
cx.shared_state().await.assert_eq(indoc! {
|
|
"22
|
|
11
|
|
3ˇ3"
|
|
});
|
|
|
|
cx.simulate_shared_keystrokes("g ;").await;
|
|
// NOTE: this matches nvim when I type it into it
|
|
// but in tests, nvim always reports the column as 0...
|
|
cx.assert_state(
|
|
indoc! {
|
|
"22
|
|
11
|
|
3ˇ3"
|
|
},
|
|
Mode::Normal,
|
|
);
|
|
cx.simulate_shared_keystrokes("g ;").await;
|
|
cx.assert_state(
|
|
indoc! {
|
|
"2ˇ2
|
|
11
|
|
33"
|
|
},
|
|
Mode::Normal,
|
|
);
|
|
cx.simulate_shared_keystrokes("g ;").await;
|
|
cx.assert_state(
|
|
indoc! {
|
|
"22
|
|
1ˇ1
|
|
33"
|
|
},
|
|
Mode::Normal,
|
|
);
|
|
cx.simulate_shared_keystrokes("g ,").await;
|
|
cx.assert_state(
|
|
indoc! {
|
|
"2ˇ2
|
|
11
|
|
33"
|
|
},
|
|
Mode::Normal,
|
|
);
|
|
cx.simulate_shared_keystrokes("shift-g i 4 4 escape").await;
|
|
cx.simulate_shared_keystrokes("g ;").await;
|
|
cx.assert_state(
|
|
indoc! {
|
|
"22
|
|
11
|
|
34ˇ43"
|
|
},
|
|
Mode::Normal,
|
|
);
|
|
cx.simulate_shared_keystrokes("g ;").await;
|
|
cx.assert_state(
|
|
indoc! {
|
|
"2ˇ2
|
|
11
|
|
3443"
|
|
},
|
|
Mode::Normal,
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_change_list_delete(cx: &mut gpui::TestAppContext) {
|
|
let mut cx = NeovimBackedTestContext::new(cx).await;
|
|
cx.set_shared_state(indoc! {
|
|
"one two
|
|
three fˇour"})
|
|
.await;
|
|
cx.simulate_shared_keystrokes("x k d i w ^ x").await;
|
|
cx.shared_state().await.assert_eq(indoc! {
|
|
"ˇne•
|
|
three fur"});
|
|
cx.simulate_shared_keystrokes("2 g ;").await;
|
|
cx.shared_state().await.assert_eq(indoc! {
|
|
"ne•
|
|
three fˇur"});
|
|
cx.simulate_shared_keystrokes("g ,").await;
|
|
cx.shared_state().await.assert_eq(indoc! {
|
|
"ˇne•
|
|
three fur"});
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_gi(cx: &mut gpui::TestAppContext) {
|
|
let mut cx = NeovimBackedTestContext::new(cx).await;
|
|
cx.set_shared_state(indoc! {
|
|
"one two
|
|
three fˇr"})
|
|
.await;
|
|
cx.simulate_shared_keystrokes("i o escape k g i").await;
|
|
cx.simulate_shared_keystrokes("u escape").await;
|
|
cx.shared_state().await.assert_eq(indoc! {
|
|
"one two
|
|
three foˇur"});
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn test_dot_mark(cx: &mut gpui::TestAppContext) {
|
|
let mut cx = NeovimBackedTestContext::new(cx).await;
|
|
cx.set_shared_state(indoc! {
|
|
"one two
|
|
three fˇr"})
|
|
.await;
|
|
cx.simulate_shared_keystrokes("i o escape k ` .").await;
|
|
cx.shared_state().await.assert_eq(indoc! {
|
|
"one two
|
|
three fˇor"});
|
|
}
|
|
}
|