diff --git a/src/bridge/events.rs b/src/bridge/events.rs index f0eabf8..13bd994 100644 --- a/src/bridge/events.rs +++ b/src/bridge/events.rs @@ -1,13 +1,13 @@ +use std::convert::TryInto; use std::error; use std::fmt; -use std::convert::TryInto; use rmpv::Value; use skulpin::skia_safe::Color4f; use crate::editor::EDITOR; +use crate::editor::{Colors, CursorMode, CursorShape, Style}; use crate::error_handling::ResultPanicExplanation; -use crate::editor::{Colors, Style, CursorMode, CursorShape}; #[derive(Debug, Clone)] pub enum EventParseError { @@ -18,7 +18,7 @@ pub enum EventParseError { InvalidI64(Value), InvalidBool(Value), InvalidWindowAnchor(Value), - InvalidEventFormat + InvalidEventFormat, } type Result = std::result::Result; @@ -31,8 +31,10 @@ impl fmt::Display for EventParseError { EventParseError::InvalidU64(value) => write!(f, "invalid u64 format {}", value), EventParseError::InvalidI64(value) => write!(f, "invalid i64 format {}", value), EventParseError::InvalidBool(value) => write!(f, "invalid bool format {}", value), - EventParseError::InvalidWindowAnchor(value) => write!(f, "invalid window anchor format {}", value), - EventParseError::InvalidEventFormat => write!(f, "invalid event format") + EventParseError::InvalidWindowAnchor(value) => { + write!(f, "invalid window anchor format {}", value) + } + EventParseError::InvalidEventFormat => write!(f, "invalid event format"), } } } @@ -47,7 +49,7 @@ impl error::Error for EventParseError { pub struct GridLineCell { pub text: String, pub highlight_id: Option, - pub repeat: Option + pub repeat: Option, } pub type StyledContent = Vec<(u64, String)>; @@ -66,7 +68,7 @@ pub enum MessageKind { ReturnPrompt, QuickFix, SearchCount, - Warning + Warning, } impl MessageKind { @@ -84,7 +86,7 @@ impl MessageKind { "quickfix" => MessageKind::QuickFix, "search_count" => MessageKind::SearchCount, "wmsg" => MessageKind::Warning, - _ => MessageKind::Unknown + _ => MessageKind::Unknown, } } } @@ -101,7 +103,7 @@ pub enum GuiOption { Pumblend(u64), ShowTabLine(u64), TermGuiColors(bool), - Unknown(String, Value) + Unknown(String, Value), } #[derive(Debug)] @@ -109,44 +111,137 @@ pub enum WindowAnchor { NorthWest, NorthEast, SouthWest, - SouthEast + SouthEast, } #[derive(Debug)] pub enum RedrawEvent { - SetTitle { title: String }, - ModeInfoSet { cursor_modes: Vec }, - OptionSet { gui_option: GuiOption }, - ModeChange { mode_index: u64 }, + SetTitle { + title: String, + }, + ModeInfoSet { + cursor_modes: Vec, + }, + OptionSet { + gui_option: GuiOption, + }, + ModeChange { + mode_index: u64, + }, BusyStart, BusyStop, Flush, - Resize { grid: u64, width: u64, height: u64 }, - DefaultColorsSet { colors: Colors }, - HighlightAttributesDefine { id: u64, style: Style }, - GridLine { grid: u64, row: u64, column_start: u64, cells: Vec }, - Clear { grid: u64 }, - CursorGoto { grid: u64, row: u64, column: u64 }, - Scroll { grid: u64, top: u64, bottom: u64, left: u64, right: u64, rows: i64, columns: i64 }, - WindowPosition { grid: u64, window: u64, start_row: u64, start_column: u64, width: u64, height: u64 }, - WindowFloatPosition { grid: u64, window: u64, anchor: WindowAnchor, anchor_grid: u64, anchor_row: u64, anchor_column: u64, focusable: bool }, - WindowExternalPosition { grid: u64, window: u64 }, - WindowHide { grid: u64 }, - WindowClose { grid: u64 }, - MessageSetPosition { grid: u64, row: u64, scrolled: bool, separator_character: String }, - CommandLineShow { content: StyledContent, position: u64, first_character: String, prompt: String, indent: u64, level: u64 }, - CommandLinePosition { position: u64, level: u64 }, - CommandLineSpecialCharacter { character: String, shift: bool, level: u64 }, + Resize { + grid: u64, + width: u64, + height: u64, + }, + DefaultColorsSet { + colors: Colors, + }, + HighlightAttributesDefine { + id: u64, + style: Style, + }, + GridLine { + grid: u64, + row: u64, + column_start: u64, + cells: Vec, + }, + Clear { + grid: u64, + }, + CursorGoto { + grid: u64, + row: u64, + column: u64, + }, + Scroll { + grid: u64, + top: u64, + bottom: u64, + left: u64, + right: u64, + rows: i64, + columns: i64, + }, + WindowPosition { + grid: u64, + window: u64, + start_row: u64, + start_column: u64, + width: u64, + height: u64, + }, + WindowFloatPosition { + grid: u64, + window: u64, + anchor: WindowAnchor, + anchor_grid: u64, + anchor_row: u64, + anchor_column: u64, + focusable: bool, + }, + WindowExternalPosition { + grid: u64, + window: u64, + }, + WindowHide { + grid: u64, + }, + WindowClose { + grid: u64, + }, + MessageSetPosition { + grid: u64, + row: u64, + scrolled: bool, + separator_character: String, + }, + CommandLineShow { + content: StyledContent, + position: u64, + first_character: String, + prompt: String, + indent: u64, + level: u64, + }, + CommandLinePosition { + position: u64, + level: u64, + }, + CommandLineSpecialCharacter { + character: String, + shift: bool, + level: u64, + }, CommandLineHide, - CommandLineBlockShow { lines: Vec }, - CommandLineBlockAppend { line: StyledContent }, + CommandLineBlockShow { + lines: Vec, + }, + CommandLineBlockAppend { + line: StyledContent, + }, CommandLineBlockHide, - MessageShow { kind: MessageKind, content: StyledContent, replace_last: bool }, + MessageShow { + kind: MessageKind, + content: StyledContent, + replace_last: bool, + }, MessageClear, - MessageShowMode { content: StyledContent }, - MessageShowCommand { content: StyledContent }, - MessageRuler { content: StyledContent }, - MessageHistoryShow { entries: Vec<(MessageKind, StyledContent)>} + MessageShowMode { + content: StyledContent, + }, + MessageShowCommand { + content: StyledContent, + }, + MessageRuler { + content: StyledContent, + }, + MessageHistoryShow { + entries: Vec<(MessageKind, StyledContent)>, + }, } fn unpack_color(packed_color: u64) -> Color4f { @@ -158,7 +253,7 @@ fn unpack_color(packed_color: u64) -> Color4f { r: r / 255.0, g: g / 255.0, b: b / 255.0, - a: 1.0 + a: 1.0, } } @@ -177,7 +272,9 @@ fn extract_values>(values: Vec, mut arr: Arr) -> Resu } fn parse_array(array_value: Value) -> Result> { - array_value.try_into().map_err(EventParseError::InvalidArray) + array_value + .try_into() + .map_err(EventParseError::InvalidArray) } fn parse_map(map_value: Value) -> Result> { @@ -185,7 +282,9 @@ fn parse_map(map_value: Value) -> Result> { } fn parse_string(string_value: Value) -> Result { - string_value.try_into().map_err(EventParseError::InvalidString) + string_value + .try_into() + .map_err(EventParseError::InvalidString) } fn parse_u64(u64_value: Value) -> Result { @@ -204,12 +303,13 @@ fn parse_set_title(set_title_arguments: Vec) -> Result { let [title] = extract_values(set_title_arguments, [Value::Nil])?; Ok(RedrawEvent::SetTitle { - title: parse_string(title)? + title: parse_string(title)?, }) } fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result { - let [_cursor_style_enabled, mode_info] = extract_values(mode_info_set_arguments, [Value::Nil, Value::Nil])?; + let [_cursor_style_enabled, mode_info] = + extract_values(mode_info_set_arguments, [Value::Nil, Value::Nil])?; let mode_info_values = parse_array(mode_info)?; let mut cursor_modes = Vec::with_capacity(mode_info_values.len()); @@ -222,22 +322,22 @@ fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result { mode_info.shape = CursorShape::from_type_name(&parse_string(value)?); - }, + } "cell_percentage" => { mode_info.cell_percentage = Some(parse_u64(value)? as f32 / 100.0); - }, + } "blinkwait" => { mode_info.blinkwait = Some(parse_u64(value)?); - }, + } "blinkon" => { mode_info.blinkon = Some(parse_u64(value)?); - }, + } "blinkoff" => { mode_info.blinkoff = Some(parse_u64(value)?); } "attr_id" => { mode_info.style_id = Some(parse_u64(value)?); - }, + } _ => {} } } @@ -245,9 +345,7 @@ fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result) -> Result { @@ -267,8 +365,8 @@ fn parse_option_set(option_set_arguments: Vec) -> Result { "pumblend" => GuiOption::Pumblend(parse_u64(value)?), "showtabline" => GuiOption::ShowTabLine(parse_u64(value)?), "termguicolors" => GuiOption::TermGuiColors(parse_bool(value)?), - _ => GuiOption::Unknown(name, value) - } + _ => GuiOption::Unknown(name, value), + }, }) } @@ -276,30 +374,32 @@ fn parse_mode_change(mode_change_arguments: Vec) -> Result { let [_mode, mode_index] = extract_values(mode_change_arguments, [Value::Nil, Value::Nil])?; Ok(RedrawEvent::ModeChange { - mode_index: parse_u64(mode_index)? + mode_index: parse_u64(mode_index)?, }) } fn parse_grid_resize(grid_resize_arguments: Vec) -> Result { - let [grid_id, width, height] = extract_values(grid_resize_arguments, [Value::Nil, Value::Nil, Value::Nil])?; + let [grid_id, width, height] = + extract_values(grid_resize_arguments, [Value::Nil, Value::Nil, Value::Nil])?; Ok(RedrawEvent::Resize { grid: parse_u64(grid_id)?, width: parse_u64(width)?, - height: parse_u64(height)? + height: parse_u64(height)?, }) } fn parse_default_colors(default_colors_arguments: Vec) -> Result { let values = [Value::Nil, Value::Nil, Value::Nil, Value::Nil, Value::Nil]; - let [foreground, background, special, _term_foreground, _term_background] = extract_values(default_colors_arguments, values)?; + let [foreground, background, special, _term_foreground, _term_background] = + extract_values(default_colors_arguments, values)?; Ok(RedrawEvent::DefaultColorsSet { colors: Colors { foreground: Some(unpack_color(parse_u64(foreground)?)), background: Some(unpack_color(parse_u64(background)?)), special: Some(unpack_color(parse_u64(special)?)), - } + }, }) } @@ -311,17 +411,25 @@ fn parse_style(style_map: Value) -> Result