zig fmt: Patch rename stream to ais (auto indenting stream) & other small refactors

master
Lachlan Easton 2020-09-02 20:16:28 +10:00
parent 7841c9b7d1
commit bb848dbeee
8 changed files with 653 additions and 668 deletions

View File

@ -5,13 +5,13 @@ const assert = std.debug.assert;
/// Automatically inserts indentation of written data by keeping
/// track of the current indentation level
pub fn AutoIndentingStream(comptime WriterType: type) type {
pub fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
return struct {
const Self = @This();
pub const Error = WriterType.Error;
pub const Error = UnderlyingWriter.Error;
pub const Writer = io.Writer(*Self, Error, write);
writer_pointer: *WriterType,
underlying_writer: UnderlyingWriter,
indent_count: usize = 0,
indent_delta: usize,
@ -20,10 +20,6 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
applied_indent: usize = 0, // the most recently applied indent
indent_next_line: usize = 0, // not used until the next line
pub fn init(indent_delta: usize, writer_pointer: *WriterType) Self {
return Self{ .writer_pointer = writer_pointer, .indent_delta = indent_delta };
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
@ -55,7 +51,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
if (bytes.len == 0)
return @as(usize, 0);
try self.writer_pointer.writer().writeAll(bytes);
try self.underlying_writer.writeAll(bytes);
if (bytes[bytes.len - 1] == '\n')
self.resetLine();
return bytes.len;
@ -115,7 +111,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
fn applyIndent(self: *Self) Error!void {
const current_indent = self.currentIndent();
if (self.current_line_empty and current_indent > 0) {
try self.writer_pointer.writer().writeByteNTimes(' ', current_indent);
try self.underlying_writer.writeByteNTimes(' ', current_indent);
self.applied_indent = current_indent;
}
@ -143,8 +139,10 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
pub fn autoIndentingStream(
indent_delta: usize,
underlying_stream: anytype,
) AutoIndentingStream(@TypeOf(underlying_stream).Child) {
comptime assert(@typeInfo(@TypeOf(underlying_stream)) == .Pointer);
return AutoIndentingStream(@TypeOf(underlying_stream).Child).init(indent_delta, underlying_stream);
underlying_writer: anytype,
) AutoIndentingStream(@TypeOf(underlying_writer)) {
return AutoIndentingStream(@TypeOf(underlying_writer)){
.underlying_writer = underlying_writer,
.indent_delta = indent_delta,
};
}

View File

@ -10,19 +10,11 @@ pub fn ChangeDetectionStream(comptime WriterType: type) type {
pub const Error = WriterType.Error;
pub const Writer = io.Writer(*Self, Error, write);
anything_changed: bool = false,
writer_pointer: *const WriterType,
anything_changed: bool,
underlying_writer: WriterType,
source_index: usize,
source: []const u8,
pub fn init(source: []const u8, writer_pointer: *const WriterType) Self {
return Self{
.writer_pointer = writer_pointer,
.source_index = 0,
.source = source,
};
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
@ -41,7 +33,7 @@ pub fn ChangeDetectionStream(comptime WriterType: type) type {
}
}
return self.writer_pointer.write(bytes);
return self.underlying_writer.write(bytes);
}
pub fn changeDetected(self: *Self) bool {
@ -52,8 +44,12 @@ pub fn ChangeDetectionStream(comptime WriterType: type) type {
pub fn changeDetectionStream(
source: []const u8,
underlying_stream: anytype,
) ChangeDetectionStream(@TypeOf(underlying_stream).Child) {
comptime assert(@typeInfo(@TypeOf(underlying_stream)) == .Pointer);
return ChangeDetectionStream(@TypeOf(underlying_stream).Child).init(source, underlying_stream);
underlying_writer: anytype,
) ChangeDetectionStream(@TypeOf(underlying_writer)) {
return ChangeDetectionStream(@TypeOf(underlying_writer)){
.anything_changed = false,
.underlying_writer = underlying_writer,
.source_index = 0,
.source = source,
};
}

View File

@ -4,24 +4,16 @@ const assert = std.debug.assert;
/// An OutStream that returns whether the given character has been written to it.
/// The contents are not written to anything.
pub fn FindByteOutStream(comptime WriterType: type) type {
pub fn FindByteOutStream(comptime UnderlyingWriter: type) type {
return struct {
const Self = @This();
pub const Error = WriterType.Error;
pub const Error = UnderlyingWriter.Error;
pub const Writer = io.Writer(*Self, Error, write);
writer_pointer: *const WriterType,
underlying_writer: UnderlyingWriter,
byte_found: bool,
byte: u8,
pub fn init(byte: u8, writer_pointer: *const WriterType) Self {
return Self{
.writer_pointer = writer_pointer,
.byte = byte,
.byte_found = false,
};
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
@ -34,11 +26,15 @@ pub fn FindByteOutStream(comptime WriterType: type) type {
break :blk false;
};
}
return self.writer_pointer.writer().write(bytes);
return self.underlying_writer.write(bytes);
}
};
}
pub fn findByteOutStream(byte: u8, underlying_stream: anytype) FindByteOutStream(@TypeOf(underlying_stream).Child) {
comptime assert(@typeInfo(@TypeOf(underlying_stream)) == .Pointer);
return FindByteOutStream(@TypeOf(underlying_stream).Child).init(byte, underlying_stream);
pub fn findByteOutStream(byte: u8, underlying_writer: anytype) FindByteOutStream(@TypeOf(underlying_writer)) {
return FindByteOutStream(@TypeOf(underlying_writer)){
.underlying_writer = underlying_writer,
.byte = byte,
.byte_found = false,
};
}

View File

@ -18,10 +18,6 @@ pub fn Writer(
const Self = @This();
pub const Error = WriteError;
pub fn writer(self: *const Self) Self {
return self.*;
}
pub fn write(self: Self, bytes: []const u8) Error!usize {
return writeFn(self.context, bytes);
}

View File

@ -3364,7 +3364,7 @@ fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *b
errdefer buffer.deinit();
const outStream = buffer.outStream();
anything_changed.* = try std.zig.render(allocator, &outStream, tree);
anything_changed.* = try std.zig.render(allocator, outStream, tree);
return buffer.toOwnedSlice();
}
fn testTransform(source: []const u8, expected_source: []const u8) !void {

File diff suppressed because it is too large Load Diff

View File

@ -682,13 +682,13 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
process.exit(1);
}
if (check_flag) {
const anything_changed = try std.zig.render(gpa, &io.null_out_stream, tree);
const anything_changed = try std.zig.render(gpa, io.null_out_stream, tree);
const code = if (anything_changed) @as(u8, 1) else @as(u8, 0);
process.exit(code);
}
const stdout = io.getStdOut().outStream();
_ = try std.zig.render(gpa, &stdout, tree);
_ = try std.zig.render(gpa, stdout, tree);
return;
}
@ -830,7 +830,7 @@ fn fmtPathFile(
}
if (check_mode) {
const anything_changed = try std.zig.render(fmt.gpa, &io.null_out_stream, tree);
const anything_changed = try std.zig.render(fmt.gpa, io.null_out_stream, tree);
if (anything_changed) {
std.debug.print("{}\n", .{file_path});
fmt.any_error = true;
@ -840,7 +840,7 @@ fn fmtPathFile(
try fmt.out_buffer.ensureCapacity(source_code.len);
fmt.out_buffer.items.len = 0;
const writer = fmt.out_buffer.writer();
const anything_changed = try std.zig.render(fmt.gpa, &writer, tree);
const anything_changed = try std.zig.render(fmt.gpa, writer, tree);
if (!anything_changed)
return; // Good thing we didn't waste any file system access on this.

View File

@ -151,7 +151,7 @@ export fn stage2_free_clang_errors(errors_ptr: [*]translate_c.ClangErrMsg, error
export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {
const c_out_stream = std.io.cOutStream(output_file);
_ = std.zig.render(std.heap.c_allocator, &c_out_stream, tree) catch |e| switch (e) {
_ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {
error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
error.NotOpenForWriting => unreachable,
error.SystemResources => return .SystemResources,