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

@ -1,6 +1,6 @@
use collections::HashMap;
use editor::{display_map::ToDisplayPoint, scroll::Autoscroll};
use gpui::ViewContext;
use gpui::{Context, Window};
use language::{Bias, Point, SelectionGoal};
use multi_buffer::MultiBufferRow;
@ -24,15 +24,16 @@ impl Vim {
motion: Motion,
times: Option<usize>,
mode: CaseTarget,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
self.update_editor(window, cx, |_, editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
let mut selection_starts: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = map.display_point_to_anchor(selection.head(), Bias::Left);
selection_starts.insert(selection.id, anchor);
@ -40,13 +41,17 @@ impl Vim {
});
});
match mode {
CaseTarget::Lowercase => editor.convert_to_lower_case(&Default::default(), cx),
CaseTarget::Uppercase => editor.convert_to_upper_case(&Default::default(), cx),
CaseTarget::Lowercase => {
editor.convert_to_lower_case(&Default::default(), window, cx)
}
CaseTarget::Uppercase => {
editor.convert_to_upper_case(&Default::default(), window, cx)
}
CaseTarget::OppositeCase => {
editor.convert_to_opposite_case(&Default::default(), cx)
editor.convert_to_opposite_case(&Default::default(), window, cx)
}
}
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = selection_starts.remove(&selection.id).unwrap();
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
@ -62,13 +67,14 @@ impl Vim {
object: Object,
around: bool,
mode: CaseTarget,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |_, editor, window, cx| {
editor.transact(window, cx, |editor, window, cx| {
let mut original_positions: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
object.expand_selection(map, selection, around);
original_positions.insert(
@ -78,13 +84,17 @@ impl Vim {
});
});
match mode {
CaseTarget::Lowercase => editor.convert_to_lower_case(&Default::default(), cx),
CaseTarget::Uppercase => editor.convert_to_upper_case(&Default::default(), cx),
CaseTarget::Lowercase => {
editor.convert_to_lower_case(&Default::default(), window, cx)
}
CaseTarget::Uppercase => {
editor.convert_to_upper_case(&Default::default(), window, cx)
}
CaseTarget::OppositeCase => {
editor.convert_to_opposite_case(&Default::default(), cx)
editor.convert_to_opposite_case(&Default::default(), window, cx)
}
}
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = original_positions.remove(&selection.id).unwrap();
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
@ -94,8 +104,8 @@ impl Vim {
});
}
pub fn change_case(&mut self, _: &ChangeCase, cx: &mut ViewContext<Self>) {
self.manipulate_text(cx, |c| {
pub fn change_case(&mut self, _: &ChangeCase, window: &mut Window, cx: &mut Context<Self>) {
self.manipulate_text(window, cx, |c| {
if c.is_lowercase() {
c.to_uppercase().collect::<Vec<char>>()
} else {
@ -104,23 +114,33 @@ impl Vim {
})
}
pub fn convert_to_upper_case(&mut self, _: &ConvertToUpperCase, cx: &mut ViewContext<Self>) {
self.manipulate_text(cx, |c| c.to_uppercase().collect::<Vec<char>>())
pub fn convert_to_upper_case(
&mut self,
_: &ConvertToUpperCase,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.manipulate_text(window, cx, |c| c.to_uppercase().collect::<Vec<char>>())
}
pub fn convert_to_lower_case(&mut self, _: &ConvertToLowerCase, cx: &mut ViewContext<Self>) {
self.manipulate_text(cx, |c| c.to_lowercase().collect::<Vec<char>>())
pub fn convert_to_lower_case(
&mut self,
_: &ConvertToLowerCase,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.manipulate_text(window, cx, |c| c.to_lowercase().collect::<Vec<char>>())
}
fn manipulate_text<F>(&mut self, cx: &mut ViewContext<Self>, transform: F)
fn manipulate_text<F>(&mut self, window: &mut Window, cx: &mut Context<Self>, transform: F)
where
F: Fn(char) -> Vec<char> + Copy,
{
self.record_current_action(cx);
self.store_visual_marks(cx);
self.store_visual_marks(window, cx);
let count = Vim::take_count(cx).unwrap_or(1) as u32;
self.update_editor(cx, |vim, editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
let mut ranges = Vec::new();
let mut cursor_positions = Vec::new();
let snapshot = editor.buffer().read(cx).snapshot(cx);
@ -162,7 +182,7 @@ impl Vim {
}
}
}
editor.transact(cx, |editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
for range in ranges.into_iter().rev() {
let snapshot = editor.buffer().read(cx).snapshot(cx);
let text = snapshot
@ -172,12 +192,12 @@ impl Vim {
.collect::<String>();
editor.edit([(range, text)], cx)
}
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.select_ranges(cursor_positions)
})
});
});
self.switch_mode(Mode::Normal, true, cx)
self.switch_mode(Mode::Normal, true, window, cx)
}
}

View file

@ -10,15 +10,16 @@ use editor::{
scroll::Autoscroll,
Bias, DisplayPoint,
};
use gpui::{Context, Window};
use language::Selection;
use ui::ViewContext;
impl Vim {
pub fn change_motion(
&mut self,
motion: Motion,
times: Option<usize>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
// Some motions ignore failure when switching to normal mode
let mut motion_succeeded = matches!(
@ -29,12 +30,12 @@ impl Vim {
| Motion::Backspace
| Motion::StartOfLine { .. }
);
self.update_editor(cx, |vim, editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
// We are swapping to insert mode anyway. Just set the line end clipping behavior now
editor.set_clip_at_line_ends(false, cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
motion_succeeded |= match motion {
Motion::NextWordStart { ignore_punctuation }
@ -76,41 +77,47 @@ impl Vim {
});
});
vim.copy_selections_content(editor, motion.linewise(), cx);
editor.insert("", cx);
editor.refresh_inline_completion(true, false, cx);
editor.insert("", window, cx);
editor.refresh_inline_completion(true, false, window, cx);
});
});
if motion_succeeded {
self.switch_mode(Mode::Insert, false, cx)
self.switch_mode(Mode::Insert, false, window, cx)
} else {
self.switch_mode(Mode::Normal, false, cx)
self.switch_mode(Mode::Normal, false, window, cx)
}
}
pub fn change_object(&mut self, object: Object, around: bool, cx: &mut ViewContext<Self>) {
pub fn change_object(
&mut self,
object: Object,
around: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
let mut objects_found = false;
self.update_editor(cx, |vim, editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
// We are swapping to insert mode anyway. Just set the line end clipping behavior now
editor.set_clip_at_line_ends(false, cx);
editor.transact(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.transact(window, cx, |editor, window, cx| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
objects_found |= object.expand_selection(map, selection, around);
});
});
if objects_found {
vim.copy_selections_content(editor, false, cx);
editor.insert("", cx);
editor.refresh_inline_completion(true, false, cx);
editor.insert("", window, cx);
editor.refresh_inline_completion(true, false, window, cx);
}
});
});
if objects_found {
self.switch_mode(Mode::Insert, false, cx);
self.switch_mode(Mode::Insert, false, window, cx);
} else {
self.switch_mode(Mode::Normal, false, cx);
self.switch_mode(Mode::Normal, false, window, cx);
}
}
}

View file

@ -5,24 +5,25 @@ use editor::{
scroll::Autoscroll,
Bias, DisplayPoint,
};
use gpui::{Context, Window};
use language::{Point, Selection};
use multi_buffer::MultiBufferRow;
use ui::ViewContext;
impl Vim {
pub fn delete_motion(
&mut self,
motion: Motion,
times: Option<usize>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |vim, editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut original_columns: HashMap<_, _> = Default::default();
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
let original_head = selection.head();
original_columns.insert(selection.id, original_head.column());
@ -60,11 +61,11 @@ impl Vim {
});
});
vim.copy_selections_content(editor, motion.linewise(), cx);
editor.insert("", cx);
editor.insert("", window, cx);
// Fixup cursor position after the deletion
editor.set_clip_at_line_ends(true, cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
let mut cursor = selection.head();
if motion.linewise() {
@ -76,20 +77,26 @@ impl Vim {
selection.collapse_to(cursor, selection.goal)
});
});
editor.refresh_inline_completion(true, false, cx);
editor.refresh_inline_completion(true, false, window, cx);
});
});
}
pub fn delete_object(&mut self, object: Object, around: bool, cx: &mut ViewContext<Self>) {
pub fn delete_object(
&mut self,
object: Object,
around: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |vim, editor, cx| {
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
// Emulates behavior in vim where if we expanded backwards to include a newline
// the cursor gets set back to the start of the line
let mut should_move_to_start: HashSet<_> = Default::default();
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
object.expand_selection(map, selection, around);
let offset_range = selection.map(|p| p.to_offset(map, Bias::Left)).range();
@ -142,11 +149,11 @@ impl Vim {
});
});
vim.copy_selections_content(editor, false, cx);
editor.insert("", cx);
editor.insert("", window, cx);
// Fixup cursor position after the deletion
editor.set_clip_at_line_ends(true, cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
let mut cursor = selection.head();
if should_move_to_start.contains(&selection.id) {
@ -156,7 +163,7 @@ impl Vim {
selection.collapse_to(cursor, selection.goal)
});
});
editor.refresh_inline_completion(true, false, cx);
editor.refresh_inline_completion(true, false, window, cx);
});
});
}

View file

@ -1,5 +1,5 @@
use editor::{scroll::Autoscroll, Editor, MultiBufferSnapshot, ToOffset, ToPoint};
use gpui::{impl_actions, ViewContext};
use gpui::{impl_actions, Context, Window};
use language::{Bias, Point};
use schemars::JsonSchema;
use serde::Deserialize;
@ -23,25 +23,31 @@ struct Decrement {
impl_actions!(vim, [Increment, Decrement]);
pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
Vim::action(editor, cx, |vim, action: &Increment, cx| {
pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, |vim, action: &Increment, window, cx| {
vim.record_current_action(cx);
let count = Vim::take_count(cx).unwrap_or(1);
let step = if action.step { 1 } else { 0 };
vim.increment(count as i64, step, cx)
vim.increment(count as i64, step, window, cx)
});
Vim::action(editor, cx, |vim, action: &Decrement, cx| {
Vim::action(editor, cx, |vim, action: &Decrement, window, cx| {
vim.record_current_action(cx);
let count = Vim::take_count(cx).unwrap_or(1);
let step = if action.step { -1 } else { 0 };
vim.increment(-(count as i64), step, cx)
vim.increment(-(count as i64), step, window, cx)
});
}
impl Vim {
fn increment(&mut self, mut delta: i64, step: i32, cx: &mut ViewContext<Self>) {
self.store_visual_marks(cx);
self.update_editor(cx, |vim, editor, cx| {
fn increment(
&mut self,
mut delta: i64,
step: i32,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.store_visual_marks(window, cx);
self.update_editor(window, cx, |vim, editor, window, cx| {
let mut edits = Vec::new();
let mut new_anchors = Vec::new();
@ -76,11 +82,11 @@ impl Vim {
}
}
}
editor.transact(cx, |editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.edit(edits, cx);
let snapshot = editor.buffer().read(cx).snapshot(cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
let mut new_ranges = Vec::new();
for (visual, anchor) in new_anchors.iter() {
let mut point = anchor.to_point(&snapshot);
@ -94,7 +100,7 @@ impl Vim {
})
});
});
self.switch_mode(Mode::Normal, true, cx)
self.switch_mode(Mode::Normal, true, window, cx)
}
}

View file

@ -6,7 +6,7 @@ use editor::{
scroll::Autoscroll,
Anchor, Bias, DisplayPoint,
};
use gpui::ViewContext;
use gpui::{Context, Window};
use language::SelectionGoal;
use crate::{
@ -16,8 +16,14 @@ use crate::{
};
impl Vim {
pub fn create_mark(&mut self, text: Arc<str>, tail: bool, cx: &mut ViewContext<Self>) {
let Some(anchors) = self.update_editor(cx, |_, editor, _| {
pub fn create_mark(
&mut self,
text: Arc<str>,
tail: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(anchors) = self.update_editor(window, cx, |_, editor, _, _| {
editor
.selections
.disjoint_anchors()
@ -28,23 +34,28 @@ impl Vim {
return;
};
self.marks.insert(text.to_string(), anchors);
self.clear_operator(cx);
self.clear_operator(window, cx);
}
// When handling an action, you must create visual marks if you will switch to normal
// mode without the default selection behavior.
pub(crate) fn store_visual_marks(&mut self, cx: &mut ViewContext<Self>) {
pub(crate) fn store_visual_marks(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.mode.is_visual() {
self.create_visual_marks(self.mode, cx);
self.create_visual_marks(self.mode, window, cx);
}
}
pub(crate) fn create_visual_marks(&mut self, mode: Mode, cx: &mut ViewContext<Self>) {
pub(crate) fn create_visual_marks(
&mut self,
mode: Mode,
window: &mut Window,
cx: &mut Context<Self>,
) {
let mut starts = vec![];
let mut ends = vec![];
let mut reversed = vec![];
self.update_editor(cx, |_, editor, cx| {
self.update_editor(window, cx, |_, editor, _, cx| {
let (map, selections) = editor.selections.all_display(cx);
for selection in selections {
let end = movement::saturating_left(&map, selection.end);
@ -65,11 +76,17 @@ impl Vim {
self.stored_visual_mode.replace((mode, reversed));
}
pub fn jump(&mut self, text: Arc<str>, line: bool, cx: &mut ViewContext<Self>) {
self.pop_operator(cx);
pub fn jump(
&mut self,
text: Arc<str>,
line: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.pop_operator(window, cx);
let anchors = match &*text {
"{" | "}" => self.update_editor(cx, |_, editor, cx| {
"{" | "}" => self.update_editor(window, cx, |_, editor, _, cx| {
let (map, selections) = editor.selections.all_display(cx);
selections
.into_iter()
@ -98,12 +115,13 @@ impl Vim {
anchor: *anchor,
line,
},
window,
cx,
)
}
} else {
self.update_editor(cx, |_, editor, cx| {
let map = editor.snapshot(cx);
self.update_editor(window, cx, |_, editor, window, cx| {
let map = editor.snapshot(window, cx);
let mut ranges: Vec<Range<Anchor>> = Vec::new();
for mut anchor in anchors {
if line {
@ -118,7 +136,7 @@ impl Vim {
ranges.push(anchor..anchor);
}
}
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.select_anchor_ranges(ranges)
})
});

View file

@ -1,5 +1,5 @@
use editor::{display_map::ToDisplayPoint, movement, scroll::Autoscroll, DisplayPoint, RowExt};
use gpui::{impl_actions, ViewContext};
use gpui::{impl_actions, Context, Window};
use language::{Bias, SelectionGoal};
use schemars::JsonSchema;
use serde::Deserialize;
@ -22,14 +22,14 @@ pub struct Paste {
impl_actions!(vim, [Paste]);
impl Vim {
pub fn paste(&mut self, action: &Paste, cx: &mut ViewContext<Self>) {
pub fn paste(&mut self, action: &Paste, window: &mut Window, cx: &mut Context<Self>) {
self.record_current_action(cx);
self.store_visual_marks(cx);
self.store_visual_marks(window, cx);
let count = Vim::take_count(cx).unwrap_or(1);
self.update_editor(cx, |vim, editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let selected_register = vim.selected_register.take();
@ -159,7 +159,7 @@ impl Vim {
// and put the cursor on the first non-blank character of the first inserted line (or at the end if the first line is blank).
// otherwise vim will insert the next text at (or before) the current cursor position,
// the cursor will go to the last (or first, if is_multiline) inserted character.
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.replace_cursors_with(|map| {
let mut cursors = Vec::new();
for (anchor, line_mode, is_multiline) in &new_selections {
@ -190,7 +190,7 @@ impl Vim {
})
});
});
self.switch_mode(Mode::Normal, true, cx);
self.switch_mode(Mode::Normal, true, window, cx);
}
}

View file

@ -8,8 +8,7 @@ use crate::{
Vim,
};
use editor::Editor;
use gpui::{actions, Action, ViewContext, WindowContext};
use util::ResultExt;
use gpui::{actions, Action, App, Context, Window};
use workspace::Workspace;
actions!(vim, [Repeat, EndRepeat, ToggleRecord, ReplayLastRecording]);
@ -45,28 +44,30 @@ fn repeatable_insert(action: &ReplayableAction) -> Option<Box<dyn Action>> {
}
}
pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
Vim::action(editor, cx, |vim, _: &EndRepeat, cx| {
pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, |vim, _: &EndRepeat, window, cx| {
Vim::globals(cx).dot_replaying = false;
vim.switch_mode(Mode::Normal, false, cx)
vim.switch_mode(Mode::Normal, false, window, cx)
});
Vim::action(editor, cx, |vim, _: &Repeat, cx| vim.repeat(false, cx));
Vim::action(editor, cx, |vim, _: &Repeat, window, cx| {
vim.repeat(false, window, cx)
});
Vim::action(editor, cx, |vim, _: &ToggleRecord, cx| {
Vim::action(editor, cx, |vim, _: &ToggleRecord, window, cx| {
let globals = Vim::globals(cx);
if let Some(char) = globals.recording_register.take() {
globals.last_recorded_register = Some(char)
} else {
vim.push_operator(Operator::RecordRegister, cx);
vim.push_operator(Operator::RecordRegister, window, cx);
}
});
Vim::action(editor, cx, |vim, _: &ReplayLastRecording, cx| {
Vim::action(editor, cx, |vim, _: &ReplayLastRecording, window, cx| {
let Some(register) = Vim::globals(cx).last_recorded_register else {
return;
};
vim.replay_register(register, cx)
vim.replay_register(register, window, cx)
});
}
@ -88,7 +89,7 @@ impl Replayer {
})))
}
pub fn replay(&mut self, actions: Vec<ReplayableAction>, cx: &mut WindowContext) {
pub fn replay(&mut self, actions: Vec<ReplayableAction>, window: &mut Window, cx: &mut App) {
let mut lock = self.0.borrow_mut();
let range = lock.ix..lock.ix;
lock.actions.splice(range, actions);
@ -97,14 +98,14 @@ impl Replayer {
}
lock.running = true;
let this = self.clone();
cx.defer(move |cx| this.next(cx))
window.defer(cx, move |window, cx| this.next(window, cx))
}
pub fn stop(self) {
self.0.borrow_mut().actions.clear()
}
pub fn next(self, cx: &mut WindowContext) {
pub fn next(self, window: &mut Window, cx: &mut App) {
let mut lock = self.0.borrow_mut();
let action = if lock.ix < 10000 {
lock.actions.get(lock.ix).cloned()
@ -121,7 +122,7 @@ impl Replayer {
match action {
ReplayableAction::Action(action) => {
if should_replay(&*action) {
cx.dispatch_action(action.boxed_clone());
window.dispatch_action(action.boxed_clone(), cx);
cx.defer(move |cx| Vim::globals(cx).observe_action(action.boxed_clone()));
}
}
@ -129,41 +130,47 @@ impl Replayer {
text,
utf16_range_to_replace,
} => {
cx.window_handle()
.update(cx, |handle, cx| {
let Ok(workspace) = handle.downcast::<Workspace>() else {
return;
};
let Some(editor) = workspace
.read(cx)
.active_item(cx)
.and_then(|item| item.act_as::<Editor>(cx))
else {
return;
};
editor.update(cx, |editor, cx| {
editor.replay_insert_event(&text, utf16_range_to_replace.clone(), cx)
})
})
.log_err();
let Some(Some(workspace)) = window.root_model::<Workspace>() else {
return;
};
let Some(editor) = workspace
.read(cx)
.active_item(cx)
.and_then(|item| item.act_as::<Editor>(cx))
else {
return;
};
editor.update(cx, |editor, cx| {
editor.replay_insert_event(&text, utf16_range_to_replace.clone(), window, cx)
})
}
}
cx.defer(move |cx| self.next(cx));
window.defer(cx, move |window, cx| self.next(window, cx));
}
}
impl Vim {
pub(crate) fn record_register(&mut self, register: char, cx: &mut ViewContext<Self>) {
pub(crate) fn record_register(
&mut self,
register: char,
window: &mut Window,
cx: &mut Context<Self>,
) {
let globals = Vim::globals(cx);
globals.recording_register = Some(register);
globals.recordings.remove(&register);
globals.ignore_current_insertion = true;
self.clear_operator(cx)
self.clear_operator(window, cx)
}
pub(crate) fn replay_register(&mut self, mut register: char, cx: &mut ViewContext<Self>) {
pub(crate) fn replay_register(
&mut self,
mut register: char,
window: &mut Window,
cx: &mut Context<Self>,
) {
let mut count = Vim::take_count(cx).unwrap_or(1);
self.clear_operator(cx);
self.clear_operator(window, cx);
let globals = Vim::globals(cx);
if register == '@' {
@ -184,11 +191,17 @@ impl Vim {
globals.last_replayed_register = Some(register);
let mut replayer = globals.replayer.get_or_insert_with(Replayer::new).clone();
replayer.replay(repeated_actions, cx);
replayer.replay(repeated_actions, window, cx);
}
pub(crate) fn repeat(&mut self, from_insert_mode: bool, cx: &mut ViewContext<Self>) {
pub(crate) fn repeat(
&mut self,
from_insert_mode: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
let count = Vim::take_count(cx);
let Some((mut actions, selection, mode)) = Vim::update_globals(cx, |globals, _| {
let actions = globals.recorded_actions.clone();
if actions.is_empty() {
@ -231,13 +244,13 @@ impl Vim {
return;
};
if let Some(mode) = mode {
self.switch_mode(mode, false, cx)
self.switch_mode(mode, false, window, cx)
}
match selection {
RecordedSelection::SingleLine { cols } => {
if cols > 1 {
self.visual_motion(Motion::Right, Some(cols as usize - 1), cx)
self.visual_motion(Motion::Right, Some(cols as usize - 1), window, cx)
}
}
RecordedSelection::Visual { rows, cols } => {
@ -246,6 +259,7 @@ impl Vim {
display_lines: false,
},
Some(rows as usize),
window,
cx,
);
self.visual_motion(
@ -253,10 +267,11 @@ impl Vim {
display_lines: false,
},
None,
window,
cx,
);
if cols > 1 {
self.visual_motion(Motion::Right, Some(cols as usize - 1), cx)
self.visual_motion(Motion::Right, Some(cols as usize - 1), window, cx)
}
}
RecordedSelection::VisualBlock { rows, cols } => {
@ -265,10 +280,11 @@ impl Vim {
display_lines: false,
},
Some(rows as usize),
window,
cx,
);
if cols > 1 {
self.visual_motion(Motion::Right, Some(cols as usize - 1), cx);
self.visual_motion(Motion::Right, Some(cols as usize - 1), window, cx);
}
}
RecordedSelection::VisualLine { rows } => {
@ -277,6 +293,7 @@ impl Vim {
display_lines: false,
},
Some(rows as usize),
window,
cx,
);
}
@ -321,7 +338,8 @@ impl Vim {
let globals = Vim::globals(cx);
globals.dot_replaying = true;
let mut replayer = globals.replayer.get_or_insert_with(Replayer::new).clone();
replayer.replay(actions, cx);
replayer.replay(actions, window, cx);
}
}
@ -380,9 +398,9 @@ mod test {
cx.simulate_keystrokes("i");
// simulate brazilian input for ä.
cx.update_editor(|editor, cx| {
editor.replace_and_mark_text_in_range(None, "\"", Some(1..1), cx);
editor.replace_text_in_range(None, "ä", cx);
cx.update_editor(|editor, window, cx| {
editor.replace_and_mark_text_in_range(None, "\"", Some(1..1), window, cx);
editor.replace_text_in_range(None, "ä", window, cx);
});
cx.simulate_keystrokes("escape");
cx.assert_state("hˇällo", Mode::Normal);

View file

@ -4,7 +4,7 @@ use editor::{
scroll::ScrollAmount,
DisplayPoint, Editor, EditorSettings,
};
use gpui::{actions, ViewContext};
use gpui::{actions, Context, Window};
use language::Bias;
use settings::Settings;
@ -13,21 +13,21 @@ actions!(
[LineUp, LineDown, ScrollUp, ScrollDown, PageUp, PageDown]
);
pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
Vim::action(editor, cx, |vim, _: &LineDown, cx| {
vim.scroll(false, cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, |vim, _: &LineDown, window, cx| {
vim.scroll(false, window, cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
});
Vim::action(editor, cx, |vim, _: &LineUp, cx| {
vim.scroll(false, cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
Vim::action(editor, cx, |vim, _: &LineUp, window, cx| {
vim.scroll(false, window, cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
});
Vim::action(editor, cx, |vim, _: &PageDown, cx| {
vim.scroll(false, cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
Vim::action(editor, cx, |vim, _: &PageDown, window, cx| {
vim.scroll(false, window, cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
});
Vim::action(editor, cx, |vim, _: &PageUp, cx| {
vim.scroll(false, cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
Vim::action(editor, cx, |vim, _: &PageUp, window, cx| {
vim.scroll(false, window, cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
});
Vim::action(editor, cx, |vim, _: &ScrollDown, cx| {
vim.scroll(true, cx, |c| {
Vim::action(editor, cx, |vim, _: &ScrollDown, window, cx| {
vim.scroll(true, window, cx, |c| {
if let Some(c) = c {
ScrollAmount::Line(c)
} else {
@ -35,8 +35,8 @@ pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
}
})
});
Vim::action(editor, cx, |vim, _: &ScrollUp, cx| {
vim.scroll(true, cx, |c| {
Vim::action(editor, cx, |vim, _: &ScrollUp, window, cx| {
vim.scroll(true, window, cx, |c| {
if let Some(c) = c {
ScrollAmount::Line(-c)
} else {
@ -50,12 +50,13 @@ impl Vim {
fn scroll(
&mut self,
move_cursor: bool,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
by: fn(c: Option<f32>) -> ScrollAmount,
) {
let amount = by(Vim::take_count(cx).map(|c| c as f32));
self.update_editor(cx, |_, editor, cx| {
scroll_editor(editor, move_cursor, &amount, cx)
self.update_editor(window, cx, |_, editor, window, cx| {
scroll_editor(editor, move_cursor, &amount, window, cx)
});
}
}
@ -64,12 +65,13 @@ fn scroll_editor(
editor: &mut Editor,
preserve_cursor_position: bool,
amount: &ScrollAmount,
cx: &mut ViewContext<Editor>,
window: &mut Window,
cx: &mut Context<Editor>,
) {
let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
let old_top_anchor = editor.scroll_manager.anchor().anchor;
if editor.scroll_hover(amount, cx) {
if editor.scroll_hover(amount, window, cx) {
return;
}
@ -85,7 +87,7 @@ fn scroll_editor(
_ => amount.clone(),
};
editor.scroll_screen(&amount, cx);
editor.scroll_screen(&amount, window, cx);
if !should_move_cursor {
return;
}
@ -97,7 +99,7 @@ fn scroll_editor(
let top_anchor = editor.scroll_manager.anchor().anchor;
let vertical_scroll_margin = EditorSettings::get_global(cx).vertical_scroll_margin;
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let mut head = selection.head();
let top = top_anchor.to_display_point(map);
@ -161,7 +163,7 @@ mod test {
test::{NeovimBackedTestContext, VimTestContext},
};
use editor::{EditorSettings, ScrollBeyondLastLine};
use gpui::{point, px, size, Context};
use gpui::{point, px, size, AppContext as _};
use indoc::indoc;
use language::Point;
use settings::SettingsStore;
@ -183,21 +185,21 @@ mod test {
async fn test_scroll(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
let (line_height, visible_line_count) = cx.editor(|editor, cx| {
let (line_height, visible_line_count) = cx.editor(|editor, window, _cx| {
(
editor
.style()
.unwrap()
.text
.line_height_in_pixels(cx.rem_size()),
.line_height_in_pixels(window.rem_size()),
editor.visible_line_count().unwrap(),
)
});
let window = cx.window;
let margin = cx
.update_window(window, |_, cx| {
cx.viewport_size().height - line_height * visible_line_count
.update_window(window, |_, window, _cx| {
window.viewport_size().height - line_height * visible_line_count
})
.unwrap();
cx.simulate_window_resize(
@ -224,30 +226,33 @@ mod test {
Mode::Normal,
);
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
cx.update_editor(|editor, window, cx| {
assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
});
cx.simulate_keystrokes("ctrl-e");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 1.))
cx.update_editor(|editor, window, cx| {
assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 1.))
});
cx.simulate_keystrokes("2 ctrl-e");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.))
cx.update_editor(|editor, window, cx| {
assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 3.))
});
cx.simulate_keystrokes("ctrl-y");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 2.))
cx.update_editor(|editor, window, cx| {
assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 2.))
});
// does not select in normal mode
cx.simulate_keystrokes("g g");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
cx.update_editor(|editor, window, cx| {
assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
});
cx.simulate_keystrokes("ctrl-d");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.0));
cx.update_editor(|editor, window, cx| {
assert_eq!(
editor.snapshot(window, cx).scroll_position(),
point(0., 3.0)
);
assert_eq!(
editor.selections.newest(cx).range(),
Point::new(6, 0)..Point::new(6, 0)
@ -256,12 +261,15 @@ mod test {
// does select in visual mode
cx.simulate_keystrokes("g g");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
cx.update_editor(|editor, window, cx| {
assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
});
cx.simulate_keystrokes("v ctrl-d");
cx.update_editor(|editor, cx| {
assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.0));
cx.update_editor(|editor, window, cx| {
assert_eq!(
editor.snapshot(window, cx).scroll_position(),
point(0., 3.0)
);
assert_eq!(
editor.selections.newest(cx).range(),
Point::new(0, 0)..Point::new(6, 1)

View file

@ -1,5 +1,5 @@
use editor::Editor;
use gpui::{actions, impl_actions, impl_internal_actions, ViewContext};
use gpui::{actions, impl_actions, impl_internal_actions, Context, Window};
use language::Point;
use schemars::JsonSchema;
use search::{buffer_search, BufferSearchBar, SearchOptions};
@ -69,7 +69,7 @@ actions!(vim, [SearchSubmit, MoveToNextMatch, MoveToPrevMatch]);
impl_actions!(vim, [FindCommand, Search, MoveToPrev, MoveToNext]);
impl_internal_actions!(vim, [ReplaceCommand]);
pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, Vim::move_to_next);
Vim::action(editor, cx, Vim::move_to_prev);
Vim::action(editor, cx, Vim::move_to_next_match);
@ -81,36 +81,48 @@ pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
}
impl Vim {
fn move_to_next(&mut self, action: &MoveToNext, cx: &mut ViewContext<Self>) {
fn move_to_next(&mut self, action: &MoveToNext, window: &mut Window, cx: &mut Context<Self>) {
self.move_to_internal(
Direction::Next,
action.case_sensitive,
!action.partial_word,
action.regex,
window,
cx,
)
}
fn move_to_prev(&mut self, action: &MoveToPrev, cx: &mut ViewContext<Self>) {
fn move_to_prev(&mut self, action: &MoveToPrev, window: &mut Window, cx: &mut Context<Self>) {
self.move_to_internal(
Direction::Prev,
action.case_sensitive,
!action.partial_word,
action.regex,
window,
cx,
)
}
fn move_to_next_match(&mut self, _: &MoveToNextMatch, cx: &mut ViewContext<Self>) {
self.move_to_match_internal(self.search.direction, cx)
fn move_to_next_match(
&mut self,
_: &MoveToNextMatch,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.move_to_match_internal(self.search.direction, window, cx)
}
fn move_to_prev_match(&mut self, _: &MoveToPrevMatch, cx: &mut ViewContext<Self>) {
self.move_to_match_internal(self.search.direction.opposite(), cx)
fn move_to_prev_match(
&mut self,
_: &MoveToPrevMatch,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.move_to_match_internal(self.search.direction.opposite(), window, cx)
}
fn search(&mut self, action: &Search, cx: &mut ViewContext<Self>) {
let Some(pane) = self.pane(cx) else {
fn search(&mut self, action: &Search, window: &mut Window, cx: &mut Context<Self>) {
let Some(pane) = self.pane(window, cx) else {
return;
};
let direction = if action.backwards {
@ -119,17 +131,17 @@ impl Vim {
Direction::Next
};
let count = Vim::take_count(cx).unwrap_or(1);
let prior_selections = self.editor_selections(cx);
let prior_selections = self.editor_selections(window, cx);
pane.update(cx, |pane, cx| {
if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
search_bar.update(cx, |search_bar, cx| {
if !search_bar.show(cx) {
if !search_bar.show(window, cx) {
return;
}
let query = search_bar.query(cx);
search_bar.select_query(cx);
cx.focus_self();
search_bar.select_query(window, cx);
cx.focus_self(window);
search_bar.set_replacement(None, cx);
let mut options = SearchOptions::NONE;
@ -157,14 +169,16 @@ impl Vim {
}
// hook into the existing to clear out any vim search state on cmd+f or edit -> find.
fn search_deploy(&mut self, _: &buffer_search::Deploy, cx: &mut ViewContext<Self>) {
fn search_deploy(&mut self, _: &buffer_search::Deploy, _: &mut Window, cx: &mut Context<Self>) {
self.search = Default::default();
cx.propagate();
}
pub fn search_submit(&mut self, cx: &mut ViewContext<Self>) {
self.store_visual_marks(cx);
let Some(pane) = self.pane(cx) else { return };
pub fn search_submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.store_visual_marks(window, cx);
let Some(pane) = self.pane(window, cx) else {
return;
};
let result = pane.update(cx, |pane, cx| {
let search_bar = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>()?;
search_bar.update(cx, |search_bar, cx| {
@ -178,8 +192,8 @@ impl Vim {
count = count.saturating_sub(1)
}
self.search.count = 1;
search_bar.select_match(direction, count, cx);
search_bar.focus_editor(&Default::default(), cx);
search_bar.select_match(direction, count, window, cx);
search_bar.focus_editor(&Default::default(), window, cx);
let prior_selections: Vec<_> = self.search.prior_selections.drain(..).collect();
let prior_mode = self.search.prior_mode;
@ -195,12 +209,13 @@ impl Vim {
return;
};
let new_selections = self.editor_selections(cx);
let new_selections = self.editor_selections(window, cx);
// If the active editor has changed during a search, don't panic.
if prior_selections.iter().any(|s| {
self.update_editor(cx, |_, editor, cx| {
!s.start.is_valid(&editor.snapshot(cx).buffer_snapshot)
self.update_editor(window, cx, |_, editor, window, cx| {
!s.start
.is_valid(&editor.snapshot(window, cx).buffer_snapshot)
})
.unwrap_or(true)
}) {
@ -208,34 +223,42 @@ impl Vim {
}
if prior_mode != self.mode {
self.switch_mode(prior_mode, true, cx);
self.switch_mode(prior_mode, true, window, cx);
}
if let Some(operator) = prior_operator {
self.push_operator(operator, cx);
self.push_operator(operator, window, cx);
};
self.search_motion(
Motion::ZedSearchResult {
prior_selections,
new_selections,
},
window,
cx,
);
}
pub fn move_to_match_internal(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
let Some(pane) = self.pane(cx) else { return };
pub fn move_to_match_internal(
&mut self,
direction: Direction,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(pane) = self.pane(window, cx) else {
return;
};
let count = Vim::take_count(cx).unwrap_or(1);
let prior_selections = self.editor_selections(cx);
let prior_selections = self.editor_selections(window, cx);
let success = pane.update(cx, |pane, cx| {
let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() else {
return false;
};
search_bar.update(cx, |search_bar, cx| {
if !search_bar.has_active_match() || !search_bar.show(cx) {
if !search_bar.has_active_match() || !search_bar.show(window, cx) {
return false;
}
search_bar.select_match(direction, count, cx);
search_bar.select_match(direction, count, window, cx);
true
})
});
@ -243,12 +266,13 @@ impl Vim {
return;
}
let new_selections = self.editor_selections(cx);
let new_selections = self.editor_selections(window, cx);
self.search_motion(
Motion::ZedSearchResult {
prior_selections,
new_selections,
},
window,
cx,
);
}
@ -259,12 +283,15 @@ impl Vim {
case_sensitive: bool,
whole_word: bool,
regex: bool,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(pane) = self.pane(cx) else { return };
let Some(pane) = self.pane(window, cx) else {
return;
};
let count = Vim::take_count(cx).unwrap_or(1);
let prior_selections = self.editor_selections(cx);
let vim = cx.view().clone();
let prior_selections = self.editor_selections(window, cx);
let vim = cx.model().clone();
let searched = pane.update(cx, |pane, cx| {
self.search.direction = direction;
@ -282,32 +309,33 @@ impl Vim {
if whole_word {
options |= SearchOptions::WHOLE_WORD;
}
if !search_bar.show(cx) {
if !search_bar.show(window, cx) {
return None;
}
let Some(query) = search_bar.query_suggestion(cx) else {
drop(search_bar.search("", None, cx));
let Some(query) = search_bar.query_suggestion(window, cx) else {
drop(search_bar.search("", None, window, cx));
return None;
};
let query = regex::escape(&query);
Some(search_bar.search(&query, Some(options), cx))
Some(search_bar.search(&query, Some(options), window, cx))
});
let Some(search) = search else { return false };
let search_bar = search_bar.downgrade();
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
search.await?;
search_bar.update(&mut cx, |search_bar, cx| {
search_bar.select_match(direction, count, cx);
search_bar.update_in(&mut cx, |search_bar, window, cx| {
search_bar.select_match(direction, count, window, cx);
vim.update(cx, |vim, cx| {
let new_selections = vim.editor_selections(cx);
let new_selections = vim.editor_selections(window, cx);
vim.search_motion(
Motion::ZedSearchResult {
prior_selections,
new_selections,
},
window,
cx,
)
});
@ -318,20 +346,22 @@ impl Vim {
true
});
if !searched {
self.clear_operator(cx)
self.clear_operator(window, cx)
}
if self.mode.is_visual() {
self.switch_mode(Mode::Normal, false, cx)
self.switch_mode(Mode::Normal, false, window, cx)
}
}
fn find_command(&mut self, action: &FindCommand, cx: &mut ViewContext<Self>) {
let Some(pane) = self.pane(cx) else { return };
fn find_command(&mut self, action: &FindCommand, window: &mut Window, cx: &mut Context<Self>) {
let Some(pane) = self.pane(window, cx) else {
return;
};
pane.update(cx, |pane, cx| {
if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
let search = search_bar.update(cx, |search_bar, cx| {
if !search_bar.show(cx) {
if !search_bar.show(window, cx) {
return None;
}
let mut query = action.query.clone();
@ -347,7 +377,7 @@ impl Vim {
);
}
Some(search_bar.search(&query, Some(options), cx))
Some(search_bar.search(&query, Some(options), window, cx))
});
let Some(search) = search else { return };
let search_bar = search_bar.downgrade();
@ -356,10 +386,10 @@ impl Vim {
} else {
Direction::Next
};
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
search.await?;
search_bar.update(&mut cx, |search_bar, cx| {
search_bar.select_match(direction, 1, cx)
search_bar.update_in(&mut cx, |search_bar, window, cx| {
search_bar.select_match(direction, 1, window, cx)
})?;
anyhow::Ok(())
})
@ -368,16 +398,23 @@ impl Vim {
})
}
fn replace_command(&mut self, action: &ReplaceCommand, cx: &mut ViewContext<Self>) {
fn replace_command(
&mut self,
action: &ReplaceCommand,
window: &mut Window,
cx: &mut Context<Self>,
) {
let replacement = action.replacement.clone();
let Some(((pane, workspace), editor)) =
self.pane(cx).zip(self.workspace(cx)).zip(self.editor())
let Some(((pane, workspace), editor)) = self
.pane(window, cx)
.zip(self.workspace(window))
.zip(self.editor())
else {
return;
};
if let Some(result) = self.update_editor(cx, |vim, editor, cx| {
let range = action.range.buffer_range(vim, editor, cx)?;
let snapshot = &editor.snapshot(cx).buffer_snapshot;
if let Some(result) = self.update_editor(window, cx, |vim, editor, window, cx| {
let range = action.range.buffer_range(vim, editor, window, cx)?;
let snapshot = &editor.snapshot(window, cx).buffer_snapshot;
let end_point = Point::new(range.end.0, snapshot.line_len(range.end));
let range = snapshot.anchor_before(Point::new(range.start.0, 0))
..snapshot.anchor_after(end_point);
@ -388,13 +425,13 @@ impl Vim {
result.notify_err(workspace, cx);
})
}
let vim = cx.view().clone();
let vim = cx.model().clone();
pane.update(cx, |pane, cx| {
let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() else {
return;
};
let search = search_bar.update(cx, |search_bar, cx| {
if !search_bar.show(cx) {
if !search_bar.show(window, cx) {
return None;
}
@ -414,16 +451,16 @@ impl Vim {
);
}
search_bar.set_replacement(Some(&replacement.replacement), cx);
Some(search_bar.search(&search, Some(options), cx))
Some(search_bar.search(&search, Some(options), window, cx))
});
let Some(search) = search else { return };
let search_bar = search_bar.downgrade();
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
search.await?;
search_bar.update(&mut cx, |search_bar, cx| {
search_bar.update_in(&mut cx, |search_bar, window, cx| {
if replacement.should_replace_all {
search_bar.select_last_match(cx);
search_bar.replace_all(&Default::default(), cx);
search_bar.select_last_match(window, cx);
search_bar.replace_all(&Default::default(), window, cx);
cx.spawn(|_, mut cx| async move {
cx.background_executor()
.timer(Duration::from_millis(200))
@ -439,6 +476,7 @@ impl Vim {
display_lines: false,
},
None,
window,
cx,
)
});
@ -621,7 +659,7 @@ mod test {
cx.set_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
cx.simulate_keystrokes("/ c c");
let search_bar = cx.workspace(|workspace, cx| {
let search_bar = cx.workspace(|workspace, _, cx| {
workspace
.active_pane()
.read(cx)
@ -631,14 +669,14 @@ mod test {
.expect("Buffer search bar should be deployed")
});
cx.update_view(search_bar, |bar, cx| {
cx.update_model(search_bar, |bar, _window, cx| {
assert_eq!(bar.query(cx), "cc");
});
cx.run_until_parked();
cx.update_editor(|editor, cx| {
let highlights = editor.all_text_background_highlights(cx);
cx.update_editor(|editor, window, cx| {
let highlights = editor.all_text_background_highlights(window, cx);
assert_eq!(3, highlights.len());
assert_eq!(
DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2),
@ -679,7 +717,9 @@ mod test {
cx.simulate_keystrokes("/ d");
cx.simulate_keystrokes("enter");
cx.assert_state("aa\nbb\nˇdd\ncc\nbb\n", Mode::Normal);
cx.update_editor(|editor, cx| editor.move_to_beginning(&Default::default(), cx));
cx.update_editor(|editor, window, cx| {
editor.move_to_beginning(&Default::default(), window, cx)
});
cx.assert_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
cx.simulate_keystrokes("/ b");
cx.simulate_keystrokes("enter");

View file

@ -1,25 +1,25 @@
use editor::{movement, Editor};
use gpui::{actions, ViewContext};
use gpui::{actions, Context, Window};
use language::Point;
use crate::{motion::Motion, Mode, Vim};
actions!(vim, [Substitute, SubstituteLine]);
pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
Vim::action(editor, cx, |vim, _: &Substitute, cx| {
pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, |vim, _: &Substitute, window, cx| {
vim.start_recording(cx);
let count = Vim::take_count(cx);
vim.substitute(count, vim.mode == Mode::VisualLine, cx);
vim.substitute(count, vim.mode == Mode::VisualLine, window, cx);
});
Vim::action(editor, cx, |vim, _: &SubstituteLine, cx| {
Vim::action(editor, cx, |vim, _: &SubstituteLine, window, cx| {
vim.start_recording(cx);
if matches!(vim.mode, Mode::VisualBlock | Mode::Visual) {
vim.switch_mode(Mode::VisualLine, false, cx)
vim.switch_mode(Mode::VisualLine, false, window, cx)
}
let count = Vim::take_count(cx);
vim.substitute(count, true, cx)
vim.substitute(count, true, window, cx)
});
}
@ -28,14 +28,15 @@ impl Vim {
&mut self,
count: Option<usize>,
line_mode: bool,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.store_visual_marks(cx);
self.update_editor(cx, |vim, editor, cx| {
self.store_visual_marks(window, cx);
self.update_editor(window, cx, |vim, editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
editor.transact(cx, |editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.change_selections(None, cx, |s| {
editor.transact(window, cx, |editor, window, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
if selection.start == selection.end {
Motion::Right.expand_selection(
@ -80,7 +81,7 @@ impl Vim {
editor.edit(edits, cx);
});
});
self.switch_mode(Mode::Insert, true, cx);
self.switch_mode(Mode::Insert, true, window, cx);
}
}

View file

@ -1,30 +1,31 @@
use crate::{motion::Motion, object::Object, Vim};
use collections::HashMap;
use editor::{display_map::ToDisplayPoint, Bias};
use gpui::{Context, Window};
use language::SelectionGoal;
use ui::ViewContext;
impl Vim {
pub fn toggle_comments_motion(
&mut self,
motion: Motion,
times: Option<usize>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |_, editor, window, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
let mut selection_starts: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
selection_starts.insert(selection.id, anchor);
motion.expand_selection(map, selection, times, false, &text_layout_details);
});
});
editor.toggle_comments(&Default::default(), cx);
editor.change_selections(None, cx, |s| {
editor.toggle_comments(&Default::default(), window, cx);
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = selection_starts.remove(&selection.id).unwrap();
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
@ -38,21 +39,22 @@ impl Vim {
&mut self,
object: Object,
around: bool,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |_, editor, window, cx| {
editor.transact(window, cx, |editor, window, cx| {
let mut original_positions: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
original_positions.insert(selection.id, anchor);
object.expand_selection(map, selection, around);
});
});
editor.toggle_comments(&Default::default(), cx);
editor.change_selections(None, cx, |s| {
editor.toggle_comments(&Default::default(), window, cx);
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let anchor = original_positions.remove(&selection.id).unwrap();
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);

View file

@ -8,7 +8,8 @@ use crate::{
};
use collections::HashMap;
use editor::{ClipboardSelection, Editor};
use gpui::ViewContext;
use gpui::Context;
use gpui::Window;
use language::Point;
use multi_buffer::MultiBufferRow;
use settings::Settings;
@ -20,14 +21,15 @@ impl Vim {
&mut self,
motion: Motion,
times: Option<usize>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.update_editor(cx, |vim, editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
self.update_editor(window, cx, |vim, editor, window, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut original_positions: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let original_position = (selection.head(), selection.goal);
original_positions.insert(selection.id, original_position);
@ -35,7 +37,7 @@ impl Vim {
});
});
vim.yank_selections_content(editor, motion.linewise(), cx);
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|_, selection| {
let (head, goal) = original_positions.remove(&selection.id).unwrap();
selection.collapse_to(head, goal);
@ -43,15 +45,21 @@ impl Vim {
});
});
});
self.exit_temporary_normal(cx);
self.exit_temporary_normal(window, cx);
}
pub fn yank_object(&mut self, object: Object, around: bool, cx: &mut ViewContext<Self>) {
self.update_editor(cx, |vim, editor, cx| {
editor.transact(cx, |editor, cx| {
pub fn yank_object(
&mut self,
object: Object,
around: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.update_editor(window, cx, |vim, editor, window, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut original_positions: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let original_position = (selection.head(), selection.goal);
object.expand_selection(map, selection, around);
@ -59,7 +67,7 @@ impl Vim {
});
});
vim.yank_selections_content(editor, false, cx);
editor.change_selections(None, cx, |s| {
editor.change_selections(None, window, cx, |s| {
s.move_with(|_, selection| {
let (head, goal) = original_positions.remove(&selection.id).unwrap();
selection.collapse_to(head, goal);
@ -67,14 +75,14 @@ impl Vim {
});
});
});
self.exit_temporary_normal(cx);
self.exit_temporary_normal(window, cx);
}
pub fn yank_selections_content(
&mut self,
editor: &mut Editor,
linewise: bool,
cx: &mut ViewContext<Editor>,
cx: &mut Context<Editor>,
) {
self.copy_ranges(
editor,
@ -94,7 +102,7 @@ impl Vim {
&mut self,
editor: &mut Editor,
linewise: bool,
cx: &mut ViewContext<Editor>,
cx: &mut Context<Editor>,
) {
self.copy_ranges(
editor,
@ -116,7 +124,7 @@ impl Vim {
linewise: bool,
is_yank: bool,
selections: Vec<Range<Point>>,
cx: &mut ViewContext<Editor>,
cx: &mut Context<Editor>,
) {
let buffer = editor.buffer().read(cx).snapshot(cx);
let mut text = String::new();