From 5e4cbcc1e378bfb7ca03348cb0c17271d9e6ddf1 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 23 Jul 2021 11:12:09 +0200 Subject: [PATCH] fmt: unify struct field align code (#10887) --- vlib/v/fmt/align.v | 56 ++++++++++ vlib/v/fmt/struct.v | 104 ++---------------- .../tests/array_init_comment_ending_keep.vv | 2 +- vlib/v/fmt/tests/nested_map_type_keep.vv | 1 - .../tests/struct_init_with_comments_keep.vv | 2 +- 5 files changed, 70 insertions(+), 95 deletions(-) create mode 100644 vlib/v/fmt/align.v diff --git a/vlib/v/fmt/align.v b/vlib/v/fmt/align.v new file mode 100644 index 0000000000..f767abdb93 --- /dev/null +++ b/vlib/v/fmt/align.v @@ -0,0 +1,56 @@ +// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. +module fmt + +import math.mathutil + +const struct_field_align_threshold = 8 + +struct AlignInfo { +mut: + line_nr int + max_len int + max_type_len int +} + +struct AddInfoConfig { + use_threshold bool +} + +fn (mut infos []AlignInfo) add_new_info(len int, type_len int, line int) { + infos << AlignInfo{ + line_nr: line + max_len: len + max_type_len: type_len + } +} + +[direct_array_access] +fn (mut infos []AlignInfo) add_info(len int, type_len int, line int, cfg AddInfoConfig) { + if infos.len == 0 { + infos.add_new_info(len, type_len, line) + return + } + i := infos.len - 1 + if line - infos[i].line_nr > 1 { + infos.add_new_info(len, type_len, line) + return + } + if cfg.use_threshold { + len_diff := mathutil.abs(infos[i].max_len - len) + + mathutil.abs(infos[i].max_type_len - type_len) + + if len_diff >= fmt.struct_field_align_threshold { + infos.add_new_info(len, type_len, line) + return + } + } + infos[i].line_nr = line + if len > infos[i].max_len { + infos[i].max_len = len + } + if type_len > infos[i].max_type_len { + infos[i].max_type_len = type_len + } +} diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 799c4279ac..1b6a924981 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -7,88 +7,6 @@ import math.mathutil as mu import strings import v.ast -const ( - threshold_to_align_struct = 8 -) - -struct StructFieldAlignInfo { -mut: - first_line int - last_line int - max_type_len int - max_len int -} - -fn (mut list []StructFieldAlignInfo) add_new_info(len int, type_len int, line int) { - list << StructFieldAlignInfo{ - first_line: line - last_line: line - max_type_len: type_len - max_len: len - } -} - -[direct_array_access] -fn (mut list []StructFieldAlignInfo) add_info(len int, type_len int, line int) { - if list.len == 0 { - list.add_new_info(len, type_len, line) - return - } - i := list.len - 1 - if line - list[i].last_line > 1 { - list.add_new_info(len, type_len, line) - return - } - list[i].last_line = line - if len > list[i].max_len { - list[i].max_len = len - } - if type_len > list[i].max_type_len { - list[i].max_type_len = type_len - } -} - -struct CommentAndExprAlignInfo { -mut: - max_attrs_len int - max_type_len int - first_line int - last_line int -} - -fn (mut list []CommentAndExprAlignInfo) add_new_info(attrs_len int, type_len int, line int) { - list << CommentAndExprAlignInfo{ - max_attrs_len: attrs_len - max_type_len: type_len - first_line: line - last_line: line - } -} - -fn (mut list []CommentAndExprAlignInfo) add_info(attrs_len int, type_len int, line int) { - if list.len == 0 { - list.add_new_info(attrs_len, type_len, line) - return - } - i := list.len - 1 - if line - list[i].last_line > 1 { - list.add_new_info(attrs_len, type_len, line) - return - } - d_len := mu.abs(list[i].max_attrs_len - attrs_len) + mu.abs(list[i].max_type_len - type_len) - if !(d_len < fmt.threshold_to_align_struct) { - list.add_new_info(attrs_len, type_len, line) - return - } - list[i].last_line = line - if attrs_len > list[i].max_attrs_len { - list[i].max_attrs_len = attrs_len - } - if type_len > list[i].max_type_len { - list[i].max_type_len = type_len - } -} - pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { f.attrs(node.attrs) if node.is_pub { @@ -113,9 +31,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { return } f.writeln(' {') - mut field_aligns := []StructFieldAlignInfo{} - mut comment_aligns := []CommentAndExprAlignInfo{} - mut default_expr_aligns := []CommentAndExprAlignInfo{} + mut field_aligns := []AlignInfo{} + mut comment_aligns := []AlignInfo{} + mut default_expr_aligns := []AlignInfo{} mut field_types := []string{cap: node.fields.len} for i, field in node.fields { ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias)) @@ -126,7 +44,8 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { for comment in field.comments { if comment.pos.pos >= end_pos { if comment.pos.line_nr == field.pos.line_nr { - comment_aligns.add_info(attrs_len, field_types[i].len, comment.pos.line_nr) + comment_aligns.add_info(attrs_len, field_types[i].len, comment.pos.line_nr, + use_threshold: true) } continue } @@ -136,7 +55,8 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } field_aligns.add_info(comments_len + field.name.len, ft.len, field.pos.line_nr) if field.has_default_expr { - default_expr_aligns.add_info(attrs_len, field_types[i].len, field.pos.line_nr) + default_expr_aligns.add_info(attrs_len, field_types[i].len, field.pos.line_nr, + use_threshold: true) } } for embed in node.embeds { @@ -194,7 +114,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { f.comments(between_comments, iembed: true, has_nl: false) comments_len := f.line_len - before_len mut field_align := field_aligns[field_align_i] - if field_align.last_line < field.pos.line_nr { + if field_align.line_nr < field.pos.line_nr { field_align_i++ field_align = field_aligns[field_align_i] } @@ -209,11 +129,11 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } if field.has_default_expr { mut align := default_expr_aligns[default_expr_align_i] - if align.last_line < field.pos.line_nr { + if align.line_nr < field.pos.line_nr { default_expr_align_i++ align = default_expr_aligns[default_expr_align_i] } - pad_len := align.max_attrs_len - attrs_len + align.max_type_len - field_types[i].len + pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len f.write(strings.repeat(` `, pad_len)) f.write(' = ') if !expr_is_single_line(field.default_expr) { @@ -233,11 +153,11 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } else { if !field.has_default_expr { mut align := comment_aligns[comment_align_i] - if align.last_line < field.pos.line_nr { + if align.line_nr < field.pos.line_nr { comment_align_i++ align = comment_aligns[comment_align_i] } - pad_len := align.max_attrs_len - attrs_len + align.max_type_len - field_types[i].len + pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len f.write(strings.repeat(` `, pad_len)) } f.write(' ') diff --git a/vlib/v/fmt/tests/array_init_comment_ending_keep.vv b/vlib/v/fmt/tests/array_init_comment_ending_keep.vv index a3932a503f..9fe7624f4e 100644 --- a/vlib/v/fmt/tests/array_init_comment_ending_keep.vv +++ b/vlib/v/fmt/tests/array_init_comment_ending_keep.vv @@ -14,7 +14,7 @@ const ( }, ] } - // TODO(bradfitz): users have reported seeing this in the + // (bradfitz): users have reported seeing this in the // wild, but do browsers handle it? RFC 6265 just says "don't // do that" (section 3) and then never mentions header folding // again. diff --git a/vlib/v/fmt/tests/nested_map_type_keep.vv b/vlib/v/fmt/tests/nested_map_type_keep.vv index 713cf8059e..37efebc7ff 100644 --- a/vlib/v/fmt/tests/nested_map_type_keep.vv +++ b/vlib/v/fmt/tests/nested_map_type_keep.vv @@ -1,6 +1,5 @@ import v.ast -// TODO fix `fn foo(my_map map[string]map[string]int) []ast.FnDecl {` fn foo(my_map map[string]map[string]int) int { return 0 } diff --git a/vlib/v/fmt/tests/struct_init_with_comments_keep.vv b/vlib/v/fmt/tests/struct_init_with_comments_keep.vv index f2341e847a..60576b2d49 100644 --- a/vlib/v/fmt/tests/struct_init_with_comments_keep.vv +++ b/vlib/v/fmt/tests/struct_init_with_comments_keep.vv @@ -2,7 +2,7 @@ module abcde pub struct Builder { pub mut: - // TODO + // inline before field buf []byte str_calls int len int