improve source location of block scope with invalid ast

This commit is contained in:
Techatrix 2023-03-20 20:24:31 +01:00 committed by Lee Cannon
parent 7e652a5527
commit 84228a612e

View File

@ -2692,9 +2692,20 @@ fn makeScopeInternal(context: ScopeContext, node_idx: Ast.Node.Index) error{OutO
.block_two_semicolon, .block_two_semicolon,
=> { => {
const first_token = tree.firstToken(node_idx); 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( 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 }, .{ .block = node_idx },
); );
defer context.popScope(); defer context.popScope();