@@ -115,3 +115,70 @@ fn testNodesAtLoc(source: []const u8) !void {
115115 return error .LocEndMismatch ;
116116 }
117117}
118+
119+ test "nodesIntersectingLoc" {
120+ try testNodesIntersectingLoc (
121+ \\<loc>const a = 0;
122+ \\<loc>const b = 0;
123+ , &.{.simple_var_decl });
124+ try testNodesIntersectingLoc (
125+ \\<loc> const a = 0;
126+ \\const <loc> b = 0;
127+ , &.{ .simple_var_decl , .simple_var_decl });
128+ try testNodesIntersectingLoc (
129+ \\const a = 0;
130+ \\fn foo(_:<loc> u32) void {
131+ \\ const b = 0;
132+ \\<loc>}
133+ \\const c = 0;
134+ , &.{.fn_decl });
135+ try testNodesIntersectingLoc (
136+ \\const a = 0;
137+ \\fn foo(_: u32) void {<loc>
138+ \\ const b = 0;
139+ \\}
140+ \\const <loc> c = 0;
141+ , &.{ .fn_decl , .simple_var_decl });
142+ }
143+
144+ fn testNodesIntersectingLoc (
145+ source : [:0 ]const u8 ,
146+ expected_tags : []const std.zig.Ast.Node.Tag ,
147+ ) ! void {
148+ var ccp = try helper .collectClearPlaceholders (allocator , source );
149+ defer ccp .deinit (allocator );
150+
151+ const locs = ccp .locations .items (.new );
152+ std .debug .assert (ccp .locations .len == 2 );
153+
154+ const loc : offsets.Loc = .{
155+ .start = locs [0 ].start ,
156+ .end = locs [1 ].start ,
157+ };
158+
159+ const new_source = try allocator .dupeSentinel (u8 , ccp .new_source , 0 );
160+ defer allocator .free (new_source );
161+
162+ var tree : std.zig.Ast = try .parse (allocator , new_source , .zig );
163+ defer tree .deinit (allocator );
164+
165+ const nodes = try ast .nodesIntersectingLoc (allocator , & tree , loc );
166+ defer allocator .free (nodes );
167+
168+ const uri = "file.zig" ;
169+ var error_builder : ErrorBuilder = .init (allocator );
170+ defer error_builder .deinit ();
171+ errdefer error_builder .writeDebug ();
172+
173+ try error_builder .addFile (uri , new_source );
174+
175+ for (nodes , expected_tags ) | node , tag | {
176+ if (tree .nodeTag (node ) != tag ) {
177+ try error_builder .msgAtIndex ("expected '{t}' found '{t}'" , uri , loc .start , .err , .{
178+ tag ,
179+ tree .nodeTag (node ),
180+ });
181+ return error .NodeMismatch ;
182+ }
183+ }
184+ }
0 commit comments