Fixed node iteration:

This commit is contained in:
Alexandros Naskos 2020-05-24 17:07:48 +03:00
parent f5ca94a0d1
commit 34ba67b3d1
2 changed files with 15 additions and 15 deletions

View File

@ -220,8 +220,8 @@ fn getDeclName(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
/// Gets the child of node /// Gets the child of node
pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node { pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
var child_it = node.iterate(); var child_idx: usize = 0;
while (child_it.next()) |child| { while (node.iterate(child_idx)) |child| : (child_idx += 1) {
const child_name = getDeclName(tree, child) orelse continue; const child_name = getDeclName(tree, child) orelse continue;
if (std.mem.eql(u8, child_name, name)) return child; if (std.mem.eql(u8, child_name, name)) return child;
} }
@ -239,8 +239,8 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression { fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression {
var result: ?*ast.Node.ControlFlowExpression = null; var result: ?*ast.Node.ControlFlowExpression = null;
var child_it = base_node.iterate(); var child_idx: usize = 0;
while (child_it.next()) |child_node| { while (base_node.iterate(child_idx)) |child_node| : (child_idx += 1) {
switch (child_node.id) { switch (child_node.id) {
.ControlFlowExpression => { .ControlFlowExpression => {
const cfe = child_node.cast(ast.Node.ControlFlowExpression).?; const cfe = child_node.cast(ast.Node.ControlFlowExpression).?;
@ -529,8 +529,8 @@ pub fn declsFromIndexInternal(
switch (node.id) { switch (node.id) {
.Root, .ContainerDecl => { .Root, .ContainerDecl => {
container.* = node; container.* = node;
var node_it = node.iterate(); var node_idx: usize = 0;
while (node_it.next()) |child_node| { while (node.iterate(node_idx)) |child_node| : (node_idx += 1) {
// Skip over container fields, we can only dot access those. // Skip over container fields, we can only dot access those.
if (child_node.id == .ContainerField) continue; if (child_node.id == .ContainerField) continue;
@ -560,8 +560,8 @@ pub fn declsFromIndexInternal(
try declsFromIndexInternal(decls, tree, test_decl.body_node, container, source_index); try declsFromIndexInternal(decls, tree, test_decl.body_node, container, source_index);
}, },
.Block => { .Block => {
var node_it = node.iterate(); var inode_idx: usize = 0;
while (node_it.next()) |inode| { while (node.iterate(inode_idx)) |inode| : (inode_idx += 1) {
if (nodeComesAfterSourceIndex(tree, inode, source_index)) return; if (nodeComesAfterSourceIndex(tree, inode, source_index)) return;
try declsFromIndexInternal(decls, tree, inode, container, source_index); try declsFromIndexInternal(decls, tree, inode, container, source_index);
} }
@ -658,8 +658,8 @@ pub fn declsFromIndexInternal(
} }
pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void { pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
var node_it = node.iterate(); var node_idx: usize = 0;
while (node_it.next()) |child_node| { while (node.iterate(node_idx)) |child_node| : (node_idx += 1) {
try decls.append(child_node); try decls.append(child_node);
} }
} }
@ -685,8 +685,8 @@ fn nodeComesAfterSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usi
pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 { pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
var node = &tree.root_node.base; var node = &tree.root_node.base;
var child_it = node.iterate(); var child_idx: usize = 0;
while (child_it.next()) |child| { while (node.iterate(child_idx)) |child| : (child_idx += 1) {
if (!nodeContainsSourceIndex(tree, child, source_index)) { if (!nodeContainsSourceIndex(tree, child, source_index)) {
continue; continue;
} }
@ -702,7 +702,7 @@ pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
return import_str[1 .. import_str.len - 1]; return import_str[1 .. import_str.len - 1];
} }
node = child; node = child;
child_it = node.iterate(); child_idx = 0;
} }
return null; return null;
} }

View File

@ -176,8 +176,8 @@ fn containerToCompletion(
container: *std.zig.ast.Node, container: *std.zig.ast.Node,
config: Config, config: Config,
) !void { ) !void {
var child_it = container.iterate(); var child_idx: usize = 0;
while (child_it.next()) |child_node| { while (container.iterate(child_idx)) |child_node| : (child_idx += 1) {
// Declarations in the same file do not need to be public. // Declarations in the same file do not need to be public.
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) { if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) {
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, container, config); try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, container, config);