From de701ccfacfd886978830e9b24fa038867d71ada Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 5 Apr 2020 12:25:33 +0200 Subject: [PATCH] fmt: struct field comments on the same line --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/fmt.v | 18 +++++++++++++++--- vlib/v/parser/parser.v | 14 +++++--------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 6d350f8811..7676b02900 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -675,6 +675,7 @@ pub: text string is_multi bool line_nr int + pos token.Position } pub struct ConcatExpr { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 4c4ab2f147..550de110f2 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -341,13 +341,25 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) { else if i == node.pub_mut_pos { f.writeln('pub mut:') } - if field.comment.text != '' { + if field.comment.text != '' && field.comment.pos.line_nr < field.pos.line_nr { + // Comment on the previous line f.write('\t') f.comment(field.comment) } f.write('\t$field.name ') f.write(strings.repeat(` `, max - field.name.len)) - f.writeln(f.type_to_str(field.typ)) + f.write(f.type_to_str(field.typ)) + //f.write('// $field.pos.line_nr') + if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr { + // Same line comment + f.write(' ') + f.comment(field.comment) + } else { + //if field.comment.text != '' { + //f.write (' // com linenr=$field.comment.pos.line_nr') + //} + f.writeln('') + } } f.writeln('}\n') } @@ -669,7 +681,7 @@ fn (f mut Fmt) or_expr(or_block ast.OrExpr) { fn (f mut Fmt) comment(node ast.Comment) { if !node.text.contains('\n') { - f.writeln('// $node.text') + f.writeln('// $node.text')// $node.pos.line_nr') return } lines := node.text.split_into_lines() diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 830a465410..6db5a17d7b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -341,11 +341,13 @@ pub fn (p mut Parser) check_comment() ast.Comment { } pub fn (p mut Parser) comment() ast.Comment { + pos := p.tok.position() text := p.tok.lit p.next() //p.next_with_comment() return ast.Comment{ text: text + pos: pos } } @@ -559,9 +561,7 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit { is_short_syntax := !(p.peek_tok.kind == .colon || p.tok.kind == .rcbr) // `Vec{a,b,c}` // p.warn(is_short_syntax.str()) for p.tok.kind != .rcbr { - if p.tok.kind == .comment { - p.comment() - } + p.check_comment() mut field_name := '' if is_short_syntax { expr := p.expr(0) @@ -580,9 +580,7 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit { if p.tok.kind == .comma { p.check(.comma) } - if p.tok.kind == .comment { - p.comment() - } + p.check_comment() } node := ast.StructInit{ typ: typ @@ -1329,9 +1327,7 @@ fn (p mut Parser) array_init() ast.ArrayInit { if p.tok.kind == .comma { p.check(.comma) } - if p.tok.kind == .comment { - p.comment() - } + //p.check_comment() } line_nr := p.tok.line_nr p.check(.rsbr)