Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5e640c9
the survey does not show every fault of the rules
Galvarcus Nov 15, 2026
12b0549
Merge pull request #143 from Galvarcus/140-survey-six
Galvarcus Nov 15, 2026
5be3946
a key before the command of a mapping command line gives it as a name
Galvarcus Nov 15, 2026
f6142cc
Merge pull request #144 from Galvarcus/141-map-key-before-cmd
Galvarcus Nov 15, 2026
b2b28cc
the shell command of :! gives its words as names
Galvarcus Nov 15, 2026
1558093
Merge pull request #145 from Galvarcus/142-shell-bang
Galvarcus Nov 15, 2026
815b665
a :command behind a colon in a lambda block gives its words as names
Galvarcus Nov 15, 2026
eb10217
Merge pull request #148 from Galvarcus/146-colon-command-block
Galvarcus Nov 15, 2026
fa01e03
a comment behind the brace of a lambda block makes the next key a name
Galvarcus Nov 15, 2026
dedbfd0
Merge pull request #149 from Galvarcus/147-comment-after-lambda-brace
Galvarcus Nov 15, 2026
a9257fb
the text of a :! command line runs past its end
Galvarcus Nov 15, 2026
7f19d27
Merge pull request #152 from Galvarcus/150-shell-line-end
Galvarcus Nov 15, 2026
b88369a
a comment behind the arrow of a lambda ends the statement
Galvarcus Nov 15, 2026
4074636
Merge pull request #154 from Galvarcus/151-comment-after-arrow
Galvarcus Nov 15, 2026
23dda0a
a dot that ends a line of a lambda block reaches into the next line
Galvarcus Nov 15, 2026
2a0a7aa
Merge pull request #156 from Galvarcus/153-dot-line-end
Galvarcus Nov 15, 2026
5cb43de
the text arguments of a command in a lambda block run onto later lines
Galvarcus Nov 15, 2026
a206cb5
Merge pull request #157 from Galvarcus/155-block-line-args
Galvarcus Nov 15, 2026
ef5dabc
the version names a release without the second survey fixes
Galvarcus Nov 15, 2026
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
41 changes: 32 additions & 9 deletions src/lang/vim9/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -254,7 +258,7 @@ pub fn tokens(text: &str) -> Vec<Tok> {

// 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() {
Expand All @@ -268,7 +272,7 @@ pub fn tokens(text: &str) -> Vec<Tok> {
if depth <= 0 {
depth = 0;

if !rest.trim_end().ends_with(ARROW) {
if !code_ends(&toks[from..], &[ARROW]) {
push_newline(&mut toks, line);
}
}
Expand All @@ -278,15 +282,34 @@ pub fn tokens(text: &str) -> Vec<Tok> {
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.
Expand Down
99 changes: 90 additions & 9 deletions src/lang/vim9/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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))
})
Expand Down Expand Up @@ -2468,9 +2497,10 @@ fn sides(stmt: &[Tok], at: usize) -> Option<Sides> {

/// FUNCTION: Where each command line that the right side of a mapping
/// runs reads a command: behind `<ScriptCmd>` or `<Cmd>`, 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 `<expr>`
/// 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 `<expr>` 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<usize> {
let mut out = Vec::new();
Expand Down Expand Up @@ -2499,13 +2529,42 @@ fn map_lines(stmt: &[Tok], at: usize) -> Vec<usize> {
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: `:<C-u>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: `<cmd>` runs a command line as `<Cmd>` does.
/// REFERENCE: https://github.com/Galvarcus/GhostCode/issues/130
Expand Down Expand Up @@ -2779,7 +2838,7 @@ fn command_from(stmt: &[Tok], from: usize) -> Option<usize> {
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
Expand All @@ -2792,6 +2851,28 @@ fn command_from(stmt: &[Tok], from: usize) -> Option<usize> {
}
}

/// 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 `<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
11 changes: 10 additions & 1 deletion src/lang/vim9/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -652,7 +661,7 @@ pub fn recv_type(
locals: &Locals,
types: &Types,
) -> Option<String> {
if at < 2 || stmt[at - 1].text != "." {
if at < 2 || !behind_dot(stmt, at) {
return None;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/vim9_evaluated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
);
}
}
Loading
Loading