v.fmt: add an independent Comment.is_inline flag. Use it for fixing `[ .. //x ]` => `[ .. //x, ]`

pull/10506/head
Delyan Angelov 2021-06-18 15:47:26 +03:00
parent 2298063129
commit 9f6ddb4c21
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
7 changed files with 56 additions and 17 deletions

View File

@ -438,7 +438,7 @@ fn (t Tree) comment(node ast.Comment) &Node {
obj.add('ast_type', t.string_node('Comment')) obj.add('ast_type', t.string_node('Comment'))
obj.add('text', t.string_node(node.text)) obj.add('text', t.string_node(node.text))
obj.add('is_multi', t.bool_node(node.is_multi)) obj.add('is_multi', t.bool_node(node.is_multi))
obj.add('line_nr', t.number_node(node.line_nr)) obj.add('is_inline', t.bool_node(node.is_inline))
obj.add('pos', t.position(node.pos)) obj.add('pos', t.position(node.pos))
return obj return obj
} }

View File

@ -44,7 +44,6 @@ const (
] ]
vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [ vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [
'vlib/strconv/' /* prevent conflicts, till the new pure V string interpolation is merged */, 'vlib/strconv/' /* prevent conflicts, till the new pure V string interpolation is merged */,
'vlib/net/http/cookie_test.v' /* a very weird bug where `,` keeps on being added in comments */,
'vlib/term/ui/input.v' /* comment after a struct embed is removed */, 'vlib/term/ui/input.v' /* comment after a struct embed is removed */,
'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */, 'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */,
'vlib/readline/readline_test.v' /* vfmt eats `{ Readline }` from `import readline { Readline }` */, 'vlib/readline/readline_test.v' /* vfmt eats `{ Readline }` from `import readline { Readline }` */,

View File

@ -443,7 +443,7 @@ const (
}, },
] ]
} }
// TODO(bradfitz): users have reported seeing this in the,, // TODO(bradfitz): users have reported seeing this in the
// wild, but do browsers handle it? RFC 6265 just says "don't // wild, but do browsers handle it? RFC 6265 just says "don't
// do that" (section 3) and then never mentions header folding // do that" (section 3) and then never mentions header folding
// again. // again.

View File

@ -1382,10 +1382,10 @@ pub mut:
pub struct Comment { pub struct Comment {
pub: pub:
text string text string
is_multi bool is_multi bool // true only for /* comment */, that use many lines
line_nr int is_inline bool // true for all /* comment */ comments
pos token.Position pos token.Position
} }
pub struct ConcatExpr { pub struct ConcatExpr {

View File

@ -26,7 +26,7 @@ pub mut:
buffering bool // disables line wrapping for exprs that will be analyzed later buffering bool // disables line wrapping for exprs that will be analyzed later
par_level int // how many parentheses are put around the current expression par_level int // how many parentheses are put around the current expression
array_init_break []bool // line breaks after elements in hierarchy level of multi dimensional array array_init_break []bool // line breaks after elements in hierarchy level of multi dimensional array
array_init_depth int // current level of hierarchie in array init array_init_depth int // current level of hierarchy in array init
single_line_if bool single_line_if bool
cur_mod string cur_mod string
file ast.File file ast.File
@ -1500,12 +1500,21 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
} }
f.expr(expr) f.expr(expr)
} }
if i < node.ecmnts.len && node.ecmnts[i].len > 0 { mut last_comment_was_inline := false
mut has_comments := node.ecmnts[i].len > 0
if i < node.ecmnts.len && has_comments {
expr_pos := expr.position() expr_pos := expr.position()
for cmt in node.ecmnts[i] { for icmt, cmt in node.ecmnts[i] {
if !set_comma && cmt.pos.pos > expr_pos.pos + expr_pos.len + 2 { if !set_comma && cmt.pos.pos > expr_pos.pos + expr_pos.len + 2 {
f.write(',') if icmt > 0 {
set_comma = true if last_comment_was_inline {
f.write(',')
set_comma = true
}
} else {
f.write(',') // first comment needs a comma
set_comma = true
}
} }
if cmt.pos.line_nr > expr_pos.last_line { if cmt.pos.line_nr > expr_pos.last_line {
embed := i + 1 < node.exprs.len embed := i + 1 < node.exprs.len
@ -1516,16 +1525,21 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
f.write(' ') f.write(' ')
f.comment(cmt, iembed: true) f.comment(cmt, iembed: true)
} }
last_comment_was_inline = cmt.is_inline
} }
} }
mut put_comma := !set_comma
if has_comments && !last_comment_was_inline {
put_comma = false
}
if i == node.exprs.len - 1 { if i == node.exprs.len - 1 {
if is_new_line { if is_new_line {
if !set_comma { if put_comma {
f.write(',') f.write(',')
} }
f.writeln('') f.writeln('')
} }
} else if !set_comma { } else if put_comma {
f.write(',') f.write(',')
} }
last_line_nr = pos.last_line last_line_nr = pos.last_line

View File

@ -0,0 +1,23 @@
import net.http
const (
write_set_cookie_tests = [
ReadSetCookiesTestCase{
header: map{
'Set-Cookie': ['special-7=","']
}
cookies: [
&http.Cookie{
name: 'special-7'
value: ','
raw: 'special-8=","'
},
]
}
// TODO(bradfitz): users have reported seeing this in the
// wild, but do browsers handle it? RFC 6265 just says "don't
// do that" (section 3) and then never mentions header folding
// again.
// Header{"Set-Cookie": ["ASP.NET_SessionId=foo; path=/; HttpOnly, .ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]},
]
)

View File

@ -605,17 +605,20 @@ pub fn (mut p Parser) check_comment() ast.Comment {
pub fn (mut p Parser) comment() ast.Comment { pub fn (mut p Parser) comment() ast.Comment {
mut pos := p.tok.position() mut pos := p.tok.position()
text := p.tok.lit text := p.tok.lit
pos.last_line = pos.line_nr + text.count('\n') num_newlines := text.count('\n')
is_multi := num_newlines > 0
is_inline := text.len + 4 == p.tok.len // 4: `/` `*` `*` `/`
pos.last_line = pos.line_nr + num_newlines
p.next() p.next()
is_multi := text.contains('\n')
// Filter out false positive space indent vet errors inside comments // Filter out false positive space indent vet errors inside comments
if p.vet_errors.len > 0 && is_multi { if p.vet_errors.len > 0 && is_multi {
p.vet_errors = p.vet_errors.filter(it.typ != .space_indent p.vet_errors = p.vet_errors.filter(it.typ != .space_indent
|| it.pos.line_nr - 1 > pos.last_line || it.pos.line_nr - 1 <= pos.line_nr) || it.pos.line_nr - 1 > pos.last_line || it.pos.line_nr - 1 <= pos.line_nr)
} }
return ast.Comment{ return ast.Comment{
is_multi: is_multi
text: text text: text
is_multi: is_multi
is_inline: is_inline
pos: pos pos: pos
} }
} }