fmt: do not struggle with comments inside maps (#8897)
parent
5d653a37b6
commit
9dc770e29c
|
@ -979,9 +979,11 @@ pub mut:
|
||||||
|
|
||||||
pub struct MapInit {
|
pub struct MapInit {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
keys []Expr
|
keys []Expr
|
||||||
vals []Expr
|
vals []Expr
|
||||||
|
comments [][]Comment // comments after key-value pairs
|
||||||
|
pre_cmnts []Comment // comments before the first key-value pair
|
||||||
pub mut:
|
pub mut:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
key_type table.Type
|
key_type table.Type
|
||||||
|
|
|
@ -83,12 +83,13 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||||
pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
|
pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
|
||||||
mut prev_line := options.prev_line
|
mut prev_line := options.prev_line
|
||||||
for i, c in comments {
|
for i, c in comments {
|
||||||
|
if options.prev_line > -1 && ((c.pos.line_nr > prev_line && f.out.last_n(1) != '\n')
|
||||||
|
|| (c.pos.line_nr > prev_line + 1 && f.out.last_n(2) != '\n\n')) {
|
||||||
|
f.writeln('')
|
||||||
|
}
|
||||||
if !f.out.last_n(1)[0].is_space() {
|
if !f.out.last_n(1)[0].is_space() {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
}
|
}
|
||||||
if options.prev_line > -1 && c.pos.line_nr > prev_line + 1 {
|
|
||||||
f.writeln('')
|
|
||||||
}
|
|
||||||
f.comment(c, options)
|
f.comment(c, options)
|
||||||
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
|
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
|
|
@ -2068,6 +2068,7 @@ pub fn (mut f Fmt) map_init(it ast.MapInit) {
|
||||||
}
|
}
|
||||||
f.writeln('map{')
|
f.writeln('map{')
|
||||||
f.indent++
|
f.indent++
|
||||||
|
f.comments(it.pre_cmnts, {})
|
||||||
mut max_field_len := 0
|
mut max_field_len := 0
|
||||||
for key in it.keys {
|
for key in it.keys {
|
||||||
if key.str().len > max_field_len {
|
if key.str().len > max_field_len {
|
||||||
|
@ -2079,6 +2080,7 @@ pub fn (mut f Fmt) map_init(it ast.MapInit) {
|
||||||
f.write(': ')
|
f.write(': ')
|
||||||
f.write(strings.repeat(` `, max_field_len - key.str().len))
|
f.write(strings.repeat(` `, max_field_len - key.str().len))
|
||||||
f.expr(it.vals[i])
|
f.expr(it.vals[i])
|
||||||
|
f.comments(it.comments[i], prev_line: it.vals[i].position().last_line, has_nl: false)
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
f.indent--
|
f.indent--
|
||||||
|
|
|
@ -58,10 +58,29 @@ fn linebreaks_in_block_comments() {
|
||||||
*****/
|
*****/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map_comments() {
|
||||||
|
mymap := map{
|
||||||
|
// pre
|
||||||
|
`:`: 1
|
||||||
|
`!`: 2 // after
|
||||||
|
// and between
|
||||||
|
`%`: 3
|
||||||
|
// between
|
||||||
|
// between second
|
||||||
|
`$`: 4
|
||||||
|
`&`: 5
|
||||||
|
// post
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn ifs_comments_and_empty_lines() {
|
fn ifs_comments_and_empty_lines() {
|
||||||
if true {
|
if true {
|
||||||
}
|
}
|
||||||
// some comment after an if without else
|
// some comment direct after an if without else
|
||||||
|
if false {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
// some comment direct after an else
|
||||||
if false {
|
if false {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -160,7 +160,9 @@ fn (mut p Parser) map_init() ast.MapInit {
|
||||||
first_pos := p.prev_tok.position()
|
first_pos := p.prev_tok.position()
|
||||||
mut keys := []ast.Expr{}
|
mut keys := []ast.Expr{}
|
||||||
mut vals := []ast.Expr{}
|
mut vals := []ast.Expr{}
|
||||||
for p.tok.kind != .rcbr && p.tok.kind != .eof {
|
mut comments := [][]ast.Comment{}
|
||||||
|
pre_cmnts := p.eat_comments({})
|
||||||
|
for p.tok.kind !in [.rcbr, .eof] {
|
||||||
key := p.expr(0)
|
key := p.expr(0)
|
||||||
keys << key
|
keys << key
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
|
@ -169,10 +171,13 @@ fn (mut p Parser) map_init() ast.MapInit {
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
comments << p.eat_comments({})
|
||||||
}
|
}
|
||||||
return ast.MapInit{
|
return ast.MapInit{
|
||||||
keys: keys
|
keys: keys
|
||||||
vals: vals
|
vals: vals
|
||||||
pos: first_pos.extend_with_last_line(p.tok.position(), p.tok.line_nr)
|
pos: first_pos.extend_with_last_line(p.tok.position(), p.tok.line_nr)
|
||||||
|
comments: comments
|
||||||
|
pre_cmnts: pre_cmnts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue