From 5e640c9a2f8c4ec435e6e9e7fd3cf989642db543 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:04:27 -0500 Subject: [PATCH 01/10] the survey does not show every fault of the rules Problem: The survey does not show every fault of the rules. Solution: Pin six more Vim9 plugins in the survey list (Galvarcus). The eleven plugins in tools/survey.txt come from few authors. A scan of DanBradbury/copilot-chat.vim shows two faults that none of them shows: a key before the command of a mapping command line, and the shell command of :!. Each is filed as a sub-issue. The list now also pins copilot-chat.vim, lifepillar/vim-colortemplate, girishji/vimsuggest, gh-tui-tools/gh-review.vim, bfrg/vim-jqplay and kennypete/vim-tene, each at the commit that the survey read. All seventeen scans exit 0. related: #140 related: #141 related: #142 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- tools/survey.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tools/survey.txt b/tools/survey.txt index 01c9eae..907a9ff 100644 --- a/tools/survey.txt +++ b/tools/survey.txt @@ -31,3 +31,21 @@ https://github.com/monkoose/vim9-stargate b87f9e6f534de5cc261205f6fc7716ec1af28e # Classes and enums. https://github.com/yegappan/tetris dfc82aba0e2ec4d4e98445934e4a68fdfbb4eb70 + +# A key before the command of a mapping command line, and the shell +# command of :!. +https://github.com/DanBradbury/copilot-chat.vim c7342e476e562282a11b683705c773a9fc49c505 + +# Large, with a parser, and legacy files beside the Vim9 ones. +https://github.com/lifepillar/vim-colortemplate 91aba2e7b230c2b0f9801e178d4115571cfbf5b2 + +# Command-line mappings and autocommands. +https://github.com/girishji/vimsuggest 540a417aa9339ed33f0533a1937f6f5d3ba417cf + +https://github.com/gh-tui-tools/gh-review.vim 89fd3000df32cf7f1b95dc0dad12ac82bfa79325 + +# Jobs and popups. +https://github.com/bfrg/vim-jqplay 968b06d95879e7fbd135b862cd4755d1e1b9a10b + +# Statusline expressions. +https://github.com/kennypete/vim-tene 602d716c5354d3a2cb504e801ae8ba09bd0e3c2c From 5be3946a53b7f3282f5a88a4afa863791712b0f8 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:37:04 -0500 Subject: [PATCH 02/10] a key before the command of a mapping command line gives it as a name Problem: A key before the command of a mapping command line gives the command as a name. Solution: Read the command of a command line past the key names before it (Galvarcus). A mapping such as `:call Foo()` types `` to clear the range that a count or a selection puts on the line, then runs `call`. The rule of #131 reads the command right behind the colon, so `` stopped it and `call` was an unplaced name. past_keys now moves the head of a command line past each key name glued behind its colon. A key name with nothing behind it ends the line and stays the head, as before. The four corpora report the same with every class on. In the survey, the one change is that `call` at plugin/copilot_chat.vim:48 of DanBradbury/copilot-chat.vim is no longer an unplaced name. fixes: #141 related: #131 related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/parse.rs | 38 ++++++++++++++++++++++++++++++++++---- tests/vim9_keys.rs | 12 ++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/lang/vim9/parse.rs b/src/lang/vim9/parse.rs index 16df441..eddcd6e 100644 --- a/src/lang/vim9/parse.rs +++ b/src/lang/vim9/parse.rs @@ -2468,9 +2468,10 @@ fn sides(stmt: &[Tok], at: usize) -> Option { /// FUNCTION: Where each command line that the right side of a mapping /// runs reads a command: behind `` or ``, and behind -/// the colon that opens a command line, past any modifier. The colon -/// of a scope, as in `g:x`, opens none. The right side of an `` -/// mapping is an expression and runs no command line. +/// the colon that opens a command line, past any key name and any +/// modifier. The colon of a scope, as in `g:x`, opens none. The right +/// side of an `` mapping is an expression and runs no command +/// line. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/131 fn map_lines(stmt: &[Tok], at: usize) -> Vec { let mut out = Vec::new(); @@ -2499,13 +2500,42 @@ fn map_lines(stmt: &[Tok], at: usize) -> Vec { stmt[k - 1].text == COLON && !(stmt[k - 1].glued && stmt[k - 2].kind == TokKind::Ident); if runs || colon { - out.extend(k..=command_from(stmt, k).unwrap_or(k)); + let head = if colon { past_keys(stmt, k) } else { k }; + + out.extend(head..=command_from(stmt, head).unwrap_or(head)); } } out } +/// FUNCTION: Where a command line that opens at `from` reads its +/// command, past the key names typed before it: `:call` clears +/// the range that a count or a selection puts on the line, then reads +/// `call`. A key name with nothing behind it ends the line and stays. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/141 +fn past_keys(stmt: &[Tok], from: usize) -> usize { + let mut at = from; + + while stmt.get(at).is_some_and(|t| t.text == "<") { + let Some(len) = stmt[at + 1..] + .iter() + .position(|t| !t.glued || t.text == ">") + else { + break; + }; + let end = at + 1 + len; + + if stmt[end].text != ">" || !stmt[end].glued || end + 1 == stmt.len() { + break; + } + + at = end + 1; + } + + at +} + /// FUNCTION: Whether a word is one of a list of key names, which Vim /// reads in any case: `` runs a command line as `` does. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/130 diff --git a/tests/vim9_keys.rs b/tests/vim9_keys.rs index d47d1a1..dbe8ade 100644 --- a/tests/vim9_keys.rs +++ b/tests/vim9_keys.rs @@ -319,6 +319,18 @@ fn the_arguments_of_the_command_line_a_mapping_runs_are_vims() { } } +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/141 +#[test] +fn a_key_between_the_colon_and_the_command_of_a_mapping_is_passed() { + let text = "vim9script\nvnoremap X :call Foo()\nnnoremap Y :silent! wincmd p\n"; + + for word in ["call", "silent", "wincmd", "p"] { + assert!(vims_or_none(text, word), "{word}"); + } + + assert!(!one(text, "Foo").owned); +} + #[test] fn a_call_on_the_command_line_a_mapping_runs_keeps_its_credit() { let text = From b2b28cc70994f0597bdd8d04972d05c5d358de79 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:56:40 -0500 Subject: [PATCH 03/10] the shell command of :! gives its words as names Problem: The shell command of :! gives its words as names. Solution: Read a bang that no modifier takes as the command :!, whose line is text (Galvarcus). The search for the command of a line skipped every bang, because a modifier takes one, as in `silent!`. A bang that stands alone is the command :!, so in `!xdg-open foo` the search went on to `xdg` and gave `xdg`, `open` and `foo` as names. The same held behind `silent` and in the text that `execute` runs. shell() now tells the command from the bang of a modifier: the bang of a modifier is glued to it. The command stops the search, and its line is text to its end, bars included, as the shell reads it. The four corpora report the same with every class on. In the survey, the one change is that `xdg` and `open` at autoload/copilot_chat/auth.vim:141 of DanBradbury/copilot-chat.vim are no longer unplaced names. fixes: #142 related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/parse.rs | 28 ++++++++++++++++++++++++---- tests/vim9_evaluated.rs | 16 ++++++++++++++++ tests/vim9_excmds.rs | 23 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/lang/vim9/parse.rs b/src/lang/vim9/parse.rs index eddcd6e..fd4df29 100644 --- a/src/lang/vim9/parse.rs +++ b/src/lang/vim9/parse.rs @@ -967,6 +967,11 @@ const CODE_ARGS: &[&str] = &[ // REFERENCE: https://github.com/Galvarcus/GhostCode/issues/12 const NORMAL: &str = "normal"; +// The command that hands the rest of its line to the shell, bars +// included, and the bang that a modifier takes, as in `silent!`. +// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/142 +const SHELL: &str = "!"; + // What the name of every mapping and unmapping command ends with: // `nnoremap`, `xmap`, `iunmap`. // REFERENCE: https://github.com/Galvarcus/GhostCode/issues/12 @@ -2288,14 +2293,20 @@ fn ends_command(stmt: &[Tok], k: usize) -> bool { /// An escaped bar ends nothing, because that is how a pattern writes /// 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. +/// token is asked. `:normal` takes every bar as a key, and `:!` hands +/// every bar to the shell. /// 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 { heads.iter().copied().filter(|at| *at < i).any(|at| { if !reads_command(stmt, at) { return false; } + if shell(stmt, at) { + return true; + } + let open = (at + 1..i).all(|k| !ends_command(stmt, k)); let word = uncounted(&stmt[at].text); @@ -2355,7 +2366,7 @@ fn text_family(name: &str) -> bool { /// FUNCTION: Whether the last command of a statement takes the rest /// of its line as text or as keys: a command of the text family, -/// `:normal`, or a mapping command. +/// `:normal`, `:!`, or a mapping command. /// /// The reader of a command line asks, because the words past a gap /// in such a line are Vim's whatever the gap holds. @@ -2369,7 +2380,8 @@ pub(super) fn owns_line(stmt: &[Tok]) -> bool { .is_some_and(|at| { let word = uncounted(&stmt[at].text); - user_command(word) + shell(stmt, at) + || user_command(word) || excmds::canonical(word) .is_some_and(|name| name == NORMAL || text_family(name) || name.ends_with(MAP)) }) @@ -2809,7 +2821,7 @@ fn command_from(stmt: &[Tok], from: usize) -> Option { loop { let text = stmt.get(at)?.text.as_str(); - if text == "!" || modifier(text) { + if (text == SHELL && !shell(stmt, at)) || modifier(text) { at += 1; } else if text == COLON { at += 1 + stmt @@ -2822,6 +2834,14 @@ fn command_from(stmt: &[Tok], from: usize) -> Option { } } +/// FUNCTION: Whether the bang at `at` is the command `:!` and not the +/// bang of a modifier glued to it, as in `silent!`. The shell reads +/// the rest of the line, so its words are never the project's. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/142 +fn shell(stmt: &[Tok], at: usize) -> bool { + stmt[at].text == SHELL && !(stmt[at].glued && at > 0 && modifier(&stmt[at - 1].text)) +} + /// 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_evaluated.rs b/tests/vim9_evaluated.rs index 8349edc..9f79a48 100644 --- a/tests/vim9_evaluated.rs +++ b/tests/vim9_evaluated.rs @@ -179,3 +179,19 @@ fn a_string_in_the_brackets_of_function_still_names_a_function() { assert_eq!(count(text, "Helper"), 1); } + +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/142 +#[test] +fn the_shell_command_of_an_executed_bang_is_vims() { + let text = "vim9script\ndef Open(url: string)\n execute '!xdg-open ' .. url\nenddef\n"; + + for name in ["xdg", "open"] { + assert!( + refs(text) + .iter() + .filter(|r| r.name == name) + .all(|r| r.owned), + "{name}" + ); + } +} diff --git a/tests/vim9_excmds.rs b/tests/vim9_excmds.rs index 5a077b8..ace8724 100644 --- a/tests/vim9_excmds.rs +++ b/tests/vim9_excmds.rs @@ -574,3 +574,26 @@ fn a_dot_glued_to_a_word_named_like_a_command_reaches_into_it() { assert!(refs(text).iter().any(|r| r.name == "main" && r.member)); } + +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/142 +#[test] +fn the_shell_command_of_a_bang_is_vims() { + let text = "vim9script\n!xdg-open foo\nsilent !xdg-open bar\n:!xdg-open baz\n"; + + for name in ["xdg", "open", "foo", "bar", "baz"] { + 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"; + + assert!(!one(text, "Helper").owned); +} From 815b665856474ef2ea38ec8a166268365af56312 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:32:00 -0500 Subject: [PATCH 04/10] a :command behind a colon in a lambda block gives its words as names Problem: A :command behind a colon in a lambda block gives its words as names. Solution: Read the word of a line past the colon of a command when the lexer asks whether the line opens a block (Galvarcus). The rules of a :command line read the head of a statement alone. A `command` line in a lambda block is a statement of its own, because `command` is one of the words that end the statement before them. The lexer read the first word of the line for that, and `:command!` gives none, so the line stayed in the statement of the lambda, and its attributes, its name and `` were names. opens_block now reads the word past a colon that opens the line, as Vim does: `:command!` is `command!`. The colon of a conditional cannot stand there, because Vim9 needs a space behind it. The four corpora report the same with every class on. In the survey, girishji/vimsuggest gives 78 fewer unplaced names in each scan, and nothing else changes. fixes: #146 related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/lex.rs | 17 +++++++++++++---- tests/vim9_excmds.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/lang/vim9/lex.rs b/src/lang/vim9/lex.rs index 9bbb683..2ef475a 100644 --- a/src/lang/vim9/lex.rs +++ b/src/lang/vim9/lex.rs @@ -74,6 +74,10 @@ const ARROW: &str = "=>"; // REFERENCE: https://github.com/Galvarcus/GhostCode/issues/116 const KEY_COLON: char = ':'; +// What may open a command line before its command, as in `:command!`. +// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/146 +const COMMAND_COLON: char = ':'; + // The words that open or close a block. A bracket left unclosed by a // broken line must not swallow the file, so one of these at the start // of a line ends the statement before it, whatever the bracket depth. @@ -281,12 +285,17 @@ pub fn tokens(text: &str) -> Vec { /// FUNCTION: True when a line opens or closes a block, which ends any /// statement a broken line left open. A word with a colon against it /// is the key of a dictionary, as `command: 'x',` is, and opens -/// nothing. +/// nothing. A colon before the word changes nothing, as `:command!` +/// is `command!`. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/116 +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/146 fn opens_block(line: &str) -> bool { - RESYNC.iter().any(|word| { - opens_with(line, word) && !line.trim_start()[word.len()..].starts_with(KEY_COLON) - }) + let line = line.trim_start(); + let line = line.strip_prefix(COMMAND_COLON).unwrap_or(line); + + RESYNC + .iter() + .any(|word| opens_with(line, word) && !line[word.len()..].starts_with(KEY_COLON)) } /// The kind of body a line stands in. diff --git a/tests/vim9_excmds.rs b/tests/vim9_excmds.rs index ace8724..d294085 100644 --- a/tests/vim9_excmds.rs +++ b/tests/vim9_excmds.rs @@ -549,6 +549,24 @@ fn a_command_definition_behind_a_colon_declares_its_name() { assert!(refs(text).iter().all(|r| r.owned), "{:?}", refs(text)); } +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/146 +#[test] +fn a_command_definition_behind_a_colon_in_a_lambda_block_declares_its_name() { + let text = "vim9script\ndef Do(...a: list)\nenddef\nvar h: list = []\nh->add(() => {\n :command! -nargs=* VSFind Do()\n})\n"; + + for name in ["nargs", "VSFind", "f", "args"] { + assert!( + refs(text) + .iter() + .filter(|r| r.name == name) + .all(|r| r.owned), + "{name}" + ); + } + + assert!(!one(text, "Do").owned); +} + #[test] fn the_variable_of_a_for_in_a_command_body_is_bound() { let text = "vim9script\ncommand! -nargs=0 ShowAll for w in ['a'] | echo w | endfor\n"; From fa01e03f1778ba23dc0c1730bd1c7c55e5c654bc Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:53:00 -0500 Subject: [PATCH 05/10] a comment behind the brace of a lambda block makes the next key a name Problem: A comment behind the brace of a lambda block makes the next key of the dictionary a name. Solution: Read whether a line opens a lambda block from its tokens, past a comment (Galvarcus). The lexer records a lambda block where its line ends with `=> {`, so that a block word inside the lambda returns to the brackets around it when the lambda closes (#96). A comment behind the brace hid that end. The `if` inside the lambda then cleared the depth of the brackets, the statement ended at the `},` of the lambda, and the next key of the dictionary opened a statement of its own, as a name. opens_lambda now reads the last tokens of the line other than a comment: the arrow, then the brace. The four corpora report the same with every class on. In the survey, the one change is that `close_cb` at autoload/vimsuggest/addons/job.vim:34 of girishji/vimsuggest is no longer an unplaced name. fixes: #147 related: #96 related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/lex.rs | 19 +++++++++++++++---- tests/vim9_syntax.rs | 9 +++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/lang/vim9/lex.rs b/src/lang/vim9/lex.rs index 2ef475a..a8d547b 100644 --- a/src/lang/vim9/lex.rs +++ b/src/lang/vim9/lex.rs @@ -60,10 +60,10 @@ const PARAMS: char = '('; const LEGACY_COMMENT: char = '"'; const LEGACY_CARRIED: &str = "\"\\ "; -// What ends a line that opens a block lambda, and what opens the line -// that closes one. +// What ends a line that opens a block lambda, behind its arrow, and +// what opens the line that closes one. // REFERENCE: https://github.com/Galvarcus/GhostCode/issues/96 -const LAMBDA_BLOCK: &str = "=> {"; +const BLOCK_OPEN: &str = "{"; const BLOCK_CLOSE: char = '}'; // The arrow of a lambda, behind which Vim9 carries a line on. @@ -258,7 +258,7 @@ pub fn tokens(text: &str) -> Vec { // A line that ends by opening a block lambda leaves its depth // behind, and a lambda that closed takes its own away. - if rest.trim_end().ends_with(LAMBDA_BLOCK) { + if opens_lambda(&toks[from..]) { lambdas.push(depth); } while lambdas.last().is_some_and(|open| depth < *open) && set_aside.len() < lambdas.len() { @@ -282,6 +282,17 @@ pub fn tokens(text: &str) -> Vec { toks } +/// FUNCTION: Whether the tokens of a line end by opening a block +/// lambda: the arrow, then the brace, with nothing behind them but a +/// comment. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/147 +fn opens_lambda(line: &[Tok]) -> bool { + let mut code = line.iter().rev().filter(|t| t.kind != TokKind::Comment); + + code.next().is_some_and(|t| t.text == BLOCK_OPEN) + && code.next().is_some_and(|t| t.text == ARROW) +} + /// FUNCTION: True when a line opens or closes a block, which ends any /// statement a broken line left open. A word with a colon against it /// is the key of a dictionary, as `command: 'x',` is, and opens diff --git a/tests/vim9_syntax.rs b/tests/vim9_syntax.rs index e7a88d9..cdd0f1c 100644 --- a/tests/vim9_syntax.rs +++ b/tests/vim9_syntax.rs @@ -173,6 +173,15 @@ fn a_dict_key_after_a_block_lambda_with_an_if_is_a_key() { assert!(!names.iter().any(|n| n == "err_msg"), "{names:?}"); } +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/147 +#[test] +fn a_dict_key_after_a_block_lambda_with_a_comment_behind_its_brace_is_a_key() { + let text = "vim9script\nvar o = {\n out_cb: (ch, msg) => { # each line\n if msg == \"\"\n echo 1\n endif\n },\n close_cb: (ch) => {\n echo 2\n },\n}\necho o\n"; + let names: Vec = refs(text).into_iter().map(|r| r.name).collect(); + + assert!(!names.iter().any(|n| n == "close_cb"), "{names:?}"); +} + #[test] fn a_dict_argument_after_a_block_lambda_with_an_if_is_a_dict() { let text = "vim9script\nvar t = timer_start(20, (_) => {\n if 1\n echo 1\n endif\n}, {repeat: 0})\n"; From a9257fb26c6915ac6c723a8a3582760c4c6e5375 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:33:57 -0500 Subject: [PATCH 06/10] the text of a :! command line runs past its end Problem: The text of a :! command line runs past its end. Solution: End the shell text of a :! at the end of its line, and in a mapping at the first key name or bar (Galvarcus). The rule of #142 read every word behind a :! as shell text, to the end of the statement. In a mapping, the command line of the shell ends at the `` that runs it, and the mapping ends at a bar. In a lambda block, it ends with its line. So `:!git pull:call Missing()`, `:!ctags -R .| call Missing()` and a `Missing()` on a later line of the block marked `Missing` as a word of Vim, and a call to a function that does not exist was no longer counted. shell_text now bounds the text: the line of the :!, and in a mapping the tokens before the first key name or bar. A key name such as `` that the shell would read ends the text as well, which leaves a word counted and drops none. The four corpora and the survey report the same as before. fixes: #150 related: #142 related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/parse.rs | 18 ++++++++++++++++-- tests/vim9_excmds.rs | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) 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"; From b88369a1df9abcd9fc2744f8bdb793f839504a0c Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:02:59 -0500 Subject: [PATCH 07/10] a comment behind the arrow of a lambda ends the statement Problem: A comment behind the arrow of a lambda ends the statement. Solution: Read whether a line ends with the arrow from its tokens, past a comment (Galvarcus). Vim9 carries a line that ends with `=>` on to the body of the lambda on the next line (#94). The lexer read that end from the raw text, so a comment behind the arrow hid it. The statement then ended at the arrow, and the body became a statement of its own, where the parameters of the lambda were names. code_ends now reads the last tokens of a line other than a comment, for the arrow alone and for the arrow and the brace that open a block lambda (#147), which it replaces. The four corpora and the survey report the same as before. fixes: #151 related: #94 related: #147 related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/lex.rs | 19 +++++++++++-------- tests/vim9_syntax.rs | 9 +++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/lang/vim9/lex.rs b/src/lang/vim9/lex.rs index a8d547b..3a6fa41 100644 --- a/src/lang/vim9/lex.rs +++ b/src/lang/vim9/lex.rs @@ -258,7 +258,7 @@ pub fn tokens(text: &str) -> Vec { // A line that ends by opening a block lambda leaves its depth // behind, and a lambda that closed takes its own away. - if opens_lambda(&toks[from..]) { + if code_ends(&toks[from..], &[ARROW, BLOCK_OPEN]) { lambdas.push(depth); } while lambdas.last().is_some_and(|open| depth < *open) && set_aside.len() < lambdas.len() { @@ -272,7 +272,7 @@ pub fn tokens(text: &str) -> Vec { if depth <= 0 { depth = 0; - if !rest.trim_end().ends_with(ARROW) { + if !code_ends(&toks[from..], &[ARROW]) { push_newline(&mut toks, line); } } @@ -282,15 +282,18 @@ pub fn tokens(text: &str) -> Vec { toks } -/// FUNCTION: Whether the tokens of a line end by opening a block -/// lambda: the arrow, then the brace, with nothing behind them but a -/// comment. +/// FUNCTION: Whether the code of a line ends with these tokens, in +/// order, with nothing behind them but a comment: the arrow of a +/// lambda whose body stands on the next line, or the arrow and the +/// brace that open a block lambda. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/147 -fn opens_lambda(line: &[Tok]) -> bool { +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/151 +fn code_ends(line: &[Tok], tail: &[&str]) -> bool { let mut code = line.iter().rev().filter(|t| t.kind != TokKind::Comment); - code.next().is_some_and(|t| t.text == BLOCK_OPEN) - && code.next().is_some_and(|t| t.text == ARROW) + tail.iter() + .rev() + .all(|want| code.next().is_some_and(|t| t.text == *want)) } /// FUNCTION: True when a line opens or closes a block, which ends any diff --git a/tests/vim9_syntax.rs b/tests/vim9_syntax.rs index cdd0f1c..c3e033b 100644 --- a/tests/vim9_syntax.rs +++ b/tests/vim9_syntax.rs @@ -215,6 +215,15 @@ fn a_lambda_parameter_read_on_the_next_line_is_no_reference() { assert!(!names.iter().any(|n| n == "lnum"), "{names:?}"); } +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/151 +#[test] +fn a_lambda_parameter_read_after_a_comment_behind_the_arrow_is_no_reference() { + let text = "vim9script\nvar Add = (lnum: number) => # one more\n lnum + 1\necho Add(1)\n"; + let names: Vec = refs(text).into_iter().map(|r| r.name).collect(); + + assert!(!names.iter().any(|n| n == "lnum"), "{names:?}"); +} + /// A string inside the braces of an interpolated string belongs to the /// code there, and its quotes end nothing. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/95 From 23dda0aa0b78a3a6ce4bb610e52a2805b4ab94d1 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:23:17 -0500 Subject: [PATCH 08/10] a dot that ends a line of a lambda block reaches into the next line Problem: A dot that ends a line of a lambda block reaches into the next line. Solution: Read a word as a member only where the dot before it stands on its line (Galvarcus). The lines of a lambda block stay in one statement, so a dot that ended one line stood right before the first word of the next. The analyzer read that word as a member: after `:!ctags -R .` a call `Missing()` was `R.Missing`, a member that resolves to no function, so a call to a function that does not exist gave no unplaced name, and after `edit .` a call to a function of the project could leave the function a ghost. Vim9 allows no space behind the dot of a member, and the end of a line is one. behind_dot now asks that the dot and the word share a line, for the member flag and for the type of the receiver. A dot that opens a line still reaches into the line before it. The four corpora and the survey report the same as before. A word on the line below `edit .` is still read as an argument of `edit`, which is #155. fixes: #153 related: #140 related: #155 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/parse.rs | 2 +- src/lang/vim9/types.rs | 11 ++++++++++- tests/vim9_syntax.rs | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/lang/vim9/parse.rs b/src/lang/vim9/parse.rs index 05e2963..71e6ff9 100644 --- a/src/lang/vim9/parse.rs +++ b/src/lang/vim9/parse.rs @@ -1516,7 +1516,7 @@ impl Collect<'_> { return; } - let member = i > 0 && stmt[i - 1].text == DOT; + let member = types::behind_dot(stmt, i); let owned = vims || (member && word == NEW) diff --git a/src/lang/vim9/types.rs b/src/lang/vim9/types.rs index 217cb06..db3470e 100644 --- a/src/lang/vim9/types.rs +++ b/src/lang/vim9/types.rs @@ -640,6 +640,15 @@ fn idents(stmt: &[Tok]) -> Vec<&str> { .collect() } +/// FUNCTION: Whether the word at `at` stands behind the dot of a +/// member. Vim9 allows no space behind that dot, and the end of a +/// line is one, so a dot that ends a line reaches into nothing on the +/// next, as the `.` of `:!ctags -R .` on a line of a lambda block. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/153 +pub fn behind_dot(stmt: &[Tok], at: usize) -> bool { + at > 0 && stmt[at - 1].text == "." && stmt[at - 1].line == stmt[at].line +} + /// FUNCTION: The type of the receiver of a member access at a /// position, or none when nothing in scope gives it one. /// @@ -652,7 +661,7 @@ pub fn recv_type( locals: &Locals, types: &Types, ) -> Option { - if at < 2 || stmt[at - 1].text != "." { + if at < 2 || !behind_dot(stmt, at) { return None; } diff --git a/tests/vim9_syntax.rs b/tests/vim9_syntax.rs index c3e033b..b82508c 100644 --- a/tests/vim9_syntax.rs +++ b/tests/vim9_syntax.rs @@ -264,3 +264,22 @@ fn a_project_function_in_a_call_string_is_still_a_name() { assert_eq!(marks, vec![false]); } + +/// Vim9 allows no space behind the dot of a member, and the end of a +/// line is one, so a dot that ends a line reaches into nothing. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/153 +#[test] +fn a_dot_that_ends_a_line_of_a_lambda_block_reaches_into_nothing() { + let text = "vim9script\nvar F = () => {\n :!ctags -R .\n Missing()\n}\n"; + let missing = one(text, "Missing"); + + assert!(!missing.member && missing.recv.is_none(), "{missing:?}"); +} + +#[test] +fn a_dot_that_opens_a_line_still_reaches_into_the_line_before() { + let text = + "vim9script\ndef Make(): dict\n return {}\nenddef\nvar x = Make()\n .Value()\n"; + + assert!(one(text, "Value").member); +} From 5cb43de5a6ea192127bd946501d8239aff5014e4 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:58:41 -0500 Subject: [PATCH 09/10] the text arguments of a command in a lambda block run onto later lines Problem: The text arguments of a command in a lambda block run onto later lines. Solution: End the text of a command at a head that opens a line of the block (Galvarcus). The lines of a lambda block stay in one statement. text_args read a word as an argument of every text command before it in the statement, up to a bar, and `:normal` past every bar. So after `edit foo`, `highlight Foo guifg=red` or `normal! gg`, a call `Missing()` on the next line of the block was marked as a word of Vim, and a call to a function that does not exist gave no unplaced name. opens_block_line now tells a head that opens a line of the block, and the text of a command before it ends there. A head that a backslash carries on to a line of its own ends the text as well, which leaves a word counted and drops none. The four corpora and the survey report the same as before. fixes: #155 related: #140 related: #153 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- src/lang/vim9/parse.rs | 17 +++++++++++++++++ tests/vim9_excmds.rs | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/lang/vim9/parse.rs b/src/lang/vim9/parse.rs index 71e6ff9..5b93403 100644 --- a/src/lang/vim9/parse.rs +++ b/src/lang/vim9/parse.rs @@ -2303,6 +2303,13 @@ pub(super) fn text_args(stmt: &[Tok], i: usize, heads: &[usize]) -> bool { return false; } + // A line of a lambda block opens a command of its own, so the + // text of a command ends with its line, where no bar ends it. + // REFERENCE: https://github.com/Galvarcus/GhostCode/issues/155 + if (at + 1..=i).any(|k| opens_block_line(stmt, k, heads)) { + return false; + } + if shell(stmt, at) { return shell_text(stmt, at, i, heads); } @@ -2325,6 +2332,16 @@ pub(super) fn text_args(stmt: &[Tok], i: usize, heads: &[usize]) -> bool { }) } +/// FUNCTION: Whether the token at `k` opens a line of a lambda block: +/// a head on a line that the token before it does not share, as +/// `block_lines` gives. A head that a backslash carries on to a line +/// of its own ends the text as well, which leaves a word counted and +/// drops none. +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/155 +fn opens_block_line(stmt: &[Tok], k: usize, heads: &[usize]) -> bool { + k > 0 && stmt[k].line != stmt[k - 1].line && heads.contains(&k) +} + /// FUNCTION: A command word without the count that the lexer glues to /// it, as the `2` of `:2Diag prev`. /// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/111 diff --git a/tests/vim9_excmds.rs b/tests/vim9_excmds.rs index 1bb8e1b..7ac3963 100644 --- a/tests/vim9_excmds.rs +++ b/tests/vim9_excmds.rs @@ -638,3 +638,26 @@ fn a_call_behind_the_bang_of_a_modifier_keeps_its_credit() { assert!(!one(text, "Helper").owned); } + +/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/155 +#[test] +fn the_text_arguments_of_a_command_in_a_lambda_block_end_with_its_line() { + let text = "vim9script\nvar F = () => {\n edit foo\n Missing()\n highlight Foo guifg=red\n Missing()\n normal! gg\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 ["foo", "Foo", "guifg", "red", "gg"] { + assert!( + refs(text) + .iter() + .filter(|r| r.name == name) + .all(|r| r.owned), + "{name}" + ); + } +} From ef5dabce4f69c6a0ef06a1afb1c6c8857c54f070 Mon Sep 17 00:00:00 2001 From: Galvarcus <117151435+Galvarcus@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:11:39 -0500 Subject: [PATCH 10/10] the version names a release without the second survey fixes Problem: The version names a release without the fixes that the second survey of other plugins found. Solution: Raise the version to 0.7.1 (Galvarcus). Every change since v0.7.0 corrects a rule, so the patch rises. The analyzer no longer reports a function as a ghost when a lambda block calls it on the line below a dot, as after `edit .`. It now counts calls to functions that do not exist where it hid them, on the lines of a lambda block below a text command or below a line that ends with a dot. It also places more names. A key before the command of a mapping command line, the shell command of :!, a :command behind a colon in a lambda block, and a comment behind the arrow or the brace of a lambda no longer give unplaced names. The survey list pins six more plugins. Every parity fixture still matches. The gate compares the version with the tag, so v0.7.1 is tagged on the merge into main and not before it. related: #140 Assisted-by: Claude Opus 5 Signed-off-by: Galvarcus <117151435+Galvarcus@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f0fbba..9b4c875 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,7 +156,7 @@ checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" [[package]] name = "ghostcode" -version = "0.7.0" +version = "0.7.1" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 13dda3c..2c5d28d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghostcode" -version = "0.7.0" +version = "0.7.1" edition = "2021" description = "Finds unreferenced code in Vim9 plugins." license = "GPL-3.0"