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
#![allow(dead_code, unused_variables)]
//! `neovim` is a crate that enables building Neovim plugins with Rust.
//!
//! Neovim is a fork of Vim that enables fully asynchronous communication
//! with plugins that run as external programs, communicating via Msgpack-RPC.
//! This crate provides facilities for connecting to a Neovim instance
//! and making API calls to take actions within the editor.

extern crate mpack;

use std::env;
use std::fmt;
use std::io;
use std::process::Command;

pub use self::metadata::Metadata;
pub use self::session::Session;

mod metadata;
mod session;

/// A function as parsed from `get_api_info()`.
pub struct Function {
    pub name: String,
    pub parameters: Vec<(String, String)>,
    pub return_type: String,
    pub async: bool,
    pub can_fail: bool,
}

impl fmt::Display for Function {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "{name}({params}) -> {return_type} {can_fail}{async}",
            return_type=self.return_type,
            async=if self.async { "async" } else { "" },
            name=self.name,
            params=self.parameters.iter().map(|p| format!("{} {}", p.0, p.1)).collect::<Vec<String>>().join(", "),
            can_fail=if self.can_fail { "[can fail]" } else { "" },
        )
    }
}

/// The result of `get_api_info()`.
pub struct ApiInfo {
    pub functions: Vec<Function>,
}

/// Get API information from Vim by running `nvim --api-info` and parsing the output.
pub fn get_api_info() -> Result<ApiInfo, mpack::ReadError> {
    let cmd = env::var("NVIM_BIN").unwrap_or(String::from("nvim"));
    let output = try!(Command::new(cmd).arg("--api-info").output());
    if !output.status.success() {
        return Err(mpack::ReadError::Io(match output.status.code() {
            Some(code) => io::Error::from_raw_os_error(code),
            None => io::Error::new(io::ErrorKind::Other, "killed by signal"),
        }))
    }

    let mut r = mpack::Reader::new(&output.stdout[..]);
    let dict = try!(r.read_value()).map().unwrap();
    let dict_functions = dict.get_array("functions").unwrap();

    let mut functions = Vec::with_capacity(dict_functions.len());

    for f in dict_functions {
        let f = f.map().unwrap();
        let name = f.get_string("name").unwrap();
        let parameters = f.get_array("parameters").unwrap().into_iter().map(|p| {
            let p = p.array().unwrap();
            (p[0].clone().string().unwrap(), p[1].clone().string().unwrap())
        }).collect();
        let return_type = f.get_string("return_type").unwrap();
        let async = f.get_bool("async").unwrap();
        let can_fail = f.get_bool("can_fail").unwrap_or(false);
        functions.push(Function{
            name: name,
            parameters: parameters,
            return_type: return_type,
            async: async,
            can_fail: can_fail,
        });
    }

    Ok(ApiInfo{
        functions: functions,
    })
}