Skip to content

Commit e18642a

Browse files
authored
Update to the latest zig and zls master (#138)
1 parent b4330db commit e18642a

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
.zls = .{
88
// Update with `zig fetch --save git+https://github.com/zigtools/zls#master`
99
// IF changed THEN update .github/coverage.yml
10-
.url = "git+https://github.com/zigtools/zls?ref=master#25360ab5a94f61a4136efbdb76940286f27465dc",
11-
.hash = "zls-0.16.0-dev-rmm5fhreJAA7il8TZE_z88mOj1AuMDMxfy5I1ozf8fAH",
10+
.url = "git+https://github.com/zigtools/zls?ref=master#246f9387629c82a2cfd51ee1f7895d7c9db39b47",
11+
.hash = "zls-0.16.0-dev-rmm5fmdBJQCoJBgNx4V7WbbtQsd33XHFSs2sqwX9IvCb",
1212
},
1313
},
1414
.paths = .{

src/lib/ast.zig

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -62,47 +62,16 @@ pub fn nodeChildrenAlloc(
6262
tree: *const Ast,
6363
node: Ast.Node.Index,
6464
) error{OutOfMemory}![]Ast.Node.Index {
65-
const Context = struct {
66-
gpa: std.mem.Allocator,
67-
children: *std.ArrayList(Ast.Node.Index),
68-
69-
fn callback(self: @This(), _: *const Ast, child_node: Ast.Node.Index) error{OutOfMemory}!void {
70-
if (NodeIndexShim.init(child_node).isRoot()) return;
71-
try self.children.append(self.gpa, child_node);
72-
}
73-
};
74-
7565
var children: std.ArrayList(Ast.Node.Index) = .empty;
7666
defer children.deinit(gpa);
7767

78-
try iterateChildren(
79-
tree,
80-
node,
81-
Context{
82-
.gpa = gpa,
83-
.children = &children,
84-
},
85-
error{OutOfMemory},
86-
Context.callback,
87-
);
88-
return children.toOwnedSlice(gpa);
89-
}
90-
91-
/// Temporary work around to bug in zls 0.14 that's now fixed in zls master.
92-
/// I don't see the point in upstreaming the fix to the ZLS 0.14 branch so
93-
/// leaving this simple work around in place while we support 0.14 and then it
94-
/// can be deleted.
95-
pub inline fn iterateChildren(
96-
tree: *const Ast,
97-
node: Ast.Node.Index,
98-
context: anytype,
99-
comptime Error: type,
100-
comptime callback: fn (@TypeOf(context), *const Ast, Ast.Node.Index) Error!void,
101-
) Error!void {
102-
switch (version.zig) {
103-
.@"0.14", .@"0.15" => @panic("Not implemented"),
104-
.@"0.16" => try zls.ast.iterateChildren(tree, node, context, Error, callback),
68+
var it = zls.ast.Iterator.init(tree, node);
69+
while (it.next(tree)) |child_node| {
70+
std.debug.assert(child_node != .root);
71+
try children.append(gpa, child_node);
10572
}
73+
74+
return children.toOwnedSlice(gpa);
10675
}
10776

10877
/// `errdefer` and `defer` calls

src/lib/explorer.zig

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn jsonTree(
6868
// e.g., the "data" union structures
6969

7070
var node_children = std.json.Array.init(self.arena);
71-
try ast.iterateChildren(
71+
try iterateChildren(
7272
context_tree,
7373
child_node,
7474
@This(){
@@ -98,7 +98,7 @@ pub fn jsonTree(
9898
var root_node_children = std.json.Array.init(arena);
9999

100100
if (tree.errors.len == 0) {
101-
try ast.iterateChildren(
101+
try iterateChildren(
102102
&tree,
103103
NodeIndexShim.root.toNodeIndex(),
104104
Context{
@@ -116,6 +116,19 @@ pub fn jsonTree(
116116
return std.json.Value{ .object = root_json_object };
117117
}
118118

119+
fn iterateChildren(
120+
tree: *const Ast,
121+
node: Ast.Node.Index,
122+
context: anytype,
123+
comptime Error: type,
124+
comptime callback: fn (@TypeOf(context), *const Ast, Ast.Node.Index) Error!void,
125+
) Error!void {
126+
var it = zls.ast.Iterator.init(tree, node);
127+
while (it.next(tree)) |child_node| {
128+
try callback(context, tree, child_node);
129+
}
130+
}
131+
119132
fn errorsToJson(tree: Ast, arena: std.mem.Allocator) !std.json.Array {
120133
var json_errors = std.json.Array.init(arena);
121134

@@ -187,3 +200,4 @@ const std = @import("std");
187200
const version = @import("version.zig");
188201
const NodeIndexShim = shims.NodeIndexShim;
189202
const Ast = std.zig.Ast;
203+
const zls = @import("zls");

0 commit comments

Comments
 (0)