vim.rs

   1//! Vim support for Zed.
   2
   3#[cfg(test)]
   4mod test;
   5
   6mod change_list;
   7mod command;
   8mod digraph;
   9mod indent;
  10mod insert;
  11mod mode_indicator;
  12mod motion;
  13mod normal;
  14mod object;
  15mod replace;
  16mod state;
  17mod surrounds;
  18mod visual;
  19
  20use anyhow::Result;
  21use collections::HashMap;
  22use editor::{
  23    movement::{self, FindRange},
  24    Anchor, Bias, Editor, EditorEvent, EditorMode, ToPoint,
  25};
  26use gpui::{
  27    actions, impl_actions, Action, AppContext, Entity, EventEmitter, KeyContext, KeystrokeEvent,
  28    Render, Subscription, View, ViewContext, WeakView,
  29};
  30use insert::NormalBefore;
  31use language::{CursorShape, Point, Selection, SelectionGoal, TransactionId};
  32pub use mode_indicator::ModeIndicator;
  33use motion::Motion;
  34use normal::search::SearchSubmit;
  35use schemars::JsonSchema;
  36use serde::Deserialize;
  37use serde_derive::Serialize;
  38use settings::{update_settings_file, Settings, SettingsSources, SettingsStore};
  39use state::{Mode, Operator, RecordedSelection, SearchState, VimGlobals};
  40use std::{ops::Range, sync::Arc};
  41use surrounds::SurroundsType;
  42use ui::{IntoElement, VisualContext};
  43use workspace::{self, Pane, Workspace};
  44
  45use crate::state::ReplayableAction;
  46
  47/// Whether or not to enable Vim mode.
  48///
  49/// Default: false
  50pub struct VimModeSetting(pub bool);
  51
  52/// An Action to Switch between modes
  53#[derive(Clone, Deserialize, PartialEq)]
  54pub struct SwitchMode(pub Mode);
  55
  56/// PushOperator is used to put vim into a "minor" mode,
  57/// where it's waiting for a specific next set of keystrokes.
  58/// For example 'd' needs a motion to complete.
  59#[derive(Clone, Deserialize, PartialEq)]
  60pub struct PushOperator(pub Operator);
  61
  62/// Number is used to manage vim's count. Pushing a digit
  63/// multiplis the current value by 10 and adds the digit.
  64#[derive(Clone, Deserialize, PartialEq)]
  65struct Number(usize);
  66
  67#[derive(Clone, Deserialize, PartialEq)]
  68struct SelectRegister(String);
  69
  70actions!(
  71    vim,
  72    [
  73        ClearOperators,
  74        Tab,
  75        Enter,
  76        Object,
  77        InnerObject,
  78        FindForward,
  79        FindBackward,
  80        OpenDefaultKeymap
  81    ]
  82);
  83
  84// in the workspace namespace so it's not filtered out when vim is disabled.
  85actions!(workspace, [ToggleVimMode]);
  86
  87impl_actions!(vim, [SwitchMode, PushOperator, Number, SelectRegister]);
  88
  89/// Initializes the `vim` crate.
  90pub fn init(cx: &mut AppContext) {
  91    VimModeSetting::register(cx);
  92    VimSettings::register(cx);
  93    VimGlobals::register(cx);
  94
  95    cx.observe_new_views(|editor: &mut Editor, cx| Vim::register(editor, cx))
  96        .detach();
  97
  98    cx.observe_new_views(|workspace: &mut Workspace, _| {
  99        workspace.register_action(|workspace, _: &ToggleVimMode, cx| {
 100            let fs = workspace.app_state().fs.clone();
 101            let currently_enabled = Vim::enabled(cx);
 102            update_settings_file::<VimModeSetting>(fs, cx, move |setting, _| {
 103                *setting = Some(!currently_enabled)
 104            })
 105        });
 106
 107        workspace.register_action(|_, _: &OpenDefaultKeymap, cx| {
 108            cx.emit(workspace::Event::OpenBundledFile {
 109                text: settings::vim_keymap(),
 110                title: "Default Vim Bindings",
 111                language: "JSON",
 112            });
 113        });
 114
 115        workspace.register_action(|workspace, _: &SearchSubmit, cx| {
 116            let Some(vim) = workspace
 117                .active_item_as::<Editor>(cx)
 118                .and_then(|editor| editor.read(cx).addon::<VimAddon>().cloned())
 119            else {
 120                return;
 121            };
 122            vim.view
 123                .update(cx, |_, cx| cx.defer(|vim, cx| vim.search_submit(cx)))
 124        });
 125    })
 126    .detach();
 127}
 128
 129#[derive(Clone)]
 130pub(crate) struct VimAddon {
 131    pub(crate) view: View<Vim>,
 132}
 133
 134impl editor::Addon for VimAddon {
 135    fn extend_key_context(&self, key_context: &mut KeyContext, cx: &AppContext) {
 136        self.view.read(cx).extend_key_context(key_context)
 137    }
 138
 139    fn to_any(&self) -> &dyn std::any::Any {
 140        self
 141    }
 142}
 143
 144/// The state pertaining to Vim mode.
 145pub(crate) struct Vim {
 146    pub(crate) mode: Mode,
 147    pub last_mode: Mode,
 148
 149    /// pre_count is the number before an operator is specified (3 in 3d2d)
 150    pre_count: Option<usize>,
 151    /// post_count is the number after an operator is specified (2 in 3d2d)
 152    post_count: Option<usize>,
 153
 154    operator_stack: Vec<Operator>,
 155    pub(crate) replacements: Vec<(Range<editor::Anchor>, String)>,
 156
 157    pub(crate) marks: HashMap<String, Vec<Anchor>>,
 158    pub(crate) stored_visual_mode: Option<(Mode, Vec<bool>)>,
 159    pub(crate) change_list: Vec<Vec<Anchor>>,
 160    pub(crate) change_list_position: Option<usize>,
 161
 162    pub(crate) current_tx: Option<TransactionId>,
 163    pub(crate) current_anchor: Option<Selection<Anchor>>,
 164    pub(crate) undo_modes: HashMap<TransactionId, Mode>,
 165
 166    selected_register: Option<char>,
 167    pub search: SearchState,
 168
 169    editor: WeakView<Editor>,
 170
 171    _subscriptions: Vec<Subscription>,
 172}
 173
 174// Hack: Vim intercepts events dispatched to a window and updates the view in response.
 175// This means it needs a VisualContext. The easiest way to satisfy that constraint is
 176// to make Vim a "View" that is just never actually rendered.
 177impl Render for Vim {
 178    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
 179        gpui::Empty
 180    }
 181}
 182
 183enum VimEvent {
 184    Focused,
 185}
 186impl EventEmitter<VimEvent> for Vim {}
 187
 188impl Vim {
 189    /// The namespace for Vim actions.
 190    const NAMESPACE: &'static str = "vim";
 191
 192    pub fn new(cx: &mut ViewContext<Editor>) -> View<Self> {
 193        let editor = cx.view().clone();
 194
 195        cx.new_view(|cx| Vim {
 196            mode: Mode::Normal,
 197            last_mode: Mode::Normal,
 198            pre_count: None,
 199            post_count: None,
 200            operator_stack: Vec::new(),
 201            replacements: Vec::new(),
 202
 203            marks: HashMap::default(),
 204            stored_visual_mode: None,
 205            change_list: Vec::new(),
 206            change_list_position: None,
 207            current_tx: None,
 208            current_anchor: None,
 209            undo_modes: HashMap::default(),
 210
 211            selected_register: None,
 212            search: SearchState::default(),
 213
 214            editor: editor.downgrade(),
 215            _subscriptions: vec![
 216                cx.observe_keystrokes(Self::observe_keystrokes),
 217                cx.subscribe(&editor, |this, _, event, cx| {
 218                    this.handle_editor_event(event, cx)
 219                }),
 220            ],
 221        })
 222    }
 223
 224    fn register(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
 225        if !editor.use_modal_editing() {
 226            return;
 227        }
 228
 229        let mut was_enabled = Vim::enabled(cx);
 230        let mut was_toggle = VimSettings::get_global(cx).toggle_relative_line_numbers;
 231        cx.observe_global::<SettingsStore>(move |editor, cx| {
 232            let enabled = Vim::enabled(cx);
 233            let toggle = VimSettings::get_global(cx).toggle_relative_line_numbers;
 234            if enabled && was_enabled && (toggle != was_toggle) {
 235                if toggle {
 236                    let is_relative = editor
 237                        .addon::<VimAddon>()
 238                        .map(|vim| vim.view.read(cx).mode != Mode::Insert);
 239                    editor.set_relative_line_number(is_relative, cx)
 240                } else {
 241                    editor.set_relative_line_number(None, cx)
 242                }
 243            }
 244            was_toggle = VimSettings::get_global(cx).toggle_relative_line_numbers;
 245            if was_enabled == enabled {
 246                return;
 247            }
 248            was_enabled = enabled;
 249            if enabled {
 250                Self::activate(editor, cx)
 251            } else {
 252                Self::deactivate(editor, cx)
 253            }
 254        })
 255        .detach();
 256        if was_enabled {
 257            Self::activate(editor, cx)
 258        }
 259    }
 260
 261    fn activate(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
 262        let vim = Vim::new(cx);
 263
 264        editor.register_addon(VimAddon { view: vim.clone() });
 265
 266        vim.update(cx, |_, cx| {
 267            Vim::action(editor, cx, |vim, action: &SwitchMode, cx| {
 268                vim.switch_mode(action.0, false, cx)
 269            });
 270
 271            Vim::action(editor, cx, |vim, action: &PushOperator, cx| {
 272                vim.push_operator(action.0.clone(), cx)
 273            });
 274
 275            Vim::action(editor, cx, |vim, _: &ClearOperators, cx| {
 276                vim.clear_operator(cx)
 277            });
 278            Vim::action(editor, cx, |vim, n: &Number, cx| {
 279                vim.push_count_digit(n.0, cx);
 280            });
 281            Vim::action(editor, cx, |vim, _: &Tab, cx| {
 282                vim.input_ignored(" ".into(), cx)
 283            });
 284            Vim::action(editor, cx, |vim, _: &Enter, cx| {
 285                vim.input_ignored("\n".into(), cx)
 286            });
 287
 288            normal::register(editor, cx);
 289            insert::register(editor, cx);
 290            motion::register(editor, cx);
 291            command::register(editor, cx);
 292            replace::register(editor, cx);
 293            indent::register(editor, cx);
 294            object::register(editor, cx);
 295            visual::register(editor, cx);
 296            change_list::register(editor, cx);
 297
 298            cx.defer(|vim, cx| {
 299                vim.focused(false, cx);
 300            })
 301        })
 302    }
 303
 304    fn deactivate(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
 305        editor.set_cursor_shape(CursorShape::Bar, cx);
 306        editor.set_clip_at_line_ends(false, cx);
 307        editor.set_collapse_matches(false);
 308        editor.set_input_enabled(true);
 309        editor.set_autoindent(true);
 310        editor.selections.line_mode = false;
 311        editor.unregister_addon::<VimAddon>();
 312        editor.set_relative_line_number(None, cx);
 313        if let Some(vim) = Vim::globals(cx).focused_vim() {
 314            if vim.entity_id() == cx.view().entity_id() {
 315                Vim::globals(cx).focused_vim = None;
 316            }
 317        }
 318    }
 319
 320    /// Register an action on the editor.
 321    pub fn action<A: Action>(
 322        editor: &mut Editor,
 323        cx: &mut ViewContext<Vim>,
 324        f: impl Fn(&mut Vim, &A, &mut ViewContext<Vim>) + 'static,
 325    ) {
 326        let subscription = editor.register_action(cx.listener(f));
 327        cx.on_release(|_, _, _| drop(subscription)).detach();
 328    }
 329
 330    pub fn editor(&self) -> Option<View<Editor>> {
 331        self.editor.upgrade()
 332    }
 333
 334    pub fn workspace(&self, cx: &ViewContext<Self>) -> Option<View<Workspace>> {
 335        self.editor().and_then(|editor| editor.read(cx).workspace())
 336    }
 337
 338    pub fn pane(&self, cx: &ViewContext<Self>) -> Option<View<Pane>> {
 339        self.workspace(cx)
 340            .and_then(|workspace| workspace.read(cx).pane_for(&self.editor()?))
 341    }
 342
 343    pub fn enabled(cx: &mut AppContext) -> bool {
 344        VimModeSetting::get_global(cx).0
 345    }
 346
 347    /// Called whenever an keystroke is typed so vim can observe all actions
 348    /// and keystrokes accordingly.
 349    fn observe_keystrokes(&mut self, keystroke_event: &KeystrokeEvent, cx: &mut ViewContext<Self>) {
 350        if let Some(action) = keystroke_event.action.as_ref() {
 351            // Keystroke is handled by the vim system, so continue forward
 352            if action.name().starts_with("vim::") {
 353                return;
 354            }
 355        } else if cx.has_pending_keystrokes() || keystroke_event.keystroke.is_ime_in_progress() {
 356            return;
 357        }
 358
 359        if let Some(operator) = self.active_operator() {
 360            if !operator.is_waiting(self.mode) {
 361                self.clear_operator(cx);
 362                self.stop_recording_immediately(Box::new(ClearOperators), cx)
 363            }
 364        }
 365    }
 366
 367    fn handle_editor_event(&mut self, event: &EditorEvent, cx: &mut ViewContext<Self>) {
 368        match event {
 369            EditorEvent::Focused => self.focused(true, cx),
 370            EditorEvent::Blurred => self.blurred(cx),
 371            EditorEvent::SelectionsChanged { local: true } => {
 372                self.local_selections_changed(cx);
 373            }
 374            EditorEvent::InputIgnored { text } => {
 375                self.input_ignored(text.clone(), cx);
 376                Vim::globals(cx).observe_insertion(text, None)
 377            }
 378            EditorEvent::InputHandled {
 379                text,
 380                utf16_range_to_replace: range_to_replace,
 381            } => Vim::globals(cx).observe_insertion(text, range_to_replace.clone()),
 382            EditorEvent::TransactionBegun { transaction_id } => {
 383                self.transaction_begun(*transaction_id, cx)
 384            }
 385            EditorEvent::TransactionUndone { transaction_id } => {
 386                self.transaction_undone(transaction_id, cx)
 387            }
 388            EditorEvent::Edited { .. } => self.push_to_change_list(cx),
 389            EditorEvent::FocusedIn => self.sync_vim_settings(cx),
 390            _ => {}
 391        }
 392    }
 393
 394    fn push_operator(&mut self, operator: Operator, cx: &mut ViewContext<Self>) {
 395        if matches!(
 396            operator,
 397            Operator::Change
 398                | Operator::Delete
 399                | Operator::Replace
 400                | Operator::Indent
 401                | Operator::Outdent
 402                | Operator::Lowercase
 403                | Operator::Uppercase
 404                | Operator::OppositeCase
 405                | Operator::ToggleComments
 406        ) {
 407            self.start_recording(cx)
 408        };
 409        // Since these operations can only be entered with pre-operators,
 410        // we need to clear the previous operators when pushing,
 411        // so that the current stack is the most correct
 412        if matches!(
 413            operator,
 414            Operator::AddSurrounds { .. }
 415                | Operator::ChangeSurrounds { .. }
 416                | Operator::DeleteSurrounds
 417        ) {
 418            self.operator_stack.clear();
 419            if let Operator::AddSurrounds { target: None } = operator {
 420                self.start_recording(cx);
 421            }
 422        };
 423        self.operator_stack.push(operator);
 424        self.sync_vim_settings(cx);
 425    }
 426
 427    pub fn switch_mode(&mut self, mode: Mode, leave_selections: bool, cx: &mut ViewContext<Self>) {
 428        let last_mode = self.mode;
 429        let prior_mode = self.last_mode;
 430        let prior_tx = self.current_tx;
 431        self.last_mode = last_mode;
 432        self.mode = mode;
 433        self.operator_stack.clear();
 434        self.selected_register.take();
 435        if mode == Mode::Normal || mode != last_mode {
 436            self.current_tx.take();
 437            self.current_anchor.take();
 438        }
 439        if mode != Mode::Insert && mode != Mode::Replace {
 440            self.take_count(cx);
 441        }
 442
 443        // Sync editor settings like clip mode
 444        self.sync_vim_settings(cx);
 445
 446        if VimSettings::get_global(cx).toggle_relative_line_numbers
 447            && self.mode != self.last_mode
 448            && (self.mode == Mode::Insert || self.last_mode == Mode::Insert)
 449        {
 450            self.update_editor(cx, |vim, editor, cx| {
 451                let is_relative = vim.mode != Mode::Insert;
 452                editor.set_relative_line_number(Some(is_relative), cx)
 453            });
 454        }
 455
 456        if leave_selections {
 457            return;
 458        }
 459
 460        if !mode.is_visual() && last_mode.is_visual() {
 461            self.create_visual_marks(last_mode, cx);
 462        }
 463
 464        // Adjust selections
 465        self.update_editor(cx, |vim, editor, cx| {
 466            if last_mode != Mode::VisualBlock && last_mode.is_visual() && mode == Mode::VisualBlock
 467            {
 468                vim.visual_block_motion(true, editor, cx, |_, point, goal| Some((point, goal)))
 469            }
 470            if last_mode == Mode::Insert || last_mode == Mode::Replace {
 471                if let Some(prior_tx) = prior_tx {
 472                    editor.group_until_transaction(prior_tx, cx)
 473                }
 474            }
 475
 476            editor.change_selections(None, cx, |s| {
 477                // we cheat with visual block mode and use multiple cursors.
 478                // the cost of this cheat is we need to convert back to a single
 479                // cursor whenever vim would.
 480                if last_mode == Mode::VisualBlock
 481                    && (mode != Mode::VisualBlock && mode != Mode::Insert)
 482                {
 483                    let tail = s.oldest_anchor().tail();
 484                    let head = s.newest_anchor().head();
 485                    s.select_anchor_ranges(vec![tail..head]);
 486                } else if last_mode == Mode::Insert
 487                    && prior_mode == Mode::VisualBlock
 488                    && mode != Mode::VisualBlock
 489                {
 490                    let pos = s.first_anchor().head();
 491                    s.select_anchor_ranges(vec![pos..pos])
 492                }
 493
 494                let snapshot = s.display_map();
 495                if let Some(pending) = s.pending.as_mut() {
 496                    if pending.selection.reversed && mode.is_visual() && !last_mode.is_visual() {
 497                        let mut end = pending.selection.end.to_point(&snapshot.buffer_snapshot);
 498                        end = snapshot
 499                            .buffer_snapshot
 500                            .clip_point(end + Point::new(0, 1), Bias::Right);
 501                        pending.selection.end = snapshot.buffer_snapshot.anchor_before(end);
 502                    }
 503                }
 504
 505                s.move_with(|map, selection| {
 506                    if last_mode.is_visual() && !mode.is_visual() {
 507                        let mut point = selection.head();
 508                        if !selection.reversed && !selection.is_empty() {
 509                            point = movement::left(map, selection.head());
 510                        }
 511                        selection.collapse_to(point, selection.goal)
 512                    } else if !last_mode.is_visual() && mode.is_visual() && selection.is_empty() {
 513                        selection.end = movement::right(map, selection.start);
 514                    }
 515                });
 516            })
 517        });
 518    }
 519
 520    fn take_count(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> {
 521        let global_state = cx.global_mut::<VimGlobals>();
 522        if global_state.dot_replaying {
 523            return global_state.recorded_count;
 524        }
 525
 526        let count = if self.post_count.is_none() && self.pre_count.is_none() {
 527            return None;
 528        } else {
 529            Some(self.post_count.take().unwrap_or(1) * self.pre_count.take().unwrap_or(1))
 530        };
 531
 532        if global_state.dot_recording {
 533            global_state.recorded_count = count;
 534        }
 535        self.sync_vim_settings(cx);
 536        count
 537    }
 538
 539    pub fn cursor_shape(&self) -> CursorShape {
 540        match self.mode {
 541            Mode::Normal => {
 542                if self.operator_stack.is_empty() {
 543                    CursorShape::Block
 544                } else {
 545                    CursorShape::Underscore
 546                }
 547            }
 548            Mode::Replace => CursorShape::Underscore,
 549            Mode::Visual | Mode::VisualLine | Mode::VisualBlock => CursorShape::Block,
 550            Mode::Insert => CursorShape::Bar,
 551        }
 552    }
 553
 554    pub fn editor_input_enabled(&self) -> bool {
 555        match self.mode {
 556            Mode::Insert => {
 557                if let Some(operator) = self.operator_stack.last() {
 558                    !operator.is_waiting(self.mode)
 559                } else {
 560                    true
 561                }
 562            }
 563            Mode::Normal | Mode::Replace | Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
 564                false
 565            }
 566        }
 567    }
 568
 569    pub fn should_autoindent(&self) -> bool {
 570        !(self.mode == Mode::Insert && self.last_mode == Mode::VisualBlock)
 571    }
 572
 573    pub fn clip_at_line_ends(&self) -> bool {
 574        match self.mode {
 575            Mode::Insert | Mode::Visual | Mode::VisualLine | Mode::VisualBlock | Mode::Replace => {
 576                false
 577            }
 578            Mode::Normal => true,
 579        }
 580    }
 581
 582    pub fn extend_key_context(&self, context: &mut KeyContext) {
 583        let mut mode = match self.mode {
 584            Mode::Normal => "normal",
 585            Mode::Visual | Mode::VisualLine | Mode::VisualBlock => "visual",
 586            Mode::Insert => "insert",
 587            Mode::Replace => "replace",
 588        }
 589        .to_string();
 590
 591        let mut operator_id = "none";
 592
 593        let active_operator = self.active_operator();
 594        if active_operator.is_none() && self.pre_count.is_some()
 595            || active_operator.is_some() && self.post_count.is_some()
 596        {
 597            context.add("VimCount");
 598        }
 599
 600        if let Some(active_operator) = active_operator {
 601            if active_operator.is_waiting(self.mode) {
 602                mode = "waiting".to_string();
 603            } else {
 604                mode = "operator".to_string();
 605                operator_id = active_operator.id();
 606            }
 607        }
 608
 609        if mode != "waiting" && mode != "insert" && mode != "replace" {
 610            context.add("VimControl");
 611        }
 612        context.set("vim_mode", mode);
 613        context.set("vim_operator", operator_id);
 614    }
 615
 616    fn focused(&mut self, preserve_selection: bool, cx: &mut ViewContext<Self>) {
 617        let Some(editor) = self.editor() else {
 618            return;
 619        };
 620        let editor = editor.read(cx);
 621        let editor_mode = editor.mode();
 622        let newest_selection_empty = editor.selections.newest::<usize>(cx).is_empty();
 623
 624        if editor_mode == EditorMode::Full
 625                && !newest_selection_empty
 626                && self.mode == Mode::Normal
 627                // When following someone, don't switch vim mode.
 628                && editor.leader_peer_id().is_none()
 629        {
 630            if preserve_selection {
 631                self.switch_mode(Mode::Visual, true, cx);
 632            } else {
 633                self.update_editor(cx, |_, editor, cx| {
 634                    editor.set_clip_at_line_ends(false, cx);
 635                    editor.change_selections(None, cx, |s| {
 636                        s.move_with(|_, selection| {
 637                            selection.collapse_to(selection.start, selection.goal)
 638                        })
 639                    });
 640                });
 641            }
 642        }
 643
 644        cx.emit(VimEvent::Focused);
 645        self.sync_vim_settings(cx);
 646
 647        if VimSettings::get_global(cx).toggle_relative_line_numbers {
 648            if let Some(old_vim) = Vim::globals(cx).focused_vim() {
 649                if old_vim.entity_id() != cx.view().entity_id() {
 650                    old_vim.update(cx, |vim, cx| {
 651                        vim.update_editor(cx, |_, editor, cx| {
 652                            editor.set_relative_line_number(None, cx)
 653                        });
 654                    });
 655
 656                    self.update_editor(cx, |vim, editor, cx| {
 657                        let is_relative = vim.mode != Mode::Insert;
 658                        editor.set_relative_line_number(Some(is_relative), cx)
 659                    });
 660                }
 661            } else {
 662                self.update_editor(cx, |vim, editor, cx| {
 663                    let is_relative = vim.mode != Mode::Insert;
 664                    editor.set_relative_line_number(Some(is_relative), cx)
 665                });
 666            }
 667        }
 668        Vim::globals(cx).focused_vim = Some(cx.view().downgrade());
 669    }
 670
 671    fn blurred(&mut self, cx: &mut ViewContext<Self>) {
 672        self.stop_recording_immediately(NormalBefore.boxed_clone(), cx);
 673        self.store_visual_marks(cx);
 674        self.clear_operator(cx);
 675        self.update_editor(cx, |_, editor, cx| {
 676            editor.set_cursor_shape(language::CursorShape::Hollow, cx);
 677        });
 678    }
 679
 680    fn update_editor<S>(
 681        &mut self,
 682        cx: &mut ViewContext<Self>,
 683        update: impl FnOnce(&mut Self, &mut Editor, &mut ViewContext<Editor>) -> S,
 684    ) -> Option<S> {
 685        let editor = self.editor.upgrade()?;
 686        Some(editor.update(cx, |editor, cx| update(self, editor, cx)))
 687    }
 688
 689    fn editor_selections(&mut self, cx: &mut ViewContext<Self>) -> Vec<Range<Anchor>> {
 690        self.update_editor(cx, |_, editor, _| {
 691            editor
 692                .selections
 693                .disjoint_anchors()
 694                .iter()
 695                .map(|selection| selection.tail()..selection.head())
 696                .collect()
 697        })
 698        .unwrap_or_default()
 699    }
 700
 701    /// When doing an action that modifies the buffer, we start recording so that `.`
 702    /// will replay the action.
 703    pub fn start_recording(&mut self, cx: &mut ViewContext<Self>) {
 704        Vim::update_globals(cx, |globals, cx| {
 705            if !globals.dot_replaying {
 706                globals.dot_recording = true;
 707                globals.recorded_actions = Default::default();
 708                globals.recorded_count = None;
 709
 710                let selections = self.editor().map(|editor| {
 711                    let editor = editor.read(cx);
 712                    (
 713                        editor.selections.oldest::<Point>(cx),
 714                        editor.selections.newest::<Point>(cx),
 715                    )
 716                });
 717
 718                if let Some((oldest, newest)) = selections {
 719                    globals.recorded_selection = match self.mode {
 720                        Mode::Visual if newest.end.row == newest.start.row => {
 721                            RecordedSelection::SingleLine {
 722                                cols: newest.end.column - newest.start.column,
 723                            }
 724                        }
 725                        Mode::Visual => RecordedSelection::Visual {
 726                            rows: newest.end.row - newest.start.row,
 727                            cols: newest.end.column,
 728                        },
 729                        Mode::VisualLine => RecordedSelection::VisualLine {
 730                            rows: newest.end.row - newest.start.row,
 731                        },
 732                        Mode::VisualBlock => RecordedSelection::VisualBlock {
 733                            rows: newest.end.row.abs_diff(oldest.start.row),
 734                            cols: newest.end.column.abs_diff(oldest.start.column),
 735                        },
 736                        _ => RecordedSelection::None,
 737                    }
 738                } else {
 739                    globals.recorded_selection = RecordedSelection::None;
 740                }
 741            }
 742        })
 743    }
 744
 745    pub fn stop_replaying(&mut self, cx: &mut ViewContext<Self>) {
 746        let globals = Vim::globals(cx);
 747        globals.dot_replaying = false;
 748        if let Some(replayer) = globals.replayer.take() {
 749            replayer.stop();
 750        }
 751    }
 752
 753    /// When finishing an action that modifies the buffer, stop recording.
 754    /// as you usually call this within a keystroke handler we also ensure that
 755    /// the current action is recorded.
 756    pub fn stop_recording(&mut self, cx: &mut ViewContext<Self>) {
 757        let globals = Vim::globals(cx);
 758        if globals.dot_recording {
 759            globals.stop_recording_after_next_action = true;
 760        }
 761    }
 762
 763    /// Stops recording actions immediately rather than waiting until after the
 764    /// next action to stop recording.
 765    ///
 766    /// This doesn't include the current action.
 767    pub fn stop_recording_immediately(
 768        &mut self,
 769        action: Box<dyn Action>,
 770        cx: &mut ViewContext<Self>,
 771    ) {
 772        let globals = Vim::globals(cx);
 773        if globals.dot_recording {
 774            globals
 775                .recorded_actions
 776                .push(ReplayableAction::Action(action.boxed_clone()));
 777            globals.dot_recording = false;
 778            globals.stop_recording_after_next_action = false;
 779        }
 780    }
 781
 782    /// Explicitly record one action (equivalents to start_recording and stop_recording)
 783    pub fn record_current_action(&mut self, cx: &mut ViewContext<Self>) {
 784        self.start_recording(cx);
 785        self.stop_recording(cx);
 786    }
 787
 788    fn push_count_digit(&mut self, number: usize, cx: &mut ViewContext<Self>) {
 789        if self.active_operator().is_some() {
 790            let post_count = self.post_count.unwrap_or(0);
 791
 792            self.post_count = Some(
 793                post_count
 794                    .checked_mul(10)
 795                    .and_then(|post_count| post_count.checked_add(number))
 796                    .unwrap_or(post_count),
 797            )
 798        } else {
 799            let pre_count = self.pre_count.unwrap_or(0);
 800
 801            self.pre_count = Some(
 802                pre_count
 803                    .checked_mul(10)
 804                    .and_then(|pre_count| pre_count.checked_add(number))
 805                    .unwrap_or(pre_count),
 806            )
 807        }
 808        // update the keymap so that 0 works
 809        self.sync_vim_settings(cx)
 810    }
 811
 812    fn select_register(&mut self, register: Arc<str>, cx: &mut ViewContext<Self>) {
 813        if register.chars().count() == 1 {
 814            self.selected_register
 815                .replace(register.chars().next().unwrap());
 816        }
 817        self.operator_stack.clear();
 818        self.sync_vim_settings(cx);
 819    }
 820
 821    fn maybe_pop_operator(&mut self) -> Option<Operator> {
 822        self.operator_stack.pop()
 823    }
 824
 825    fn pop_operator(&mut self, cx: &mut ViewContext<Self>) -> Operator {
 826        let popped_operator = self.operator_stack.pop()
 827            .expect("Operator popped when no operator was on the stack. This likely means there is an invalid keymap config");
 828        self.sync_vim_settings(cx);
 829        popped_operator
 830    }
 831
 832    fn clear_operator(&mut self, cx: &mut ViewContext<Self>) {
 833        self.take_count(cx);
 834        self.selected_register.take();
 835        self.operator_stack.clear();
 836        self.sync_vim_settings(cx);
 837    }
 838
 839    fn active_operator(&self) -> Option<Operator> {
 840        self.operator_stack.last().cloned()
 841    }
 842
 843    fn transaction_begun(&mut self, transaction_id: TransactionId, _: &mut ViewContext<Self>) {
 844        let mode = if (self.mode == Mode::Insert
 845            || self.mode == Mode::Replace
 846            || self.mode == Mode::Normal)
 847            && self.current_tx.is_none()
 848        {
 849            self.current_tx = Some(transaction_id);
 850            self.last_mode
 851        } else {
 852            self.mode
 853        };
 854        if mode == Mode::VisualLine || mode == Mode::VisualBlock {
 855            self.undo_modes.insert(transaction_id, mode);
 856        }
 857    }
 858
 859    fn transaction_undone(&mut self, transaction_id: &TransactionId, cx: &mut ViewContext<Self>) {
 860        match self.mode {
 861            Mode::VisualLine | Mode::VisualBlock | Mode::Visual => {
 862                self.update_editor(cx, |vim, editor, cx| {
 863                    let original_mode = vim.undo_modes.get(transaction_id);
 864                    editor.change_selections(None, cx, |s| match original_mode {
 865                        Some(Mode::VisualLine) => {
 866                            s.move_with(|map, selection| {
 867                                selection.collapse_to(
 868                                    map.prev_line_boundary(selection.start.to_point(map)).1,
 869                                    SelectionGoal::None,
 870                                )
 871                            });
 872                        }
 873                        Some(Mode::VisualBlock) => {
 874                            let mut first = s.first_anchor();
 875                            first.collapse_to(first.start, first.goal);
 876                            s.select_anchors(vec![first]);
 877                        }
 878                        _ => {
 879                            s.move_with(|map, selection| {
 880                                selection.collapse_to(
 881                                    map.clip_at_line_end(selection.start),
 882                                    selection.goal,
 883                                );
 884                            });
 885                        }
 886                    });
 887                });
 888                self.switch_mode(Mode::Normal, true, cx)
 889            }
 890            Mode::Normal => {
 891                self.update_editor(cx, |_, editor, cx| {
 892                    editor.change_selections(None, cx, |s| {
 893                        s.move_with(|map, selection| {
 894                            selection
 895                                .collapse_to(map.clip_at_line_end(selection.end), selection.goal)
 896                        })
 897                    })
 898                });
 899            }
 900            Mode::Insert | Mode::Replace => {}
 901        }
 902    }
 903
 904    fn local_selections_changed(&mut self, cx: &mut ViewContext<Self>) {
 905        let Some(editor) = self.editor() else { return };
 906
 907        if editor.read(cx).leader_peer_id().is_some() {
 908            return;
 909        }
 910
 911        let newest = editor.read(cx).selections.newest_anchor().clone();
 912        let is_multicursor = editor.read(cx).selections.count() > 1;
 913        if self.mode == Mode::Insert && self.current_tx.is_some() {
 914            if self.current_anchor.is_none() {
 915                self.current_anchor = Some(newest);
 916            } else if self.current_anchor.as_ref().unwrap() != &newest {
 917                if let Some(tx_id) = self.current_tx.take() {
 918                    self.update_editor(cx, |_, editor, cx| {
 919                        editor.group_until_transaction(tx_id, cx)
 920                    });
 921                }
 922            }
 923        } else if self.mode == Mode::Normal && newest.start != newest.end {
 924            if matches!(newest.goal, SelectionGoal::HorizontalRange { .. }) {
 925                self.switch_mode(Mode::VisualBlock, false, cx);
 926            } else {
 927                self.switch_mode(Mode::Visual, false, cx)
 928            }
 929        } else if newest.start == newest.end
 930            && !is_multicursor
 931            && [Mode::Visual, Mode::VisualLine, Mode::VisualBlock].contains(&self.mode)
 932        {
 933            self.switch_mode(Mode::Normal, true, cx);
 934        }
 935    }
 936
 937    fn input_ignored(&mut self, text: Arc<str>, cx: &mut ViewContext<Self>) {
 938        if text.is_empty() {
 939            return;
 940        }
 941
 942        match self.active_operator() {
 943            Some(Operator::FindForward { before }) => {
 944                let find = Motion::FindForward {
 945                    before,
 946                    char: text.chars().next().unwrap(),
 947                    mode: if VimSettings::get_global(cx).use_multiline_find {
 948                        FindRange::MultiLine
 949                    } else {
 950                        FindRange::SingleLine
 951                    },
 952                    smartcase: VimSettings::get_global(cx).use_smartcase_find,
 953                };
 954                Vim::globals(cx).last_find = Some(find.clone());
 955                self.motion(find, cx)
 956            }
 957            Some(Operator::FindBackward { after }) => {
 958                let find = Motion::FindBackward {
 959                    after,
 960                    char: text.chars().next().unwrap(),
 961                    mode: if VimSettings::get_global(cx).use_multiline_find {
 962                        FindRange::MultiLine
 963                    } else {
 964                        FindRange::SingleLine
 965                    },
 966                    smartcase: VimSettings::get_global(cx).use_smartcase_find,
 967                };
 968                Vim::globals(cx).last_find = Some(find.clone());
 969                self.motion(find, cx)
 970            }
 971            Some(Operator::Replace) => match self.mode {
 972                Mode::Normal => self.normal_replace(text, cx),
 973                Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
 974                    self.visual_replace(text, cx)
 975                }
 976                _ => self.clear_operator(cx),
 977            },
 978            Some(Operator::Digraph { first_char }) => {
 979                if let Some(first_char) = first_char {
 980                    if let Some(second_char) = text.chars().next() {
 981                        self.insert_digraph(first_char, second_char, cx);
 982                    }
 983                } else {
 984                    let first_char = text.chars().next();
 985                    self.pop_operator(cx);
 986                    self.push_operator(Operator::Digraph { first_char }, cx);
 987                }
 988            }
 989            Some(Operator::AddSurrounds { target }) => match self.mode {
 990                Mode::Normal => {
 991                    if let Some(target) = target {
 992                        self.add_surrounds(text, target, cx);
 993                        self.clear_operator(cx);
 994                    }
 995                }
 996                Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
 997                    self.add_surrounds(text, SurroundsType::Selection, cx);
 998                    self.clear_operator(cx);
 999                }
1000                _ => self.clear_operator(cx),
1001            },
1002            Some(Operator::ChangeSurrounds { target }) => match self.mode {
1003                Mode::Normal => {
1004                    if let Some(target) = target {
1005                        self.change_surrounds(text, target, cx);
1006                        self.clear_operator(cx);
1007                    }
1008                }
1009                _ => self.clear_operator(cx),
1010            },
1011            Some(Operator::DeleteSurrounds) => match self.mode {
1012                Mode::Normal => {
1013                    self.delete_surrounds(text, cx);
1014                    self.clear_operator(cx);
1015                }
1016                _ => self.clear_operator(cx),
1017            },
1018            Some(Operator::Mark) => self.create_mark(text, false, cx),
1019            Some(Operator::RecordRegister) => {
1020                self.record_register(text.chars().next().unwrap(), cx)
1021            }
1022            Some(Operator::ReplayRegister) => {
1023                self.replay_register(text.chars().next().unwrap(), cx)
1024            }
1025            Some(Operator::Register) => match self.mode {
1026                Mode::Insert => {
1027                    self.update_editor(cx, |_, editor, cx| {
1028                        if let Some(register) = Vim::update_globals(cx, |globals, cx| {
1029                            globals.read_register(text.chars().next(), Some(editor), cx)
1030                        }) {
1031                            editor.do_paste(
1032                                &register.text.to_string(),
1033                                register.clipboard_selections.clone(),
1034                                false,
1035                                cx,
1036                            )
1037                        }
1038                    });
1039                    self.clear_operator(cx);
1040                }
1041                _ => {
1042                    self.select_register(text, cx);
1043                }
1044            },
1045            Some(Operator::Jump { line }) => self.jump(text, line, cx),
1046            _ => {
1047                if self.mode == Mode::Replace {
1048                    self.multi_replace(text, cx)
1049                }
1050            }
1051        }
1052    }
1053
1054    fn sync_vim_settings(&mut self, cx: &mut ViewContext<Self>) {
1055        self.update_editor(cx, |vim, editor, cx| {
1056            editor.set_cursor_shape(vim.cursor_shape(), cx);
1057            editor.set_clip_at_line_ends(vim.clip_at_line_ends(), cx);
1058            editor.set_collapse_matches(true);
1059            editor.set_input_enabled(vim.editor_input_enabled());
1060            editor.set_autoindent(vim.should_autoindent());
1061            editor.selections.line_mode = matches!(vim.mode, Mode::VisualLine);
1062            editor.set_inline_completions_enabled(matches!(vim.mode, Mode::Insert | Mode::Replace));
1063        });
1064        cx.notify()
1065    }
1066}
1067
1068impl Settings for VimModeSetting {
1069    const KEY: Option<&'static str> = Some("vim_mode");
1070
1071    type FileContent = Option<bool>;
1072
1073    fn load(sources: SettingsSources<Self::FileContent>, _: &mut AppContext) -> Result<Self> {
1074        Ok(Self(sources.user.copied().flatten().unwrap_or(
1075            sources.default.ok_or_else(Self::missing_default)?,
1076        )))
1077    }
1078}
1079
1080/// Controls when to use system clipboard.
1081#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
1082#[serde(rename_all = "snake_case")]
1083pub enum UseSystemClipboard {
1084    /// Don't use system clipboard.
1085    Never,
1086    /// Use system clipboard.
1087    Always,
1088    /// Use system clipboard for yank operations.
1089    OnYank,
1090}
1091
1092#[derive(Deserialize)]
1093struct VimSettings {
1094    pub toggle_relative_line_numbers: bool,
1095    pub use_system_clipboard: UseSystemClipboard,
1096    pub use_multiline_find: bool,
1097    pub use_smartcase_find: bool,
1098    pub custom_digraphs: HashMap<String, Arc<str>>,
1099}
1100
1101#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
1102struct VimSettingsContent {
1103    pub toggle_relative_line_numbers: Option<bool>,
1104    pub use_system_clipboard: Option<UseSystemClipboard>,
1105    pub use_multiline_find: Option<bool>,
1106    pub use_smartcase_find: Option<bool>,
1107    pub custom_digraphs: Option<HashMap<String, Arc<str>>>,
1108}
1109
1110impl Settings for VimSettings {
1111    const KEY: Option<&'static str> = Some("vim");
1112
1113    type FileContent = VimSettingsContent;
1114
1115    fn load(sources: SettingsSources<Self::FileContent>, _: &mut AppContext) -> Result<Self> {
1116        sources.json_merge()
1117    }
1118}