fmt: struct field comments on the same line

pull/4248/head
Alexander Medvednikov 2020-04-05 12:25:33 +02:00
parent 0869b230fb
commit de701ccfac
3 changed files with 21 additions and 12 deletions

View File

@ -675,6 +675,7 @@ pub:
text string text string
is_multi bool is_multi bool
line_nr int line_nr int
pos token.Position
} }
pub struct ConcatExpr { pub struct ConcatExpr {

View File

@ -341,13 +341,25 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
else if i == node.pub_mut_pos { else if i == node.pub_mut_pos {
f.writeln('pub mut:') 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.write('\t')
f.comment(field.comment) f.comment(field.comment)
} }
f.write('\t$field.name ') f.write('\t$field.name ')
f.write(strings.repeat(` `, max - field.name.len)) 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') 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) { fn (f mut Fmt) comment(node ast.Comment) {
if !node.text.contains('\n') { if !node.text.contains('\n') {
f.writeln('// $node.text') f.writeln('// $node.text')// $node.pos.line_nr')
return return
} }
lines := node.text.split_into_lines() lines := node.text.split_into_lines()

View File

@ -341,11 +341,13 @@ pub fn (p mut Parser) check_comment() ast.Comment {
} }
pub fn (p mut Parser) comment() ast.Comment { pub fn (p mut Parser) comment() ast.Comment {
pos := p.tok.position()
text := p.tok.lit text := p.tok.lit
p.next() p.next()
//p.next_with_comment() //p.next_with_comment()
return ast.Comment{ return ast.Comment{
text: text 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}` is_short_syntax := !(p.peek_tok.kind == .colon || p.tok.kind == .rcbr) // `Vec{a,b,c}`
// p.warn(is_short_syntax.str()) // p.warn(is_short_syntax.str())
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr {
if p.tok.kind == .comment { p.check_comment()
p.comment()
}
mut field_name := '' mut field_name := ''
if is_short_syntax { if is_short_syntax {
expr := p.expr(0) expr := p.expr(0)
@ -580,9 +580,7 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
} }
if p.tok.kind == .comment { p.check_comment()
p.comment()
}
} }
node := ast.StructInit{ node := ast.StructInit{
typ: typ typ: typ
@ -1329,9 +1327,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
} }
if p.tok.kind == .comment { //p.check_comment()
p.comment()
}
} }
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
p.check(.rsbr) p.check(.rsbr)