From 8d4e016cb010e26310f779f4afc0fc744c738578 Mon Sep 17 00:00:00 2001 From: KaKi87 Date: Wed, 2 Dec 2026 00:04:59 +0100 Subject: [PATCH] fix(terminal): keep bash integration working with a trailing ";" in PROMPT_COMMAND and with ble.sh (#7310) The bash integration appended ";__openidea_prompt_end" to PROMPT_COMMAND, so a user value ending in "; " became "; ;", a syntax error at every prompt that also lost the prompt-end mark. Trailing semicolons and spaces are now stripped first. ble.sh decodes CSI sequences itself, so the readline bind of the line-clearing key never fired and vi_nmap warned about kill-whole-line. With ble.sh the sequence is mapped to F20 and bound in its emacs, vi_imap and vi_nmap keymaps. Co-authored-by: KaKi87 <21284089+KaKi87@users.noreply.github.com> --- crates/ide-terminal/src/integration.rs | 24 ++++++++++++++++++--- crates/ide-terminal/src/lib.rs | 30 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/crates/ide-terminal/src/integration.rs b/crates/ide-terminal/src/integration.rs index bd0c2d0ec2..876fc24afd 100644 --- a/crates/ide-terminal/src/integration.rs +++ b/crates/ide-terminal/src/integration.rs @@ -98,10 +98,28 @@ __openidea_prompt_end() { *) PS1="${PS1}\[\e]133;B\a\]" ;; esac } -PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND;}__openidea_prompt_end" +# Drop trailing ";" and spaces first: "a; ;b" is a syntax error in PROMPT_COMMAND. +__openidea_pc="${PROMPT_COMMAND:-}" +while :; do + case "$__openidea_pc" in + *\;|*[[:space:]]) __openidea_pc="${__openidea_pc%?}" ;; + *) break ;; + esac +done +PROMPT_COMMAND="${__openidea_pc:+$__openidea_pc;}__openidea_prompt_end" +unset __openidea_pc # Run Highlighted Command empties the line with this key, in emacs and vi modes alike. -for __openidea_map in emacs vi-insert vi-command; do bind -m "$__openidea_map" '"\e[9999~": kill-whole-line'; done -unset __openidea_map +if [ -n "${BLE_VERSION-}" ]; then + # ble.sh decodes CSI sequences itself, so a readline bind of this one never fires + # (and vi_nmap has no kill-whole-line). Map it to F20 and bind that in ble.sh's keymaps. + ble-bind --csi '9999~' f20 + ble-bind -m emacs -f f20 kill-line + ble-bind -m vi_imap -f f20 kill-line + ble-bind -m vi_nmap -f f20 vi_nmap/kill-current-line +else + for __openidea_map in emacs vi-insert vi-command; do bind -m "$__openidea_map" '"\e[9999~": kill-whole-line'; done + unset __openidea_map +fi "#; const FISH_INIT: &str = concat!( diff --git a/crates/ide-terminal/src/lib.rs b/crates/ide-terminal/src/lib.rs index 06041cc37b..dcbe7bd808 100644 --- a/crates/ide-terminal/src/lib.rs +++ b/crates/ide-terminal/src/lib.rs @@ -1471,6 +1471,36 @@ mod tests { check_prompt_integration("/bin/bash", "set -o vi"); } + /// A user `PROMPT_COMMAND` ending in "; " (`history -a; $PROMPT_COMMAND` with the variable + /// empty) used to leave "; ;" once the integration appended to it: a syntax error at every + /// prompt, and no OSC 133 B. + #[cfg(unix)] + #[test] + fn bash_integration_survives_a_trailing_semicolon_in_prompt_command() { + if !Path::new("/bin/bash").exists() { + return; + } + let home = std::env::temp_dir().join(format!("openidea-prompt-command-test-{}", std::process::id())); + std::fs::create_dir_all(&home).unwrap(); + // Login shells read .bash_profile, the others .bashrc. + for rc in [".bashrc", ".bash_profile"] { + std::fs::write(home.join(rc), "PROMPT_COMMAND=\"history -a; \"\n").unwrap(); + } + let h = harness(); + let mut opts = sh(""); + opts.shell = Some("/bin/bash".into()); + opts.args = Vec::new(); + opts.shell_integration = true; + opts.env.insert("HOME".into(), home.to_string_lossy().into_owned()); + let (info, rx) = spawn_collect(&h, opts); + let mut out = read_until(&rx, "\x1b]133;B\x07"); + h.manager.write(info.id, "exit\n").unwrap(); + let _ = wait_exit(&h, info.id); + out.extend(rx.try_iter()); + assert!(!out.contains("syntax error"), "{out:?}"); + let _ = std::fs::remove_dir_all(&home); + } + #[cfg(unix)] #[test] fn zsh_integration_marks_the_prompt_and_clears_the_line() {