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