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