1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::result;
use std::net::TcpStream;
use std::io::Result;
use std::io::{Error, ErrorKind, Stdin, Stdout};
use std::process::Stdio;
use std::process::{Command, Child, ChildStdin, ChildStdout};
use std::thread::JoinHandle;
use std::time::Duration;

use std::path::Path;
#[cfg(unix)]
use unix_socket::UnixStream;

use rpc;
use rpc::handler::Handler;
use rpc::Client;

use async::AsyncCall;

use rmpv::Value;


/// An active Neovim session.
pub struct Session {
    client: ClientConnection,
    timeout: Option<Duration>,
}

macro_rules! call_args {
    () => (Vec::new());
    ($($e:expr), +,) => (call_args![$($e),*]);
    ($($e:expr), +) => {{
        let mut vec = Vec::new();
        $(
            vec.push($e.into_val());
        )*
        vec
    }};
}

impl Session {
    /// Connect to nvim instance via tcp
    pub fn new_tcp(addr: &str) -> Result<Session> {
        let stream = TcpStream::connect(addr)?;
        let read = stream.try_clone()?;
        Ok(Session {
            client: ClientConnection::Tcp(Client::new(stream, read)),
            timeout: Some(Duration::new(5, 0)),
        })
    }

    #[cfg(unix)]
    /// Connect to nvim instance via unix socket
    pub fn new_unix_socket<P: AsRef<Path>>(path: P) -> Result<Session> {
        let stream = UnixStream::connect(path)?;
        let read = stream.try_clone()?;
        Ok(Session {
            client: ClientConnection::UnixSocket(Client::new(stream, read)),
            timeout: Some(Duration::new(5, 0)),
        })
    }

    /// Connect to a Neovim instance by spawning a new one.
    pub fn new_child() -> Result<Session> {
        if cfg!(target_os = "windows") {
            Self::new_child_path("nvim.exe")
        } else {
            Self::new_child_path("nvim")
        }
    }

    /// Connect to a Neovim instance by spawning a new one
    pub fn new_child_path<S: AsRef<Path>>(program: S) -> Result<Session> {
        Self::new_child_cmd(Command::new(program.as_ref()).arg("--embed"))
    }

    /// Connect to a Neovim instance by spawning a new one
    ///
    /// stdin/stdout settings will be rewrited to `Stdio::piped()`
    pub fn new_child_cmd(cmd: &mut Command) -> Result<Session> {
         let mut child = cmd
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()?;
        let stdout = child.stdout
            .take()
            .ok_or_else(|| Error::new(ErrorKind::Other, "Can't open stdout"))?;
        let stdin = child.stdin
            .take()
            .ok_or_else(|| Error::new(ErrorKind::Other, "Can't open stdin"))?;

        Ok(Session {
            client: ClientConnection::Child(Client::new(stdout, stdin), child),
            timeout: Some(Duration::new(5, 0)),
        })
    }

    /// Connect to a Neovim instance that spawned this process over stdin/stdout.
    pub fn new_parent() -> Result<Session> {
        use std::io;

        Ok(Session {
            client: ClientConnection::Parent(Client::new(io::stdin(), io::stdout())),
            timeout: Some(Duration::new(5, 0)),
        })
    }

    /// Set call timeout
    pub fn set_timeout(&mut self, timeout: Duration) {
        self.timeout = Some(timeout);
    }

    pub fn set_infinity_timeout(&mut self) {
        self.timeout = None;
    }

    /// Start processing rpc response and notifications
    pub fn start_event_loop_handler<H>(&mut self, handler: H)
        where H: Handler + Send + 'static
    {
        match self.client {
            ClientConnection::Child(ref mut client, _) => client.start_event_loop_handler(handler),
            ClientConnection::Parent(ref mut client) => client.start_event_loop_handler(handler),
            ClientConnection::Tcp(ref mut client) => client.start_event_loop_handler(handler),

            #[cfg(unix)]
            ClientConnection::UnixSocket(ref mut client) => {
                client.start_event_loop_handler(handler)
            }
        }
    }

    /// Start processing rpc response and notifications
    pub fn start_event_loop(&mut self) {
        match self.client {
            ClientConnection::Child(ref mut client, _) => client.start_event_loop(),
            ClientConnection::Parent(ref mut client) => client.start_event_loop(),
            ClientConnection::Tcp(ref mut client) => client.start_event_loop(),

            #[cfg(unix)]
            ClientConnection::UnixSocket(ref mut client) => client.start_event_loop(),
        }
    }

    /// Sync call. Call can be made only after event loop begin processing
    pub fn call(&mut self, method: &str, args: Vec<Value>) -> result::Result<Value, Value> {
        match self.client {
            ClientConnection::Child(ref mut client, _) => client.call(method, args, self.timeout),
            ClientConnection::Parent(ref mut client) => client.call(method, args, self.timeout),
            ClientConnection::Tcp(ref mut client) => client.call(method, args, self.timeout),

            #[cfg(unix)]
            ClientConnection::UnixSocket(ref mut client) => client.call(method, args, self.timeout),
        }
    }

    /// Create async call will be executed when only after call() function.
    pub fn call_async<R: rpc::FromVal<Value>>(&mut self, method: &str, args: Vec<Value>) -> AsyncCall<R> {
        AsyncCall::new(&mut self.client, method.to_owned(), args)
    }

    /// Wait dispatch thread to finish.
    ///
    /// This can happens in case child process connection is lost for some reason.
    pub fn take_dispatch_guard(&mut self) -> JoinHandle<()> {
        match self.client {
            ClientConnection::Child(ref mut client, _) => client.take_dispatch_guard(),
            ClientConnection::Parent(ref mut client) => client.take_dispatch_guard(),
            ClientConnection::Tcp(ref mut client) => client.take_dispatch_guard(),

            #[cfg(unix)]
            ClientConnection::UnixSocket(ref mut client) => client.take_dispatch_guard(),
        }
    }
}

pub enum ClientConnection {
    Child(Client<ChildStdout, ChildStdin>, Child),
    Parent(Client<Stdin, Stdout>),
    Tcp(Client<TcpStream, TcpStream>),

    #[cfg(unix)]
    UnixSocket(Client<UnixStream, UnixStream>),
}