diff options
Diffstat (limited to 'src/locations.zig')
| -rw-r--r-- | src/locations.zig | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/src/locations.zig b/src/locations.zig index 8f8546f6..61bbbd1a 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -5,10 +5,15 @@ const syntax = @import("syntax.zig"); const filesystem = @import("fs.zig"); pub const Config = @import("locations_config.zig").Config; -pub const context_prefix = "| "; +pub const context_prefix = " "; + +pub fn isContextLine(line: []const u8) bool { + return std.mem.startsWith(u8, line, context_prefix) or std.mem.startsWith(u8, line, "| "); +} pub const Row = struct { kind: enum { match, context }, + declaration: bool = false, path: []u8, at: look.Spot, location_end: usize, @@ -27,7 +32,7 @@ pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { pub fn equalRows(a: []const Row, b: []const Row) bool { if (a.len != b.len) return false; for (a, b) |left, right| { - if (left.kind != right.kind or !std.meta.eql(left.at, right.at) or + if (left.kind != right.kind or left.declaration != right.declaration or !std.meta.eql(left.at, right.at) or left.location_end != right.location_end or left.code_start != right.code_start or !std.mem.eql(u8, left.path, right.path) or !std.mem.eql(u8, left.colors, right.colors)) return false; } @@ -65,9 +70,16 @@ pub const Parsed = struct { pub fn parse(line: []const u8) Parsed { if (std.mem.indexOfScalar(u8, line, '\t')) |tab| { const prefix = std.mem.trimEnd(u8, line[0..tab], " "); - const target = look.parsePathLine(prefix); - if (target.at.line > 0 and target.end == prefix.len) - return .{ .path = target.path, .at = target.at, .end = prefix.len, .code_start = tab + 1 }; + const target = parseRaw(prefix); + if (target.at.line > 0) { + const suffix = std.mem.trim(u8, prefix[target.end..], " "); + var stars = true; + for (suffix) |byte| if (byte != '*') { + stars = false; + break; + }; + if (stars) return .{ .path = target.path, .at = target.at, .end = target.end, .code_start = tab + 1 }; + } } return parseRaw(line); } @@ -100,6 +112,8 @@ const Pending = struct { code: []const u8, original: ?usize, hidden: bool = false, + declaration: bool = false, + depth: usize = 0, colors: []const u8 = &.{}, fn lessThan(_: void, a: Pending, b: Pending) bool { @@ -155,7 +169,7 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us syntax.contextDeclarations(arena, path, source) catch &.{} else &.{}; - for (matches.items[first..end]) |match| { + for (pending.items[group_start..]) |*match| { const row = match.at.line -| 1; if (row >= count) continue; const lo = row -| p.locations_config.context; @@ -163,22 +177,24 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us var context_row = lo; while (context_row <= hi) : (context_row += 1) { if (context_row == row) continue; - wanted[context_row] = @max(wanted[context_row], 1); + wanted[context_row] |= 1; } for (declarations) |declaration| { if (declaration.start_line > row) break; if (declaration.end_line < row) continue; + match.depth += 1; var header = declaration.start_line; while (header <= declaration.header_end_line and header < row) : (header += 1) - wanted[header] = if (p.locations_config.tslocations) @max(wanted[header], 1) else 2; + wanted[header] |= 2; } } for (wanted, 0..) |kind, row| { if (kind > 0) { const line = source_lines.items[row]; const offset = @intFromPtr(line.ptr) - @intFromPtr(source.ptr); - const colors = if (offset + line.len <= source_colors.len) source_colors[offset..][0..line.len] else &.{}; - try appendContext(arena, &pending, path, line, row, kind == 2, colors); + const declaration = kind & 2 != 0; + const colors = if (!declaration and offset + line.len <= source_colors.len) source_colors[offset..][0..line.len] else &.{}; + try appendContext(arena, &pending, path, line, row, declaration, declaration and !p.locations_config.tslocations, colors); } } } @@ -195,7 +211,7 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us first = end; } var width: usize = 0; - for (pending.items) |row| width = @max(width, pardes.panes.File.displayWidth(row.label) + @as(usize, if (row.original == null) 2 else 0)); + for (pending.items) |row| width = @max(width, labelWidth(row)); var out: std.Io.Writer.Allocating = .init(p.gpa); errdefer out.deinit(); var rows: std.ArrayList(Row) = .empty; @@ -213,8 +229,11 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us if (context) try out.writer.writeAll(context_prefix); try out.writer.writeAll(row.label); const location_end = if (row.hidden) 0 else out.written().len - before; - const used = pardes.panes.File.displayWidth(row.label) + @as(usize, if (context) 2 else 0); - try out.writer.splatByteAll(' ', width - used); + if (row.depth > 0) { + try out.writer.writeByte(' '); + try out.writer.splatByteAll('*', row.depth); + } + try out.writer.splatByteAll(' ', width - labelWidth(row)); try out.writer.writeByte('\t'); const code_start = out.written().len - before; try out.writer.writeAll(row.code); @@ -223,7 +242,7 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us errdefer p.gpa.free(owned_path); const owned_colors = try p.gpa.dupe(u8, row.colors); errdefer p.gpa.free(owned_colors); - try rows.append(p.gpa, .{ .kind = if (context) .context else .match, .path = owned_path, .at = row.at, .location_end = location_end, .code_start = code_start, .colors = owned_colors }); + try rows.append(p.gpa, .{ .kind = if (context) .context else .match, .declaration = row.declaration, .path = owned_path, .at = row.at, .location_end = location_end, .code_start = code_start, .colors = owned_colors }); if (anchor != null and row.original == anchor) mapped = index; } const owned_rows = try rows.toOwnedSlice(p.gpa); @@ -231,7 +250,13 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us return .{ .content = try out.toOwnedSlice(), .rows = owned_rows, .anchor = mapped }; } -fn appendContext(arena: std.mem.Allocator, rows: *std.ArrayList(Pending), path: []const u8, source: []const u8, row: usize, hidden: bool, colors: []const u8) !void { +fn labelWidth(row: Pending) usize { + return pardes.panes.File.displayWidth(row.label) + + @as(usize, if (row.original == null) context_prefix.len else 0) + + @as(usize, if (row.depth > 0) row.depth + 1 else 0); +} + +fn appendContext(arena: std.mem.Allocator, rows: *std.ArrayList(Pending), path: []const u8, source: []const u8, row: usize, declaration: bool, hidden: bool, colors: []const u8) !void { try rows.append(arena, .{ .path = path, .at = .{ .line = row + 1 }, @@ -239,6 +264,7 @@ fn appendContext(arena: std.mem.Allocator, rows: *std.ArrayList(Pending), path: .code = source, .original = null, .hidden = hidden, + .declaration = declaration, .colors = colors, }); } |
