nvim_gtk/plug_manager/
vim_plug.rs

1use std::rc::Rc;
2
3use neovim_lib::{NeovimApi, NeovimApiAsync};
4
5use crate::nvim::{NeovimClient, ErrorReport, NeovimRef};
6use crate::value::ValueMapExt;
7
8pub struct Manager {
9    nvim: Option<Rc<NeovimClient>>,
10}
11
12impl Manager {
13    pub fn new() -> Self {
14        Manager { nvim: None }
15    }
16
17    pub fn initialize(&mut self, nvim: Rc<NeovimClient>) {
18        self.nvim = Some(nvim);
19    }
20
21    fn nvim(&self) -> Option<NeovimRef> {
22        self.nvim.as_ref().unwrap().nvim()
23    }
24
25    pub fn get_plugs(&self) -> Result<Box<[VimPlugInfo]>, String> {
26        if let Some(mut nvim) = self.nvim() {
27            let g_plugs = nvim.eval("g:plugs").map_err(|e| {
28                format!("Can't retrive g:plugs map: {}", e)
29            })?;
30
31            let plugs_map = g_plugs
32                .as_map()
33                .ok_or_else(|| "Can't retrive g:plugs map".to_owned())?
34                .to_attrs_map()?;
35
36            let g_plugs_order = nvim.eval("g:plugs_order").map_err(|e| format!("{}", e))?;
37
38            let order_arr = g_plugs_order.as_array().ok_or_else(
39                || "Can't find g:plugs_order array"
40                    .to_owned(),
41            )?;
42
43            let plugs_info: Vec<VimPlugInfo> = order_arr
44                .iter()
45                .map(|n| n.as_str())
46                .filter_map(|name| if let Some(name) = name {
47                    plugs_map
48                        .get(name)
49                        .and_then(|desc| desc.as_map())
50                        .and_then(|desc| desc.to_attrs_map().ok())
51                        .and_then(|desc| {
52                            let uri = desc.get("uri").and_then(|uri| uri.as_str());
53                            if let Some(uri) = uri {
54                                Some(VimPlugInfo::new(name.to_owned(), uri.to_owned()))
55                            } else {
56                                None
57                            }
58                        })
59                } else {
60                    None
61                })
62                .collect();
63            Ok(plugs_info.into_boxed_slice())
64        } else {
65            Err("Nvim not initialized".to_owned())
66        }
67    }
68
69    pub fn is_loaded(&self) -> bool {
70        if let Some(mut nvim) = self.nvim() {
71            let loaded_plug = nvim.eval("exists('g:loaded_plug')");
72            loaded_plug
73                .ok_and_report()
74                .and_then(|loaded_plug| loaded_plug.as_i64())
75                .map_or(false, |loaded_plug| loaded_plug > 0)
76        } else {
77            false
78        }
79    }
80
81    pub fn reload(&self, path: &str) {
82        if let Some(mut nvim) = self.nvim() {
83            nvim.command_async(&format!("source {}", path))
84                .cb(|r| r.report_err())
85                .call()
86        }
87    }
88}
89
90#[derive(Debug)]
91pub struct VimPlugInfo {
92    pub name: String,
93    pub uri: String,
94}
95
96impl VimPlugInfo {
97    pub fn new(name: String, uri: String) -> Self {
98        VimPlugInfo { name, uri }
99    }
100}