From 46cd8828917b2e7850ccc1639f173849718e76b7 Mon Sep 17 00:00:00 2001 From: exoticus <56480961+exoticus@users.noreply.github.com> Date: Mon, 20 Apr 2020 19:12:05 +0200 Subject: [PATCH] added EditorMode to the editor --- src/bridge/events.rs | 25 ++++++++++++++++++++++++- src/editor/mod.rs | 18 +++++++++++------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/bridge/events.rs b/src/bridge/events.rs index 5e1f037..5cf7adb 100644 --- a/src/bridge/events.rs +++ b/src/bridge/events.rs @@ -114,6 +114,20 @@ pub enum WindowAnchor { SouthEast, } +#[derive(Debug)] +pub enum EditorMode { + // The set of modes reported will change in new versions of Nvim, for + // instance more sub-modes and temporary states might be represented as + // separate modes. (however we can safely do this as these are the main modes) + // for instance if we are in Terminal mode and even though status-line shows Terminal, + // we still get one of these as the _editor_ mode + Normal, + Insert, + Visual, + CmdLine, + Unknown(String), +} + #[derive(Debug)] pub enum RedrawEvent { SetTitle { @@ -126,6 +140,7 @@ pub enum RedrawEvent { gui_option: GuiOption, }, ModeChange { + mode: EditorMode, mode_index: u64, }, MouseOn, @@ -373,9 +388,17 @@ fn parse_option_set(option_set_arguments: Vec) -> Result { } fn parse_mode_change(mode_change_arguments: Vec) -> Result { - let [_mode, mode_index] = extract_values(mode_change_arguments, [Value::Nil, Value::Nil])?; + let [mode, mode_index] = extract_values(mode_change_arguments, [Value::Nil, Value::Nil])?; + let mode_name = parse_string(mode)?; Ok(RedrawEvent::ModeChange { + mode: match mode_name.as_str() { + "normal" => EditorMode::Normal, + "insert" => EditorMode::Insert, + "visual" => EditorMode::Visual, + "cmdline_normal" => EditorMode::CmdLine, + _ => EditorMode::Unknown(mode_name), + }, mode_index: parse_u64(mode_index)?, }) } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 3c816dd..f1439be 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -10,7 +10,7 @@ use parking_lot::Mutex; use skulpin::skia_safe::colors; use unicode_segmentation::UnicodeSegmentation; -use crate::bridge::{GridLineCell, GuiOption, RedrawEvent}; +use crate::bridge::{EditorMode, GridLineCell, GuiOption, RedrawEvent}; use crate::redraw_scheduler::REDRAW_SCHEDULER; use crate::window::window_geometry_or_default; pub use cursor::{Cursor, CursorMode, CursorShape}; @@ -39,7 +39,8 @@ pub struct Editor { pub default_style: Arc