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