From 90aebcfa0fa162169cf551e454a41cd9e14fad54 Mon Sep 17 00:00:00 2001 From: Kris Hjorth Date: Thu, 9 Jun 2022 00:23:49 +0200 Subject: [PATCH 1/2] Get channel from nvim.get_api_info(). I don't know the history behind the channel list parsing, but this seems to be a lot cleaner. There also seems to be a race condition in the parsing approach, which often causes the channel to not be detected when reconnecting to a remote Neovim instance. --- src/bridge/events.rs | 113 ------------------------------------------- src/bridge/setup.rs | 16 ++---- 2 files changed, 3 insertions(+), 126 deletions(-) diff --git a/src/bridge/events.rs b/src/bridge/events.rs index 731daee..918e202 100644 --- a/src/bridge/events.rs +++ b/src/bridge/events.rs @@ -276,48 +276,6 @@ pub enum RedrawEvent { }, } -#[derive(Debug)] -pub enum ChannelStreamType { - Stdio, - Stderr, - Socket, - Job, -} - -impl Default for ChannelStreamType { - fn default() -> Self { - Self::Stdio - } -} - -#[derive(Debug)] -pub enum ChannelMode { - Bytes, - Terminal, - Rpc, -} - -impl Default for ChannelMode { - fn default() -> Self { - Self::Bytes - } -} - -#[derive(Debug, Default)] -pub struct ClientInfo { - pub name: String, -} - -#[derive(Debug, Default)] -pub struct ChannelInfo { - pub id: u64, - pub stream: ChannelStreamType, - pub mode: ChannelMode, - pub pty: Option, - pub buffer: Option, - pub client: Option, -} - fn unpack_color(packed_color: u64) -> Color4f { let packed_color = packed_color as u32; let r = ((packed_color & 0x00ff_0000) >> 16) as f32; @@ -919,74 +877,3 @@ pub fn parse_redraw_event(event_value: Value) -> Result> { Ok(parsed_events) } - -pub fn parse_channel_stream_type(channel_stream_value: Value) -> Result { - match parse_string(channel_stream_value)?.as_ref() { - "stdio" => Ok(ChannelStreamType::Stdio), - "stderr" => Ok(ChannelStreamType::Stderr), - "socket" => Ok(ChannelStreamType::Socket), - "job" => Ok(ChannelStreamType::Job), - stream_type => Err(ParseError::Format(format!("{:?}", stream_type))), - } -} - -pub fn parse_channel_mode(channel_mode_value: Value) -> Result { - match parse_string(channel_mode_value)?.as_ref() { - "bytes" => Ok(ChannelMode::Bytes), - "terminal" => Ok(ChannelMode::Terminal), - "rpc" => Ok(ChannelMode::Rpc), - channel_mode => Err(ParseError::Format(format!("{:?}", channel_mode))), - } -} - -pub fn parse_client_info(client_info_value: Value) -> Result { - let client_info_map = parse_map(client_info_value)?; - - let mut client_info = ClientInfo::default(); - - for info_property in client_info_map { - if let (Value::String(name), value) = info_property { - match (name.as_str().unwrap(), value) { - ("name", name) => client_info.name = parse_string(name)?, - _ => debug!("Ignored client type property: {}", name), - } - } else { - debug!("Invalid client info format"); - } - } - - Ok(client_info) -} - -pub fn parse_channel_info(channel_value: Value) -> Result { - let channel_map = parse_map(channel_value)?; - - let mut channel_info = ChannelInfo::default(); - - for channel_property in channel_map { - if let (Value::String(name), value) = channel_property { - match (name.as_str().unwrap(), value) { - ("id", channel_id) => channel_info.id = parse_u64(channel_id)?, - ("stream", stream) => channel_info.stream = parse_channel_stream_type(stream)?, - ("mode", mode) => channel_info.mode = parse_channel_mode(mode)?, - ("pty", pty) => channel_info.pty = Some(parse_string(pty)?), - ("buffer", buffer) => channel_info.buffer = Some(parse_string(buffer)?), - ("client", client_info) => { - channel_info.client = Some(parse_client_info(client_info)?) - } - _ => debug!("Ignored channel info property: {}", name), - } - } else { - debug!("Invalid channel info format"); - } - } - - Ok(channel_info) -} - -pub fn parse_channel_list(channel_infos: Vec) -> Result> { - channel_infos - .into_iter() - .map(parse_channel_info) - .collect::>>() -} diff --git a/src/bridge/setup.rs b/src/bridge/setup.rs index af42d9e..3ad765d 100644 --- a/src/bridge/setup.rs +++ b/src/bridge/setup.rs @@ -3,7 +3,7 @@ use nvim_rs::Neovim; use rmpv::Value; use crate::{ - bridge::{events::*, TxWrapper}, + bridge::TxWrapper, error_handling::ResultPanicExplanation, }; @@ -84,20 +84,10 @@ pub async fn setup_neovide_specific_state(nvim: &Neovim, is_remote: b // Retrieve the channel number for communicating with neovide let neovide_channel: Option = nvim - .list_chans() + .get_api_info() .await .ok() - .and_then(|channel_values| parse_channel_list(channel_values).ok()) - .and_then(|channel_list| { - channel_list.iter().find_map(|channel| match channel { - ChannelInfo { - id, - client: Some(ClientInfo { name, .. }), - .. - } if name == "neovide" => Some(*id), - _ => None, - }) - }); + .and_then(|info| info[0].as_u64()); if let Some(neovide_channel) = neovide_channel { // Record the channel to the log From 1083efb6a5338b3d0263ec1dcb690436c0894bfd Mon Sep 17 00:00:00 2001 From: MultisampledNight Date: Thu, 9 Jun 2022 11:44:09 +0200 Subject: [PATCH 2/2] Run cargo fmt --- src/bridge/setup.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/bridge/setup.rs b/src/bridge/setup.rs index 3ad765d..d4cadb8 100644 --- a/src/bridge/setup.rs +++ b/src/bridge/setup.rs @@ -2,10 +2,7 @@ use log::{info, warn}; use nvim_rs::Neovim; use rmpv::Value; -use crate::{ - bridge::TxWrapper, - error_handling::ResultPanicExplanation, -}; +use crate::{bridge::TxWrapper, error_handling::ResultPanicExplanation}; pub async fn setup_neovide_remote_clipboard(nvim: &Neovim, neovide_channel: u64) { // users can opt-out with