parser,fmt: handle array pre-comments separately from exprs (#8884)

pull/8888/head
Lukas Neubert 2021-02-21 19:18:19 +01:00 committed by GitHub
parent 18e88d2fc8
commit f18adf7759
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 1 deletions

View File

@ -942,6 +942,7 @@ pub:
elem_type_pos token.Position // `Type` in []Type{} position elem_type_pos token.Position // `Type` in []Type{} position
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
ecmnts [][]Comment // optional iembed comments after each expr ecmnts [][]Comment // optional iembed comments after each expr
pre_cmnts []Comment
is_fixed bool is_fixed bool
has_val bool // fixed size literal `[expr, expr]!` has_val bool // fixed size literal `[expr, expr]!`
mod string mod string

View File

@ -1961,6 +1961,18 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
mut inc_indent := false mut inc_indent := false
mut last_line_nr := it.pos.line_nr // to have the same newlines between array elements mut last_line_nr := it.pos.line_nr // to have the same newlines between array elements
f.array_init_depth++ f.array_init_depth++
for i, c in it.pre_cmnts {
if c.pos.line_nr > last_line_nr {
f.writeln('')
} else if i > 0 {
f.write(' ')
}
f.comment(c, level: .indent, iembed: true)
last_line_nr = c.pos.last_line
}
if it.exprs.len == 0 && it.pre_cmnts.len > 0 && it.pre_cmnts[0].pos.line_nr != it.pos.line_nr {
f.writeln('')
}
for i, expr in it.exprs { for i, expr in it.exprs {
line_nr := expr.position().line_nr line_nr := expr.position().line_nr
if i == 0 { if i == 0 {

View File

@ -16,6 +16,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
mut elem_type_pos := first_pos mut elem_type_pos := first_pos
mut exprs := []ast.Expr{} mut exprs := []ast.Expr{}
mut ecmnts := [][]ast.Comment{} mut ecmnts := [][]ast.Comment{}
mut pre_cmnts := []ast.Comment{}
mut is_fixed := false mut is_fixed := false
mut has_val := false mut has_val := false
mut has_type := false mut has_type := false
@ -39,6 +40,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
last_pos = p.tok.position() last_pos = p.tok.position()
} else { } else {
// [1,2,3] or [const]byte // [1,2,3] or [const]byte
pre_cmnts = p.eat_comments({})
for i := 0; p.tok.kind !in [.rsbr, .eof]; i++ { for i := 0; p.tok.kind !in [.rsbr, .eof]; i++ {
exprs << p.expr(0) exprs << p.expr(0)
ecmnts << p.eat_comments({}) ecmnts << p.eat_comments({})
@ -141,6 +143,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
typ: array_type typ: array_type
exprs: exprs exprs: exprs
ecmnts: ecmnts ecmnts: ecmnts
pre_cmnts: pre_cmnts
pos: pos pos: pos
elem_type_pos: elem_type_pos elem_type_pos: elem_type_pos
has_len: has_len has_len: has_len