Have wuffsfmt align the "T"s in "x T" fields

This commit is contained in:
Nigel Tao committed 2018-12-27 15:09:52 +11:00
1 parent 1ceb304ad9
commit d26fa09d07
7 files changed
+58 -43

No files matched your search

+31 -16
View File
@@ -43,7 +43,10 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
indent := 0
buf := make([]byte, 0, 1024)
commentLine := uint32(0)
inStruct := false
varNameLength := uint32(0)
prevLine := src[0].Line - 1
prevLineHanging := false
@@ -79,6 +82,7 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
if _, err = w.Write(buf); err != nil {
return err
}
varNameLength = 0
prevLine = commentLine
}
@@ -113,19 +117,28 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
}
buf = appendTabs(buf, indent+indentAdjustment)
if (len(lineTokens) < 2) || (lineTokens[0].ID != t.IDVar) {
// Apply or update varNameLength.
if len(lineTokens) < 3 {
varNameLength = 0
} else {
} else if id := lineTokens[0].ID; id == t.IDPri || id == t.IDPub {
inStruct = lineTokens[1].ID == t.IDStruct
varNameLength = 0
} else if id == t.IDVar || inStruct {
if varNameLength == 0 {
varNameLength = measureVarNameLength(tm, lineTokens, src)
}
name := tm.ByID(lineTokens[1].ID)
buf = append(buf, "var "...)
if id == t.IDVar {
buf = append(buf, "var "...)
lineTokens = lineTokens[1:]
}
name := tm.ByID(lineTokens[0].ID)
lineTokens = lineTokens[1:]
buf = append(buf, name...)
for i := uint32(len(name)); i <= varNameLength; i++ {
buf = append(buf, ' ')
}
lineTokens = lineTokens[2:]
} else {
varNameLength = 0
}
// Render the lineTokens.
@@ -241,19 +254,21 @@ func measureVarNameLength(tm *t.Map, lineTokens []t.Token, remaining []t.Token)
if len(lineTokens) < 2 {
return 0
}
line := lineTokens[0].Line
length := len(tm.ByID(lineTokens[1].ID))
for len(remaining) >= 2 && (remaining[0].ID == t.IDVar) && (remaining[0].Line == line+1) {
line = remaining[0].Line
length = max(length, len(tm.ByID(remaining[1].ID)))
remaining = remaining[2:]
for len(remaining) > 0 {
id := remaining[0].ID
x := 0 // "x T" struct field.
if lineTokens[0].ID == t.IDVar {
x = 1 // "var x T" var statement.
}
line := lineTokens[0].Line
length := len(tm.ByID(lineTokens[x].ID))
for (len(remaining) > x) && ((x == 0) || (remaining[0].ID == t.IDVar)) && (remaining[0].Line == line+1) {
line = remaining[0].Line
length = max(length, len(tm.ByID(remaining[x].ID)))
remaining = remaining[x+1:]
for len(remaining) > 0 && remaining[0].Line == line {
remaining = remaining[1:]
if id == t.IDSemicolon {
break
}
}
}
return uint32(length)
+1 -1
View File
@@ -14,7 +14,7 @@
// TODO: drop the '?' but still generate wuffs_adler32__hasher__initialize?
pub struct hasher?(
state base.u32,
state base.u32,
started base.bool,
)
+3 -3
View File
@@ -56,7 +56,7 @@ pri const dcode_magic_numbers array[32] base.u32 = [
pub struct decoder?(
// These fields yield src's bits in Least Significant Bits order.
bits base.u32,
bits base.u32,
n_bits base.u32,
// huffs and n_huffs_bits are the lookup tables for Huffman decodings.
@@ -92,7 +92,7 @@ pub struct decoder?(
//
// TODO: replace the magic "big enough" 1234 with something more
// principled, perhaps discovered via an exhaustive search.
huffs array[2] array[1234] base.u32,
huffs array[2] array[1234] base.u32,
n_huffs_bits array[2] base.u32[..9],
// history holds up to the last 32KiB of decoded output, if the decoding
@@ -106,7 +106,7 @@ pub struct decoder?(
//
// history[history_index & 0x7FFF] is where the next byte of decoded output
// will be written.
history array[0x8000] base.u8, // 32 KiB.
history array[0x8000] base.u8, // 32 KiB.
history_index base.u32,
// code_lengths is used to pass out-of-band data to init_huff.
+14 -14
View File
@@ -41,7 +41,7 @@ pri const interlace_start array[5] base.u32 = [0xFFFFFFFF, 1, 2, 4, 0]
pri const interlace_delta array[5] base.u8 = [1, 2, 4, 8, 8]
pub struct decoder?(
width base.u32,
width base.u32,
height base.u32,
// Call sequence states:
@@ -72,8 +72,8 @@ pub struct decoder?(
// - IC is decode_image_config, implicit means nullptr args.dst
call_sequence base.u8,
end_of_data base.bool,
restarted base.bool,
end_of_data base.bool,
restarted base.bool,
previous_lzw_decode_ended_abruptly base.bool,
which_palette base.u8[..1],
@@ -84,18 +84,18 @@ pub struct decoder?(
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
// animation loops is 1.
seen_num_loops base.bool,
num_loops base.u32,
num_loops base.u32,
seen_graphic_control base.bool,
seen_graphic_control base.bool,
gc_has_transparent_index base.bool,
gc_transparent_index base.u8,
gc_disposal base.u8,
gc_transparent_index base.u8,
gc_disposal base.u8,
// There are 7056000 flicks per centisecond.
gc_duration base.u64[..0xFFFF * 7056000],
frame_config_io_position base.u64,
frame_config_io_position base.u64,
num_decoded_frame_configs_value base.u64,
num_decoded_frames_value base.u64,
num_decoded_frames_value base.u64,
frame_rect_x0 base.u32,
frame_rect_y0 base.u32,
@@ -103,13 +103,13 @@ pub struct decoder?(
frame_rect_y1 base.u32,
// The dst_etc fields are the output cursor during copy_to_image_buffer.
dst_x base.u32,
dst_y base.u32,
dst_x base.u32,
dst_y base.u32,
dirty_y base.range_ie_u32,
compressed_ri base.u64,
compressed_wi base.u64,
compressed array[4096] base.u8,
compressed array[4096] base.u8,
// palettes[0] and palettes[1] are the Global and Local Color Table.
palettes array[2] array[4 * 256] base.u8,
@@ -117,8 +117,8 @@ pub struct decoder?(
dst_palette array[4 * 256] base.u8,
swizzler base.pixel_swizzler,
util base.utility,
lzw lzw.decoder,
util base.utility,
lzw lzw.decoder,
)
pub func decoder.decode_image_config!??(dst nptr base.image_config, src base.io_reader) {
+2 -2
View File
@@ -21,8 +21,8 @@ pub status "?bad encoding flags"
pub status "?bad header"
pub struct decoder?(
flate deflate.decoder,
checksum crc32.ieee_hasher,
flate deflate.decoder,
checksum crc32.ieee_hasher,
ignore_checksum base.bool,
)
+5 -5
View File
@@ -24,15 +24,15 @@ pub struct decoder?(
// read_from state that does not change during a decode call.
literal_width base.u32[..8],
clear_code base.u32[..256],
end_code base.u32[..257],
clear_code base.u32[..256],
end_code base.u32[..257],
// read_from state that does change during a decode call.
save_code base.u32[..4096],
prev_code base.u32[..4095],
width base.u32[..12],
bits base.u32,
n_bits base.u32[..31],
width base.u32[..12],
bits base.u32,
n_bits base.u32[..31],
output_wi base.u32[..8191],
// read_from return value. The read_from method effectively returns a
+2 -2
View File
@@ -23,8 +23,8 @@ pub status "?bad parity check"
pri status "?TODO: unsupported preset dictionary"
pub struct decoder?(
flate deflate.decoder,
checksum adler32.hasher,
flate deflate.decoder,
checksum adler32.hasher,
ignore_checksum base.bool,
)