diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 14bc7173ef..d7eb5e5d22 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -865,22 +865,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.lock_expr(node) } ast.MapInit { - if node.keys.len == 0 { - 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('}') + f.map_init(node) } ast.MatchExpr { 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) { type_sym := f.table.get_type_symbol(it.typ) // f.write('') diff --git a/vlib/v/fmt/tests/maps_expected.vv b/vlib/v/fmt/tests/maps_expected.vv index 701a6d8664..122114a299 100644 --- a/vlib/v/fmt/tests/maps_expected.vv +++ b/vlib/v/fmt/tests/maps_expected.vv @@ -1,9 +1,16 @@ const ( reserved_types = { - 'i8': true - 'i16': true - 'int': true - 'i64': true + 'i8': true + 'i16': true + 'int': true + 'i64': true 'i128': true } ) + +numbers := { + 'one': 1 + 'two': 2 + 'sevenhundredseventyseven': 777 + 'fivethousandthreehundredtwentyseven': 5327 +} diff --git a/vlib/v/fmt/tests/maps_input.vv b/vlib/v/fmt/tests/maps_input.vv index 04d7a01df6..6eb783e285 100644 --- a/vlib/v/fmt/tests/maps_input.vv +++ b/vlib/v/fmt/tests/maps_input.vv @@ -7,3 +7,10 @@ reserved_types = { 'i128': true } ) + +numbers := { + 'one': 1 + 'two': 2 + 'sevenhundredseventyseven': 777 +'fivethousandthreehundredtwentyseven': 5327 +}