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
19 changes: 11 additions & 8 deletions src/lang/vim9/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,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 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() {
Expand All @@ -272,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 @@ -282,15 +282,18 @@ pub fn tokens(text: &str) -> Vec<Tok> {
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
Expand Down
9 changes: 9 additions & 0 deletions tests/vim9_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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
Expand Down
Loading