Added switch to declFromIndex, fixed occasional crash

This commit is contained in:
Alexandros Naskos 2020-05-23 02:33:42 +03:00
parent a189fa171c
commit 34adb4e22f
2 changed files with 16 additions and 2 deletions

View File

@ -523,7 +523,7 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
const is_contained = nodeContainsSourceIndex(tree, child_node, source_index); 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 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) { if (is_contained) {
try declsFromIndexInternal(decls, tree, child_node, source_index); 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... // TODO: These convey no type information...
.Payload => try decls.append(node.cast(ast.Node.Payload).?.error_symbol), .Payload => try decls.append(node.cast(ast.Node.Payload).?.error_symbol),
.PointerPayload => try decls.append(node.cast(ast.Node.PointerPayload).?.value_symbol), .PointerPayload => try decls.append(node.cast(ast.Node.PointerPayload).?.value_symbol),

View File

@ -293,10 +293,11 @@ fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []cons
{} {}
var end_idx = pos_index; 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) (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]; return text[start_idx + 1 .. end_idx];
} }