GitNexus/gitnexus/test/fixtures/sample-code/simple.zig
Navid EMAD d02f09c53a
feat(zig): export fn visibility, opaque containers, named test blocks, member ownership
Ports the parts of upstream PR #305 (closed, unmerged) that our Zig
provider lacked, plus two gaps found while porting.

- `export fn` / `export var` (C-ABI linkage, never `pub`) are exported;
  the pub/export predicate is now shared by the export checker and the
  method/variable extractors' visibility (`hasZigVisibilityKeyword`).
- `const H = opaque { … }` is a Struct-labelled container (it may own
  methods, never fields) in both the structure queries and the scope
  query; ZIG_CONTAINER_TYPES is the single source for the extractor
  configs.
- `test "name" { … }` blocks are Function nodes named by the string
  node WITH quotes, so `test "add"` beside `fn add` cannot merge onto
  Function:<file>:add; `test_declaration` joins FUNCTION_NODE_TYPES and
  the Zig method config names it in the enclosing-function walk, so
  calls inside a test attribute to the test. Anonymous `test {}` and
  decl-tests `test add {}` are scopes without a node (an empty-name hook
  result stops the walk instead of falling through to the identifier of
  the function under test).
- Empty container bodies (`struct {}`, `opaque {}`) no longer mint a
  nameless Property: tree-sitter-zig 1.1.2 recovers them as a
  container_field with a MISSING identifier; #not-eq? guards in both
  queries and the field extractor drop it.
- Owner walk (`findEnclosingClassInfo`): an anonymous container bound
  by the enclosing `variable_declaration` takes the binding identifier,
  same shape as the Go `type_spec` branch. Before this NO Zig member
  had an owner — zero HAS_METHOD / HAS_PROPERTY edges for Zig.

Not ported from #305, deliberately: `builtInNames` (a bare-name call-site
drop filter; `alloc`/`free`/`append`/`print` are the most common user
method names in Zig and `std.*` receivers are already external via the
import binding), `usingnamespace` (removed in Zig 0.15), `@cImport`,
`build.zig` ignore, and the web-app changes.
2026-08-24 19:56:13 +02:00

49 lines
834 B
Zig

const std = @import("std");
pub fn add(a: i32, b: i32) i32 {
return a + b;
}
fn private_helper() i32 {
return 0;
}
pub const Point = struct {
x: i32,
y: i32,
pub fn init(x: i32, y: i32) Point {
return .{ .x = x, .y = y };
}
pub fn distance(self: Point, other: Point) i32 {
return (self.x - other.x) + (self.y - other.y);
}
};
const Color = enum { red, green, blue };
pub fn main() void {
const p = Point.init(1, 2);
_ = p.distance(Point{ .x = 3, .y = 4 });
_ = add(1, 2);
}
// C-ABI export (no `pub`), an opaque FFI handle, and test blocks.
export fn c_add(a: i32, b: i32) i32 {
return a + b;
}
pub const Handle = opaque {
pub fn close(self: *Handle) void {
_ = self;
}
};
test "add works" {
_ = add(1, 2);
}
test {
_ = c_add(1, 2);
}