2022-03-25 02:24:36 +00:00
|
|
|
use editor::{EditorBlurred, EditorCreated, EditorFocused, EditorMode, EditorReleased};
|
|
|
|
use gpui::MutableAppContext;
|
|
|
|
|
2022-04-15 23:00:44 +00:00
|
|
|
use crate::{state::Mode, Vim};
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
cx.subscribe_global(editor_created).detach();
|
|
|
|
cx.subscribe_global(editor_focused).detach();
|
|
|
|
cx.subscribe_global(editor_blurred).detach();
|
|
|
|
cx.subscribe_global(editor_released).detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn editor_created(EditorCreated(editor): &EditorCreated, cx: &mut MutableAppContext) {
|
2022-04-15 23:00:44 +00:00
|
|
|
cx.update_default_global(|vim: &mut Vim, cx| {
|
|
|
|
vim.editors.insert(editor.id(), editor.downgrade());
|
2022-06-29 18:58:12 +00:00
|
|
|
vim.sync_vim_settings(cx);
|
2022-03-25 02:24:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn editor_focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppContext) {
|
2022-05-18 18:10:24 +00:00
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.active_editor = Some(editor.downgrade());
|
|
|
|
vim.selection_subscription = Some(cx.subscribe(editor, |editor, event, cx| {
|
2022-05-26 19:47:16 +00:00
|
|
|
if editor.read(cx).leader_replica_id().is_none() {
|
2022-12-08 00:39:32 +00:00
|
|
|
if let editor::Event::SelectionsChanged { local: true } = event {
|
|
|
|
let newest_empty = editor.read(cx).selections.newest::<usize>(cx).is_empty();
|
|
|
|
editor_local_selections_changed(newest_empty, cx);
|
2022-05-26 19:47:16 +00:00
|
|
|
}
|
2022-05-18 18:10:24 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-06-30 19:32:53 +00:00
|
|
|
if !vim.enabled {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let editor = editor.read(cx);
|
2022-07-18 20:33:55 +00:00
|
|
|
let editor_mode = editor.mode();
|
|
|
|
let newest_selection_empty = editor.selections.newest::<usize>(cx).is_empty();
|
|
|
|
|
2023-01-18 01:32:10 +00:00
|
|
|
if editor_mode == EditorMode::Full && !newest_selection_empty {
|
2022-07-18 20:33:55 +00:00
|
|
|
vim.switch_mode(Mode::Visual { line: false }, true, cx);
|
2022-05-02 18:48:46 +00:00
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn editor_blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppContext) {
|
2022-05-18 18:10:24 +00:00
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
if let Some(previous_editor) = vim.active_editor.clone() {
|
2022-03-25 02:24:36 +00:00
|
|
|
if previous_editor == editor.clone() {
|
2022-05-18 18:10:24 +00:00
|
|
|
vim.active_editor = None;
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-29 18:58:12 +00:00
|
|
|
vim.sync_vim_settings(cx);
|
2022-03-26 20:30:55 +00:00
|
|
|
})
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn editor_released(EditorReleased(editor): &EditorReleased, cx: &mut MutableAppContext) {
|
2022-04-15 23:00:44 +00:00
|
|
|
cx.update_default_global(|vim: &mut Vim, _| {
|
|
|
|
vim.editors.remove(&editor.id());
|
|
|
|
if let Some(previous_editor) = vim.active_editor.clone() {
|
2022-03-25 02:24:36 +00:00
|
|
|
if previous_editor == editor.clone() {
|
2022-04-15 23:00:44 +00:00
|
|
|
vim.active_editor = None;
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-05-18 18:10:24 +00:00
|
|
|
|
|
|
|
fn editor_local_selections_changed(newest_empty: bool, cx: &mut MutableAppContext) {
|
|
|
|
Vim::update(cx, |vim, cx| {
|
2022-05-26 19:47:16 +00:00
|
|
|
if vim.enabled && vim.state.mode == Mode::Normal && !newest_empty {
|
2022-07-18 20:33:55 +00:00
|
|
|
vim.switch_mode(Mode::Visual { line: false }, false, cx)
|
2022-05-18 18:10:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|