Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/lang/vim9/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
Expand Down Expand Up @@ -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 `<CR>`,
/// and the mapping ends at a bar. A key name ends the text there even
/// where the shell would read it, as `<Bar>`, 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.
Expand Down
23 changes: 23 additions & 0 deletions tests/vim9_excmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <F5> :!ctags -R .<CR>| call Missing()\nnnoremap x :!git pull<CR>:call Missing()<CR>\nvar F = () => {\n :!ctags -R\n Missing()\n}\n";
let missing: Vec<Reference> = 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";
Expand Down
Loading