mirror of
https://github.com/vlang/v.git
synced 2025-09-15 02:18:47 -04:00
ast, parser, fmt: fix formatting return with comments in parentheses (#19188)
This commit is contained in:
parent
c3be85e87d
commit
01cb30c023
@ -1380,7 +1380,8 @@ pub struct ParExpr {
|
|||||||
pub:
|
pub:
|
||||||
pos token.Pos
|
pos token.Pos
|
||||||
pub mut:
|
pub mut:
|
||||||
expr Expr
|
expr Expr
|
||||||
|
comments []Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
[minify]
|
[minify]
|
||||||
|
@ -1393,7 +1393,7 @@ pub fn (mut f Fmt) return_stmt(node ast.Return) {
|
|||||||
f.comments(pre_comments)
|
f.comments(pre_comments)
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
}
|
}
|
||||||
if expr is ast.ParExpr {
|
if expr is ast.ParExpr && expr.comments.len == 0 {
|
||||||
f.expr(expr.expr)
|
f.expr(expr.expr)
|
||||||
} else {
|
} else {
|
||||||
f.expr(expr)
|
f.expr(expr)
|
||||||
@ -2698,12 +2698,22 @@ pub fn (mut f Fmt) par_expr(node ast.ParExpr) {
|
|||||||
for mut expr is ast.ParExpr {
|
for mut expr is ast.ParExpr {
|
||||||
expr = expr.expr
|
expr = expr.expr
|
||||||
}
|
}
|
||||||
requires_paren := expr !is ast.Ident
|
requires_paren := expr !is ast.Ident || node.comments.len > 0
|
||||||
if requires_paren {
|
if requires_paren {
|
||||||
f.par_level++
|
f.par_level++
|
||||||
f.write('(')
|
f.write('(')
|
||||||
}
|
}
|
||||||
|
pre_comments := node.comments.filter(it.pos.pos < expr.pos().pos)
|
||||||
|
post_comments := node.comments[pre_comments.len..]
|
||||||
|
if pre_comments.len > 0 {
|
||||||
|
f.comments(pre_comments)
|
||||||
|
f.write(' ')
|
||||||
|
}
|
||||||
f.expr(expr)
|
f.expr(expr)
|
||||||
|
if post_comments.len > 0 {
|
||||||
|
f.comments(post_comments)
|
||||||
|
f.write(' ')
|
||||||
|
}
|
||||||
if requires_paren {
|
if requires_paren {
|
||||||
f.par_level--
|
f.par_level--
|
||||||
f.write(')')
|
f.write(')')
|
||||||
|
22
vlib/v/fmt/tests/return_with_comments_expected.vv
Normal file
22
vlib/v/fmt/tests/return_with_comments_expected.vv
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
fn foo1() int {
|
||||||
|
return (0 /* some comment */ )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo2() int {
|
||||||
|
return ( /* some comment */ 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo3() int {
|
||||||
|
return ( /* some comment1 */ 0 /* some comment2 */ )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo4() int {
|
||||||
|
return ( /* some comment1 */ /* some comment2 */ 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo5() int {
|
||||||
|
return ( /* some comment1 */ /* some comment2 */ 0 /* some comment3 */ /* some comment4 */ )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
22
vlib/v/fmt/tests/return_with_comments_input.vv
Normal file
22
vlib/v/fmt/tests/return_with_comments_input.vv
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
fn foo1() int {
|
||||||
|
return (0/* some comment */)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo2() int {
|
||||||
|
return (/* some comment */0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo3() int {
|
||||||
|
return (/* some comment1 */ 0/* some comment2 */)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo4() int {
|
||||||
|
return (/* some comment1 */ /* some comment2 */0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo5() int {
|
||||||
|
return (/* some comment1 */ /* some comment2 */0/* some comment3 */ /* some comment4 */)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
@ -147,11 +147,14 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
|
|||||||
.lpar {
|
.lpar {
|
||||||
mut pos := p.tok.pos()
|
mut pos := p.tok.pos()
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
|
mut comments := p.eat_comments()
|
||||||
node = p.expr(0)
|
node = p.expr(0)
|
||||||
|
comments << p.eat_comments()
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
node = ast.ParExpr{
|
node = ast.ParExpr{
|
||||||
expr: node
|
expr: node
|
||||||
pos: pos.extend(p.prev_tok.pos())
|
pos: pos.extend(p.prev_tok.pos())
|
||||||
|
comments: comments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_if {
|
.key_if {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user