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" diff --git a/src/lang/vim9/lex.rs b/src/lang/vim9/lex.rs index 9bbb683..3a6fa41 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. @@ -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. @@ -254,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 code_ends(&toks[from..], &[ARROW, BLOCK_OPEN]) { lambdas.push(depth); } while lambdas.last().is_some_and(|open| depth < *open) && set_aside.len() < lambdas.len() { @@ -268,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); } } @@ -278,15 +282,34 @@ pub fn tokens(text: &str) -> Vec { toks } +/// 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 +/// 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); + + 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 /// 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/src/lang/vim9/parse.rs b/src/lang/vim9/parse.rs index 16df441..5b93403 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 @@ -1511,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) @@ -2288,14 +2293,27 @@ 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 +/// 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 { heads.iter().copied().filter(|at| *at < i).any(|at| { if !reads_command(stmt, at) { 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); + } + let open = (at + 1..i).all(|k| !ends_command(stmt, k)); let word = uncounted(&stmt[at].text); @@ -2314,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 @@ -2355,7 +2383,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 +2397,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)) }) @@ -2468,9 +2497,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 +2529,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 @@ -2779,7 +2838,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 @@ -2792,6 +2851,28 @@ 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 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/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_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..7ac3963 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"; @@ -574,3 +592,72 @@ 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}" + ); + } +} + +/// 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"; + + 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}" + ); + } +} 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 = diff --git a/tests/vim9_syntax.rs b/tests/vim9_syntax.rs index e7a88d9..b82508c 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"; @@ -206,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 @@ -246,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); +} 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