From 84228a612e680afa47202e7749dda9ab0dd9ff5c Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Mon, 20 Mar 2023 20:24:31 +0100 Subject: [PATCH] improve source location of block scope with invalid ast --- src/analysis.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/analysis.zig b/src/analysis.zig index 76f025a..b1a0c64 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -2692,9 +2692,20 @@ fn makeScopeInternal(context: ScopeContext, node_idx: Ast.Node.Index) error{OutO .block_two_semicolon, => { const first_token = tree.firstToken(node_idx); + const last_token = ast.lastToken(tree, node_idx); + + // the last token may not always be the closing brace because of broken ast + // so we look at most 16 characters ahead to find the closing brace + // TODO this should automatically be done by `ast.lastToken` + var end_index = offsets.tokenToLoc(tree, last_token).start; + const lookahead_buffer = tree.source[end_index..@min(tree.source.len, end_index + 16)]; + end_index += std.mem.indexOfScalar(u8, lookahead_buffer, '}') orelse 0; const scope_index = try context.pushScope( - offsets.nodeToLoc(tree, node_idx), + .{ + .start = offsets.tokenToIndex(tree, main_tokens[node_idx]), + .end = end_index, + }, .{ .block = node_idx }, ); defer context.popScope();