summaryrefslogtreecommitdiff
path: root/core/syntax.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/syntax.zig')
-rw-r--r--core/syntax.zig48
1 files changed, 36 insertions, 12 deletions
diff --git a/core/syntax.zig b/core/syntax.zig
index 5cdc2ff3..9fbad3cf 100644
--- a/core/syntax.zig
+++ b/core/syntax.zig
@@ -1135,6 +1135,9 @@ const SymbolWalk = struct {
fn visit(w: *SymbolWalk, node: ts.Node) !void {
while (w.depth > 0 and w.scopes[w.depth - 1].end <= node.startByte()) w.depth -= 1;
const in: Scope = if (w.depth > 0) w.scopes[w.depth - 1] else .{ .end = 0, .kind = .module, .symbol = 0 };
+ // A test's body (a macro's) lists nothing: its types are its
+ // locals' (a test's `const Find = struct`).
+ if (in.kind == .other) return;
const kind_name = node.kind();
const rule = for (w.rules) |r| {
if (std.mem.eql(u8, r.node, kind_name)) break r;
@@ -1145,11 +1148,17 @@ const SymbolWalk = struct {
if (local and (found.kind == .constant or found.kind == .variable)) return;
var named = in.symbol;
if (found.name) |name| {
+ // A longer name is cut (a test's sentence), on a character.
+ var end = name.endByte();
+ if (end - name.startByte() > max_name) {
+ end = name.startByte() + max_name;
+ while (end > name.startByte() and w.source[end] & 0xc0 == 0x80) end -= 1;
+ }
try w.out.append(w.gpa, .{
.kind = found.kind,
.line = name.startPoint().row,
.name_start = name.startByte(),
- .name_end = name.endByte(),
+ .name_end = end,
.end = found.end,
.parent = in.symbol,
});
@@ -1190,7 +1199,10 @@ fn symbolOf(rule: Rule, node: ts.Node, source: []const u8, in: SymbolKind) ?stru
if (kind == .variable and constant(source, node.startByte(), name orelse return null)) kind = .constant;
},
.value => {
+ // Named by a name, never a pattern (`const { a, b } = ...`,
+ // OCaml's `let () = ...`).
name = nameOf(node, rule.name);
+ if (name) |n| if (!nameLike(n.kind())) return null;
kind = valueKind(node, source) orelse kind;
// JavaScript's keyword is its declaration's (`const a = 1, b`).
const from = if (std.mem.eql(u8, rule.node, "variable_declarator")) (node.parent() orelse node).startByte() else node.startByte();
@@ -1228,20 +1240,24 @@ fn symbolOf(rule: Rule, node: ts.Node, source: []const u8, in: SymbolKind) ?stru
if (childOfKind(node, "Block")) |block| end = block.startByte();
}
if (name == null and kind != .scope) return null;
- if (name) |n| if (n.endByte() <= n.startByte() or n.endByte() - n.startByte() > max_name) return null;
+ if (name) |n| if (n.endByte() <= n.startByte()) return null;
return .{ .kind = kind, .name = name, .end = end };
}
-/// The longest name a symbol has: a longer one is no name.
+/// The longest name a symbol has: a longer one is cut.
const max_name = 200;
-/// The node naming `node`: its field `field`; without one, its declarator's
-/// (C's `int *f(void)`: `f`); else the first name under it, past its
-/// type, value, body and parameters (Kotlin's, Fortran's).
+/// The node naming `node`: its field `field`, whole on its line (C++'s
+/// `namespace v8::internal`, Rust's `impl Vec<T>`); without one, its
+/// declarator's (C's `int *f(void)`: `f`); else the first name under it,
+/// past its type, value, body and parameters (Kotlin's, Fortran's).
fn nameOf(node: ts.Node, field: []const u8) ?ts.Node {
if (field.len == 0) return null;
var n = node;
- if (node.childByFieldName(field)) |f| n = f else while (n.childByFieldName("declarator")) |d| n = d;
+ if (node.childByFieldName(field)) |f| {
+ if (f.startPoint().row == f.endPoint().row) return f;
+ n = f;
+ } else while (n.childByFieldName("declarator")) |d| n = d;
if (nameLike(n.kind())) return n;
if (n.childByFieldName("name")) |f| if (nameLike(f.kind())) return f;
return firstName(n, 3);
@@ -2786,12 +2802,20 @@ test "syntax symbols: a Zig file's types, functions, fields and the rest by thei
\\fn List(comptime T: type) type {
\\ return struct { items: []T };
\\}
- \\test "count" {}
+ \\test "count" {
+ \\ const Local = struct {
+ \\ fn helper() void {}
+ \\ };
+ \\ _ = Local;
+ \\}
\\test {}
\\extern fn tick() void;
\\
;
- const lines = try symbolLines(gpa, "a.zig", source);
+ // A test's name longer than a row's room is cut, its body still none.
+ const xs: [250]u8 = @splat('x');
+ const long = "test \"" ++ xs ++ "\" {\n const Inner = struct {};\n _ = Inner;\n}\n";
+ const lines = try symbolLines(gpa, "a.zig", source ++ long);
defer gpa.free(lines);
try std.testing.expectEqualStrings(
\\n std
@@ -2810,7 +2834,7 @@ test "syntax symbols: a Zig file's types, functions, fields and the rest by thei
\\· "count"
\\f tick
\\
- , lines);
+ ++ "· \"" ++ xs[0 .. max_name - 1].* ++ "\n", lines);
}
test "syntax symbols: C, C++, Rust, Go, Python and JavaScript by their kind tables, members named in their types, an impl's in its type, locals none" {
@@ -2820,11 +2844,11 @@ test "syntax symbols: C, C++, Rust, Go, Python and JavaScript by their kind tabl
defer stop();
const cases = [_]struct { path: []const u8, source: []const u8, want: []const u8 }{
.{ .path = "a.c", .source = "#define LIMIT 4\nstruct point { int x; int *y; };\ntypedef struct { int a; } pair;\nenum color { RED };\nstatic int counter = 0;\nconst int limit = 4;\nint add(int a) { int local = a; return local; }\nint later(void);\nstruct point p;\n", .want = "· LIMIT\nt point\nm point.x\nm point.y\nt pair\nm pair.a\nt color\nm color.RED\nv counter\nc limit\nf add\nf later\nv p\n" },
- .{ .path = "a.cc", .source = "namespace torque {\nclass Parser : public Base {\n public:\n Parser();\n int Parse(int depth) const { return depth; }\n int depth_;\n};\nint Parser::Run(int n) { int local = n; return local; }\nusing Alias = int;\n}\n", .want = "n torque\nt torque.Parser\nf torque.Parser.Parser\nf torque.Parser.Parse\nm torque.Parser.depth_\nf torque.Parser::Run\nt torque.Alias\n" },
+ .{ .path = "a.cc", .source = "namespace torque {\nclass Parser : public Base {\n public:\n Parser();\n int Parse(int depth) const { return depth; }\n int depth_;\n};\nint Parser::Run(int n) { int local = n; return local; }\nusing Alias = int;\n}\nnamespace v8::internal {\nint flags;\n}\n", .want = "n torque\nt torque.Parser\nf torque.Parser.Parser\nf torque.Parser.Parse\nm torque.Parser.depth_\nf torque.Parser::Run\nt torque.Alias\nn v8::internal\nv v8::internal.flags\n" },
.{ .path = "a.rs", .source = "mod m {\npub struct S { pub a: u32 }\nenum E { A, B(u32) }\ntrait T { fn f(&self); }\nimpl S { pub fn new() -> Self { let x = 1; S { a: x } } }\nconst C: u32 = 1;\nstatic G: u32 = 2;\nmacro_rules! mac { () => {} }\n}\n", .want = "n m\nt m.S\nm m.S.a\nt m.E\nm m.E.A\nm m.E.B\nt m.T\nf m.T.f\nf m.S.new\nc m.C\nv m.G\n· m.mac\n" },
.{ .path = "a.go", .source = "package main\ntype S struct { A int }\ntype I interface { M() }\nconst C = 1\nvar V = 2\nfunc F() { var x = 1; _ = x }\nfunc (s *S) M() {}\n", .want = "t S\nm S.A\nt I\nf I.M\nc C\nv V\nf F\nf M\n" },
.{ .path = "a.py", .source = "X = 1\nclass C:\n attr = 2\n def m(self):\n y = 3\n self.z = 4\ndef f():\n pass\n", .want = "v X\nt C\nm C.attr\nf C.m\nf f\n" },
- .{ .path = "a.js", .source = "const A = 1;\nlet b = 2;\nfunction f() { const local = 1; }\nclass K { field = 1; m() {} }\nconst arrow = () => 1;\nconst Klass = class {};\n", .want = "c A\nv b\nf f\nt K\nm K.field\nf K.m\nf arrow\nt Klass\n" },
+ .{ .path = "a.js", .source = "const A = 1;\nlet b = 2;\nfunction f() { const local = 1; }\nclass K { field = 1; m() {} }\nconst arrow = () => 1;\nconst Klass = class {};\nconst { x, y } = A;\n", .want = "c A\nv b\nf f\nt K\nm K.field\nf K.m\nf arrow\nt Klass\n" },
};
for (cases) |case| {
const lines = try symbolLines(gpa, case.path, case.source);