neovim_connection.rs

  1use std::path::PathBuf;
  2#[cfg(feature = "neovim")]
  3use std::{
  4    cmp,
  5    ops::{Deref, DerefMut, Range},
  6};
  7
  8#[cfg(feature = "neovim")]
  9use async_compat::Compat;
 10#[cfg(feature = "neovim")]
 11use async_trait::async_trait;
 12#[cfg(feature = "neovim")]
 13use gpui::Keystroke;
 14
 15#[cfg(feature = "neovim")]
 16use language::Point;
 17
 18#[cfg(feature = "neovim")]
 19use nvim_rs::{
 20    create::tokio::new_child_cmd, error::LoopError, Handler, Neovim, UiAttachOptions, Value,
 21};
 22#[cfg(feature = "neovim")]
 23use parking_lot::ReentrantMutex;
 24use serde::{Deserialize, Serialize};
 25#[cfg(feature = "neovim")]
 26use tokio::{
 27    process::{Child, ChildStdin, Command},
 28    task::JoinHandle,
 29};
 30
 31use crate::state::Mode;
 32use collections::VecDeque;
 33
 34// Neovim doesn't like to be started simultaneously from multiple threads. We use this lock
 35// to ensure we are only constructing one neovim connection at a time.
 36#[cfg(feature = "neovim")]
 37static NEOVIM_LOCK: ReentrantMutex<()> = ReentrantMutex::new(());
 38
 39#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
 40pub enum NeovimData {
 41    Put { state: String },
 42    Key(String),
 43    Get { state: String, mode: Option<Mode> },
 44    ReadRegister { name: char, value: String },
 45    Exec { command: String },
 46    SetOption { value: String },
 47}
 48
 49pub struct NeovimConnection {
 50    data: VecDeque<NeovimData>,
 51    #[cfg(feature = "neovim")]
 52    test_case_id: String,
 53    #[cfg(feature = "neovim")]
 54    nvim: Neovim<nvim_rs::compat::tokio::Compat<ChildStdin>>,
 55    #[cfg(feature = "neovim")]
 56    _join_handle: JoinHandle<Result<(), Box<LoopError>>>,
 57    #[cfg(feature = "neovim")]
 58    _child: Child,
 59}
 60
 61impl NeovimConnection {
 62    pub async fn new(test_case_id: String) -> Self {
 63        #[cfg(feature = "neovim")]
 64        let handler = NvimHandler {};
 65        #[cfg(feature = "neovim")]
 66        let (nvim, join_handle, child) = Compat::new(async {
 67            // Ensure we don't create neovim connections in parallel
 68            let _lock = NEOVIM_LOCK.lock();
 69            let (nvim, join_handle, child) = new_child_cmd(
 70                &mut Command::new("nvim")
 71                    .arg("--embed")
 72                    .arg("--clean")
 73                    // disable swap (otherwise after about 1000 test runs you run out of swap file names)
 74                    .arg("-n")
 75                    // disable writing files (just in case)
 76                    .arg("-m"),
 77                handler,
 78            )
 79            .await
 80            .expect("Could not connect to neovim process");
 81
 82            nvim.ui_attach(100, 100, &UiAttachOptions::default())
 83                .await
 84                .expect("Could not attach to ui");
 85
 86            // Makes system act a little more like zed in terms of indentation
 87            nvim.set_option("smartindent", nvim_rs::Value::Boolean(true))
 88                .await
 89                .expect("Could not set smartindent on startup");
 90
 91            (nvim, join_handle, child)
 92        })
 93        .await;
 94
 95        Self {
 96            #[cfg(feature = "neovim")]
 97            data: Default::default(),
 98            #[cfg(not(feature = "neovim"))]
 99            data: Self::read_test_data(&test_case_id),
100            #[cfg(feature = "neovim")]
101            test_case_id,
102            #[cfg(feature = "neovim")]
103            nvim,
104            #[cfg(feature = "neovim")]
105            _join_handle: join_handle,
106            #[cfg(feature = "neovim")]
107            _child: child,
108        }
109    }
110
111    // Sends a keystroke to the neovim process.
112    #[cfg(feature = "neovim")]
113    pub async fn send_keystroke(&mut self, keystroke_text: &str) {
114        let mut keystroke = Keystroke::parse(keystroke_text).unwrap();
115
116        if keystroke.key == "<" {
117            keystroke.key = "lt".to_string()
118        }
119
120        let special = keystroke.modifiers.shift
121            || keystroke.modifiers.control
122            || keystroke.modifiers.alt
123            || keystroke.modifiers.command
124            || keystroke.key.len() > 1;
125        let start = if special { "<" } else { "" };
126        let shift = if keystroke.modifiers.shift { "S-" } else { "" };
127        let ctrl = if keystroke.modifiers.control {
128            "C-"
129        } else {
130            ""
131        };
132        let alt = if keystroke.modifiers.alt { "M-" } else { "" };
133        let cmd = if keystroke.modifiers.command {
134            "D-"
135        } else {
136            ""
137        };
138        let end = if special { ">" } else { "" };
139
140        let key = format!("{start}{shift}{ctrl}{alt}{cmd}{}{end}", keystroke.key);
141
142        self.data
143            .push_back(NeovimData::Key(keystroke_text.to_string()));
144        self.nvim
145            .input(&key)
146            .await
147            .expect("Could not input keystroke");
148    }
149
150    #[cfg(not(feature = "neovim"))]
151    pub async fn send_keystroke(&mut self, keystroke_text: &str) {
152        if matches!(self.data.front(), Some(NeovimData::Get { .. })) {
153            self.data.pop_front();
154        }
155        assert_eq!(
156            self.data.pop_front(),
157            Some(NeovimData::Key(keystroke_text.to_string())),
158            "operation does not match recorded script. re-record with --features=neovim"
159        );
160    }
161
162    #[cfg(feature = "neovim")]
163    pub async fn set_state(&mut self, marked_text: &str) {
164        let (text, selections) = parse_state(&marked_text);
165
166        let nvim_buffer = self
167            .nvim
168            .get_current_buf()
169            .await
170            .expect("Could not get neovim buffer");
171        let lines = text
172            .split('\n')
173            .map(|line| line.to_string())
174            .collect::<Vec<_>>();
175
176        nvim_buffer
177            .set_lines(0, -1, false, lines)
178            .await
179            .expect("Could not set nvim buffer text");
180
181        self.nvim
182            .input("<escape>")
183            .await
184            .expect("Could not send escape to nvim");
185        self.nvim
186            .input("<escape>")
187            .await
188            .expect("Could not send escape to nvim");
189
190        let nvim_window = self
191            .nvim
192            .get_current_win()
193            .await
194            .expect("Could not get neovim window");
195
196        if selections.len() != 1 {
197            panic!("must have one selection");
198        }
199        let selection = &selections[0];
200
201        let cursor = selection.start;
202        nvim_window
203            .set_cursor((cursor.row as i64 + 1, cursor.column as i64))
204            .await
205            .expect("Could not set nvim cursor position");
206
207        if !selection.is_empty() {
208            self.nvim
209                .input("v")
210                .await
211                .expect("could not enter visual mode");
212
213            let cursor = selection.end;
214            nvim_window
215                .set_cursor((cursor.row as i64 + 1, cursor.column as i64))
216                .await
217                .expect("Could not set nvim cursor position");
218        }
219
220        if let Some(NeovimData::Get { mode, state }) = self.data.back() {
221            if *mode == Some(Mode::Normal) && *state == marked_text {
222                return;
223            }
224        }
225        self.data.push_back(NeovimData::Put {
226            state: marked_text.to_string(),
227        })
228    }
229
230    #[cfg(not(feature = "neovim"))]
231    pub async fn set_state(&mut self, marked_text: &str) {
232        if let Some(NeovimData::Get { mode, state: text }) = self.data.front() {
233            if *mode == Some(Mode::Normal) && *text == marked_text {
234                return;
235            }
236            self.data.pop_front();
237        }
238        assert_eq!(
239            self.data.pop_front(),
240            Some(NeovimData::Put {
241                state: marked_text.to_string()
242            }),
243            "operation does not match recorded script. re-record with --features=neovim"
244        );
245    }
246
247    #[cfg(feature = "neovim")]
248    pub async fn set_option(&mut self, value: &str) {
249        self.nvim
250            .command_output(format!("set {}", value).as_str())
251            .await
252            .unwrap();
253
254        self.data.push_back(NeovimData::SetOption {
255            value: value.to_string(),
256        })
257    }
258
259    #[cfg(not(feature = "neovim"))]
260    pub async fn set_option(&mut self, value: &str) {
261        if let Some(NeovimData::Get { .. }) = self.data.front() {
262            self.data.pop_front();
263        };
264        assert_eq!(
265            self.data.pop_front(),
266            Some(NeovimData::SetOption {
267                value: value.to_string(),
268            }),
269            "operation does not match recorded script. re-record with --features=neovim"
270        );
271    }
272
273    #[cfg(feature = "neovim")]
274    pub async fn exec(&mut self, value: &str) {
275        self.nvim.command_output(value).await.unwrap();
276
277        self.data.push_back(NeovimData::Exec {
278            command: value.to_string(),
279        })
280    }
281
282    #[cfg(not(feature = "neovim"))]
283    pub async fn exec(&mut self, value: &str) {
284        if let Some(NeovimData::Get { .. }) = self.data.front() {
285            self.data.pop_front();
286        };
287        assert_eq!(
288            self.data.pop_front(),
289            Some(NeovimData::Exec {
290                command: value.to_string(),
291            }),
292            "operation does not match recorded script. re-record with --features=neovim"
293        );
294    }
295
296    #[cfg(not(feature = "neovim"))]
297    pub async fn read_register(&mut self, register: char) -> String {
298        if let Some(NeovimData::Get { .. }) = self.data.front() {
299            self.data.pop_front();
300        };
301        if let Some(NeovimData::ReadRegister { name, value }) = self.data.pop_front() {
302            if name == register {
303                return value;
304            }
305        }
306
307        panic!("operation does not match recorded script. re-record with --features=neovim")
308    }
309
310    #[cfg(feature = "neovim")]
311    pub async fn read_register(&mut self, name: char) -> String {
312        let value = self
313            .nvim
314            .command_output(format!("echo getreg('{}')", name).as_str())
315            .await
316            .unwrap();
317
318        self.data.push_back(NeovimData::ReadRegister {
319            name,
320            value: value.clone(),
321        });
322
323        value
324    }
325
326    #[cfg(feature = "neovim")]
327    async fn read_position(&mut self, cmd: &str) -> u32 {
328        self.nvim
329            .command_output(cmd)
330            .await
331            .unwrap()
332            .parse::<u32>()
333            .unwrap()
334    }
335
336    #[cfg(feature = "neovim")]
337    pub async fn state(&mut self) -> (Option<Mode>, String) {
338        let nvim_buffer = self
339            .nvim
340            .get_current_buf()
341            .await
342            .expect("Could not get neovim buffer");
343        let text = nvim_buffer
344            .get_lines(0, -1, false)
345            .await
346            .expect("Could not get buffer text")
347            .join("\n");
348
349        // nvim columns are 1-based, so -1.
350        let mut cursor_row = self.read_position("echo line('.')").await - 1;
351        let mut cursor_col = self.read_position("echo col('.')").await - 1;
352        let mut selection_row = self.read_position("echo line('v')").await - 1;
353        let mut selection_col = self.read_position("echo col('v')").await - 1;
354        let total_rows = self.read_position("echo line('$')").await - 1;
355
356        let nvim_mode_text = self
357            .nvim
358            .get_mode()
359            .await
360            .expect("Could not get mode")
361            .into_iter()
362            .find_map(|(key, value)| {
363                if key.as_str() == Some("mode") {
364                    Some(value.as_str().unwrap().to_owned())
365                } else {
366                    None
367                }
368            })
369            .expect("Could not find mode value");
370
371        let mode = match nvim_mode_text.as_ref() {
372            "i" => Some(Mode::Insert),
373            "n" => Some(Mode::Normal),
374            "v" => Some(Mode::Visual),
375            "V" => Some(Mode::VisualLine),
376            "\x16" => Some(Mode::VisualBlock),
377            _ => None,
378        };
379
380        let mut selections = Vec::new();
381        // Vim uses the index of the first and last character in the selection
382        // Zed uses the index of the positions between the characters, so we need
383        // to add one to the end in visual mode.
384        match mode {
385            Some(Mode::VisualBlock) if selection_row != cursor_row => {
386                // in zed we fake a block selection by using multiple cursors (one per line)
387                // this code emulates that.
388                // to deal with casees where the selection is not perfectly rectangular we extract
389                // the content of the selection via the "a register to get the shape correctly.
390                self.nvim.input("\"aygv").await.unwrap();
391                let content = self.nvim.command_output("echo getreg('a')").await.unwrap();
392                let lines = content.split('\n').collect::<Vec<_>>();
393                let top = cmp::min(selection_row, cursor_row);
394                let left = cmp::min(selection_col, cursor_col);
395                for row in top..=cmp::max(selection_row, cursor_row) {
396                    let content = if row - top >= lines.len() as u32 {
397                        ""
398                    } else {
399                        lines[(row - top) as usize]
400                    };
401                    let line_len = self
402                        .read_position(format!("echo strlen(getline({}))", row + 1).as_str())
403                        .await;
404
405                    if left > line_len {
406                        continue;
407                    }
408
409                    let start = Point::new(row, left);
410                    let end = Point::new(row, left + content.len() as u32);
411                    if cursor_col >= selection_col {
412                        selections.push(start..end)
413                    } else {
414                        selections.push(end..start)
415                    }
416                }
417            }
418            Some(Mode::Visual) | Some(Mode::VisualLine) | Some(Mode::VisualBlock) => {
419                if (selection_row, selection_col) > (cursor_row, cursor_col) {
420                    let selection_line_length =
421                        self.read_position("echo strlen(getline(line('v')))").await;
422                    if selection_line_length > selection_col {
423                        selection_col += 1;
424                    } else if selection_row < total_rows {
425                        selection_col = 0;
426                        selection_row += 1;
427                    }
428                } else {
429                    let cursor_line_length =
430                        self.read_position("echo strlen(getline(line('.')))").await;
431                    if cursor_line_length > cursor_col {
432                        cursor_col += 1;
433                    } else if cursor_row < total_rows {
434                        cursor_col = 0;
435                        cursor_row += 1;
436                    }
437                }
438                selections.push(
439                    Point::new(selection_row, selection_col)..Point::new(cursor_row, cursor_col),
440                )
441            }
442            Some(Mode::Insert) | Some(Mode::Normal) | Some(Mode::Replace) | None => selections
443                .push(Point::new(selection_row, selection_col)..Point::new(cursor_row, cursor_col)),
444        }
445
446        let ranges = encode_ranges(&text, &selections);
447        let state = NeovimData::Get {
448            mode,
449            state: ranges.clone(),
450        };
451
452        if self.data.back() != Some(&state) {
453            self.data.push_back(state.clone());
454        }
455
456        (mode, ranges)
457    }
458
459    #[cfg(not(feature = "neovim"))]
460    pub async fn state(&mut self) -> (Option<Mode>, String) {
461        if let Some(NeovimData::Get { state: raw, mode }) = self.data.front() {
462            (*mode, raw.to_string())
463        } else {
464            panic!("operation does not match recorded script. re-record with --features=neovim");
465        }
466    }
467
468    pub async fn mode(&mut self) -> Option<Mode> {
469        self.state().await.0
470    }
471
472    pub async fn marked_text(&mut self) -> String {
473        self.state().await.1
474    }
475
476    fn test_data_path(test_case_id: &str) -> PathBuf {
477        let mut data_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
478        data_path.push("test_data");
479        data_path.push(format!("{}.json", test_case_id));
480        data_path
481    }
482
483    #[cfg(not(feature = "neovim"))]
484    fn read_test_data(test_case_id: &str) -> VecDeque<NeovimData> {
485        let path = Self::test_data_path(test_case_id);
486        let json = std::fs::read_to_string(path).expect(
487            "Could not read test data. Is it generated? Try running test with '--features neovim'",
488        );
489
490        let mut result = VecDeque::new();
491        for line in json.lines() {
492            result.push_back(
493                serde_json::from_str(line)
494                    .expect("invalid test data. regenerate it with '--features neovim'"),
495            );
496        }
497        result
498    }
499
500    #[cfg(feature = "neovim")]
501    fn write_test_data(test_case_id: &str, data: &VecDeque<NeovimData>) {
502        let path = Self::test_data_path(test_case_id);
503        let mut json = Vec::new();
504        for entry in data {
505            serde_json::to_writer(&mut json, entry).unwrap();
506            json.push(b'\n');
507        }
508        std::fs::create_dir_all(path.parent().unwrap())
509            .expect("could not create test data directory");
510        std::fs::write(path, json).expect("could not write out test data");
511    }
512}
513
514#[cfg(feature = "neovim")]
515impl Deref for NeovimConnection {
516    type Target = Neovim<nvim_rs::compat::tokio::Compat<ChildStdin>>;
517
518    fn deref(&self) -> &Self::Target {
519        &self.nvim
520    }
521}
522
523#[cfg(feature = "neovim")]
524impl DerefMut for NeovimConnection {
525    fn deref_mut(&mut self) -> &mut Self::Target {
526        &mut self.nvim
527    }
528}
529
530#[cfg(feature = "neovim")]
531impl Drop for NeovimConnection {
532    fn drop(&mut self) {
533        Self::write_test_data(&self.test_case_id, &self.data);
534    }
535}
536
537#[cfg(feature = "neovim")]
538#[derive(Clone)]
539struct NvimHandler {}
540
541#[cfg(feature = "neovim")]
542#[async_trait]
543impl Handler for NvimHandler {
544    type Writer = nvim_rs::compat::tokio::Compat<ChildStdin>;
545
546    async fn handle_request(
547        &self,
548        _event_name: String,
549        _arguments: Vec<Value>,
550        _neovim: Neovim<Self::Writer>,
551    ) -> Result<Value, Value> {
552        unimplemented!();
553    }
554
555    async fn handle_notify(
556        &self,
557        _event_name: String,
558        _arguments: Vec<Value>,
559        _neovim: Neovim<Self::Writer>,
560    ) {
561    }
562}
563
564#[cfg(feature = "neovim")]
565fn parse_state(marked_text: &str) -> (String, Vec<Range<Point>>) {
566    let (text, ranges) = util::test::marked_text_ranges(marked_text, true);
567    let point_ranges = ranges
568        .into_iter()
569        .map(|byte_range| {
570            let mut point_range = Point::zero()..Point::zero();
571            let mut ix = 0;
572            let mut position = Point::zero();
573            for c in text.chars().chain(['\0']) {
574                if ix == byte_range.start {
575                    point_range.start = position;
576                }
577                if ix == byte_range.end {
578                    point_range.end = position;
579                }
580                let len_utf8 = c.len_utf8();
581                ix += len_utf8;
582                if c == '\n' {
583                    position.row += 1;
584                    position.column = 0;
585                } else {
586                    position.column += len_utf8 as u32;
587                }
588            }
589            point_range
590        })
591        .collect::<Vec<_>>();
592    (text, point_ranges)
593}
594
595#[cfg(feature = "neovim")]
596fn encode_ranges(text: &str, point_ranges: &Vec<Range<Point>>) -> String {
597    let byte_ranges = point_ranges
598        .into_iter()
599        .map(|range| {
600            let mut byte_range = 0..0;
601            let mut ix = 0;
602            let mut position = Point::zero();
603            for c in text.chars().chain(['\0']) {
604                if position == range.start {
605                    byte_range.start = ix;
606                }
607                if position == range.end {
608                    byte_range.end = ix;
609                }
610                let len_utf8 = c.len_utf8();
611                ix += len_utf8;
612                if c == '\n' {
613                    position.row += 1;
614                    position.column = 0;
615                } else {
616                    position.column += len_utf8 as u32;
617                }
618            }
619            byte_range
620        })
621        .collect::<Vec<_>>();
622    util::test::generate_marked_text(text, &byte_ranges[..], true)
623}