output: print filename under search if there are many

when searching over multiple files, it's unclear where a type is coming
from. zdoc now outputs the filename before printing out search results.
0.10
alex 2 years ago
parent 78607c3a96
commit 1359c9ecb1

@ -63,6 +63,10 @@ pub fn main() !void {
};
const list = try expandSourcePath(alloc, zsource);
for (list.items) |src| {
// no need to print the filename under search if there's only one.
if (list.items.len > 1) {
ais.setSearchFile(src);
}
// todo: consider replacing arena with something else to dealloc already
// analyzed files early and possibly reduce memory footprint.
const contents = try readFile(alloc, src);

@ -2651,11 +2651,30 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
/// not used until the next line
indent_next_line: usize = 0,
/// indicates whether at least one search_file has ever been printed out.
first_search_file: bool = true,
/// the name of the file currently under search.
/// the ield is reset to null once printed out.
/// use setSearchFile to set a new name before starting a search.
search_file: ?[]const u8 = null,
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
pub fn write(self: *Self, bytes: []const u8) WriteError!usize {
if (self.search_file) |name| {
if (!self.first_search_file) {
try self.insertNewline();
}
const n = @minimum(name.len, 60);
const prefix = if (name.len > n) "..." else "";
const truncname = name[name.len - n .. name.len];
try std.fmt.format(self.underlying_writer, "//--- file: {s}{s} ---\n", .{ prefix, truncname });
self.search_file = null;
self.first_search_file = false;
}
if (bytes.len == 0)
return @as(usize, 0);
@ -2663,6 +2682,13 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
return self.writeNoIndent(bytes);
}
/// setSearchFile records the name to print out before the first self.write
/// occurs. the name is always printed in between newlines.
pub fn setSearchFile(self: *Self, name: []const u8) void {
self.search_file = name;
self.resetLine();
}
// Change the indent delta without changing the final indentation level
pub fn setIndentDelta(self: *Self, new_indent_delta: usize) void {
if (self.indent_delta == new_indent_delta) {

Loading…
Cancel
Save