mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 12:59:04 +00:00
zig fmt compiling
This commit is contained in:
parent
5ada2e3937
commit
4aed226e07
@ -193,16 +193,14 @@ fn recvReadInt(ws: *WebSocket, comptime I: type) !I {
|
||||
};
|
||||
}
|
||||
|
||||
pub const WriteError = std.http.Server.Response.WriteError;
|
||||
|
||||
pub fn writeMessage(ws: *WebSocket, message: []const u8, opcode: Opcode) WriteError!void {
|
||||
pub fn writeMessage(ws: *WebSocket, message: []const u8, opcode: Opcode) anyerror!void {
|
||||
const iovecs: [1]std.posix.iovec_const = .{
|
||||
.{ .base = message.ptr, .len = message.len },
|
||||
};
|
||||
return writeMessagev(ws, &iovecs, opcode);
|
||||
}
|
||||
|
||||
pub fn writeMessagev(ws: *WebSocket, message: []const std.posix.iovec_const, opcode: Opcode) WriteError!void {
|
||||
pub fn writeMessagev(ws: *WebSocket, message: []const std.posix.iovec_const, opcode: Opcode) anyerror!void {
|
||||
const total_len = l: {
|
||||
var total_len: u64 = 0;
|
||||
for (message) |iovec| total_len += iovec.len;
|
||||
|
||||
@ -1576,7 +1576,7 @@ fn renderBuiltinCall(
|
||||
defer r.gpa.free(new_string);
|
||||
|
||||
try renderToken(r, builtin_token + 1, .none); // (
|
||||
try ais.print("\"{}\"", .{std.zig.fmtEscapes(new_string)});
|
||||
try ais.print("\"{f}\"", .{std.zig.fmtEscapes(new_string)});
|
||||
return renderToken(r, str_lit_token + 1, space); // )
|
||||
}
|
||||
}
|
||||
@ -2888,7 +2888,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
|
||||
.success => |codepoint| {
|
||||
if (codepoint <= 0x7f) {
|
||||
const buf = [1]u8{@as(u8, @intCast(codepoint))};
|
||||
try ais.print("{}", .{std.zig.fmtEscapes(&buf)});
|
||||
try ais.print("{f}", .{std.zig.fmtEscapes(&buf)});
|
||||
} else {
|
||||
try ais.writeAll(escape_sequence);
|
||||
}
|
||||
@ -2900,7 +2900,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
|
||||
},
|
||||
0x00...('\\' - 1), ('\\' + 1)...0x7f => {
|
||||
const buf = [1]u8{byte};
|
||||
try ais.print("{}", .{std.zig.fmtEscapes(&buf)});
|
||||
try ais.print("{f}", .{std.zig.fmtEscapes(&buf)});
|
||||
pos += 1;
|
||||
},
|
||||
0x80...0xff => {
|
||||
@ -3305,8 +3305,9 @@ const AutoIndentingStream = struct {
|
||||
if (bytes[bytes.len - 1] == '\n') ais.resetLine();
|
||||
}
|
||||
|
||||
/// Assumes that if the printed data ends with a newline, it is directly
|
||||
/// contained in the format string.
|
||||
pub fn print(ais: *AutoIndentingStream, comptime format: []const u8, args: anytype) anyerror!void {
|
||||
comptime assert(format[format.len - 1] != '}');
|
||||
try ais.applyIndent();
|
||||
if (ais.disabled_offset == null) try ais.underlying_writer.print(format, args);
|
||||
if (format[format.len - 1] == '\n') ais.resetLine();
|
||||
|
||||
@ -122,9 +122,9 @@ pub const Oid = union(Format) {
|
||||
pub fn format(
|
||||
oid: Oid,
|
||||
comptime fmt: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
writer: anytype,
|
||||
) @TypeOf(writer).Error!void {
|
||||
options: std.fmt.Options,
|
||||
writer: *std.io.BufferedWriter,
|
||||
) anyerror!void {
|
||||
_ = fmt;
|
||||
_ = options;
|
||||
try writer.print("{x}", .{oid.slice()});
|
||||
|
||||
54
src/fmt.zig
54
src/fmt.zig
@ -26,16 +26,13 @@ const Fmt = struct {
|
||||
color: Color,
|
||||
gpa: Allocator,
|
||||
arena: Allocator,
|
||||
out_buffer: std.ArrayList(u8),
|
||||
out_buffer: std.ArrayListUnmanaged(u8),
|
||||
stdout: *std.io.BufferedWriter,
|
||||
|
||||
const SeenMap = std.AutoHashMap(fs.File.INode, void);
|
||||
};
|
||||
|
||||
pub fn run(
|
||||
gpa: Allocator,
|
||||
arena: Allocator,
|
||||
args: []const []const u8,
|
||||
) !void {
|
||||
pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
|
||||
var color: Color = .auto;
|
||||
var stdin_flag = false;
|
||||
var check_flag = false;
|
||||
@ -52,8 +49,7 @@ pub fn run(
|
||||
const arg = args[i];
|
||||
if (mem.startsWith(u8, arg, "-")) {
|
||||
if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.writeAll(usage_fmt);
|
||||
try std.io.getStdOut().writeAll(usage_fmt);
|
||||
return process.cleanExit();
|
||||
} else if (mem.eql(u8, arg, "--color")) {
|
||||
if (i + 1 >= args.len) {
|
||||
@ -138,8 +134,11 @@ pub fn run(
|
||||
try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color);
|
||||
process.exit(2);
|
||||
}
|
||||
const formatted = try tree.render(gpa);
|
||||
defer gpa.free(formatted);
|
||||
var aw: std.io.AllocatingWriter = undefined;
|
||||
const bw = aw.init(gpa);
|
||||
defer aw.deinit();
|
||||
try tree.render(gpa, bw, .{});
|
||||
const formatted = aw.getWritten();
|
||||
|
||||
if (check_flag) {
|
||||
const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code));
|
||||
@ -153,6 +152,12 @@ pub fn run(
|
||||
fatal("expected at least one source file argument", .{});
|
||||
}
|
||||
|
||||
var stdout_buffer: [4096]u8 = undefined;
|
||||
var stdout: std.io.BufferedWriter = .{
|
||||
.buffer = &stdout_buffer,
|
||||
.unbuffered_writer = std.io.getStdOut().writer(),
|
||||
};
|
||||
|
||||
var fmt: Fmt = .{
|
||||
.gpa = gpa,
|
||||
.arena = arena,
|
||||
@ -161,10 +166,11 @@ pub fn run(
|
||||
.check_ast = check_ast_flag,
|
||||
.force_zon = force_zon,
|
||||
.color = color,
|
||||
.out_buffer = std.ArrayList(u8).init(gpa),
|
||||
.out_buffer = .empty,
|
||||
.stdout = &stdout,
|
||||
};
|
||||
defer fmt.seen.deinit();
|
||||
defer fmt.out_buffer.deinit();
|
||||
defer fmt.out_buffer.deinit(gpa);
|
||||
|
||||
// Mark any excluded files/directories as already seen,
|
||||
// so that they are skipped later during actual processing
|
||||
@ -322,15 +328,19 @@ fn fmtPathFile(
|
||||
|
||||
// As a heuristic, we make enough capacity for the same as the input source.
|
||||
fmt.out_buffer.shrinkRetainingCapacity(0);
|
||||
try fmt.out_buffer.ensureTotalCapacity(source_code.len);
|
||||
try fmt.out_buffer.ensureTotalCapacity(gpa, source_code.len);
|
||||
|
||||
try tree.renderToArrayList(&fmt.out_buffer, .{});
|
||||
{
|
||||
var aw: std.io.AllocatingWriter = undefined;
|
||||
const bw = aw.fromArrayList(gpa, &fmt.out_buffer);
|
||||
defer fmt.out_buffer = aw.toArrayList();
|
||||
try tree.render(gpa, bw, .{});
|
||||
}
|
||||
if (mem.eql(u8, fmt.out_buffer.items, source_code))
|
||||
return;
|
||||
|
||||
if (check_mode) {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{file_path});
|
||||
try fmt.stdout.print("{s}\n", .{file_path});
|
||||
fmt.any_error = true;
|
||||
} else {
|
||||
var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });
|
||||
@ -338,8 +348,7 @@ fn fmtPathFile(
|
||||
|
||||
try af.file.writeAll(fmt.out_buffer.items);
|
||||
try af.finish();
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{file_path});
|
||||
try fmt.stdout.print("{s}\n", .{file_path});
|
||||
}
|
||||
}
|
||||
|
||||
@ -350,3 +359,12 @@ const process = std.process;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Color = std.zig.Color;
|
||||
const fatal = std.process.fatal;
|
||||
|
||||
/// Provided for debugging/testing purposes; unused by the compiler.
|
||||
pub fn main() !void {
|
||||
const gpa = std.heap.smp_allocator;
|
||||
var arena_instance = std.heap.ArenaAllocator.init(gpa);
|
||||
const arena = arena_instance.allocator();
|
||||
const args = try process.argsAlloc(arena);
|
||||
return run(gpa, arena, args[1..]);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user