vtcode-core 0.103.1

Core library for VT Code - a Rust-based terminal coding agent
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::Duration;

use crate::utils::file_utils::read_file_with_context_sync;
use anyhow::{Context, Result, anyhow};
use ratatui::crossterm::ExecutableCommand;
use ratatui::crossterm::event;
use ratatui::crossterm::terminal::{
    Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode,
    enable_raw_mode, is_raw_mode_enabled,
};
use tempfile::NamedTempFile;
use tracing::debug;
use vtcode_commons::EditorTarget;

/// Result from running a terminal application
#[derive(Debug)]
pub struct TerminalAppResult {
    /// Exit code from the application
    pub exit_code: i32,
    /// Whether the application completed successfully
    pub success: bool,
}

/// Runtime configuration for launching an external editor.
#[derive(Debug, Clone)]
pub struct EditorLaunchConfig {
    /// Preferred editor command override (supports args, e.g. `code --wait`)
    pub preferred_editor: Option<String>,
    /// Wait for the editor process to exit before returning.
    pub wait_for_editor: bool,
}

impl Default for EditorLaunchConfig {
    fn default() -> Self {
        Self {
            preferred_editor: None,
            wait_for_editor: true,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalCommandStrategy {
    Shell,
    PowerShell,
}

/// Manages launching terminal applications
pub struct TerminalAppLauncher {
    workspace_root: PathBuf,
}

impl TerminalAppLauncher {
    /// Create a new terminal app launcher
    pub fn new(workspace_root: PathBuf) -> Self {
        Self { workspace_root }
    }

    /// Launch user's preferred editor with optional file
    ///
    /// If a file is provided, it will be opened in the editor.
    /// If no file is provided, a temporary file will be created and its
    /// contents returned after editing.
    ///
    /// Uses the configured editor command, then VISUAL/EDITOR, then common editor defaults.
    ///
    /// # Errors
    ///
    /// Returns an error if the editor fails to launch or if file operations fail.
    pub fn launch_editor(&self, file: Option<PathBuf>) -> Result<Option<String>> {
        self.launch_editor_with_config(file, EditorLaunchConfig::default())
    }

    /// Launch user's preferred editor with explicit launch configuration.
    ///
    /// `preferred_editor`, when set, takes precedence over VISUAL/EDITOR env vars.
    pub fn launch_editor_with_config(
        &self,
        file: Option<PathBuf>,
        config: EditorLaunchConfig,
    ) -> Result<Option<String>> {
        let target = file.map(|path| EditorTarget::new(path, None));
        self.launch_editor_target_with_config(target, config)
    }

    /// Launch user's preferred editor with an optional file target and location.
    ///
    /// `preferred_editor`, when set, takes precedence over VISUAL/EDITOR env vars.
    pub fn launch_editor_target_with_config(
        &self,
        target: Option<EditorTarget>,
        config: EditorLaunchConfig,
    ) -> Result<Option<String>> {
        let (target, is_temp) = if let Some(target) = target {
            (target, false)
        } else {
            // Create temp file for editing
            let temp =
                NamedTempFile::new().context("failed to create temporary file for editing")?;
            // Keep temp file alive by persisting it
            let (_, path) = temp.keep().context("failed to persist temporary file")?;
            (EditorTarget::new(path, None), true)
        };
        let file_path = target.path().to_path_buf();
        let mut wait_for_editor = is_temp || config.wait_for_editor;
        let preferred_editor = config
            .preferred_editor
            .as_deref()
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(ToOwned::to_owned);

        debug!(
            path = %file_path.display(),
            wait_for_editor,
            "launching editor"
        );

        let mut cmd = if let Some(preferred) = preferred_editor.as_deref() {
            debug!("using configured preferred editor command: {}", preferred);
            Self::build_editor_command_from_string(preferred, &target, wait_for_editor)
                .with_context(|| {
                    format!(
                        "failed to parse tools.editor.preferred_editor '{}'",
                        preferred
                    )
                })?
        } else if let Some(env_command) = Self::editor_command_from_env() {
            debug!("using editor command from environment: {}", env_command);
            Self::build_editor_command_from_string(&env_command, &target, wait_for_editor)
                .with_context(|| format!("failed to parse editor command '{}'", env_command))?
        } else {
            // If EDITOR/VISUAL not set, search for available editors in PATH
            debug!("EDITOR/VISUAL not set, searching for available editors");
            Self::try_common_editors(&target, wait_for_editor).context(
                "failed to detect editor: set tools.editor.preferred_editor, \
                 or set EDITOR/VISUAL, or install an editor in PATH",
            )?
        };

        if !wait_for_editor {
            let program = cmd.get_program().to_string_lossy().to_string();
            if Self::program_requires_terminal(&program) {
                debug!(
                    program = %program,
                    "forcing synchronous launch for terminal-based editor"
                );
                wait_for_editor = true;
            }
        }

        if wait_for_editor {
            self.suspend_terminal_for_command(|| {
                let status = cmd
                    .current_dir(&self.workspace_root)
                    .status()
                    .context("failed to spawn editor")?;

                if !status.success() {
                    return Err(anyhow!(
                        "editor exited with non-zero status: {}",
                        status.code().unwrap_or(-1)
                    ));
                }

                Ok(())
            })?;
        } else {
            cmd.current_dir(&self.workspace_root)
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .spawn()
                .context("failed to spawn editor")?;
        }

        // Read temp file contents if it was a temp file
        let content = if is_temp {
            let content = read_file_with_context_sync(&file_path, "edited temporary file")
                .context("failed to read edited content from temporary file")?;
            fs::remove_file(&file_path).context("failed to remove temporary file")?;
            Some(content)
        } else {
            None
        };

        Ok(content)
    }

    fn build_editor_command_from_string(
        command: &str,
        target: &EditorTarget,
        wait_for_editor: bool,
    ) -> Result<Command> {
        let tokens = shell_words::split(command)
            .with_context(|| format!("invalid editor command: {}", command))?;
        let (program, args) = tokens
            .split_first()
            .ok_or_else(|| anyhow!("editor command cannot be empty"))?;
        let adapter = EditorAdapter::from_program(program);
        let mut cmd = Command::new(program);
        cmd.args(filtered_editor_args(adapter, args, wait_for_editor));
        Self::append_editor_target_args(&mut cmd, program, target);
        Ok(cmd)
    }

    /// Try common editors in priority order as fallback when EDITOR/VISUAL not set
    fn try_common_editors(target: &EditorTarget, wait_for_editor: bool) -> Result<Command> {
        let candidates = if cfg!(target_os = "windows") {
            vec![
                "code --wait",
                "code",
                "zed --wait",
                "zed",
                "subl -w",
                "subl",
                "notepad++",
                "notepad",
            ]
        } else if cfg!(target_os = "macos") {
            vec![
                "code --wait",
                "code",
                "zed --wait",
                "zed",
                "subl -w",
                "subl",
                "mate -w",
                "mate",
                "open -a TextEdit",
                "nvim",
                "vim",
                "vi",
                "nano",
                "emacs",
            ]
        } else {
            vec![
                "code --wait",
                "code",
                "zed --wait",
                "zed",
                "subl -w",
                "subl",
                "mate -w",
                "mate",
                "nvim",
                "vim",
                "vi",
                "nano",
                "emacs",
            ]
        };

        for candidate in candidates {
            let tokens = match shell_words::split(candidate) {
                Ok(tokens) => tokens,
                Err(_) => continue,
            };
            let Some(program) = tokens.first() else {
                continue;
            };
            if which::which(program).is_ok() {
                debug!("found fallback editor: {}", candidate);
                return Self::build_editor_command_from_string(candidate, target, wait_for_editor);
            }
        }

        Err(anyhow!(
            "no editor found in PATH. Install an editor (e.g. nvim, code, zed, emacs), \
             or configure tools.editor.preferred_editor"
        ))
    }

    fn editor_command_from_env() -> Option<String> {
        ["VISUAL", "EDITOR"]
            .into_iter()
            .find_map(|key| std::env::var(key).ok())
            .map(|value| value.trim().to_string())
            .filter(|value| !value.is_empty())
    }

    fn program_requires_terminal(program: &str) -> bool {
        let normalized = Path::new(program)
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or(program)
            .to_ascii_lowercase();

        matches!(
            normalized.as_str(),
            "vi" | "vim" | "nvim" | "nano" | "emacs" | "pico" | "hx" | "helix"
        )
    }

    fn append_editor_target_args(cmd: &mut Command, program: &str, target: &EditorTarget) {
        let adapter = EditorAdapter::from_program(program);
        let file_path = target.path();

        match (adapter, target.point()) {
            (EditorAdapter::Vscode, Some(point)) => {
                cmd.arg("-g");
                cmd.arg(format_location_arg(file_path, point.line, point.column));
            }
            (EditorAdapter::ColonLocation, Some(point)) => {
                cmd.arg(format_location_arg(file_path, point.line, point.column));
            }
            (EditorAdapter::Vim, Some(point)) => {
                if let Some(column) = point.column {
                    cmd.arg(format!("+call cursor({},{})", point.line, column));
                } else {
                    cmd.arg(format!("+{}", point.line));
                }
                cmd.arg(file_path);
            }
            _ => {
                cmd.arg(file_path);
            }
        }
    }

    /// Suspend terminal UI state and run external command
    ///
    /// This is the unified method for launching external applications while
    /// properly managing terminal state. It follows the Ratatui recipe:
    /// <https://ratatui.rs/recipes/apps/spawn-vim/>
    ///
    /// The sequence ensures:
    /// 1. Event handler is stopped (if applicable)
    /// 2. Alternate screen is left
    /// 3. Pending events are drained (CRITICAL!)
    /// 4. Raw mode is disabled
    /// 5. External command runs freely
    /// 6. Raw mode is re-enabled
    /// 7. Alternate screen is re-entered
    /// 8. Terminal is cleared (removes artifacts)
    /// 9. Event handler is restarted (if applicable)
    ///
    /// # Errors
    ///
    /// Returns an error if terminal state management fails or command fails.
    fn suspend_terminal_for_command<F, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Result<T>,
    {
        let was_raw_mode = match is_raw_mode_enabled() {
            Ok(enabled) => enabled,
            Err(error) => {
                debug!(%error, "failed to query raw mode status; assuming non-raw terminal state");
                false
            }
        };

        if was_raw_mode {
            // Leave alternate screen
            io::stdout()
                .execute(LeaveAlternateScreen)
                .context("failed to leave alternate screen")?;

            // CRITICAL: Drain any pending crossterm events BEFORE disabling raw mode.
            // This prevents the external app from receiving garbage input (like terminal
            // capability responses or buffered keystrokes) that might have been sent to the TUI.
            while event::poll(Duration::from_millis(0)).unwrap_or(false) {
                let _ = event::read();
            }

            // Disable raw mode
            disable_raw_mode().context("failed to disable raw mode")?;
        }

        // Run the command
        let result = f();

        if was_raw_mode {
            // Always attempt every restore step so we minimize the chance of leaving the terminal
            // in a partially restored state.
            let mut restore_errors = Vec::new();

            if let Err(error) = enable_raw_mode() {
                restore_errors.push(format!("failed to re-enable raw mode: {}", error));
            }

            if let Err(error) = io::stdout().execute(EnterAlternateScreen) {
                restore_errors.push(format!("failed to re-enter alternate screen: {}", error));
            }

            // This prevents ANSI escape codes from external apps' background color requests
            // from appearing in the TUI.
            if let Err(error) = io::stdout().execute(Clear(ClearType::All)) {
                restore_errors.push(format!("failed to clear terminal: {}", error));
            }

            if !restore_errors.is_empty() {
                let restore_summary = restore_errors.join("; ");
                return match result {
                    Ok(_) => Err(anyhow!("terminal restore failed: {}", restore_summary)),
                    Err(command_error) => Err(command_error
                        .context(format!("terminal restore also failed: {}", restore_summary))),
                };
            }
        }

        result
    }

    pub fn run_command_with_strategy(
        &self,
        command: &str,
        strategy: TerminalCommandStrategy,
    ) -> Result<TerminalAppResult> {
        self.suspend_terminal_for_command(|| {
            let mut cmd = match strategy {
                TerminalCommandStrategy::Shell => {
                    #[cfg(target_os = "windows")]
                    {
                        let mut command_builder = Command::new("cmd");
                        command_builder.arg("/C").arg(command);
                        command_builder
                    }
                    #[cfg(not(target_os = "windows"))]
                    {
                        let mut command_builder = Command::new("/bin/sh");
                        command_builder.arg("-lc").arg(command);
                        command_builder
                    }
                }
                TerminalCommandStrategy::PowerShell => {
                    let mut command_builder = if cfg!(target_os = "windows") {
                        Command::new("powershell")
                    } else {
                        Command::new("pwsh")
                    };
                    command_builder
                        .arg("-NoLogo")
                        .arg("-NoProfile")
                        .arg("-Command")
                        .arg(command);
                    command_builder
                }
            };

            let status = cmd
                .current_dir(&self.workspace_root)
                .status()
                .with_context(|| format!("failed to spawn update command: {}", command))?;

            Ok(TerminalAppResult {
                exit_code: status.code().unwrap_or(-1),
                success: status.success(),
            })
        })
    }

    /// Launch git interface (Lazygit or interactive git)
    ///
    /// This will attempt to launch Lazygit if available, otherwise falls back
    /// to an interactive git command.
    ///
    /// # Errors
    ///
    /// Returns an error if the git interface fails to launch.
    pub fn launch_git_interface(&self) -> Result<()> {
        self.suspend_terminal_for_command(|| {
            let git_cmd = if which::which("lazygit").is_ok() {
                "lazygit"
            } else {
                "git"
            };

            let status = Command::new(git_cmd)
                .current_dir(&self.workspace_root)
                .status()
                .with_context(|| format!("failed to spawn {}", git_cmd))?;

            if !status.success() {
                return Err(anyhow!(
                    "{} exited with non-zero status: {}",
                    git_cmd,
                    status.code().unwrap_or(-1)
                ));
            }

            Ok(())
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EditorAdapter {
    Plain,
    Vscode,
    ColonLocation,
    Mate,
    MacOpen,
    Vim,
}

impl EditorAdapter {
    fn from_program(program: &str) -> Self {
        let program = Path::new(program)
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or(program)
            .to_ascii_lowercase();

        match program.as_str() {
            "code" | "code-insiders" => Self::Vscode,
            "zed" | "subl" => Self::ColonLocation,
            "mate" => Self::Mate,
            "open" => Self::MacOpen,
            "nvim" | "vim" | "vi" => Self::Vim,
            _ => Self::Plain,
        }
    }
}

fn filtered_editor_args(
    adapter: EditorAdapter,
    args: &[String],
    wait_for_editor: bool,
) -> Vec<String> {
    if wait_for_editor {
        return args.to_vec();
    }

    args.iter()
        .filter(|arg| !matches_wait_flag(adapter, arg))
        .cloned()
        .collect()
}

fn matches_wait_flag(adapter: EditorAdapter, arg: &str) -> bool {
    match adapter {
        EditorAdapter::Vscode => arg == "--wait",
        EditorAdapter::ColonLocation | EditorAdapter::Mate => arg == "--wait" || arg == "-w",
        EditorAdapter::MacOpen => arg == "-W",
        EditorAdapter::Plain | EditorAdapter::Vim => false,
    }
}

fn format_location_arg(path: &Path, line: usize, column: Option<usize>) -> String {
    let column = column.unwrap_or(1);
    format!("{}:{}:{}", path.display(), line, column)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::OsStr;

    #[test]
    fn test_launcher_creation() {
        let launcher = TerminalAppLauncher::new(PathBuf::from("/tmp"));
        // Just verify it can be created without panicking
        assert_eq!(launcher.workspace_root, PathBuf::from("/tmp"));
    }

    #[test]
    fn test_build_editor_command_supports_arguments() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "code --wait",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), None),
            true,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(command.get_program(), OsStr::new("code"));
        assert_eq!(args, vec!["--wait".to_string(), "/tmp/test.rs".to_string()]);
    }

    #[test]
    fn test_build_editor_command_rejects_empty_string() {
        let result = TerminalAppLauncher::build_editor_command_from_string(
            "   ",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), None),
            true,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_build_editor_command_uses_vscode_go_to_location() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "code --wait",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), Some(":12:4".to_string())),
            true,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(
            args,
            vec![
                "--wait".to_string(),
                "-g".to_string(),
                "/tmp/test.rs:12:4".to_string()
            ]
        );
    }

    #[test]
    fn test_build_editor_command_uses_colon_location_for_zed() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "zed",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), Some(":12".to_string())),
            true,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(args, vec!["/tmp/test.rs:12:1".to_string()]);
    }

    #[test]
    fn test_build_editor_command_uses_cursor_command_for_vim() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "nvim",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), Some(":12:4".to_string())),
            true,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(
            args,
            vec!["+call cursor(12,4)".to_string(), "/tmp/test.rs".to_string()]
        );
    }

    #[test]
    fn test_build_editor_command_degrades_unknown_commands_to_file_only() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "custom-editor --flag",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), Some(":12:4".to_string())),
            true,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(args, vec!["--flag".to_string(), "/tmp/test.rs".to_string()]);
    }

    #[test]
    fn test_build_editor_command_strips_vscode_wait_flag_when_not_waiting() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "code --wait",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), Some(":12:4".to_string())),
            false,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(
            args,
            vec!["-g".to_string(), "/tmp/test.rs:12:4".to_string()]
        );
    }

    #[test]
    fn test_build_editor_command_strips_sublime_wait_flag_when_not_waiting() {
        let command = TerminalAppLauncher::build_editor_command_from_string(
            "subl -w",
            &EditorTarget::new(PathBuf::from("/tmp/test.rs"), None),
            false,
        )
        .expect("command should parse");
        let args: Vec<String> = command
            .get_args()
            .map(|value| value.to_string_lossy().to_string())
            .collect();

        assert_eq!(args, vec!["/tmp/test.rs".to_string()]);
    }

    #[test]
    fn test_program_requires_terminal_detects_terminal_editors() {
        assert!(TerminalAppLauncher::program_requires_terminal("nvim"));
        assert!(TerminalAppLauncher::program_requires_terminal(
            "/usr/bin/vim"
        ));
        assert!(TerminalAppLauncher::program_requires_terminal("helix"));
        assert!(!TerminalAppLauncher::program_requires_terminal("code"));
        assert!(!TerminalAppLauncher::program_requires_terminal("zed"));
    }
}