diff --git a/vlib/v/fmt/comments.v b/vlib/v/fmt/comments.v index 67d5b93236..45a705ea1f 100644 --- a/vlib/v/fmt/comments.v +++ b/vlib/v/fmt/comments.v @@ -27,6 +27,9 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { f.writeln(node.text) return } + if options.level == .indent { + f.indent++ + } if options.iembed { x := node.text.trim_left('\x01') if x.contains('\n') { @@ -36,9 +39,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { } else { f.write('/* ${x.trim(' ')} */') } - return - } - if !node.text.contains('\n') { + } else if !node.text.contains('\n') { is_separate_line := !options.inline || node.text.starts_with('\x01') mut s := node.text.trim_left('\x01') mut out_s := '//' @@ -54,16 +55,19 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { f.write(' ') } f.write(out_s) - return + } else { + lines := node.text.trim_space().split_into_lines() + f.writeln('/*') + for line in lines { + f.writeln(line) + f.empty_line = false + } + f.empty_line = true + f.write('*/') } - lines := node.text.trim_space().split_into_lines() - f.writeln('/*') - for line in lines { - f.writeln(line) - f.empty_line = false + if options.level == .indent { + f.indent-- } - f.empty_line = true - f.write('*/') } pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) { @@ -71,21 +75,19 @@ pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) { if !f.out.last_n(1)[0].is_space() { f.write(' ') } - if options.level == .indent { - f.indent++ - } f.comment(c, options) - if i < comments.len - 1 || options.has_nl { + if !options.iembed && (i < comments.len - 1 || options.has_nl) { f.writeln('') } - if options.level == .indent { - f.indent-- - } } } +pub fn (mut f Fmt) comments_before_field(comments []ast.Comment) { + // They behave the same as comments after the last field. This alias is just for clarity. + f.comments_after_last_field(comments) +} + pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) { - // Handle comments after last field for comment in comments { f.indent++ f.empty_line = true diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index a086271ca2..7622d7ef79 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -699,26 +699,16 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { f.writeln('module:') } end_pos := field.pos.pos + field.pos.len - comments := field.comments - // Handle comments before field - mut comm_idx := 0 - f.indent++ - for comm_idx < comments.len && comments[comm_idx].pos.pos < field.pos.pos { - f.empty_line = true - f.comment(comments[comm_idx], {}) - f.writeln('') - comm_idx++ - } - f.indent-- + 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)..] + // Handle comments before the field + f.comments_before_field(before_comments) f.write('\t$field.name ') // Handle comments between field name and type - mut comments_len := 0 - for comm_idx < comments.len && comments[comm_idx].pos.pos < end_pos { - comment_text := '/* ${comments[comm_idx].text.trim_left('\x01')} */ ' // TODO handle in a function - comments_len += comment_text.len - f.write(comment_text) - comm_idx++ - } + before_len := f.line_len + 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 { field_align_i++ @@ -751,9 +741,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { inc_indent = false } } - // Handle comments after field type (same line) - if comm_idx < comments.len { - if comments[comm_idx].pos.line_nr > field.pos.line_nr { + // 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 { @@ -767,7 +757,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } f.write(' ') } - f.comments(comments[comm_idx..], level: .indent) + f.comments(after_type_comments, level: .indent) } else { f.writeln('') } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 9366355ab6..935261d4f9 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -600,9 +600,10 @@ pub fn (mut p Parser) eat_comments() []ast.Comment { } pub fn (mut p Parser) eat_line_end_comments() []ast.Comment { + line := p.prev_tok.line_nr mut comments := []ast.Comment{} for { - if p.tok.kind != .comment || p.tok.line_nr != p.prev_tok.line_nr { + if p.tok.kind != .comment || p.tok.line_nr > line { break } comments << p.comment()