1//! Vim support for Zed.
2
3#[cfg(test)]
4mod test;
5
6mod command;
7mod editor_events;
8mod insert;
9mod mode_indicator;
10mod motion;
11mod normal;
12mod object;
13mod replace;
14mod state;
15mod surrounds;
16mod utils;
17mod visual;
18
19use anyhow::Result;
20use collections::HashMap;
21use command_palette_hooks::{CommandPaletteFilter, CommandPaletteInterceptor};
22use editor::{
23 movement::{self, FindRange},
24 Anchor, Editor, EditorEvent, EditorMode,
25};
26use gpui::{
27 actions, impl_actions, Action, AppContext, EntityId, FocusableView, Global, KeystrokeEvent,
28 Subscription, View, ViewContext, WeakView, WindowContext,
29};
30use language::{CursorShape, Point, Selection, SelectionGoal, TransactionId};
31pub use mode_indicator::ModeIndicator;
32use motion::Motion;
33use normal::normal_replace;
34use replace::multi_replace;
35use schemars::JsonSchema;
36use serde::Deserialize;
37use serde_derive::Serialize;
38use settings::{update_settings_file, Settings, SettingsSources, SettingsStore};
39use state::{EditorState, Mode, Operator, RecordedSelection, WorkspaceState};
40use std::{ops::Range, sync::Arc};
41use surrounds::{add_surrounds, change_surrounds, delete_surrounds};
42use ui::BorrowAppContext;
43use visual::{visual_block_motion, visual_replace};
44use workspace::{self, Workspace};
45
46use crate::state::ReplayableAction;
47
48/// Whether or not to enable Vim mode (work in progress).
49///
50/// Default: false
51pub struct VimModeSetting(pub bool);
52
53/// An Action to Switch between modes
54#[derive(Clone, Deserialize, PartialEq)]
55pub struct SwitchMode(pub Mode);
56
57/// PushOperator is used to put vim into a "minor" mode,
58/// where it's waiting for a specific next set of keystrokes.
59/// For example 'd' needs a motion to complete.
60#[derive(Clone, Deserialize, PartialEq)]
61pub struct PushOperator(pub Operator);
62
63/// Number is used to manage vim's count. Pushing a digit
64/// multiplis the current value by 10 and adds the digit.
65#[derive(Clone, Deserialize, PartialEq)]
66struct Number(usize);
67
68actions!(
69 vim,
70 [
71 Tab,
72 Enter,
73 Object,
74 InnerObject,
75 FindForward,
76 FindBackward,
77 OpenDefaultKeymap
78 ]
79);
80
81// in the workspace namespace so it's not filtered out when vim is disabled.
82actions!(workspace, [ToggleVimMode]);
83
84impl_actions!(vim, [SwitchMode, PushOperator, Number]);
85
86/// Initializes the `vim` crate.
87pub fn init(cx: &mut AppContext) {
88 cx.set_global(Vim::default());
89 VimModeSetting::register(cx);
90 VimSettings::register(cx);
91
92 cx.observe_keystrokes(observe_keystrokes).detach();
93 editor_events::init(cx);
94
95 cx.observe_new_views(|workspace: &mut Workspace, cx| register(workspace, cx))
96 .detach();
97
98 // Any time settings change, update vim mode to match. The Vim struct
99 // will be initialized as disabled by default, so we filter its commands
100 // out when starting up.
101 CommandPaletteFilter::update_global(cx, |filter, _| {
102 filter.hide_namespace(Vim::NAMESPACE);
103 });
104 cx.update_global(|vim: &mut Vim, cx: &mut AppContext| {
105 vim.set_enabled(VimModeSetting::get_global(cx).0, cx)
106 });
107 cx.observe_global::<SettingsStore>(|cx| {
108 cx.update_global(|vim: &mut Vim, cx: &mut AppContext| {
109 vim.set_enabled(VimModeSetting::get_global(cx).0, cx)
110 });
111 })
112 .detach();
113}
114
115fn register(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
116 workspace.register_action(|_: &mut Workspace, &SwitchMode(mode): &SwitchMode, cx| {
117 Vim::update(cx, |vim, cx| vim.switch_mode(mode, false, cx))
118 });
119 workspace.register_action(
120 |_: &mut Workspace, PushOperator(operator): &PushOperator, cx| {
121 Vim::update(cx, |vim, cx| vim.push_operator(operator.clone(), cx))
122 },
123 );
124 workspace.register_action(|_: &mut Workspace, n: &Number, cx: _| {
125 Vim::update(cx, |vim, cx| vim.push_count_digit(n.0, cx));
126 });
127
128 workspace.register_action(|_: &mut Workspace, _: &Tab, cx| {
129 Vim::active_editor_input_ignored(" ".into(), cx)
130 });
131
132 workspace.register_action(|_: &mut Workspace, _: &Enter, cx| {
133 Vim::active_editor_input_ignored("\n".into(), cx)
134 });
135
136 workspace.register_action(|workspace: &mut Workspace, _: &ToggleVimMode, cx| {
137 let fs = workspace.app_state().fs.clone();
138 let currently_enabled = VimModeSetting::get_global(cx).0;
139 update_settings_file::<VimModeSetting>(fs, cx, move |setting| {
140 *setting = Some(!currently_enabled)
141 })
142 });
143
144 workspace.register_action(|_: &mut Workspace, _: &OpenDefaultKeymap, cx| {
145 cx.emit(workspace::Event::OpenBundledFile {
146 text: settings::vim_keymap(),
147 title: "Default Vim Bindings",
148 language: "JSON",
149 });
150 });
151
152 normal::register(workspace, cx);
153 insert::register(workspace, cx);
154 motion::register(workspace, cx);
155 command::register(workspace, cx);
156 replace::register(workspace, cx);
157 object::register(workspace, cx);
158 visual::register(workspace, cx);
159}
160
161/// Called whenever an keystroke is typed so vim can observe all actions
162/// and keystrokes accordingly.
163fn observe_keystrokes(keystroke_event: &KeystrokeEvent, cx: &mut WindowContext) {
164 if let Some(action) = keystroke_event
165 .action
166 .as_ref()
167 .map(|action| action.boxed_clone())
168 {
169 Vim::update(cx, |vim, _| {
170 if vim.workspace_state.recording {
171 vim.workspace_state
172 .recorded_actions
173 .push(ReplayableAction::Action(action.boxed_clone()));
174
175 if vim.workspace_state.stop_recording_after_next_action {
176 vim.workspace_state.recording = false;
177 vim.workspace_state.stop_recording_after_next_action = false;
178 }
179 }
180 });
181
182 // Keystroke is handled by the vim system, so continue forward
183 if action.name().starts_with("vim::") {
184 return;
185 }
186 } else if cx.has_pending_keystrokes() {
187 return;
188 }
189
190 Vim::update(cx, |vim, cx| match vim.active_operator() {
191 Some(
192 Operator::FindForward { .. }
193 | Operator::FindBackward { .. }
194 | Operator::Replace
195 | Operator::AddSurrounds { .. }
196 | Operator::ChangeSurrounds { .. }
197 | Operator::DeleteSurrounds,
198 ) => {}
199 Some(_) => {
200 vim.clear_operator(cx);
201 }
202 _ => {}
203 });
204}
205
206/// The state pertaining to Vim mode.
207#[derive(Default)]
208struct Vim {
209 active_editor: Option<WeakView<Editor>>,
210 editor_subscription: Option<Subscription>,
211 enabled: bool,
212 editor_states: HashMap<EntityId, EditorState>,
213 workspace_state: WorkspaceState,
214 default_state: EditorState,
215}
216
217impl Global for Vim {}
218
219impl Vim {
220 /// The namespace for Vim actions.
221 const NAMESPACE: &'static str = "vim";
222
223 fn read(cx: &mut AppContext) -> &Self {
224 cx.global::<Self>()
225 }
226
227 fn update<F, S>(cx: &mut WindowContext, update: F) -> S
228 where
229 F: FnOnce(&mut Self, &mut WindowContext) -> S,
230 {
231 cx.update_global(update)
232 }
233
234 fn activate_editor(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
235 if !editor.read(cx).use_modal_editing() {
236 return;
237 }
238
239 self.active_editor = Some(editor.clone().downgrade());
240 self.editor_subscription = Some(cx.subscribe(&editor, |editor, event, cx| match event {
241 EditorEvent::SelectionsChanged { local: true } => {
242 let editor = editor.read(cx);
243 if editor.leader_peer_id().is_none() {
244 let newest = editor.selections.newest_anchor().clone();
245 let is_multicursor = editor.selections.count() > 1;
246 Vim::update(cx, |vim, cx| {
247 vim.local_selections_changed(newest, is_multicursor, cx);
248 })
249 }
250 }
251 EditorEvent::InputIgnored { text } => {
252 Vim::active_editor_input_ignored(text.clone(), cx);
253 Vim::record_insertion(text, None, cx)
254 }
255 EditorEvent::InputHandled {
256 text,
257 utf16_range_to_replace: range_to_replace,
258 } => Vim::record_insertion(text, range_to_replace.clone(), cx),
259 EditorEvent::TransactionBegun { transaction_id } => Vim::update(cx, |vim, cx| {
260 vim.transaction_begun(*transaction_id, cx);
261 }),
262 EditorEvent::TransactionUndone { transaction_id } => Vim::update(cx, |vim, cx| {
263 vim.transaction_undone(transaction_id, cx);
264 }),
265 _ => {}
266 }));
267
268 let editor = editor.read(cx);
269 let editor_mode = editor.mode();
270 let newest_selection_empty = editor.selections.newest::<usize>(cx).is_empty();
271
272 if editor_mode == EditorMode::Full
273 && !newest_selection_empty
274 && self.state().mode == Mode::Normal
275 // When following someone, don't switch vim mode.
276 && editor.leader_peer_id().is_none()
277 {
278 self.switch_mode(Mode::Visual, true, cx);
279 }
280
281 self.sync_vim_settings(cx);
282 }
283
284 fn record_insertion(
285 text: &Arc<str>,
286 range_to_replace: Option<Range<isize>>,
287 cx: &mut WindowContext,
288 ) {
289 Vim::update(cx, |vim, _| {
290 if vim.workspace_state.recording {
291 vim.workspace_state
292 .recorded_actions
293 .push(ReplayableAction::Insertion {
294 text: text.clone(),
295 utf16_range_to_replace: range_to_replace,
296 });
297 if vim.workspace_state.stop_recording_after_next_action {
298 vim.workspace_state.recording = false;
299 vim.workspace_state.stop_recording_after_next_action = false;
300 }
301 }
302 });
303 }
304
305 fn update_active_editor<S>(
306 &mut self,
307 cx: &mut WindowContext,
308 update: impl FnOnce(&mut Vim, &mut Editor, &mut ViewContext<Editor>) -> S,
309 ) -> Option<S> {
310 let editor = self.active_editor.clone()?.upgrade()?;
311 Some(editor.update(cx, |editor, cx| update(self, editor, cx)))
312 }
313
314 fn editor_selections(&mut self, cx: &mut WindowContext) -> Vec<Range<Anchor>> {
315 self.update_active_editor(cx, |_, editor, _| {
316 editor
317 .selections
318 .disjoint_anchors()
319 .iter()
320 .map(|selection| selection.tail()..selection.head())
321 .collect()
322 })
323 .unwrap_or_default()
324 }
325
326 /// When doing an action that modifies the buffer, we start recording so that `.`
327 /// will replay the action.
328 pub fn start_recording(&mut self, cx: &mut WindowContext) {
329 if !self.workspace_state.replaying {
330 self.workspace_state.recording = true;
331 self.workspace_state.recorded_actions = Default::default();
332 self.workspace_state.recorded_count = None;
333
334 let selections = self
335 .active_editor
336 .as_ref()
337 .and_then(|editor| editor.upgrade())
338 .map(|editor| {
339 let editor = editor.read(cx);
340 (
341 editor.selections.oldest::<Point>(cx),
342 editor.selections.newest::<Point>(cx),
343 )
344 });
345
346 if let Some((oldest, newest)) = selections {
347 self.workspace_state.recorded_selection = match self.state().mode {
348 Mode::Visual if newest.end.row == newest.start.row => {
349 RecordedSelection::SingleLine {
350 cols: newest.end.column - newest.start.column,
351 }
352 }
353 Mode::Visual => RecordedSelection::Visual {
354 rows: newest.end.row - newest.start.row,
355 cols: newest.end.column,
356 },
357 Mode::VisualLine => RecordedSelection::VisualLine {
358 rows: newest.end.row - newest.start.row,
359 },
360 Mode::VisualBlock => RecordedSelection::VisualBlock {
361 rows: newest.end.row.abs_diff(oldest.start.row),
362 cols: newest.end.column.abs_diff(oldest.start.column),
363 },
364 _ => RecordedSelection::None,
365 }
366 } else {
367 self.workspace_state.recorded_selection = RecordedSelection::None;
368 }
369 }
370 }
371
372 pub fn stop_replaying(&mut self) {
373 self.workspace_state.replaying = false;
374 }
375
376 /// When finishing an action that modifies the buffer, stop recording.
377 /// as you usually call this within a keystroke handler we also ensure that
378 /// the current action is recorded.
379 pub fn stop_recording(&mut self) {
380 if self.workspace_state.recording {
381 self.workspace_state.stop_recording_after_next_action = true;
382 }
383 }
384
385 /// Stops recording actions immediately rather than waiting until after the
386 /// next action to stop recording.
387 ///
388 /// This doesn't include the current action.
389 pub fn stop_recording_immediately(&mut self, action: Box<dyn Action>) {
390 if self.workspace_state.recording {
391 self.workspace_state
392 .recorded_actions
393 .push(ReplayableAction::Action(action.boxed_clone()));
394 self.workspace_state.recording = false;
395 self.workspace_state.stop_recording_after_next_action = false;
396 }
397 }
398
399 /// Explicitly record one action (equivalents to start_recording and stop_recording)
400 pub fn record_current_action(&mut self, cx: &mut WindowContext) {
401 self.start_recording(cx);
402 self.stop_recording();
403 }
404
405 fn switch_mode(&mut self, mode: Mode, leave_selections: bool, cx: &mut WindowContext) {
406 let state = self.state();
407 let last_mode = state.mode;
408 let prior_mode = state.last_mode;
409 let prior_tx = state.current_tx;
410 self.update_state(|state| {
411 state.last_mode = last_mode;
412 state.mode = mode;
413 state.operator_stack.clear();
414 state.current_tx.take();
415 state.current_anchor.take();
416 });
417 if mode != Mode::Insert {
418 self.take_count(cx);
419 }
420
421 // Sync editor settings like clip mode
422 self.sync_vim_settings(cx);
423
424 if leave_selections {
425 return;
426 }
427
428 // Adjust selections
429 self.update_active_editor(cx, |_, editor, cx| {
430 if last_mode != Mode::VisualBlock && last_mode.is_visual() && mode == Mode::VisualBlock
431 {
432 visual_block_motion(true, editor, cx, |_, point, goal| Some((point, goal)))
433 }
434 if last_mode == Mode::Insert || last_mode == Mode::Replace {
435 if let Some(prior_tx) = prior_tx {
436 editor.group_until_transaction(prior_tx, cx)
437 }
438 }
439
440 editor.change_selections(None, cx, |s| {
441 // we cheat with visual block mode and use multiple cursors.
442 // the cost of this cheat is we need to convert back to a single
443 // cursor whenever vim would.
444 if last_mode == Mode::VisualBlock
445 && (mode != Mode::VisualBlock && mode != Mode::Insert)
446 {
447 let tail = s.oldest_anchor().tail();
448 let head = s.newest_anchor().head();
449 s.select_anchor_ranges(vec![tail..head]);
450 } else if last_mode == Mode::Insert
451 && prior_mode == Mode::VisualBlock
452 && mode != Mode::VisualBlock
453 {
454 let pos = s.first_anchor().head();
455 s.select_anchor_ranges(vec![pos..pos])
456 }
457
458 s.move_with(|map, selection| {
459 if last_mode.is_visual() && !mode.is_visual() {
460 let mut point = selection.head();
461 if !selection.reversed && !selection.is_empty() {
462 point = movement::left(map, selection.head());
463 }
464 selection.collapse_to(point, selection.goal)
465 } else if !last_mode.is_visual() && mode.is_visual() {
466 if selection.is_empty() {
467 selection.end = movement::right(map, selection.start);
468 }
469 } else if last_mode == Mode::Replace {
470 if selection.head().column() != 0 {
471 let point = movement::left(map, selection.head());
472 selection.collapse_to(point, selection.goal)
473 }
474 }
475 });
476 })
477 });
478 }
479
480 fn push_count_digit(&mut self, number: usize, cx: &mut WindowContext) {
481 if self.active_operator().is_some() {
482 self.update_state(|state| {
483 state.post_count = Some(state.post_count.unwrap_or(0) * 10 + number)
484 })
485 } else {
486 self.update_state(|state| {
487 state.pre_count = Some(state.pre_count.unwrap_or(0) * 10 + number)
488 })
489 }
490 // update the keymap so that 0 works
491 self.sync_vim_settings(cx)
492 }
493
494 fn take_count(&mut self, cx: &mut WindowContext) -> Option<usize> {
495 if self.workspace_state.replaying {
496 return self.workspace_state.recorded_count;
497 }
498
499 let count = if self.state().post_count == None && self.state().pre_count == None {
500 return None;
501 } else {
502 Some(self.update_state(|state| {
503 state.post_count.take().unwrap_or(1) * state.pre_count.take().unwrap_or(1)
504 }))
505 };
506 if self.workspace_state.recording {
507 self.workspace_state.recorded_count = count;
508 }
509 self.sync_vim_settings(cx);
510 count
511 }
512
513 fn push_operator(&mut self, operator: Operator, cx: &mut WindowContext) {
514 if matches!(
515 operator,
516 Operator::Change | Operator::Delete | Operator::Replace
517 ) {
518 self.start_recording(cx)
519 };
520 self.update_state(|state| state.operator_stack.push(operator));
521 self.sync_vim_settings(cx);
522 }
523
524 fn maybe_pop_operator(&mut self) -> Option<Operator> {
525 self.update_state(|state| state.operator_stack.pop())
526 }
527
528 fn pop_operator(&mut self, cx: &mut WindowContext) -> Operator {
529 let popped_operator = self.update_state(|state| state.operator_stack.pop())
530 .expect("Operator popped when no operator was on the stack. This likely means there is an invalid keymap config");
531 self.sync_vim_settings(cx);
532 popped_operator
533 }
534
535 fn clear_operator(&mut self, cx: &mut WindowContext) {
536 self.take_count(cx);
537 self.update_state(|state| state.operator_stack.clear());
538 self.sync_vim_settings(cx);
539 }
540
541 fn active_operator(&self) -> Option<Operator> {
542 self.state().operator_stack.last().cloned()
543 }
544
545 fn transaction_begun(&mut self, transaction_id: TransactionId, _: &mut WindowContext) {
546 self.update_state(|state| {
547 let mode = if (state.mode == Mode::Insert
548 || state.mode == Mode::Replace
549 || state.mode == Mode::Normal)
550 && state.current_tx.is_none()
551 {
552 state.current_tx = Some(transaction_id);
553 state.last_mode
554 } else {
555 state.mode
556 };
557 if mode == Mode::VisualLine || mode == Mode::VisualBlock {
558 state.undo_modes.insert(transaction_id, mode);
559 }
560 });
561 }
562
563 fn transaction_undone(&mut self, transaction_id: &TransactionId, cx: &mut WindowContext) {
564 if !self.state().mode.is_visual() {
565 return;
566 };
567 self.update_active_editor(cx, |vim, editor, cx| {
568 let original_mode = vim.state().undo_modes.get(transaction_id);
569 editor.change_selections(None, cx, |s| match original_mode {
570 Some(Mode::VisualLine) => {
571 s.move_with(|map, selection| {
572 selection.collapse_to(
573 map.prev_line_boundary(selection.start.to_point(map)).1,
574 SelectionGoal::None,
575 )
576 });
577 }
578 Some(Mode::VisualBlock) => {
579 let mut first = s.first_anchor();
580 first.collapse_to(first.start, first.goal);
581 s.select_anchors(vec![first]);
582 }
583 _ => {
584 s.move_with(|_, selection| {
585 selection.collapse_to(selection.start, selection.goal);
586 });
587 }
588 });
589 });
590 self.switch_mode(Mode::Normal, true, cx)
591 }
592
593 fn local_selections_changed(
594 &mut self,
595 newest: Selection<editor::Anchor>,
596 is_multicursor: bool,
597 cx: &mut WindowContext,
598 ) {
599 let state = self.state();
600 if state.mode == Mode::Insert && state.current_tx.is_some() {
601 if state.current_anchor.is_none() {
602 self.update_state(|state| state.current_anchor = Some(newest));
603 } else if state.current_anchor.as_ref().unwrap() != &newest {
604 if let Some(tx_id) = self.update_state(|state| state.current_tx.take()) {
605 self.update_active_editor(cx, |_, editor, cx| {
606 editor.group_until_transaction(tx_id, cx)
607 });
608 }
609 }
610 } else if state.mode == Mode::Normal && newest.start != newest.end {
611 if matches!(newest.goal, SelectionGoal::HorizontalRange { .. }) {
612 self.switch_mode(Mode::VisualBlock, false, cx);
613 } else {
614 self.switch_mode(Mode::Visual, false, cx)
615 }
616 } else if newest.start == newest.end
617 && !is_multicursor
618 && [Mode::Visual, Mode::VisualLine, Mode::VisualBlock].contains(&state.mode)
619 {
620 self.switch_mode(Mode::Normal, true, cx)
621 }
622 }
623
624 fn active_editor_input_ignored(text: Arc<str>, cx: &mut WindowContext) {
625 if text.is_empty() {
626 return;
627 }
628
629 match Vim::read(cx).active_operator() {
630 Some(Operator::FindForward { before }) => {
631 let find = Motion::FindForward {
632 before,
633 char: text.chars().next().unwrap(),
634 mode: if VimSettings::get_global(cx).use_multiline_find {
635 FindRange::MultiLine
636 } else {
637 FindRange::SingleLine
638 },
639 smartcase: VimSettings::get_global(cx).use_smartcase_find,
640 };
641 Vim::update(cx, |vim, _| {
642 vim.workspace_state.last_find = Some(find.clone())
643 });
644 motion::motion(find, cx)
645 }
646 Some(Operator::FindBackward { after }) => {
647 let find = Motion::FindBackward {
648 after,
649 char: text.chars().next().unwrap(),
650 mode: if VimSettings::get_global(cx).use_multiline_find {
651 FindRange::MultiLine
652 } else {
653 FindRange::SingleLine
654 },
655 smartcase: VimSettings::get_global(cx).use_smartcase_find,
656 };
657 Vim::update(cx, |vim, _| {
658 vim.workspace_state.last_find = Some(find.clone())
659 });
660 motion::motion(find, cx)
661 }
662 Some(Operator::Replace) => match Vim::read(cx).state().mode {
663 Mode::Normal => normal_replace(text, cx),
664 Mode::Visual | Mode::VisualLine | Mode::VisualBlock => visual_replace(text, cx),
665 _ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
666 },
667 Some(Operator::AddSurrounds { target }) => match Vim::read(cx).state().mode {
668 Mode::Normal => {
669 if let Some(target) = target {
670 add_surrounds(text, target, cx);
671 Vim::update(cx, |vim, cx| vim.clear_operator(cx));
672 }
673 }
674 _ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
675 },
676 Some(Operator::ChangeSurrounds { target }) => match Vim::read(cx).state().mode {
677 Mode::Normal => {
678 if let Some(target) = target {
679 change_surrounds(text, target, cx);
680 Vim::update(cx, |vim, cx| vim.clear_operator(cx));
681 }
682 }
683 _ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
684 },
685 Some(Operator::DeleteSurrounds) => match Vim::read(cx).state().mode {
686 Mode::Normal => {
687 delete_surrounds(text, cx);
688 Vim::update(cx, |vim, cx| vim.clear_operator(cx));
689 }
690 _ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
691 },
692 _ => match Vim::read(cx).state().mode {
693 Mode::Replace => multi_replace(text, cx),
694 _ => {}
695 },
696 }
697 }
698
699 fn set_enabled(&mut self, enabled: bool, cx: &mut AppContext) {
700 if self.enabled == enabled {
701 return;
702 }
703 if !enabled {
704 CommandPaletteInterceptor::update_global(cx, |interceptor, _| {
705 interceptor.clear();
706 });
707 CommandPaletteFilter::update_global(cx, |filter, _| {
708 filter.hide_namespace(Self::NAMESPACE);
709 });
710 *self = Default::default();
711 return;
712 }
713
714 self.enabled = true;
715 CommandPaletteFilter::update_global(cx, |filter, _| {
716 filter.show_namespace(Self::NAMESPACE);
717 });
718 CommandPaletteInterceptor::update_global(cx, |interceptor, _| {
719 interceptor.set(Box::new(command::command_interceptor));
720 });
721
722 if let Some(active_window) = cx
723 .active_window()
724 .and_then(|window| window.downcast::<Workspace>())
725 {
726 active_window
727 .update(cx, |workspace, cx| {
728 let active_editor = workspace.active_item_as::<Editor>(cx);
729 if let Some(active_editor) = active_editor {
730 self.activate_editor(active_editor, cx);
731 self.switch_mode(Mode::Normal, false, cx);
732 }
733 })
734 .ok();
735 }
736 }
737
738 /// Returns the state of the active editor.
739 pub fn state(&self) -> &EditorState {
740 if let Some(active_editor) = self.active_editor.as_ref() {
741 if let Some(state) = self.editor_states.get(&active_editor.entity_id()) {
742 return state;
743 }
744 }
745
746 &self.default_state
747 }
748
749 /// Updates the state of the active editor.
750 pub fn update_state<T>(&mut self, func: impl FnOnce(&mut EditorState) -> T) -> T {
751 let mut state = self.state().clone();
752 let ret = func(&mut state);
753
754 if let Some(active_editor) = self.active_editor.as_ref() {
755 self.editor_states.insert(active_editor.entity_id(), state);
756 }
757
758 ret
759 }
760
761 fn sync_vim_settings(&mut self, cx: &mut WindowContext) {
762 self.update_active_editor(cx, |vim, editor, cx| {
763 let state = vim.state();
764 editor.set_cursor_shape(state.cursor_shape(), cx);
765 editor.set_clip_at_line_ends(state.clip_at_line_ends(), cx);
766 editor.set_collapse_matches(true);
767 editor.set_input_enabled(!state.vim_controlled());
768 editor.set_autoindent(state.should_autoindent());
769 editor.selections.line_mode = matches!(state.mode, Mode::VisualLine);
770 if editor.is_focused(cx) {
771 editor.set_keymap_context_layer::<Self>(state.keymap_context_layer(), cx);
772 // disables vim if the rename editor is focused,
773 // but not if the command palette is open.
774 } else if editor.focus_handle(cx).contains_focused(cx) {
775 editor.remove_keymap_context_layer::<Self>(cx)
776 }
777 });
778 }
779
780 fn unhook_vim_settings(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
781 if editor.mode() == EditorMode::Full {
782 editor.set_cursor_shape(CursorShape::Bar, cx);
783 editor.set_clip_at_line_ends(false, cx);
784 editor.set_collapse_matches(false);
785 editor.set_input_enabled(true);
786 editor.set_autoindent(true);
787 editor.selections.line_mode = false;
788 }
789 editor.remove_keymap_context_layer::<Self>(cx)
790 }
791}
792
793impl Settings for VimModeSetting {
794 const KEY: Option<&'static str> = Some("vim_mode");
795
796 type FileContent = Option<bool>;
797
798 fn load(sources: SettingsSources<Self::FileContent>, _: &mut AppContext) -> Result<Self> {
799 Ok(Self(sources.user.copied().flatten().unwrap_or(
800 sources.default.ok_or_else(Self::missing_default)?,
801 )))
802 }
803}
804
805/// Controls when to use system clipboard.
806#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
807#[serde(rename_all = "snake_case")]
808pub enum UseSystemClipboard {
809 /// Don't use system clipboard.
810 Never,
811 /// Use system clipboard.
812 Always,
813 /// Use system clipboard for yank operations.
814 OnYank,
815}
816
817#[derive(Deserialize)]
818struct VimSettings {
819 // all vim uses vim clipboard
820 // vim always uses system cliupbaord
821 // some magic where yy is system and dd is not.
822 pub use_system_clipboard: UseSystemClipboard,
823 pub use_multiline_find: bool,
824 pub use_smartcase_find: bool,
825}
826
827#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
828struct VimSettingsContent {
829 pub use_system_clipboard: Option<UseSystemClipboard>,
830 pub use_multiline_find: Option<bool>,
831 pub use_smartcase_find: Option<bool>,
832}
833
834impl Settings for VimSettings {
835 const KEY: Option<&'static str> = Some("vim");
836
837 type FileContent = VimSettingsContent;
838
839 fn load(sources: SettingsSources<Self::FileContent>, _: &mut AppContext) -> Result<Self> {
840 sources.json_merge()
841 }
842}