Skip to content

Commit

Permalink
ast.lastToken: Handle switch nodes containing an invalid condition
Browse files Browse the repository at this point in the history
  • Loading branch information
llogick committed Sep 27, 2023
1 parent 155293e commit 8a28cd1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,10 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
},
.switch_comma, .@"switch" => {
const lhs = datas[n].lhs;
const l_brace = tree.lastToken(lhs) + 2; //lparen + rbrace
var l_brace = tree.lastToken(lhs) + 2; // + 2 => (last) token of the condition + the .r_paren
// If the condition within the switch is invalid, eg `switch (a.) {}`,
// l_brace would be the index of the .r_paren of the switch ----^
if (token_tags[l_brace] != .l_brace) l_brace += 1;
return findMatchingRBrace(token_tags, l_brace) orelse @intCast(tree.tokens.len - 1);
},
.@"asm" => {
Expand Down
9 changes: 9 additions & 0 deletions tests/lsp_features/folding_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ test "foldingRange - multi-line string literal" {
});
}

test "foldingRange - invalid condition within a `switch`" {
try testFoldingRange(
\\switch (a.) {
\\}
, &.{
.{ .startLine = 0, .startCharacter = 11, .endLine = 1, .endCharacter = 0 },
});
}

fn testFoldingRange(source: []const u8, expect: []const types.FoldingRange) !void {
var ctx = try Context.init();
defer ctx.deinit();
Expand Down

0 comments on commit 8a28cd1

Please sign in to comment.