From ad18d614ad1065fd7c284a0513ee30e0cbb3208d Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:39:31 +0100 Subject: [PATCH] fmt: update determining of struct field comments --- vlib/v/fmt/struct.v | 47 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index cba91d3070bda3..93d82db76cecf4 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -101,22 +101,23 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) { else {} } end_pos := field.pos.pos + field.pos.len - before_comments := field.comments.filter(it.pos.pos < field.pos.pos) - between_comments := field.comments[before_comments.len..].filter(it.pos.pos < end_pos) - after_type_comments := field.comments[(before_comments.len + between_comments.len)..] + mut pre_cmts, mut end_cmts, mut next_line_cmts := []ast.Comment{}, []ast.Comment{}, []ast.Comment{} + for cmt in field.comments { + match true { + cmt.pos.pos < field.pos.pos { pre_cmts << cmt } + cmt.pos.line_nr > field.pos.last_line { next_line_cmts << cmt } + else { end_cmts << cmt } + } + } // Handle comments before the field - f.comments_before_field(before_comments) + f.comments_before_field(pre_cmts) volatile_prefix := if field.is_volatile { 'volatile ' } else { '' } f.write('\t${volatile_prefix}${field.name} ') - // Handle comments between field name and type - before_len := f.line_len - f.comments(between_comments, has_nl: false) - comments_len := f.line_len - before_len if field_aligns[field_align_i].line_nr < field.pos.line_nr { field_align_i++ } field_align := field_aligns[field_align_i] - f.write(strings.repeat(` `, field_align.max_len - field.name.len - comments_len)) + f.write(strings.repeat(` `, field_align.max_len - field.name.len)) // Handle anon structs recursively if !f.write_anon_struct_field_decl(field.typ, field.anon_struct_decl) { f.write(field_types[i]) @@ -153,25 +154,25 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) { f.write(strings.repeat(` `, field_align.max_type_len - field_types[i].len)) f.single_line_attrs(field.attrs, same_line: true) } - // Handle comments after field type - if after_type_comments.len > 0 { - if after_type_comments[0].pos.line_nr > field.pos.line_nr { - f.writeln('') - } else { - if !field.has_default_expr { - if comment_aligns[comment_align_i].line_nr < field.pos.line_nr { - comment_align_i++ - } - align := comment_aligns[comment_align_i] - pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len - f.write(strings.repeat(` `, pad_len)) + // Handle comments at the end of the line + if end_cmts.len > 0 { + if !field.has_default_expr { + if comment_aligns[comment_align_i].line_nr < field.pos.line_nr { + comment_align_i++ } - f.write(' ') + align := comment_aligns[comment_align_i] + pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len + f.write(strings.repeat(` `, pad_len)) } - f.comments(after_type_comments, level: .indent) + f.write(' ') + f.comments(end_cmts, level: .indent) } else { f.writeln('') } + // Handle comments on the next lines + if next_line_cmts.len > 0 { + f.comments(next_line_cmts, level: .indent) + } } if is_anon || node.end_comments.len > 0 { f.write('}')