fmt: fix alignment of struct init fields (#22025)

This commit is contained in:
yuyi 2024-08-11 14:11:24 +08:00 committed by GitHub
parent 99da5726db
commit c51d30bf53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
671 changed files with 18817 additions and 18787 deletions

View file

@ -155,7 +155,11 @@ fn (mut vd VDoc) create_search_results(mod string, dn doc.DocNode, out Output) {
dn_description := trim_doc_node_description(dn.name, comments) dn_description := trim_doc_node_description(dn.name, comments)
vd.search_index << dn.name vd.search_index << dn.name
vd.search_data << SearchResult{ vd.search_data << SearchResult{
prefix: if dn.parent_name != '' { '${dn.kind} (${dn.parent_name})' } else { '${dn.kind} ' } prefix: if dn.parent_name != '' {
'${dn.kind} (${dn.parent_name})'
} else {
'${dn.kind} '
}
description: dn_description description: dn_description
badge: mod badge: mod
link: vd.get_file_name(mod, out) + '#' + get_node_id(dn) link: vd.get_file_name(mod, out) + '#' + get_node_id(dn)

View file

@ -42,8 +42,9 @@ fn main() {
is_werror: '-W' in vet_options is_werror: '-W' in vet_options
is_verbose: '-verbose' in vet_options || '-v' in vet_options is_verbose: '-verbose' in vet_options || '-v' in vet_options
show_warnings: '-hide-warnings' !in vet_options && '-w' !in vet_options show_warnings: '-hide-warnings' !in vet_options && '-w' !in vet_options
use_color: '-color' in vet_options || (term_colors && '-nocolor' !in vet_options)
doc_private_fns_too: '-p' in vet_options doc_private_fns_too: '-p' in vet_options
use_color: '-color' in vet_options
|| (term_colors && '-nocolor' !in vet_options)
} }
} }
mut paths := cmdline.only_non_options(vet_options) mut paths := cmdline.only_non_options(vet_options)

View file

@ -106,9 +106,9 @@ const test_cases = [
results: Chunk{ results: Chunk{
chunk_number: 0 chunk_number: 0
chaining_value: iv chaining_value: iv
block_words: [u32(0x44434241), 0x48474645, 0x4c4b4a49, 0x504f4e4d, 0x54535251, 0x58575655, block_words: [u32(0x44434241), 0x48474645, 0x4c4b4a49, 0x504f4e4d, 0x54535251,
0x61205a59, 0x65646362, 0x69686766, 0x6d6c6b6a, 0x71706f6e, 0x75747372, 0x79787776, 0x58575655, 0x61205a59, 0x65646362, 0x69686766, 0x6d6c6b6a, 0x71706f6e, 0x75747372,
0x3130207a, 0x35343332, 0x00383736] 0x79787776, 0x3130207a, 0x35343332, 0x00383736]
flags: 0x0b flags: 0x0b
} }
words: [u32(0x39c41fc6), 0x2dd7c57b, 0xb8b16421, 0x360cbedb, 0x462d5672, 0x56713bb5, words: [u32(0x39c41fc6), 0x2dd7c57b, 0xb8b16421, 0x360cbedb, 0x462d5672, 0x56713bb5,
@ -126,9 +126,9 @@ const test_cases = [
results: Chunk{ results: Chunk{
chunk_number: 0 chunk_number: 0
chaining_value: iv chaining_value: iv
block_words: [u32(0x44434241), 0x48474645, 0x4c4b4a49, 0x504f4e4d, 0x54535251, 0x58575655, block_words: [u32(0x44434241), 0x48474645, 0x4c4b4a49, 0x504f4e4d, 0x54535251,
0x61205a59, 0x65646362, 0x69686766, 0x6d6c6b6a, 0x71706f6e, 0x75747372, 0x79787776, 0x58575655, 0x61205a59, 0x65646362, 0x69686766, 0x6d6c6b6a, 0x71706f6e, 0x75747372,
0x3130207a, 0x35343332, 0x39383736] 0x79787776, 0x3130207a, 0x35343332, 0x39383736]
flags: 0x0b flags: 0x0b
} }
words: [u32(0x6010817a), 0x21deb495, 0x0826485b, 0x6f895da5, 0x9363242a, 0x176b60a9, words: [u32(0x6010817a), 0x21deb495, 0x0826485b, 0x6f895da5, 0x9363242a, 0x176b60a9,

View file

@ -1399,7 +1399,8 @@ fn (mut fm FlagMapper) map_posix_short(flag_ctx FlagContext, field StructField)
delimiter: used_delimiter delimiter: used_delimiter
name: flag_name name: flag_name
pos: pos pos: pos
repeats: count_of_next_first_letter_repeats + count_of_first_letter_repeats repeats: count_of_next_first_letter_repeats +
count_of_first_letter_repeats
} }
fm.handled_pos << pos fm.handled_pos << pos
fm.handled_pos << pos + 1 // next fm.handled_pos << pos + 1 // next

View file

@ -148,7 +148,10 @@ pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
mut checker := &Checker{ mut checker := &Checker{
table: table table: table
pref: pref_ pref: pref_
timers: util.new_timers(should_print: timers_should_print, label: 'checker') timers: util.new_timers(
should_print: timers_should_print
label: 'checker'
)
match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit
v_current_commit_hash: if pref_.building_v { version.githash(pref_.vroot) or { v_current_commit_hash: if pref_.building_v { version.githash(pref_.vroot) or {
@VCURRENTHASH} } else { @VCURRENTHASH } @VCURRENTHASH} } else { @VCURRENTHASH }

View file

@ -275,10 +275,24 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
} }
f.comments(node.update_expr_comments, same_line: true, has_nl: true, level: .keep) f.comments(node.update_expr_comments, same_line: true, has_nl: true, level: .keep)
} }
mut value_aligns := FieldAlign{}
mut comment_aligns := FieldAlign{}
for init_field in node.init_fields {
value_aligns.add_info(init_field.name.len, init_field.pos.line_nr)
if init_field.comments.len > 0 {
comment_aligns.add_info(init_field.expr.str().len + 1, init_field.pos.line_nr)
}
}
for i, init_field in node.init_fields { for i, init_field in node.init_fields {
f.write('${init_field.name}: ') f.write('${init_field.name}: ')
if !single_line_fields {
f.write(strings.repeat(` `, value_aligns.max_len(init_field.pos.line_nr) - init_field.name.len))
}
f.expr(init_field.expr) f.expr(init_field.expr)
if init_field.comments.len > 0 {
f.write(strings.repeat(` `, comment_aligns.max_len(init_field.pos.line_nr) - init_field.expr.str().len))
f.comments(init_field.comments, same_line: true, has_nl: false, level: .indent) f.comments(init_field.comments, same_line: true, has_nl: false, level: .indent)
}
if single_line_fields { if single_line_fields {
if i < node.init_fields.len - 1 { if i < node.init_fields.len - 1 {
f.write(', ') f.write(', ')

View file

@ -320,7 +320,10 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
indent: -1 indent: -1
module_built: module_built module_built: module_built
timers_should_print: timers_should_print timers_should_print: timers_should_print
timers: util.new_timers(should_print: timers_should_print, label: 'global_cgen') timers: util.new_timers(
should_print: timers_should_print
label: 'global_cgen'
)
inner_loop: unsafe { &ast.empty_stmt } inner_loop: unsafe { &ast.empty_stmt }
field_data_type: ast.Type(table.find_type_idx('FieldData')) field_data_type: ast.Type(table.find_type_idx('FieldData'))
enum_data_type: ast.Type(table.find_type_idx('EnumData')) enum_data_type: ast.Type(table.find_type_idx('EnumData'))

View file

@ -495,7 +495,11 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
update_expr_comments: update_expr_comments update_expr_comments: update_expr_comments
has_update_expr: has_update_expr has_update_expr: has_update_expr
name_pos: first_pos name_pos: first_pos
pos: first_pos.extend(if kind == .short_syntax { p.tok.pos() } else { p.prev_tok.pos() }) pos: first_pos.extend(if kind == .short_syntax {
p.tok.pos()
} else {
p.prev_tok.pos()
})
no_keys: no_keys no_keys: no_keys
is_short_syntax: kind == .short_syntax is_short_syntax: kind == .short_syntax
is_anon: kind == .anon is_anon: kind == .anon