fmt: improve map value alignment (#7105) (#7151)

pull/7157/head
Roland Lammel 2020-12-06 02:04:45 +01:00 committed by GitHub
parent 79a20c338b
commit 028c82e255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 20 deletions

View File

@ -865,22 +865,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.lock_expr(node) f.lock_expr(node)
} }
ast.MapInit { ast.MapInit {
if node.keys.len == 0 { f.map_init(node)
f.write(f.table.type_to_str(node.typ))
f.write('{}')
return
}
f.writeln('{')
f.indent++
for i, key in node.keys {
f.expr(key)
// f.write(strings.repeat(` `, max - field.name.len))
f.write(': ')
f.expr(node.vals[i])
f.writeln('')
}
f.indent--
f.write('}')
} }
ast.MatchExpr { ast.MatchExpr {
f.match_expr(node) f.match_expr(node)
@ -1805,6 +1790,31 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
} }
} }
pub fn (mut f Fmt) map_init(it ast.MapInit) {
if it.keys.len == 0 {
f.write(f.table.type_to_str(it.typ))
f.write('{}')
return
}
f.writeln('{')
f.indent++
mut max_field_len := 0
for key in it.keys {
if key.str().len > max_field_len {
max_field_len = key.str().len
}
}
for i, key in it.keys {
f.expr(key)
f.write(': ')
f.write(strings.repeat(` `, max_field_len - key.str().len))
f.expr(it.vals[i])
f.writeln('')
}
f.indent--
f.write('}')
}
pub fn (mut f Fmt) struct_init(it ast.StructInit) { pub fn (mut f Fmt) struct_init(it ast.StructInit) {
type_sym := f.table.get_type_symbol(it.typ) type_sym := f.table.get_type_symbol(it.typ)
// f.write('<old name: $type_sym.name>') // f.write('<old name: $type_sym.name>')

View File

@ -1,9 +1,16 @@
const ( const (
reserved_types = { reserved_types = {
'i8': true 'i8': true
'i16': true 'i16': true
'int': true 'int': true
'i64': true 'i64': true
'i128': true 'i128': true
} }
) )
numbers := {
'one': 1
'two': 2
'sevenhundredseventyseven': 777
'fivethousandthreehundredtwentyseven': 5327
}

View File

@ -7,3 +7,10 @@ reserved_types = {
'i128': true 'i128': true
} }
) )
numbers := {
'one': 1
'two': 2
'sevenhundredseventyseven': 777
'fivethousandthreehundredtwentyseven': 5327
}