diff --git a/src/analysis.zig b/src/analysis.zig index df7c131..87d6b0a 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -523,7 +523,7 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, const is_contained = nodeContainsSourceIndex(tree, child_node, source_index); // If the cursor is in a variable decls it will insert itself anyway, we don't need to take care of it. - if ((is_contained and child_node.id != .VarDecl) or !is_contained) try decls.append(child_node); + if ((is_contained and child_node.id != .VarDecl) or !is_contained) try decls.append(child_node); if (is_contained) { try declsFromIndexInternal(decls, tree, child_node, source_index); } @@ -610,6 +610,19 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, } } }, + .Switch => { + const switch_node = node.cast(ast.Node.Switch).?; + var case_it = switch_node.cases.iterator(0); + while (case_it.next()) |case| { + const case_node = case.*.cast(ast.Node.SwitchCase).?; + if (nodeContainsSourceIndex(tree, case_node.expr, source_index)) { + if (case_node.payload) |payload| { + try declsFromIndexInternal(decls, tree, payload, source_index); + } + return try declsFromIndexInternal(decls, tree, case_node.expr, source_index); + } + } + }, // TODO: These convey no type information... .Payload => try decls.append(node.cast(ast.Node.Payload).?.error_symbol), .PointerPayload => try decls.append(node.cast(ast.Node.PointerPayload).?.value_symbol), diff --git a/src/main.zig b/src/main.zig index 341022c..689b9c6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -293,10 +293,11 @@ fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []cons {} var end_idx = pos_index; - while (end_idx < handle.document.text.len - 1 and + while (end_idx < handle.document.text.len and (std.ascii.isAlNum(text[end_idx]) or text[end_idx] == '_')) : (end_idx += 1) {} + if (end_idx <= start_idx) return &[0]u8{}; return text[start_idx + 1 .. end_idx]; }