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
185
186
187
188
use std::cell::{Cell, RefCell, RefMut};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, MutexGuard};

use super::ErrorReport;
use neovim_lib::{Neovim, NeovimApi};

#[derive(Clone, Copy, PartialEq)]
enum NeovimClientState {
    Uninitialized,
    InitInProgress,
    Initialized,
    Error,
}

pub enum NeovimRef<'a> {
    SingleThreaded(RefMut<'a, Neovim>),
    MultiThreaded(MutexGuard<'a, Option<Neovim>>),
}

impl<'a> NeovimRef<'a> {
    fn from_nvim(nvim: RefMut<'a, Neovim>) -> Self {
        NeovimRef::SingleThreaded(nvim)
    }

    fn try_nvim_async(nvim_async: &'a NeovimClientAsync) -> Option<NeovimRef<'a>> {
        let guard = nvim_async.nvim.try_lock();

        if let Ok(guard) = guard {
            if guard.is_some() {
                return Some(NeovimRef::MultiThreaded(guard));
            }
        }

        None
    }

    fn from_nvim_async(nvim_async: &'a NeovimClientAsync) -> Option<NeovimRef<'a>> {
        let guard = nvim_async.nvim.lock().unwrap();

        if guard.is_some() {
            Some(NeovimRef::MultiThreaded(guard))
        } else {
            None
        }
    }

    pub fn non_blocked(mut self) -> Option<Self> {
        self.get_mode().ok_and_report().and_then(|mode| {
            mode.iter()
                .find(|kv| kv.0.as_str().map(|key| key == "blocking").unwrap_or(false))
                .map(|kv| kv.1.as_bool().unwrap_or(false))
                .and_then(|block| if block { None } else { Some(self) })
        })
    }
}

impl<'a> Deref for NeovimRef<'a> {
    type Target = Neovim;

    fn deref(&self) -> &Neovim {
        match *self {
            NeovimRef::SingleThreaded(ref nvim) => &*nvim,
            NeovimRef::MultiThreaded(ref nvim) => (&*nvim).as_ref().unwrap(),
        }
    }
}

impl<'a> DerefMut for NeovimRef<'a> {
    fn deref_mut(&mut self) -> &mut Neovim {
        match *self {
            NeovimRef::SingleThreaded(ref mut nvim) => &mut *nvim,
            NeovimRef::MultiThreaded(ref mut nvim) => (&mut *nvim).as_mut().unwrap(),
        }
    }
}

pub struct NeovimClientAsync {
    nvim: Arc<Mutex<Option<Neovim>>>,
}

impl NeovimClientAsync {
    fn new() -> Self {
        NeovimClientAsync {
            nvim: Arc::new(Mutex::new(None)),
        }
    }

    pub fn borrow(&self) -> Option<NeovimRef> {
        NeovimRef::from_nvim_async(self)
    }

    pub fn try_borrow(&self) -> Option<NeovimRef> {
        NeovimRef::try_nvim_async(self)
    }
}

impl Clone for NeovimClientAsync {
    fn clone(&self) -> Self {
        NeovimClientAsync {
            nvim: self.nvim.clone(),
        }
    }
}

pub struct NeovimClient {
    state: Cell<NeovimClientState>,
    nvim: RefCell<Option<Neovim>>,
    nvim_async: NeovimClientAsync,
}

impl NeovimClient {
    pub fn new() -> Self {
        NeovimClient {
            state: Cell::new(NeovimClientState::Uninitialized),
            nvim: RefCell::new(None),
            nvim_async: NeovimClientAsync::new(),
        }
    }

    pub fn clear(&self) {
        let mut nvim = self.nvim.borrow_mut();
        if nvim.is_some() {
            nvim.take();
        } else {
            self.nvim_async.nvim.lock().unwrap().take();
        }
    }

    pub fn async_to_sync(&self) {
        let mut lock = self.nvim_async.nvim.lock().unwrap();
        let nvim = lock.take().unwrap();
        *self.nvim.borrow_mut() = Some(nvim);
    }

    pub fn set_nvim_async(&self, nvim: Neovim) -> NeovimClientAsync {
        *self.nvim_async.nvim.lock().unwrap() = Some(nvim);
        self.nvim_async.clone()
    }

    pub fn set_initialized(&self) {
        self.state.set(NeovimClientState::Initialized);
    }

    pub fn set_error(&self) {
        self.state.set(NeovimClientState::Error);
    }

    pub fn set_in_progress(&self) {
        self.state.set(NeovimClientState::InitInProgress);
    }

    pub fn is_initialized(&self) -> bool {
        self.state.get() == NeovimClientState::Initialized
    }

    pub fn is_uninitialized(&self) -> bool {
        self.state.get() == NeovimClientState::Uninitialized
    }

    pub fn is_initializing(&self) -> bool {
        self.state.get() == NeovimClientState::InitInProgress
    }

    /// In case neovimref locked in another thread
    /// this method can return None
    pub fn try_nvim(&self) -> Option<NeovimRef> {
        let nvim = self.nvim.borrow_mut();
        if nvim.is_some() {
            Some(NeovimRef::from_nvim(RefMut::map(nvim, |n| {
                n.as_mut().unwrap()
            })))
        } else {
            self.nvim_async.try_borrow()
        }
    }

    pub fn nvim(&self) -> Option<NeovimRef> {
        let nvim = self.nvim.borrow_mut();
        if nvim.is_some() {
            Some(NeovimRef::from_nvim(RefMut::map(nvim, |n| {
                n.as_mut().unwrap()
            })))
        } else {
            self.nvim_async.borrow()
        }
    }
}