summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/Output.zig9
-rw-r--r--core/Pick.zig10
-rw-r--r--core/Query.zig7
-rw-r--r--core/root.zig2
-rw-r--r--core/syntax.zig48
5 files changed, 55 insertions, 21 deletions
diff --git a/core/Output.zig b/core/Output.zig
index fe1543fe..e55ff4a3 100644
--- a/core/Output.zig
+++ b/core/Output.zig
@@ -198,11 +198,15 @@ pub fn traits(o: Origin) Traits {
};
}
+/// A list laid out as Locations (each place's lines from its file, in
+/// their order): not a file's symbols, a row each in their own order (UX
+/// DESIGN N: types, functions, fields, the rest).
pub fn usesLocationsConfig(from: Origin) bool {
const tr = traits(from);
return tr.steps and !tr.commands and from != .kind and
!std.meta.eql(from, Origin{ .query = .completion }) and
- !std.meta.eql(from, Origin{ .query = .callstack });
+ !std.meta.eql(from, Origin{ .query = .callstack }) and
+ !std.meta.eql(from, Origin{ .query = .document_symbols });
}
pub fn fileTraits(out: ?State) Traits {
@@ -244,6 +248,8 @@ pub fn isResultRow(file: *const File.State, row: usize) bool {
return !locations.isContextLine(modal.lineSlice(file.content, row));
}
+/// A language server's places in the order of their places; a file's
+/// symbols stay in theirs (lsp_client: types, functions, fields, the rest).
fn sortResults(arena: std.mem.Allocator, from: Origin, content: []u8, anchor: ?usize) !?usize {
switch (from) {
.search => {},
@@ -258,7 +264,6 @@ fn sortResults(arena: std.mem.Allocator, from: Origin, content: []u8, anchor: ?u
.outgoing_calls,
.supertypes,
.subtypes,
- .document_symbols,
.workspace_symbols,
.diagnostics,
.workspace_diagnostics,
diff --git a/core/Pick.zig b/core/Pick.zig
index 2b5714cb..dcb838f3 100644
--- a/core/Pick.zig
+++ b/core/Pick.zig
@@ -677,10 +677,9 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) std.me
for (marks_in, row.marks) |*m, at| m.* = at -| line;
row.marks = marks_in;
const name_at = cand.key - line;
- const dot = std.mem.lastIndexOfScalar(u8, row.text[name_at .. name_at + cand.key_len], '.');
row.kind = @intCast(name_at - 1);
row.pre = @intCast(name_at);
- row.faint = @intCast(if (dot) |at| at + 1 else 0);
+ row.faint = cand.base;
if (name_at + cand.key_len < row.text.len) row.quiet = .{ @intCast(name_at + cand.key_len), @intCast(row.text.len) };
const owner = p.panes[p.paneBySerial(pk.owner) orelse return row] orelse return row;
const f = owner.file orelse return row;
@@ -3357,7 +3356,7 @@ const counts_zig =
\\pub fn count(text: []const u8) Counts {
\\ return .{ .lines = text.len };
\\}
- \\test "count" {}
+ \\test "count.add" {}
\\
;
@@ -3381,9 +3380,12 @@ test "Symbols (UX DESIGN N): a Zig file's rows types, functions, fields, then th
\\ 1 n std = @import("std")
\\ 5 c Counts.zero : Counts = .{}
\\12 v hits : u32 = 0
- \\16 · "count"
+ \\16 · "count.add"
\\
, try rowLines(p, a));
+ // The qualifier dim, `Counts.`; a name holding a `.` none of it.
+ try std.testing.expectEqual(@as(u16, "Counts.".len), (try rowAt(p, live(p).?, a, 4)).faint);
+ try std.testing.expectEqual(@as(u16, 0), (try rowAt(p, live(p).?, a, 11)).faint);
// Drawn: `m Counts.lines : usize = 0` (not the cursor's row).
const pane = p.panes[p.paneBySerial(live(p).?.landing).?].?;
_ = try p.render(a);
diff --git a/core/Query.zig b/core/Query.zig
index 743e9540..a5926e23 100644
--- a/core/Query.zig
+++ b/core/Query.zig
@@ -785,7 +785,8 @@ pub fn gather(p: *Pardes, names: *Names, source: Source, dir: []const u8, self:
// line, its mark, its name qualified by the ones it is in, then
// what its first line says after the name; types, functions,
// fields, then the rest, each in the text's order (UX DESIGN N).
- // The name alone is matched.
+ // The qualified name alone is matched, its own name the
+ // basename.
for (0..4) |group| for (symbols) |sym| {
if (sym.kind.group() != group or sym.kind == .scope or sym.end > text.len) continue;
var buf: [512]u8 = undefined;
@@ -793,13 +794,15 @@ pub fn gather(p: *Pardes, names: *Names, source: Source, dir: []const u8, self:
row.print("{d: >6} {s} ", .{ sym.line + 1, sym.kind.mark() }) catch continue;
const key_at = row.end;
qualify(&row, symbols, text, sym.parent, 4) catch continue;
+ // The name its basename: the qualifier dim (Pick.rowAt).
+ const base = row.end - key_at;
row.writeAll(text[sym.name_start..sym.name_end]) catch continue;
const key_len = row.end - key_at;
const rest = text[sym.name_end..@max(sym.name_end, sym.end)];
var detail = std.mem.trim(u8, rest[0 .. std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len], " \t\r");
detail = std.mem.trimEnd(u8, std.mem.trimEnd(u8, detail, "{,;"), " \t");
if (detail.len > 0) row.print(" {s}", .{detail[0..@min(detail.len, buf.len - row.end -| 2)]}) catch {};
- names.add(gpa, row.buffered(), key_at, key_len, 0, sym.line + 1) catch break;
+ names.add(gpa, row.buffered(), key_at, key_len, @intCast(base), sym.line + 1) catch break;
names.cands.items[names.cands.items.len - 1].group = @intCast(group);
};
},
diff --git a/core/root.zig b/core/root.zig
index 86ca62e0..a9c95a96 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -461,7 +461,7 @@ test {
_ = @import("FrameStats.zig");
_ = @import("Query.zig");
_ = @import("Names.zig");
- _ = @import("syntax.zig"); // its tests ran nowhere before
+ _ = @import("syntax.zig"); // without it a -Dtest-filter run compiles none of its tests (a whole run has them)
_ = @import("Stream.zig");
_ = @import("Tables.zig");
_ = @import("Handover.zig");
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);