diff --git a/src/lang/vim9/parse.rs b/src/lang/vim9/parse.rs index fd4df29..05e2963 100644 --- a/src/lang/vim9/parse.rs +++ b/src/lang/vim9/parse.rs @@ -2294,7 +2294,7 @@ fn ends_command(stmt: &[Tok], k: usize) -> bool { /// its alternation. `heads` still lists the word behind one, so the /// nearest head cannot be trusted here and every head before the /// token is asked. `:normal` takes every bar as a key, and `:!` hands -/// every bar to the shell. +/// the rest of its line to the shell, which `shell_text` bounds. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/12 /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/142 pub(super) fn text_args(stmt: &[Tok], i: usize, heads: &[usize]) -> bool { @@ -2304,7 +2304,7 @@ pub(super) fn text_args(stmt: &[Tok], i: usize, heads: &[usize]) -> bool { } if shell(stmt, at) { - return true; + return shell_text(stmt, at, i, heads); } let open = (at + 1..i).all(|k| !ends_command(stmt, k)); @@ -2842,6 +2842,20 @@ fn shell(stmt: &[Tok], at: usize) -> bool { stmt[at].text == SHELL && !(stmt[at].glued && at > 0 && modifier(&stmt[at - 1].text)) } +/// FUNCTION: Whether the token at `i` is shell text of the `:!` at +/// `at`. The shell reads to the end of the line, bars included, but in +/// a mapping the command line ends at the key that runs it, as ``, +/// and the mapping ends at a bar. A key name ends the text there even +/// where the shell would read it, as ``, which leaves a word +/// counted and drops none. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/150 +fn shell_text(stmt: &[Tok], at: usize, i: usize, heads: &[usize]) -> bool { + let mapping = heads.iter().any(|h| *h < at && maps(stmt, *h)); + + stmt[i].line == stmt[at].line + && !(mapping && (at + 1..i).any(|k| stmt[k].text == "<" || ends_command(stmt, k))) +} + /// FUNCTION: Whether a token is part of a range: the whole buffer, a /// line number, the current or the last line, an offset, or the /// separator between two addresses. diff --git a/tests/vim9_excmds.rs b/tests/vim9_excmds.rs index d294085..1bb8e1b 100644 --- a/tests/vim9_excmds.rs +++ b/tests/vim9_excmds.rs @@ -609,6 +609,29 @@ fn the_shell_command_of_a_bang_is_vims() { } } +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/150 +#[test] +fn the_shell_text_of_a_bang_ends_with_its_command_line() { + let text = "vim9script\nnnoremap :!ctags -R .| call Missing()\nnnoremap x :!git pull:call Missing()\nvar F = () => {\n :!ctags -R\n Missing()\n}\n"; + let missing: Vec = refs(text) + .into_iter() + .filter(|r| r.name == "Missing") + .collect(); + + assert_eq!(missing.len(), 3, "{missing:?}"); + assert!(missing.iter().all(|r| !r.owned), "{missing:?}"); + + for name in ["ctags", "git", "pull"] { + assert!( + refs(text) + .iter() + .filter(|r| r.name == name) + .all(|r| r.owned), + "{name}" + ); + } +} + #[test] fn a_call_behind_the_bang_of_a_modifier_keeps_its_credit() { let text = "vim9script\ndef Helper()\nenddef\nsilent! Helper()\n";