vim: Improve lifecycle (#16477)
Closes #13579 A major painpoint in the Vim crate has been life-cycle management. We used to have one global Vim instance that tried to track per-editor state; this led to a number of subtle issues (e.g. #13579, the mode indicator being global, and quick toggling between windows letting vim mode's notion of the active editor get out of sync). This PR changes the internal structure of the code so that there is now one `Vim` instance per `Editor` (stored as an `Addon`); and the global stuff is separated out. This fixes the above problems, and tidies up a bunch of the mess in the codebase. Release Notes: * vim: Fixed accidental visual mode in project search and go to references ([#13579](https://github.com/zed-industries/zed/issues/13579)).
This commit is contained in:
@@ -10,7 +10,7 @@ use language::language_settings::{AllLanguageSettings, SoftWrap};
|
||||
use util::test::marked_text_offsets;
|
||||
|
||||
use super::{neovim_connection::NeovimConnection, VimTestContext};
|
||||
use crate::{state::Mode, Vim};
|
||||
use crate::state::{Mode, VimGlobals};
|
||||
|
||||
pub struct NeovimBackedTestContext {
|
||||
cx: VimTestContext,
|
||||
@@ -263,8 +263,7 @@ impl NeovimBackedTestContext {
|
||||
state: self.shared_state().await,
|
||||
neovim: self.neovim.read_register(register).await,
|
||||
editor: self.update(|cx| {
|
||||
Vim::read(cx)
|
||||
.workspace_state
|
||||
cx.global::<VimGlobals>()
|
||||
.registers
|
||||
.get(®ister)
|
||||
.cloned()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use editor::test::editor_lsp_test_context::EditorLspTestContext;
|
||||
use gpui::{Context, SemanticVersion, View, VisualContext};
|
||||
use gpui::{Context, SemanticVersion, UpdateGlobal, View, VisualContext};
|
||||
use search::{project_search::ProjectSearchBar, BufferSearchBar};
|
||||
|
||||
use crate::{state::Operator, *};
|
||||
@@ -12,7 +12,7 @@ pub struct VimTestContext {
|
||||
|
||||
impl VimTestContext {
|
||||
pub fn init(cx: &mut gpui::TestAppContext) {
|
||||
if cx.has_global::<Vim>() {
|
||||
if cx.has_global::<VimGlobals>() {
|
||||
return;
|
||||
}
|
||||
cx.update(|cx| {
|
||||
@@ -119,23 +119,31 @@ impl VimTestContext {
|
||||
}
|
||||
|
||||
pub fn mode(&mut self) -> Mode {
|
||||
self.cx.read(|cx| cx.global::<Vim>().state().mode)
|
||||
self.update_editor(|editor, cx| editor.addon::<VimAddon>().unwrap().view.read(cx).mode)
|
||||
}
|
||||
|
||||
pub fn active_operator(&mut self) -> Option<Operator> {
|
||||
self.cx
|
||||
.read(|cx| cx.global::<Vim>().state().operator_stack.last().cloned())
|
||||
self.update_editor(|editor, cx| {
|
||||
editor
|
||||
.addon::<VimAddon>()
|
||||
.unwrap()
|
||||
.view
|
||||
.read(cx)
|
||||
.operator_stack
|
||||
.last()
|
||||
.cloned()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_state(&mut self, text: &str, mode: Mode) {
|
||||
let window = self.window;
|
||||
self.cx.set_state(text);
|
||||
self.update_window(window, |_, cx| {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
let vim = self.update_editor(|editor, _cx| editor.addon::<VimAddon>().cloned().unwrap());
|
||||
|
||||
self.update(|cx| {
|
||||
vim.view.update(cx, |vim, cx| {
|
||||
vim.switch_mode(mode, true, cx);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
});
|
||||
self.cx.cx.cx.run_until_parked();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user