neovim_connection.rs

  1#[cfg(feature = "neovim")]
  2use std::ops::{Deref, DerefMut};
  3use std::{ops::Range, path::PathBuf};
  4
  5#[cfg(feature = "neovim")]
  6use async_compat::Compat;
  7#[cfg(feature = "neovim")]
  8use async_trait::async_trait;
  9#[cfg(feature = "neovim")]
 10use gpui::keymap_matcher::Keystroke;
 11
 12use language::Point;
 13
 14#[cfg(feature = "neovim")]
 15use nvim_rs::{
 16    create::tokio::new_child_cmd, error::LoopError, Handler, Neovim, UiAttachOptions, Value,
 17};
 18#[cfg(feature = "neovim")]
 19use parking_lot::ReentrantMutex;
 20use serde::{Deserialize, Serialize};
 21#[cfg(feature = "neovim")]
 22use tokio::{
 23    process::{Child, ChildStdin, Command},
 24    task::JoinHandle,
 25};
 26
 27use crate::state::Mode;
 28use collections::VecDeque;
 29
 30// Neovim doesn't like to be started simultaneously from multiple threads. We use this lock
 31// to ensure we are only constructing one neovim connection at a time.
 32#[cfg(feature = "neovim")]
 33static NEOVIM_LOCK: ReentrantMutex<()> = ReentrantMutex::new(());
 34
 35#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
 36pub enum NeovimData {
 37    Put { state: String },
 38    Key(String),
 39    Get { state: String, mode: Option<Mode> },
 40}
 41
 42pub struct NeovimConnection {
 43    data: VecDeque<NeovimData>,
 44    #[cfg(feature = "neovim")]
 45    test_case_id: String,
 46    #[cfg(feature = "neovim")]
 47    nvim: Neovim<nvim_rs::compat::tokio::Compat<ChildStdin>>,
 48    #[cfg(feature = "neovim")]
 49    _join_handle: JoinHandle<Result<(), Box<LoopError>>>,
 50    #[cfg(feature = "neovim")]
 51    _child: Child,
 52}
 53
 54impl NeovimConnection {
 55    pub async fn new(test_case_id: String) -> Self {
 56        #[cfg(feature = "neovim")]
 57        let handler = NvimHandler {};
 58        #[cfg(feature = "neovim")]
 59        let (nvim, join_handle, child) = Compat::new(async {
 60            // Ensure we don't create neovim connections in parallel
 61            let _lock = NEOVIM_LOCK.lock();
 62            let (nvim, join_handle, child) = new_child_cmd(
 63                &mut Command::new("nvim").arg("--embed").arg("--clean"),
 64                handler,
 65            )
 66            .await
 67            .expect("Could not connect to neovim process");
 68
 69            nvim.ui_attach(100, 100, &UiAttachOptions::default())
 70                .await
 71                .expect("Could not attach to ui");
 72
 73            // Makes system act a little more like zed in terms of indentation
 74            nvim.set_option("smartindent", nvim_rs::Value::Boolean(true))
 75                .await
 76                .expect("Could not set smartindent on startup");
 77
 78            (nvim, join_handle, child)
 79        })
 80        .await;
 81
 82        Self {
 83            #[cfg(feature = "neovim")]
 84            data: Default::default(),
 85            #[cfg(not(feature = "neovim"))]
 86            data: Self::read_test_data(&test_case_id),
 87            #[cfg(feature = "neovim")]
 88            test_case_id,
 89            #[cfg(feature = "neovim")]
 90            nvim,
 91            #[cfg(feature = "neovim")]
 92            _join_handle: join_handle,
 93            #[cfg(feature = "neovim")]
 94            _child: child,
 95        }
 96    }
 97
 98    // Sends a keystroke to the neovim process.
 99    #[cfg(feature = "neovim")]
100    pub async fn send_keystroke(&mut self, keystroke_text: &str) {
101        let keystroke = Keystroke::parse(keystroke_text).unwrap();
102        let special = keystroke.shift
103            || keystroke.ctrl
104            || keystroke.alt
105            || keystroke.cmd
106            || keystroke.key.len() > 1;
107        let start = if special { "<" } else { "" };
108        let shift = if keystroke.shift { "S-" } else { "" };
109        let ctrl = if keystroke.ctrl { "C-" } else { "" };
110        let alt = if keystroke.alt { "M-" } else { "" };
111        let cmd = if keystroke.cmd { "D-" } else { "" };
112        let end = if special { ">" } else { "" };
113
114        let key = format!("{start}{shift}{ctrl}{alt}{cmd}{}{end}", keystroke.key);
115
116        self.data
117            .push_back(NeovimData::Key(keystroke_text.to_string()));
118        self.nvim
119            .input(&key)
120            .await
121            .expect("Could not input keystroke");
122    }
123
124    #[cfg(not(feature = "neovim"))]
125    pub async fn send_keystroke(&mut self, keystroke_text: &str) {
126        if matches!(self.data.front(), Some(NeovimData::Get { .. })) {
127            self.data.pop_front();
128        }
129        assert_eq!(
130            self.data.pop_front(),
131            Some(NeovimData::Key(keystroke_text.to_string())),
132            "operation does not match recorded script. re-record with --features=neovim"
133        );
134    }
135
136    #[cfg(feature = "neovim")]
137    pub async fn set_state(&mut self, marked_text: &str) {
138        let (text, selection) = parse_state(&marked_text);
139
140        let nvim_buffer = self
141            .nvim
142            .get_current_buf()
143            .await
144            .expect("Could not get neovim buffer");
145        let lines = text
146            .split('\n')
147            .map(|line| line.to_string())
148            .collect::<Vec<_>>();
149
150        nvim_buffer
151            .set_lines(0, -1, false, lines)
152            .await
153            .expect("Could not set nvim buffer text");
154
155        self.nvim
156            .input("<escape>")
157            .await
158            .expect("Could not send escape to nvim");
159        self.nvim
160            .input("<escape>")
161            .await
162            .expect("Could not send escape to nvim");
163
164        let nvim_window = self
165            .nvim
166            .get_current_win()
167            .await
168            .expect("Could not get neovim window");
169
170        let cursor = selection.start;
171        nvim_window
172            .set_cursor((cursor.row as i64 + 1, cursor.column as i64))
173            .await
174            .expect("Could not set nvim cursor position");
175
176        if !selection.is_empty() {
177            self.nvim
178                .input("v")
179                .await
180                .expect("could not enter visual mode");
181
182            let cursor = selection.end;
183            nvim_window
184                .set_cursor((cursor.row as i64 + 1, cursor.column as i64))
185                .await
186                .expect("Could not set nvim cursor position");
187        }
188
189        if let Some(NeovimData::Get { mode, state }) = self.data.back() {
190            if *mode == Some(Mode::Normal) && *state == marked_text {
191                return;
192            }
193        }
194        self.data.push_back(NeovimData::Put {
195            state: marked_text.to_string(),
196        })
197    }
198
199    #[cfg(not(feature = "neovim"))]
200    pub async fn set_state(&mut self, marked_text: &str) {
201        if let Some(NeovimData::Get { mode, state: text }) = self.data.front() {
202            if *mode == Some(Mode::Normal) && *text == marked_text {
203                return;
204            }
205            self.data.pop_front();
206        }
207        assert_eq!(
208            self.data.pop_front(),
209            Some(NeovimData::Put {
210                state: marked_text.to_string()
211            }),
212            "operation does not match recorded script. re-record with --features=neovim"
213        );
214    }
215
216    #[cfg(feature = "neovim")]
217    pub async fn state(&mut self) -> (Option<Mode>, String, Range<Point>) {
218        let nvim_buffer = self
219            .nvim
220            .get_current_buf()
221            .await
222            .expect("Could not get neovim buffer");
223        let text = nvim_buffer
224            .get_lines(0, -1, false)
225            .await
226            .expect("Could not get buffer text")
227            .join("\n");
228
229        let cursor_row: u32 = self
230            .nvim
231            .command_output("echo line('.')")
232            .await
233            .unwrap()
234            .parse::<u32>()
235            .unwrap()
236            - 1; // Neovim rows start at 1
237        let cursor_col: u32 = self
238            .nvim
239            .command_output("echo col('.')")
240            .await
241            .unwrap()
242            .parse::<u32>()
243            .unwrap()
244            - 1; // Neovim columns start at 1
245
246        let nvim_mode_text = self
247            .nvim
248            .get_mode()
249            .await
250            .expect("Could not get mode")
251            .into_iter()
252            .find_map(|(key, value)| {
253                if key.as_str() == Some("mode") {
254                    Some(value.as_str().unwrap().to_owned())
255                } else {
256                    None
257                }
258            })
259            .expect("Could not find mode value");
260
261        let mode = match nvim_mode_text.as_ref() {
262            "i" => Some(Mode::Insert),
263            "n" => Some(Mode::Normal),
264            "v" => Some(Mode::Visual { line: false }),
265            "V" => Some(Mode::Visual { line: true }),
266            _ => None,
267        };
268
269        let (start, end) = if let Some(Mode::Visual { .. }) = mode {
270            self.nvim
271                .input("<escape>")
272                .await
273                .expect("Could not exit visual mode");
274            let nvim_buffer = self
275                .nvim
276                .get_current_buf()
277                .await
278                .expect("Could not get neovim buffer");
279            let (start_row, start_col) = nvim_buffer
280                .get_mark("<")
281                .await
282                .expect("Could not get selection start");
283            let (end_row, end_col) = nvim_buffer
284                .get_mark(">")
285                .await
286                .expect("Could not get selection end");
287            self.nvim
288                .input("gv")
289                .await
290                .expect("Could not reselect visual selection");
291
292            if cursor_row == start_row as u32 - 1 && cursor_col == start_col as u32 {
293                (
294                    Point::new(end_row as u32 - 1, end_col as u32),
295                    Point::new(start_row as u32 - 1, start_col as u32),
296                )
297            } else {
298                (
299                    Point::new(start_row as u32 - 1, start_col as u32),
300                    Point::new(end_row as u32 - 1, end_col as u32),
301                )
302            }
303        } else {
304            (
305                Point::new(cursor_row, cursor_col),
306                Point::new(cursor_row, cursor_col),
307            )
308        };
309
310        let state = NeovimData::Get {
311            mode,
312            state: encode_range(&text, start..end),
313        };
314
315        if self.data.back() != Some(&state) {
316            self.data.push_back(state.clone());
317        }
318
319        (mode, text, start..end)
320    }
321
322    #[cfg(not(feature = "neovim"))]
323    pub async fn state(&mut self) -> (Option<Mode>, String, Range<Point>) {
324        if let Some(NeovimData::Get { state: text, mode }) = self.data.front() {
325            let (text, range) = parse_state(text);
326            (*mode, text, range)
327        } else {
328            panic!("operation does not match recorded script. re-record with --features=neovim");
329        }
330    }
331
332    pub async fn selection(&mut self) -> Range<Point> {
333        self.state().await.2
334    }
335
336    pub async fn mode(&mut self) -> Option<Mode> {
337        self.state().await.0
338    }
339
340    pub async fn text(&mut self) -> String {
341        self.state().await.1
342    }
343
344    fn test_data_path(test_case_id: &str) -> PathBuf {
345        let mut data_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
346        data_path.push("test_data");
347        data_path.push(format!("{}.json", test_case_id));
348        data_path
349    }
350
351    #[cfg(not(feature = "neovim"))]
352    fn read_test_data(test_case_id: &str) -> VecDeque<NeovimData> {
353        let path = Self::test_data_path(test_case_id);
354        let json = std::fs::read_to_string(path).expect(
355            "Could not read test data. Is it generated? Try running test with '--features neovim'",
356        );
357
358        let mut result = VecDeque::new();
359        for line in json.lines() {
360            result.push_back(
361                serde_json::from_str(line)
362                    .expect("invalid test data. regenerate it with '--features neovim'"),
363            );
364        }
365        result
366    }
367
368    #[cfg(feature = "neovim")]
369    fn write_test_data(test_case_id: &str, data: &VecDeque<NeovimData>) {
370        let path = Self::test_data_path(test_case_id);
371        let mut json = Vec::new();
372        for entry in data {
373            serde_json::to_writer(&mut json, entry).unwrap();
374            json.push(b'\n');
375        }
376        std::fs::create_dir_all(path.parent().unwrap())
377            .expect("could not create test data directory");
378        std::fs::write(path, json).expect("could not write out test data");
379    }
380}
381
382#[cfg(feature = "neovim")]
383impl Deref for NeovimConnection {
384    type Target = Neovim<nvim_rs::compat::tokio::Compat<ChildStdin>>;
385
386    fn deref(&self) -> &Self::Target {
387        &self.nvim
388    }
389}
390
391#[cfg(feature = "neovim")]
392impl DerefMut for NeovimConnection {
393    fn deref_mut(&mut self) -> &mut Self::Target {
394        &mut self.nvim
395    }
396}
397
398#[cfg(feature = "neovim")]
399impl Drop for NeovimConnection {
400    fn drop(&mut self) {
401        Self::write_test_data(&self.test_case_id, &self.data);
402    }
403}
404
405#[cfg(feature = "neovim")]
406#[derive(Clone)]
407struct NvimHandler {}
408
409#[cfg(feature = "neovim")]
410#[async_trait]
411impl Handler for NvimHandler {
412    type Writer = nvim_rs::compat::tokio::Compat<ChildStdin>;
413
414    async fn handle_request(
415        &self,
416        _event_name: String,
417        _arguments: Vec<Value>,
418        _neovim: Neovim<Self::Writer>,
419    ) -> Result<Value, Value> {
420        unimplemented!();
421    }
422
423    async fn handle_notify(
424        &self,
425        _event_name: String,
426        _arguments: Vec<Value>,
427        _neovim: Neovim<Self::Writer>,
428    ) {
429    }
430}
431
432fn parse_state(marked_text: &str) -> (String, Range<Point>) {
433    let (text, ranges) = util::test::marked_text_ranges(marked_text, true);
434    let byte_range = ranges[0].clone();
435    let mut point_range = Point::zero()..Point::zero();
436    let mut ix = 0;
437    let mut position = Point::zero();
438    for c in text.chars().chain(['\0']) {
439        if ix == byte_range.start {
440            point_range.start = position;
441        }
442        if ix == byte_range.end {
443            point_range.end = position;
444        }
445        let len_utf8 = c.len_utf8();
446        ix += len_utf8;
447        if c == '\n' {
448            position.row += 1;
449            position.column = 0;
450        } else {
451            position.column += len_utf8 as u32;
452        }
453    }
454    (text, point_range)
455}
456
457#[cfg(feature = "neovim")]
458fn encode_range(text: &str, range: Range<Point>) -> String {
459    let mut byte_range = 0..0;
460    let mut ix = 0;
461    let mut position = Point::zero();
462    for c in text.chars().chain(['\0']) {
463        if position == range.start {
464            byte_range.start = ix;
465        }
466        if position == range.end {
467            byte_range.end = ix;
468        }
469        let len_utf8 = c.len_utf8();
470        ix += len_utf8;
471        if c == '\n' {
472            position.row += 1;
473            position.column = 0;
474        } else {
475            position.column += len_utf8 as u32;
476        }
477    }
478    util::test::generate_marked_text(text, &[byte_range], true)
479}