zig fmt: Set indent_delta to 2 when rendering inline asm

This commit is contained in:
Lachlan Easton 2020-08-31 23:32:42 +10:00
parent 5aca3baea6
commit 029ec456bc
3 changed files with 33 additions and 20 deletions

View File

@ -13,7 +13,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
writer_pointer: *WriterType,
indent_stack: usize = 0,
indent_count: usize = 0,
indent_delta: usize,
current_line_empty: bool = true,
indent_one_shot_count: usize = 0, // automatically popped when applied
@ -24,9 +24,6 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
return Self{ .writer_pointer = writer_pointer, .indent_delta = indent_delta };
}
/// Release all allocated memory.
pub fn deinit(self: Self) void {}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
@ -39,6 +36,21 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
return self.writeNoIndent(bytes);
}
// Change the indent delta without changing the final indentation level
pub fn setIndentDelta(self: *Self, indent_delta: usize) void {
if (self.indent_delta == indent_delta) {
return;
} else if (self.indent_delta > indent_delta) {
assert(self.indent_delta % indent_delta == 0);
self.indent_count = self.indent_count * (self.indent_delta / indent_delta);
} else {
// assert that the current indentation (in spaces) in a multiple of the new delta
assert((self.indent_count * self.indent_delta) % indent_delta == 0);
self.indent_count = self.indent_count / (indent_delta / self.indent_delta);
}
self.indent_delta = indent_delta;
}
fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize {
if (bytes.len == 0)
return @as(usize, 0);
@ -68,7 +80,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
pub fn pushIndent(self: *Self) void {
// Doesn't actually write any indentation.
// Just primes the stream to be able to write the correct indentation if it needs to.
self.indent_stack += 1;
self.indent_count += 1;
}
/// Push an indent that is automatically popped after being applied
@ -92,9 +104,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
}
pub fn popIndent(self: *Self) void {
assert(self.indent_stack != 0);
self.indent_stack -= 1;
self.indent_next_line = std.math.min(self.indent_stack, self.indent_next_line); // Tentative indent may have been popped before there was a newline
assert(self.indent_count != 0);
self.indent_count -= 1;
self.indent_next_line = std.math.min(self.indent_count, self.indent_next_line); // Tentative indent may have been popped before there was a newline
}
/// Writes ' ' bytes if the current line is empty
@ -105,7 +117,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
self.applied_indent = current_indent;
}
self.indent_stack -= self.indent_one_shot_count;
self.indent_count -= self.indent_one_shot_count;
self.indent_one_shot_count = 0;
self.current_line_empty = false;
}
@ -118,9 +130,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
fn currentIndent(self: *Self) usize {
var indent_current: usize = 0;
if (self.indent_stack > 0) {
const stack_top = self.indent_stack - self.indent_next_line;
indent_current = stack_top * self.indent_delta;
if (self.indent_count > 0) {
const indent_count = self.indent_count - self.indent_next_line;
indent_current = indent_count * self.indent_delta;
}
return indent_current;
}

View File

@ -2827,7 +2827,7 @@ test "zig fmt: inline asm" {
\\ return asm volatile ("syscall"
\\ : [ret] "={rax}" (-> usize)
\\ : [number] "{rax}" (number),
\\ [arg1] "{rdi}" (arg1)
\\ [arg1] "{rdi}" (arg1)
\\ : "rcx", "r11"
\\ );
\\}
@ -2930,14 +2930,14 @@ test "zig fmt: inline asm parameter alignment" {
\\ \\ foo
\\ \\ bar
\\ : [_] "" (-> usize),
\\ [_] "" (-> usize)
\\ [_] "" (-> usize)
\\ );
\\ asm volatile (
\\ \\ foo
\\ \\ bar
\\ :
\\ : [_] "" (0),
\\ [_] "" (0)
\\ [_] "" (0)
\\ );
\\ asm volatile (
\\ \\ foo
@ -2950,9 +2950,9 @@ test "zig fmt: inline asm parameter alignment" {
\\ \\ foo
\\ \\ bar
\\ : [_] "" (-> usize),
\\ [_] "" (-> usize)
\\ [_] "" (-> usize)
\\ : [_] "" (0),
\\ [_] "" (0)
\\ [_] "" (0)
\\ : "", ""
\\ );
\\}

View File

@ -11,6 +11,7 @@ const ast = std.zig.ast;
const Token = std.zig.Token;
const indent_delta = 4;
const asm_indent_delta = 2;
pub const Error = error{
/// Ran out of memory allocating call stack frames to complete rendering.
@ -25,7 +26,6 @@ pub fn render(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree) (meta
var s = stream.*;
var change_detection_stream = std.io.changeDetectionStream(tree.source, &s);
var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &change_detection_stream);
defer auto_indenting_stream.deinit();
try renderRoot(allocator, &auto_indenting_stream, tree);
@ -784,7 +784,6 @@ fn renderExpression(
// Null stream for counting the printed length of each expression
var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &counting_stream);
defer auto_indenting_stream.deinit();
for (exprs) |expr, i| {
counting_stream.bytes_written = 0;
@ -903,7 +902,6 @@ fn renderExpression(
for (field_inits) |field_init| {
var find_stream = std.io.findByteOutStream('\n', &std.io.null_out_stream);
var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &find_stream);
defer auto_indenting_stream.deinit();
try renderExpression(allocator, &auto_indenting_stream, tree, field_init, Space.None);
if (find_stream.byte_found) break :blk false;
@ -1959,6 +1957,9 @@ fn renderExpression(
try renderExpression(allocator, stream, tree, asm_node.template, Space.Newline);
stream.setIndentDelta(asm_indent_delta);
defer stream.setIndentDelta(indent_delta);
const colon1 = tree.nextToken(asm_node.template.lastToken());
const colon2 = if (asm_node.outputs.len == 0) blk: {