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.
macos-click-through
Kris Hjorth 2 years ago
parent d113a0e2c3
commit 90aebcfa0f

@ -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<String>,
pub buffer: Option<String>,
pub client: Option<ClientInfo>,
}
fn unpack_color(packed_color: u64) -> Color4f { fn unpack_color(packed_color: u64) -> Color4f {
let packed_color = packed_color as u32; let packed_color = packed_color as u32;
let r = ((packed_color & 0x00ff_0000) >> 16) as f32; let r = ((packed_color & 0x00ff_0000) >> 16) as f32;
@ -919,74 +877,3 @@ pub fn parse_redraw_event(event_value: Value) -> Result<Vec<RedrawEvent>> {
Ok(parsed_events) Ok(parsed_events)
} }
pub fn parse_channel_stream_type(channel_stream_value: Value) -> Result<ChannelStreamType> {
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<ChannelMode> {
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<ClientInfo> {
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<ChannelInfo> {
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<Value>) -> Result<Vec<ChannelInfo>> {
channel_infos
.into_iter()
.map(parse_channel_info)
.collect::<Result<Vec<ChannelInfo>>>()
}

@ -3,7 +3,7 @@ use nvim_rs::Neovim;
use rmpv::Value; use rmpv::Value;
use crate::{ use crate::{
bridge::{events::*, TxWrapper}, bridge::TxWrapper,
error_handling::ResultPanicExplanation, error_handling::ResultPanicExplanation,
}; };
@ -84,20 +84,10 @@ pub async fn setup_neovide_specific_state(nvim: &Neovim<TxWrapper>, is_remote: b
// Retrieve the channel number for communicating with neovide // Retrieve the channel number for communicating with neovide
let neovide_channel: Option<u64> = nvim let neovide_channel: Option<u64> = nvim
.list_chans() .get_api_info()
.await .await
.ok() .ok()
.and_then(|channel_values| parse_channel_list(channel_values).ok()) .and_then(|info| info[0].as_u64());
.and_then(|channel_list| {
channel_list.iter().find_map(|channel| match channel {
ChannelInfo {
id,
client: Some(ClientInfo { name, .. }),
..
} if name == "neovide" => Some(*id),
_ => None,
})
});
if let Some(neovide_channel) = neovide_channel { if let Some(neovide_channel) = neovide_channel {
// Record the channel to the log // Record the channel to the log

Loading…
Cancel
Save