| author | |
| committer | |
| log | 029ec456bce5fc6c57eea496db1cebed55e31ede |
| tree | b04ebc1ba122151f53c3da7261a8c109f3ba0ae7 |
| parent | 5aca3baea62326dee301ec29c567dc224baa4a08 |
3 files changed, 33 insertions(+), 20 deletions(-)
lib/std/io/auto_indenting_stream.zig+24-12| ... | ... | @@ -13,7 +13,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 13 | 13 | |
| 14 | 14 | writer_pointer: *WriterType, |
| 15 | 15 | |
| 16 | indent_stack: usize = 0, | |
| 16 | indent_count: usize = 0, | |
| 17 | 17 | indent_delta: usize, |
| 18 | 18 | current_line_empty: bool = true, |
| 19 | 19 | indent_one_shot_count: usize = 0, // automatically popped when applied |
| ... | ... | @@ -24,9 +24,6 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 24 | 24 | return Self{ .writer_pointer = writer_pointer, .indent_delta = indent_delta }; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | /// Release all allocated memory. | |
| 28 | pub fn deinit(self: Self) void {} | |
| 29 | ||
| 30 | 27 | pub fn writer(self: *Self) Writer { |
| 31 | 28 | return .{ .context = self }; |
| 32 | 29 | } |
| ... | ... | @@ -39,6 +36,21 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 39 | 36 | return self.writeNoIndent(bytes); |
| 40 | 37 | } |
| 41 | 38 | |
| 39 | // Change the indent delta without changing the final indentation level | |
| 40 | pub fn setIndentDelta(self: *Self, indent_delta: usize) void { | |
| 41 | if (self.indent_delta == indent_delta) { | |
| 42 | return; | |
| 43 | } else if (self.indent_delta > indent_delta) { | |
| 44 | assert(self.indent_delta % indent_delta == 0); | |
| 45 | self.indent_count = self.indent_count * (self.indent_delta / indent_delta); | |
| 46 | } else { | |
| 47 | // assert that the current indentation (in spaces) in a multiple of the new delta | |
| 48 | assert((self.indent_count * self.indent_delta) % indent_delta == 0); | |
| 49 | self.indent_count = self.indent_count / (indent_delta / self.indent_delta); | |
| 50 | } | |
| 51 | self.indent_delta = indent_delta; | |
| 52 | } | |
| 53 | ||
| 42 | 54 | fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize { |
| 43 | 55 | if (bytes.len == 0) |
| 44 | 56 | return @as(usize, 0); |
| ... | ... | @@ -68,7 +80,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 68 | 80 | pub fn pushIndent(self: *Self) void { |
| 69 | 81 | // Doesn't actually write any indentation. |
| 70 | 82 | // Just primes the stream to be able to write the correct indentation if it needs to. |
| 71 | self.indent_stack += 1; | |
| 83 | self.indent_count += 1; | |
| 72 | 84 | } |
| 73 | 85 | |
| 74 | 86 | /// Push an indent that is automatically popped after being applied |
| ... | ... | @@ -92,9 +104,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 92 | 104 | } |
| 93 | 105 | |
| 94 | 106 | pub fn popIndent(self: *Self) void { |
| 95 | assert(self.indent_stack != 0); | |
| 96 | self.indent_stack -= 1; | |
| 97 | 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 | |
| 107 | assert(self.indent_count != 0); | |
| 108 | self.indent_count -= 1; | |
| 109 | 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 | |
| 98 | 110 | } |
| 99 | 111 | |
| 100 | 112 | /// Writes ' ' bytes if the current line is empty |
| ... | ... | @@ -105,7 +117,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 105 | 117 | self.applied_indent = current_indent; |
| 106 | 118 | } |
| 107 | 119 | |
| 108 | self.indent_stack -= self.indent_one_shot_count; | |
| 120 | self.indent_count -= self.indent_one_shot_count; | |
| 109 | 121 | self.indent_one_shot_count = 0; |
| 110 | 122 | self.current_line_empty = false; |
| 111 | 123 | } |
| ... | ... | @@ -118,9 +130,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type { |
| 118 | 130 | |
| 119 | 131 | fn currentIndent(self: *Self) usize { |
| 120 | 132 | var indent_current: usize = 0; |
| 121 | if (self.indent_stack > 0) { | |
| 122 | const stack_top = self.indent_stack - self.indent_next_line; | |
| 123 | indent_current = stack_top * self.indent_delta; | |
| 133 | if (self.indent_count > 0) { | |
| 134 | const indent_count = self.indent_count - self.indent_next_line; | |
| 135 | indent_current = indent_count * self.indent_delta; | |
| 124 | 136 | } |
| 125 | 137 | return indent_current; |
| 126 | 138 | } |
lib/std/zig/parser_test.zig+5-5| ... | ... | @@ -2827,7 +2827,7 @@ test "zig fmt: inline asm" { |
| 2827 | 2827 | \\ return asm volatile ("syscall" |
| 2828 | 2828 | \\ : [ret] "={rax}" (-> usize) |
| 2829 | 2829 | \\ : [number] "{rax}" (number), |
| 2830 | \\ [arg1] "{rdi}" (arg1) | |
| 2830 | \\ [arg1] "{rdi}" (arg1) | |
| 2831 | 2831 | \\ : "rcx", "r11" |
| 2832 | 2832 | \\ ); |
| 2833 | 2833 | \\} |
| ... | ... | @@ -2930,14 +2930,14 @@ test "zig fmt: inline asm parameter alignment" { |
| 2930 | 2930 | \\ \\ foo |
| 2931 | 2931 | \\ \\ bar |
| 2932 | 2932 | \\ : [_] "" (-> usize), |
| 2933 | \\ [_] "" (-> usize) | |
| 2933 | \\ [_] "" (-> usize) | |
| 2934 | 2934 | \\ ); |
| 2935 | 2935 | \\ asm volatile ( |
| 2936 | 2936 | \\ \\ foo |
| 2937 | 2937 | \\ \\ bar |
| 2938 | 2938 | \\ : |
| 2939 | 2939 | \\ : [_] "" (0), |
| 2940 | \\ [_] "" (0) | |
| 2940 | \\ [_] "" (0) | |
| 2941 | 2941 | \\ ); |
| 2942 | 2942 | \\ asm volatile ( |
| 2943 | 2943 | \\ \\ foo |
| ... | ... | @@ -2950,9 +2950,9 @@ test "zig fmt: inline asm parameter alignment" { |
| 2950 | 2950 | \\ \\ foo |
| 2951 | 2951 | \\ \\ bar |
| 2952 | 2952 | \\ : [_] "" (-> usize), |
| 2953 | \\ [_] "" (-> usize) | |
| 2953 | \\ [_] "" (-> usize) | |
| 2954 | 2954 | \\ : [_] "" (0), |
| 2955 | \\ [_] "" (0) | |
| 2955 | \\ [_] "" (0) | |
| 2956 | 2956 | \\ : "", "" |
| 2957 | 2957 | \\ ); |
| 2958 | 2958 | \\} |
lib/std/zig/render.zig+4-3| ... | ... | @@ -11,6 +11,7 @@ const ast = std.zig.ast; |
| 11 | 11 | const Token = std.zig.Token; |
| 12 | 12 | |
| 13 | 13 | const indent_delta = 4; |
| 14 | const asm_indent_delta = 2; | |
| 14 | 15 | |
| 15 | 16 | pub const Error = error{ |
| 16 | 17 | /// 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 |
| 25 | 26 | var s = stream.*; |
| 26 | 27 | var change_detection_stream = std.io.changeDetectionStream(tree.source, &s); |
| 27 | 28 | var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &change_detection_stream); |
| 28 | defer auto_indenting_stream.deinit(); | |
| 29 | 29 | |
| 30 | 30 | try renderRoot(allocator, &auto_indenting_stream, tree); |
| 31 | 31 | |
| ... | ... | @@ -784,7 +784,6 @@ fn renderExpression( |
| 784 | 784 | // Null stream for counting the printed length of each expression |
| 785 | 785 | var counting_stream = std.io.countingOutStream(std.io.null_out_stream); |
| 786 | 786 | var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &counting_stream); |
| 787 | defer auto_indenting_stream.deinit(); | |
| 788 | 787 | |
| 789 | 788 | for (exprs) |expr, i| { |
| 790 | 789 | counting_stream.bytes_written = 0; |
| ... | ... | @@ -903,7 +902,6 @@ fn renderExpression( |
| 903 | 902 | for (field_inits) |field_init| { |
| 904 | 903 | var find_stream = std.io.findByteOutStream('\n', &std.io.null_out_stream); |
| 905 | 904 | var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &find_stream); |
| 906 | defer auto_indenting_stream.deinit(); | |
| 907 | 905 | |
| 908 | 906 | try renderExpression(allocator, &auto_indenting_stream, tree, field_init, Space.None); |
| 909 | 907 | if (find_stream.byte_found) break :blk false; |
| ... | ... | @@ -1959,6 +1957,9 @@ fn renderExpression( |
| 1959 | 1957 | |
| 1960 | 1958 | try renderExpression(allocator, stream, tree, asm_node.template, Space.Newline); |
| 1961 | 1959 | |
| 1960 | stream.setIndentDelta(asm_indent_delta); | |
| 1961 | defer stream.setIndentDelta(indent_delta); | |
| 1962 | ||
| 1962 | 1963 | const colon1 = tree.nextToken(asm_node.template.lastToken()); |
| 1963 | 1964 | |
| 1964 | 1965 | const colon2 = if (asm_node.outputs.len == 0) blk: { |