Smart Neovim launcher for yyx990803/launch-editor
1use std::{
2 ffi::OsString,
3 num::NonZeroU64,
4 path::Path,
5 process::{Command, Stdio},
6};
7
8use anyhow::Result;
9use log::debug;
10
11use crate::platform::{Process, nvim_sockets};
12
13pub fn instances() -> Result<impl Iterator<Item = Neovim>> {
14 Ok(nvim_sockets()?.map(|(process, socket)| Neovim { process, socket }))
15}
16
17pub struct Neovim {
18 pub process: Process,
19 socket: OsString,
20}
21
22impl Neovim {
23 pub fn edit(
24 &self,
25 path: &Path,
26 line: Option<NonZeroU64>,
27 col: Option<NonZeroU64>,
28 ) -> std::io::Result<()> {
29 let col = col.unwrap_or(NonZeroU64::MIN);
30 let cmd = match line {
31 Some(line) => format!(
32 "<C-\\><C-N>:n +call\\ cursor({line},{col}) {path}<CR>",
33 path = path.display()
34 ),
35 _ => format!("<C-\\><C-N>:n {path}<CR>", path = path.display()),
36 };
37
38 debug!("Sending {cmd:?} to {}", self.socket.display());
39 Command::new("nvim")
40 .arg("--server")
41 .arg(&self.socket)
42 .arg("--remote-send")
43 .arg(cmd)
44 .stdout(Stdio::null())
45 .spawn()?;
46 debug!("Command executed successfully!");
47 Ok(())
48 }
49}