Skip to main content

reovim_module_vim/visual/
exit.rs

1//! Visual mode exit commands.
2//!
3//! Provides commands to exit visual mode and return to normal mode.
4
5use {
6    reovim_driver_command::{Command, CommandContext, CommandHandler, CommandResult},
7    reovim_driver_session::{
8        BufferApi, SessionRuntime, TransitionContext,
9        api::{ChangeTracker, ModeApi},
10    },
11    reovim_kernel::api::v1::CommandId,
12};
13
14use crate::{ids, modes::VimMode};
15
16/// Exit visual mode and return to normal mode.
17#[derive(Debug, Clone, Copy, Default)]
18pub struct ExitVisualMode;
19
20impl Command for ExitVisualMode {
21    fn id(&self) -> CommandId {
22        ids::EXIT_VISUAL
23    }
24
25    fn description(&self) -> &'static str {
26        "Exit visual mode and return to normal mode"
27    }
28}
29
30impl CommandHandler for ExitVisualMode {
31    #[cfg_attr(coverage_nightly, coverage(off))]
32    fn execute(&self, runtime: &mut SessionRuntime<'_>, _args: &CommandContext) -> CommandResult {
33        // Clear selection on the active window
34        if let Some(window) = runtime.windows_mut().active_mut() {
35            window.selection = None;
36        }
37
38        // #474: Notify other clients that selection was cleared
39        if let Some(buffer_id) = runtime.active_buffer() {
40            runtime.record_selection_change(buffer_id);
41        }
42
43        // Change to normal mode
44        runtime.set_mode(VimMode::NORMAL_ID, TransitionContext::new());
45
46        CommandResult::Success
47    }
48}