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    Handler, Neovim, UiAttachOptions, Value, create::tokio::new_child_cmd, error::LoopError,
 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: 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.platform
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.platform {
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 == 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 == 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) -> (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" => Mode::Insert,
373            "n" => Mode::Normal,
374            "v" => Mode::Visual,
375            "V" => Mode::VisualLine,
376            "R" => Mode::Replace,
377            "\x16" => Mode::VisualBlock,
378            _ => panic!("unexpected vim mode: {nvim_mode_text}"),
379        };
380
381        let mut selections = Vec::new();
382        // Vim uses the index of the first and last character in the selection
383        // Zed uses the index of the positions between the characters, so we need
384        // to add one to the end in visual mode.
385        match mode {
386            Mode::VisualBlock if selection_row != cursor_row => {
387                // in zed we fake a block selection by using multiple cursors (one per line)
388                // this code emulates that.
389                // to deal with casees where the selection is not perfectly rectangular we extract
390                // the content of the selection via the "a register to get the shape correctly.
391                self.nvim.input("\"aygv").await.unwrap();
392                let content = self.nvim.command_output("echo getreg('a')").await.unwrap();
393                let lines = content.split('\n').collect::<Vec<_>>();
394                let top = cmp::min(selection_row, cursor_row);
395                let left = cmp::min(selection_col, cursor_col);
396                for row in top..=cmp::max(selection_row, cursor_row) {
397                    let content = if row - top >= lines.len() as u32 {
398                        ""
399                    } else {
400                        lines[(row - top) as usize]
401                    };
402                    let line_len = self
403                        .read_position(format!("echo strlen(getline({}))", row + 1).as_str())
404                        .await;
405
406                    if left > line_len {
407                        continue;
408                    }
409
410                    let start = Point::new(row, left);
411                    let end = Point::new(row, left + content.len() as u32);
412                    if cursor_col >= selection_col {
413                        selections.push(start..end)
414                    } else {
415                        selections.push(end..start)
416                    }
417                }
418            }
419            Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
420                if (selection_row, selection_col) > (cursor_row, cursor_col) {
421                    let selection_line_length =
422                        self.read_position("echo strlen(getline(line('v')))").await;
423                    if selection_line_length > selection_col {
424                        selection_col += 1;
425                    } else if selection_row < total_rows {
426                        selection_col = 0;
427                        selection_row += 1;
428                    }
429                } else {
430                    let cursor_line_length =
431                        self.read_position("echo strlen(getline(line('.')))").await;
432                    if cursor_line_length > cursor_col {
433                        cursor_col += 1;
434                    } else if cursor_row < total_rows {
435                        cursor_col = 0;
436                        cursor_row += 1;
437                    }
438                }
439                selections.push(
440                    Point::new(selection_row, selection_col)..Point::new(cursor_row, cursor_col),
441                )
442            }
443            Mode::Insert | Mode::Normal | Mode::Replace => selections
444                .push(Point::new(selection_row, selection_col)..Point::new(cursor_row, cursor_col)),
445            Mode::HelixNormal => unreachable!(),
446        }
447
448        let ranges = encode_ranges(&text, &selections);
449        let state = NeovimData::Get {
450            mode,
451            state: ranges.clone(),
452        };
453
454        if self.data.back() != Some(&state) {
455            self.data.push_back(state.clone());
456        }
457
458        (mode, ranges)
459    }
460
461    #[cfg(not(feature = "neovim"))]
462    pub async fn state(&mut self) -> (Mode, String) {
463        if let Some(NeovimData::Get { state: raw, mode }) = self.data.front() {
464            (*mode, raw.to_string())
465        } else {
466            panic!("operation does not match recorded script. re-record with --features=neovim");
467        }
468    }
469
470    fn test_data_path(test_case_id: &str) -> PathBuf {
471        let mut data_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
472        data_path.push("test_data");
473        data_path.push(format!("{}.json", test_case_id));
474        data_path
475    }
476
477    #[cfg(not(feature = "neovim"))]
478    fn read_test_data(test_case_id: &str) -> VecDeque<NeovimData> {
479        let path = Self::test_data_path(test_case_id);
480        let json = std::fs::read_to_string(path).expect(
481            "Could not read test data. Is it generated? Try running test with '--features neovim'",
482        );
483
484        let mut result = VecDeque::new();
485        for line in json.lines() {
486            result.push_back(
487                serde_json::from_str(line)
488                    .expect("invalid test data. regenerate it with '--features neovim'"),
489            );
490        }
491        result
492    }
493
494    #[cfg(feature = "neovim")]
495    fn write_test_data(test_case_id: &str, data: &VecDeque<NeovimData>) {
496        let path = Self::test_data_path(test_case_id);
497        let mut json = Vec::new();
498        for entry in data {
499            serde_json::to_writer(&mut json, entry).unwrap();
500            json.push(b'\n');
501        }
502        std::fs::create_dir_all(path.parent().unwrap())
503            .expect("could not create test data directory");
504        std::fs::write(path, json).expect("could not write out test data");
505    }
506}
507
508#[cfg(feature = "neovim")]
509impl Deref for NeovimConnection {
510    type Target = Neovim<nvim_rs::compat::tokio::Compat<ChildStdin>>;
511
512    fn deref(&self) -> &Self::Target {
513        &self.nvim
514    }
515}
516
517#[cfg(feature = "neovim")]
518impl DerefMut for NeovimConnection {
519    fn deref_mut(&mut self) -> &mut Self::Target {
520        &mut self.nvim
521    }
522}
523
524#[cfg(feature = "neovim")]
525impl Drop for NeovimConnection {
526    fn drop(&mut self) {
527        Self::write_test_data(&self.test_case_id, &self.data);
528    }
529}
530
531#[cfg(feature = "neovim")]
532#[derive(Clone)]
533struct NvimHandler {}
534
535#[cfg(feature = "neovim")]
536#[async_trait]
537impl Handler for NvimHandler {
538    type Writer = nvim_rs::compat::tokio::Compat<ChildStdin>;
539
540    async fn handle_request(
541        &self,
542        _event_name: String,
543        _arguments: Vec<Value>,
544        _neovim: Neovim<Self::Writer>,
545    ) -> Result<Value, Value> {
546        unimplemented!();
547    }
548
549    async fn handle_notify(
550        &self,
551        _event_name: String,
552        _arguments: Vec<Value>,
553        _neovim: Neovim<Self::Writer>,
554    ) {
555    }
556}
557
558#[cfg(feature = "neovim")]
559fn parse_state(marked_text: &str) -> (String, Vec<Range<Point>>) {
560    let (text, ranges) = util::test::marked_text_ranges(marked_text, true);
561    let point_ranges = ranges
562        .into_iter()
563        .map(|byte_range| {
564            let mut point_range = Point::zero()..Point::zero();
565            let mut ix = 0;
566            let mut position = Point::zero();
567            for c in text.chars().chain(['\0']) {
568                if ix == byte_range.start {
569                    point_range.start = position;
570                }
571                if ix == byte_range.end {
572                    point_range.end = position;
573                }
574                let len_utf8 = c.len_utf8();
575                ix += len_utf8;
576                if c == '\n' {
577                    position.row += 1;
578                    position.column = 0;
579                } else {
580                    position.column += len_utf8 as u32;
581                }
582            }
583            point_range
584        })
585        .collect::<Vec<_>>();
586    (text, point_ranges)
587}
588
589#[cfg(feature = "neovim")]
590fn encode_ranges(text: &str, point_ranges: &Vec<Range<Point>>) -> String {
591    let byte_ranges = point_ranges
592        .into_iter()
593        .map(|range| {
594            let mut byte_range = 0..0;
595            let mut ix = 0;
596            let mut position = Point::zero();
597            for c in text.chars().chain(['\0']) {
598                if position == range.start {
599                    byte_range.start = ix;
600                }
601                if position == range.end {
602                    byte_range.end = ix;
603                }
604                let len_utf8 = c.len_utf8();
605                ix += len_utf8;
606                if c == '\n' {
607                    position.row += 1;
608                    position.column = 0;
609                } else {
610                    position.column += len_utf8 as u32;
611                }
612            }
613            byte_range
614        })
615        .collect::<Vec<_>>();
616    util::test::generate_marked_text(text, &byte_ranges[..], true)
617}