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 async fn read_position(&mut self, cmd: &str) -> u32 {
218 self.nvim
219 .command_output(cmd)
220 .await
221 .unwrap()
222 .parse::<u32>()
223 .unwrap()
224 }
225
226 #[cfg(feature = "neovim")]
227 pub async fn state(&mut self) -> (Option<Mode>, String, Range<Point>) {
228 let nvim_buffer = self
229 .nvim
230 .get_current_buf()
231 .await
232 .expect("Could not get neovim buffer");
233 let text = nvim_buffer
234 .get_lines(0, -1, false)
235 .await
236 .expect("Could not get buffer text")
237 .join("\n");
238
239 // nvim columns are 1-based, so -1.
240 let mut cursor_row = self.read_position("echo line('.')").await - 1;
241 let mut cursor_col = self.read_position("echo col('.')").await - 1;
242 let mut selection_row = self.read_position("echo line('v')").await - 1;
243 let mut selection_col = self.read_position("echo col('v')").await - 1;
244 let total_rows = self.read_position("echo line('$')").await - 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 // Vim uses the index of the first and last character in the selection
270 // Zed uses the index of the positions between the characters, so we need
271 // to add one to the end in visual mode.
272 match mode {
273 Some(Mode::Visual { .. }) => {
274 if selection_col > cursor_col {
275 let selection_line_length =
276 self.read_position("echo strlen(getline(line('v')))").await;
277 if selection_line_length > selection_col {
278 selection_col += 1;
279 } else if selection_row < total_rows {
280 selection_col = 0;
281 selection_row += 1;
282 }
283 } else {
284 let cursor_line_length =
285 self.read_position("echo strlen(getline(line('.')))").await;
286 if cursor_line_length > cursor_col {
287 cursor_col += 1;
288 } else if cursor_row < total_rows {
289 cursor_col = 0;
290 cursor_row += 1;
291 }
292 }
293 }
294 Some(Mode::Insert) | Some(Mode::Normal) | None => {}
295 }
296
297 let (start, end) = (
298 Point::new(selection_row, selection_col),
299 Point::new(cursor_row, cursor_col),
300 );
301
302 let state = NeovimData::Get {
303 mode,
304 state: encode_range(&text, start..end),
305 };
306
307 if self.data.back() != Some(&state) {
308 self.data.push_back(state.clone());
309 }
310
311 (mode, text, start..end)
312 }
313
314 #[cfg(not(feature = "neovim"))]
315 pub async fn state(&mut self) -> (Option<Mode>, String, Range<Point>) {
316 if let Some(NeovimData::Get { state: text, mode }) = self.data.front() {
317 let (text, range) = parse_state(text);
318 (*mode, text, range)
319 } else {
320 panic!("operation does not match recorded script. re-record with --features=neovim");
321 }
322 }
323
324 pub async fn selection(&mut self) -> Range<Point> {
325 self.state().await.2
326 }
327
328 pub async fn mode(&mut self) -> Option<Mode> {
329 self.state().await.0
330 }
331
332 pub async fn text(&mut self) -> String {
333 self.state().await.1
334 }
335
336 fn test_data_path(test_case_id: &str) -> PathBuf {
337 let mut data_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
338 data_path.push("test_data");
339 data_path.push(format!("{}.json", test_case_id));
340 data_path
341 }
342
343 #[cfg(not(feature = "neovim"))]
344 fn read_test_data(test_case_id: &str) -> VecDeque<NeovimData> {
345 let path = Self::test_data_path(test_case_id);
346 let json = std::fs::read_to_string(path).expect(
347 "Could not read test data. Is it generated? Try running test with '--features neovim'",
348 );
349
350 let mut result = VecDeque::new();
351 for line in json.lines() {
352 result.push_back(
353 serde_json::from_str(line)
354 .expect("invalid test data. regenerate it with '--features neovim'"),
355 );
356 }
357 result
358 }
359
360 #[cfg(feature = "neovim")]
361 fn write_test_data(test_case_id: &str, data: &VecDeque<NeovimData>) {
362 let path = Self::test_data_path(test_case_id);
363 let mut json = Vec::new();
364 for entry in data {
365 serde_json::to_writer(&mut json, entry).unwrap();
366 json.push(b'\n');
367 }
368 std::fs::create_dir_all(path.parent().unwrap())
369 .expect("could not create test data directory");
370 std::fs::write(path, json).expect("could not write out test data");
371 }
372}
373
374#[cfg(feature = "neovim")]
375impl Deref for NeovimConnection {
376 type Target = Neovim<nvim_rs::compat::tokio::Compat<ChildStdin>>;
377
378 fn deref(&self) -> &Self::Target {
379 &self.nvim
380 }
381}
382
383#[cfg(feature = "neovim")]
384impl DerefMut for NeovimConnection {
385 fn deref_mut(&mut self) -> &mut Self::Target {
386 &mut self.nvim
387 }
388}
389
390#[cfg(feature = "neovim")]
391impl Drop for NeovimConnection {
392 fn drop(&mut self) {
393 Self::write_test_data(&self.test_case_id, &self.data);
394 }
395}
396
397#[cfg(feature = "neovim")]
398#[derive(Clone)]
399struct NvimHandler {}
400
401#[cfg(feature = "neovim")]
402#[async_trait]
403impl Handler for NvimHandler {
404 type Writer = nvim_rs::compat::tokio::Compat<ChildStdin>;
405
406 async fn handle_request(
407 &self,
408 _event_name: String,
409 _arguments: Vec<Value>,
410 _neovim: Neovim<Self::Writer>,
411 ) -> Result<Value, Value> {
412 unimplemented!();
413 }
414
415 async fn handle_notify(
416 &self,
417 _event_name: String,
418 _arguments: Vec<Value>,
419 _neovim: Neovim<Self::Writer>,
420 ) {
421 }
422}
423
424fn parse_state(marked_text: &str) -> (String, Range<Point>) {
425 let (text, ranges) = util::test::marked_text_ranges(marked_text, true);
426 let byte_range = ranges[0].clone();
427 let mut point_range = Point::zero()..Point::zero();
428 let mut ix = 0;
429 let mut position = Point::zero();
430 for c in text.chars().chain(['\0']) {
431 if ix == byte_range.start {
432 point_range.start = position;
433 }
434 if ix == byte_range.end {
435 point_range.end = position;
436 }
437 let len_utf8 = c.len_utf8();
438 ix += len_utf8;
439 if c == '\n' {
440 position.row += 1;
441 position.column = 0;
442 } else {
443 position.column += len_utf8 as u32;
444 }
445 }
446 (text, point_range)
447}
448
449#[cfg(feature = "neovim")]
450fn encode_range(text: &str, range: Range<Point>) -> String {
451 let mut byte_range = 0..0;
452 let mut ix = 0;
453 let mut position = Point::zero();
454 for c in text.chars().chain(['\0']) {
455 if position == range.start {
456 byte_range.start = ix;
457 }
458 if position == range.end {
459 byte_range.end = ix;
460 }
461 let len_utf8 = c.len_utf8();
462 ix += len_utf8;
463 if c == '\n' {
464 position.row += 1;
465 position.column = 0;
466 } else {
467 position.column += len_utf8 as u32;
468 }
469 }
470 util::test::generate_marked_text(text, &[byte_range], true)
471}