neovide 0.17.3

Neovide: No Nonsense Neovim Gui
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command as StdCommand;

use tokio::process::Command as TokioCommand;

use crate::cmd_line::CmdLineSettings;
use crate::utils::expand_tilde;
use crate::utils::handle_wslpaths;

#[cfg(target_os = "macos")]
const FORKED_FROM_TTY_ENV_VAR: &str = "NEOVIDE_FORKED_FROM_TTY";

/// For route-local startup targets for an embedded nvim instance.
///
/// we keep these separate from process-global cmd line settings because a reused
/// neovide process can open additional windows with their own launch context.
/// that context needs to be part of the actual nvim argv so e.g :restart replays
/// the same buffers/tabs, instead of only preserving the initial empty startup.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OpenArgs {
    pub files_to_open: Vec<String>,
    pub tabs: bool,
    pub neovim_bin: Option<String>,
    pub neovim_args: Option<Vec<String>>,
}

/// Mode is how neovide should populate argv when launching an embedded nvim instance.
///
/// Startup uses the original process command line targets,
/// Args uses route-local targets such as macOS handoff new windows,
/// None launches a blank embedded instance.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OpenMode {
    None,
    Startup,
    Args(OpenArgs),
}

#[derive(Debug, Clone)]
struct CommandSpec {
    program: String,
    args: Vec<String>,
    #[cfg(target_os = "windows")]
    creation_flags: Option<u32>,
}

impl CommandSpec {
    fn new(program: impl Into<String>, args: Vec<String>) -> Self {
        Self {
            program: program.into(),
            args,
            #[cfg(target_os = "windows")]
            creation_flags: None,
        }
    }

    #[cfg(target_os = "windows")]
    fn with_creation_flags(mut self, flags: u32) -> Self {
        self.creation_flags = Some(flags);
        self
    }
}

pub fn create_blocking_nvim_command(cmdline_settings: &CmdLineSettings, embed: bool) -> StdCommand {
    let (bin, args) = build_nvim_command_parts(cmdline_settings, embed, OpenMode::Startup);
    let cwd = command_cwd(cmdline_settings, None);
    let spec = create_command_spec(&bin, &args, cmdline_settings, cwd.as_deref());
    let mut cmd = std_command_from_spec(spec);
    if let Some(dir) = cwd {
        cmd.current_dir(dir);
    }
    cmd
}

pub fn create_tokio_nvim_command(
    cmdline_settings: &CmdLineSettings,
    embed: bool,
    cwd: Option<&Path>,
    mode: OpenMode,
) -> TokioCommand {
    let (bin, args) = build_nvim_command_parts(cmdline_settings, embed, mode);
    let cwd = command_cwd(cmdline_settings, cwd);
    let spec = create_command_spec(&bin, &args, cmdline_settings, cwd.as_deref());
    let mut cmd = tokio_command_from_spec(spec);
    if let Some(dir) = cwd {
        cmd.current_dir(dir);
    }
    cmd
}

fn command_cwd(settings: &CmdLineSettings, cwd: Option<&Path>) -> Option<PathBuf> {
    cwd.map(Path::to_path_buf).or_else(|| {
        settings.chdir.as_deref().map(|dir| {
            if dir.starts_with('~') { PathBuf::from(expand_tilde(dir)) } else { PathBuf::from(dir) }
        })
    })
}

fn build_nvim_command_parts(
    cmdline_settings: &CmdLineSettings,
    embed: bool,
    mode: OpenMode,
) -> (String, Vec<String>) {
    // For OpenMode::Args (handoff/new-window routes), use the route's own
    // bin/args. None means "not specified" = default nvim, NOT the global
    // CmdLineSettings value (which belongs to the original invocation).
    // For Startup/None modes, use global CmdLineSettings as before.
    let (bin, mut args) = match &mode {
        OpenMode::Args(open_args) => {
            let bin = open_args.neovim_bin.clone().unwrap_or_else(|| "nvim".to_owned());
            let args = open_args.neovim_args.clone().unwrap_or_default();
            (bin, args)
        }
        _ => {
            let bin = cmdline_settings.neovim_bin.clone().unwrap_or_else(|| "nvim".to_owned());
            let args = cmdline_settings.neovim_args.clone();
            (bin, args)
        }
    };

    if embed {
        append_embed_arg(&mut args);
    }

    args.extend(build_open_args(cmdline_settings, mode));

    (bin, args)
}

fn build_open_args(cmdline_settings: &CmdLineSettings, open_mode: OpenMode) -> Vec<String> {
    let (files_to_open, tabs) = match open_mode {
        OpenMode::None => return Vec::new(),
        OpenMode::Startup => (cmdline_settings.files_to_open.clone(), cmdline_settings.tabs),
        OpenMode::Args(args) => (args.files_to_open, args.tabs),
    };

    tabs.then(|| "-p".to_string())
        .into_iter()
        .chain(handle_wslpaths(files_to_open, cmdline_settings.wsl))
        .collect()
}

fn append_embed_arg(args: &mut Vec<String>) {
    if !args.iter().any(|arg| arg == "--embed") {
        args.push("--embed".to_string());
    }
}

fn tokio_command_from_spec(spec: CommandSpec) -> TokioCommand {
    let CommandSpec {
        program,
        args,
        #[cfg(target_os = "windows")]
        creation_flags,
    } = spec;
    let mut result = TokioCommand::new(program);
    result.args(&args);
    #[cfg(target_os = "windows")]
    if let Some(flags) = creation_flags {
        result.creation_flags(flags);
    }
    result
}

fn std_command_from_spec(spec: CommandSpec) -> StdCommand {
    let CommandSpec {
        program,
        args,
        #[cfg(target_os = "windows")]
        creation_flags,
    } = spec;
    let mut result = StdCommand::new(program);
    result.args(&args);
    #[cfg(target_os = "windows")]
    if let Some(flags) = creation_flags {
        result.creation_flags(flags);
    }
    result
}

#[cfg(target_os = "macos")]
fn launched_from_desktop() -> bool {
    if std::env::var_os(FORKED_FROM_TTY_ENV_VAR).is_some() {
        return false;
    }

    // On macOS, apps launched from Finder or `open` are spawned by launchd = PPID 1.
    // This is more reliable than $TERM for detecting GUI vs terminal launches,
    // so we use this as a heuristic instead of relying on $TERM.
    // https://en.wikipedia.org/wiki/Launchd#Components
    use rustix::process;
    matches!(process::getppid(), Some(ppid) if ppid.is_init())
}

// Creates a shell command if needed on this platform.
#[cfg(target_os = "macos")]
fn create_command_spec(
    command: &str,
    args: &[String],
    _cmdline_settings: &CmdLineSettings,
    cwd: Option<&Path>,
) -> CommandSpec {
    use uzers::os::unix::UserExt;
    if !launched_from_desktop() {
        // If we're not launched from the desktop, assume a terminal launch and avoid
        // re-initializing the environment. See https://github.com/neovide/neovide/issues/2584
        CommandSpec::new(command, args.to_vec())
    } else {
        // Otherwise run inside a login shell to ensure the environment matches a
        // normal GUI launch. See https://github.com/neovide/neovide/issues/2584
        let user = uzers::get_user_by_uid(uzers::get_current_uid()).unwrap();
        let shell = user.shell();

        CommandSpec::new(
            "/usr/bin/login",
            vec![
                // -f: Bypasses authentication for the already-logged-in user.
                // -p: Preserves the environment.
                // -q: Forces quiet logins, as if a .hushlogin is present.
                "-fpq".to_string(),
                user.name().to_str().unwrap().to_string(),
                shell.to_str().unwrap().to_string(),
                "-c".to_string(),
                build_login_shell_command(command, args, cwd),
            ],
        )
    }
}

#[cfg(target_os = "macos")]
fn build_login_shell_command(command: &str, args: &[String], cwd: Option<&Path>) -> String {
    let quoted_command = shlex::try_join(
        std::iter::once(command).chain(args.iter().map(std::string::String::as_str)),
    )
    .expect("Failed to join command");

    match cwd {
        Some(dir) => {
            let cwd_path = dir.to_string_lossy();
            let cwd = shlex::try_quote(cwd_path.as_ref()).expect("Failed to quote cwd");
            format!("cd {cwd} && exec {quoted_command}")
        }
        None => format!("exec {quoted_command}"),
    }
}

// Creates a shell command if needed on this platform.
#[cfg(target_os = "windows")]
fn create_command_spec(
    command: &str,
    args: &[String],
    cmdline_settings: &CmdLineSettings,
    _cwd: Option<&Path>,
) -> CommandSpec {
    let spec = if cfg!(target_os = "windows") && cmdline_settings.wsl {
        let args =
            shlex::try_join(args.iter().map(|s| s.as_ref())).expect("Failed to join arguments");
        CommandSpec::new(
            "wsl",
            vec![
                "$SHELL".to_string(),
                "-l".to_string(),
                "-c".to_string(),
                format!("{command} {args}"),
            ],
        )
    } else {
        // There's no need to go through the shell on Windows when not using WSL
        CommandSpec::new(command, args.to_vec())
    };
    spec.with_creation_flags(windows::Win32::System::Threading::CREATE_NO_WINDOW.0)
}

// Creates a shell command if needed on this platform.
// On Linux we can just launch directly
#[cfg(target_os = "linux")]
fn create_command_spec(
    command: &str,
    args: &[String],
    _cmdline_settings: &CmdLineSettings,
    _cwd: Option<&Path>,
) -> CommandSpec {
    CommandSpec::new(command, args.to_vec())
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::path::PathBuf;

    use super::*;
    use crate::cmd_line::handle_command_line_arguments;
    use crate::settings::Settings;

    fn parse_cmdline_settings(args: &[&str]) -> CmdLineSettings {
        let settings = Settings::new();
        let args = args.iter().map(|arg| arg.to_string()).collect();
        handle_command_line_arguments(args, &settings).expect("Could not parse arguments");
        settings.get::<CmdLineSettings>()
    }

    #[test]
    fn build_nvim_command_parts_places_embed_before_auto_open_args() {
        let cmdline_settings =
            parse_cmdline_settings(&["neovide", "./foo.txt", "./bar.md", "--grid=420x240"]);

        let (_, args) = build_nvim_command_parts(&cmdline_settings, true, OpenMode::Startup);

        assert_eq!(args, vec!["--embed", "-p", "./foo.txt", "./bar.md"]);
    }

    #[test]
    fn build_nvim_command_parts_skips_auto_open_args_when_requested() {
        let cmdline_settings =
            parse_cmdline_settings(&["neovide", "./foo.txt", "./bar.md", "--grid=420x240"]);

        let (_, args) = build_nvim_command_parts(&cmdline_settings, true, OpenMode::None);

        assert_eq!(args, vec!["--embed"]);
    }

    #[test]
    fn build_nvim_command_parts_uses_route_auto_open_args() {
        let cmdline_settings =
            parse_cmdline_settings(&["neovide", "./foo.txt", "./bar.md", "--grid=420x240"]);
        let open_args = OpenArgs {
            files_to_open: vec!["/tmp/project".to_string()],
            tabs: false,
            neovim_bin: None,
            neovim_args: None,
        };

        let (bin, args) =
            build_nvim_command_parts(&cmdline_settings, true, OpenMode::Args(open_args));

        // Route args mode does NOT inherit global cmdline settings
        assert_eq!(bin, "nvim");
        assert_eq!(args, vec!["--embed", "/tmp/project"]);
    }

    #[test]
    fn build_nvim_command_parts_uses_route_neovim_bin_override() {
        let cmdline_settings = parse_cmdline_settings(&["neovide", "--neovim-bin", "nvim-0.9"]);
        let open_args = OpenArgs {
            files_to_open: vec![],
            tabs: false,
            neovim_bin: Some("nvim-0.10".to_string()),
            neovim_args: Some(vec!["--clean".to_string()]),
        };

        let (bin, args) =
            build_nvim_command_parts(&cmdline_settings, true, OpenMode::Args(open_args));

        // Uses the route's override, not the global --neovim-bin=nvim-0.9
        assert_eq!(bin, "nvim-0.10");
        assert_eq!(args, vec!["--clean", "--embed"]);
    }

    #[test]
    fn build_nvim_command_parts_route_none_does_not_inherit_global() {
        let cmdline_settings =
            parse_cmdline_settings(&["neovide", "--neovim-bin", "nvim-0.9", "--", "--clean"]);
        let open_args =
            OpenArgs { files_to_open: vec![], tabs: false, neovim_bin: None, neovim_args: None };

        let (bin, args) =
            build_nvim_command_parts(&cmdline_settings, true, OpenMode::Args(open_args));

        // None means default nvim, NOT the global nvim-0.9
        assert_eq!(bin, "nvim");
        assert_eq!(args, vec!["--embed"]);
    }

    #[test]
    fn build_nvim_command_parts_preserves_launcher_args_before_embed() {
        let cmdline_settings = parse_cmdline_settings(&[
            "neovide",
            "--no-tabs",
            "--neovim-bin",
            "ssh",
            "--",
            "my-server",
            "nvim",
        ]);

        let (bin, args) = build_nvim_command_parts(&cmdline_settings, true, OpenMode::Startup);

        assert_eq!(bin, "ssh");
        assert_eq!(args, vec!["my-server", "nvim", "--embed"]);
    }

    #[test]
    fn command_cwd_prefers_override() {
        let cmdline_settings = parse_cmdline_settings(&["neovide", "--chdir", "/random/path"]);

        assert_eq!(
            command_cwd(&cmdline_settings, Some(Path::new("/route/cwd"))),
            Some(PathBuf::from("/route/cwd"))
        );
    }

    #[test]
    fn command_cwd_falls_back_to_cmdline_setting() {
        let cmdline_settings = parse_cmdline_settings(&["neovide", "--chdir", "/random/path"]);

        assert_eq!(command_cwd(&cmdline_settings, None), Some(PathBuf::from("/random/path")));
    }

    #[test]
    fn command_cwd_expands_tilde_in_cmdline_setting() {
        let cmdline_settings = parse_cmdline_settings(&["neovide", "--chdir", "~"]);

        assert_eq!(command_cwd(&cmdline_settings, None), Some(PathBuf::from(expand_tilde("~"))));
    }

    #[test]
    fn command_cwd_expands_tilde_subpath_in_cmdline_setting() {
        let cmdline_settings =
            parse_cmdline_settings(&["neovide", "--chdir", "~/some/other/project"]);

        assert_eq!(
            command_cwd(&cmdline_settings, None),
            Some(PathBuf::from(expand_tilde("~/some/other/project")))
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn build_login_shell_command_preserves_cwd() {
        let command = build_login_shell_command(
            "/bin/nvim",
            &["--embed".to_string(), "-p".to_string(), "/path/to/project/file.txt".to_string()],
            Some(Path::new("/path/to/new/cwd")),
        );

        assert_eq!(
            command,
            "cd /path/to/new/cwd && exec /bin/nvim --embed -p /path/to/project/file.txt"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn build_login_shell_command_skips_cd_without_override() {
        let command = build_login_shell_command(
            "/bin/nvim",
            &["--embed".to_string(), "-p".to_string(), "/path/to/project/file.txt".to_string()],
            None,
        );

        assert_eq!(command, "exec /bin/nvim --embed -p /path/to/project/file.txt");
    }
}