From b25288338c6b96d18eb76cda54b3fd4815b88877 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 28 Jul 2023 16:42:10 +0800 Subject: [PATCH] parser, fmt: fix formatting interface fields with pre-comments (fix #18980) (#18988) --- vlib/v/fmt/fmt.v | 11 ++++++----- .../interface_fields_with_pre_comments_keep.vv | 14 ++++++++++++++ vlib/v/parser/struct.v | 9 ++------- 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 vlib/v/fmt/tests/interface_fields_with_pre_comments_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 36f24fafe4..a4e9f7173d 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1290,13 +1290,14 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { pub fn (mut f Fmt) interface_field(field ast.StructField) { ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias)) - 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)..] + end_comments := field.comments.filter(it.pos.pos > field.pos.pos) + if before_comments.len > 0 { + f.comments(before_comments, level: .indent) + } f.write('\t${field.name} ${ft}') - if after_type_comments.len > 0 { - f.comments(after_type_comments, level: .indent) + if end_comments.len > 0 { + f.comments(end_comments, level: .indent) } else { f.writeln('') } diff --git a/vlib/v/fmt/tests/interface_fields_with_pre_comments_keep.vv b/vlib/v/fmt/tests/interface_fields_with_pre_comments_keep.vv new file mode 100644 index 0000000000..b8d9e3dc44 --- /dev/null +++ b/vlib/v/fmt/tests/interface_fields_with_pre_comments_keep.vv @@ -0,0 +1,14 @@ +module main + +fn main() { +} + +pub interface IOperateInfo { +mut: + // title + title string + // msg + msg string + // id + id string +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 2c7f9929d2..86291a7b06 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -702,18 +702,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { info.methods << tmethod } else { // interface fields + mut comments := p.eat_comments() field_pos := p.tok.pos() field_name := p.check_name() mut type_pos := p.tok.pos() field_typ := p.parse_type() type_pos = type_pos.extend(p.prev_tok.pos()) - mut comments := []ast.Comment{} - for p.tok.kind == .comment { - comments << p.comment() - if p.tok.kind == .rcbr { - break - } - } + comments << p.eat_comments(same_line: true) fields << ast.StructField{ name: field_name pos: field_pos