fmt: allow for comments in call args and if expressions branches (#5871)
parent
fb76e02c59
commit
8653605b0a
|
@ -288,11 +288,12 @@ pub mut:
|
||||||
|
|
||||||
pub struct CallArg {
|
pub struct CallArg {
|
||||||
pub:
|
pub:
|
||||||
is_mut bool
|
is_mut bool
|
||||||
share table.ShareType
|
share table.ShareType
|
||||||
expr Expr
|
expr Expr
|
||||||
|
comments []Comment
|
||||||
pub mut:
|
pub mut:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Return {
|
pub struct Return {
|
||||||
|
@ -445,14 +446,15 @@ pub mut:
|
||||||
|
|
||||||
pub struct IfExpr {
|
pub struct IfExpr {
|
||||||
pub:
|
pub:
|
||||||
tok_kind token.Kind
|
tok_kind token.Kind
|
||||||
left Expr // `a` in `a := if ...`
|
left Expr // `a` in `a := if ...`
|
||||||
pos token.Position
|
pos token.Position
|
||||||
|
post_comments []Comment
|
||||||
pub mut:
|
pub mut:
|
||||||
branches []IfBranch // includes all `else if` branches
|
branches []IfBranch // includes all `else if` branches
|
||||||
is_expr bool
|
is_expr bool
|
||||||
typ table.Type
|
typ table.Type
|
||||||
has_else bool
|
has_else bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct IfBranch {
|
pub struct IfBranch {
|
||||||
|
|
|
@ -253,9 +253,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||||
}
|
}
|
||||||
match node {
|
match node {
|
||||||
ast.AssignStmt {
|
ast.AssignStmt {
|
||||||
f.comments(node.comments, {
|
f.comments(node.comments, {})
|
||||||
inline: false
|
|
||||||
})
|
|
||||||
for i, left in node.left {
|
for i, left in node.left {
|
||||||
if left is ast.Ident {
|
if left is ast.Ident {
|
||||||
var_info := left.var_info()
|
var_info := left.var_info()
|
||||||
|
@ -334,6 +332,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||||
name := it.name.after('.')
|
name := it.name.after('.')
|
||||||
f.writeln('enum $name {')
|
f.writeln('enum $name {')
|
||||||
f.comments(it.comments, {
|
f.comments(it.comments, {
|
||||||
|
inline: true
|
||||||
level: .indent
|
level: .indent
|
||||||
})
|
})
|
||||||
for field in it.fields {
|
for field in it.fields {
|
||||||
|
@ -343,6 +342,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||||
f.expr(field.expr)
|
f.expr(field.expr)
|
||||||
}
|
}
|
||||||
f.comments(field.comments, {
|
f.comments(field.comments, {
|
||||||
|
inline: true
|
||||||
has_nl: false
|
has_nl: false
|
||||||
level: .indent
|
level: .indent
|
||||||
})
|
})
|
||||||
|
@ -351,9 +351,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||||
f.writeln('}\n')
|
f.writeln('}\n')
|
||||||
}
|
}
|
||||||
ast.ExprStmt {
|
ast.ExprStmt {
|
||||||
f.comments(it.comments, {
|
f.comments(it.comments, {})
|
||||||
inline: false
|
|
||||||
})
|
|
||||||
f.expr(it.expr)
|
f.expr(it.expr)
|
||||||
if !f.single_line_if {
|
if !f.single_line_if {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
@ -444,9 +442,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||||
f.mod(it)
|
f.mod(it)
|
||||||
}
|
}
|
||||||
ast.Return {
|
ast.Return {
|
||||||
f.comments(it.comments, {
|
f.comments(it.comments, {})
|
||||||
inline: false
|
|
||||||
})
|
|
||||||
f.write('return')
|
f.write('return')
|
||||||
if it.exprs.len > 1 {
|
if it.exprs.len > 1 {
|
||||||
// multiple returns
|
// multiple returns
|
||||||
|
@ -631,7 +627,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
for j < comments.len && comments[j].pos.pos < field.pos.pos {
|
for j < comments.len && comments[j].pos.pos < field.pos.pos {
|
||||||
f.indent++
|
f.indent++
|
||||||
f.empty_line = true
|
f.empty_line = true
|
||||||
f.comment(comments[j], {})
|
f.comment(comments[j], {
|
||||||
|
inline: true
|
||||||
|
})
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
f.indent--
|
f.indent--
|
||||||
j++
|
j++
|
||||||
|
@ -665,7 +663,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
for comment in node.end_comments {
|
for comment in node.end_comments {
|
||||||
f.indent++
|
f.indent++
|
||||||
f.empty_line = true
|
f.empty_line = true
|
||||||
f.comment(comment, {})
|
f.comment(comment, {
|
||||||
|
inline: true
|
||||||
|
})
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
f.indent--
|
f.indent--
|
||||||
}
|
}
|
||||||
|
@ -781,7 +781,9 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
|
||||||
f.write('`$node.val`')
|
f.write('`$node.val`')
|
||||||
}
|
}
|
||||||
ast.Comment {
|
ast.Comment {
|
||||||
f.comment(node, {})
|
f.comment(node, {
|
||||||
|
inline: true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
ast.ComptimeCall {
|
ast.ComptimeCall {
|
||||||
if node.is_vweb {
|
if node.is_vweb {
|
||||||
|
@ -1104,7 +1106,7 @@ enum CommentsLevel {
|
||||||
|
|
||||||
struct CommentsOptions {
|
struct CommentsOptions {
|
||||||
has_nl bool = true
|
has_nl bool = true
|
||||||
inline bool = true
|
inline bool
|
||||||
level CommentsLevel = .keep
|
level CommentsLevel = .keep
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1280,19 +1282,29 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
|
||||||
(it.is_expr || f.is_assign)
|
(it.is_expr || f.is_assign)
|
||||||
f.single_line_if = single_line
|
f.single_line_if = single_line
|
||||||
for i, branch in it.branches {
|
for i, branch in it.branches {
|
||||||
if branch.comments.len > 0 {
|
|
||||||
f.comments(branch.comments, {})
|
|
||||||
}
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
|
f.comments(branch.comments, {})
|
||||||
f.write('if ')
|
f.write('if ')
|
||||||
f.expr(branch.cond)
|
f.expr(branch.cond)
|
||||||
f.write(' {')
|
f.write(' {')
|
||||||
} else if i < it.branches.len - 1 || !it.has_else {
|
} else if i < it.branches.len - 1 || !it.has_else {
|
||||||
f.write('} else if ')
|
if branch.comments.len > 0 {
|
||||||
|
f.writeln('}')
|
||||||
|
f.comments(branch.comments, {})
|
||||||
|
} else {
|
||||||
|
f.write('} ')
|
||||||
|
}
|
||||||
|
f.write('else if ')
|
||||||
f.expr(branch.cond)
|
f.expr(branch.cond)
|
||||||
f.write(' {')
|
f.write(' {')
|
||||||
} else if i == it.branches.len - 1 && it.has_else {
|
} else if i == it.branches.len - 1 && it.has_else {
|
||||||
f.write('} else {')
|
if branch.comments.len > 0 {
|
||||||
|
f.writeln('}')
|
||||||
|
f.comments(branch.comments, {})
|
||||||
|
} else {
|
||||||
|
f.write('} ')
|
||||||
|
}
|
||||||
|
f.write('else {')
|
||||||
}
|
}
|
||||||
if single_line {
|
if single_line {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
|
@ -1306,6 +1318,12 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
|
||||||
}
|
}
|
||||||
f.write('}')
|
f.write('}')
|
||||||
f.single_line_if = false
|
f.single_line_if = false
|
||||||
|
if it.post_comments.len > 0 {
|
||||||
|
f.writeln('')
|
||||||
|
f.comments(it.post_comments, {
|
||||||
|
has_nl: false
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
|
pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
|
||||||
|
@ -1318,6 +1336,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
for arg in node.args {
|
||||||
|
f.comments(arg.comments, {})
|
||||||
|
}
|
||||||
if node.is_method {
|
if node.is_method {
|
||||||
/*
|
/*
|
||||||
// x.foo!() experiment
|
// x.foo!() experiment
|
||||||
|
@ -1425,7 +1446,9 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
|
||||||
}
|
}
|
||||||
for branch in it.branches {
|
for branch in it.branches {
|
||||||
if branch.comment.text != '' {
|
if branch.comment.text != '' {
|
||||||
f.comment(branch.comment, {})
|
f.comment(branch.comment, {
|
||||||
|
inline: true
|
||||||
|
})
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
if !branch.is_else {
|
if !branch.is_else {
|
||||||
|
@ -1457,7 +1480,9 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if branch.post_comments.len > 0 {
|
if branch.post_comments.len > 0 {
|
||||||
f.comments(branch.post_comments, {})
|
f.comments(branch.post_comments, {
|
||||||
|
inline: true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.indent--
|
f.indent--
|
||||||
|
@ -1691,7 +1716,9 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
|
||||||
comments := field.comments
|
comments := field.comments
|
||||||
mut j := 0
|
mut j := 0
|
||||||
for j < comments.len && comments[j].pos.pos < field.pos.pos {
|
for j < comments.len && comments[j].pos.pos < field.pos.pos {
|
||||||
f.comment(comments[j], {})
|
f.comment(comments[j], {
|
||||||
|
inline: true
|
||||||
|
})
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,4 +16,22 @@ fn main() {
|
||||||
// just to make it worse
|
// just to make it worse
|
||||||
b, c := a, 2
|
b, c := a, 2
|
||||||
d := c // and an extra one
|
d := c // and an extra one
|
||||||
|
// before arg comment
|
||||||
|
// after arg comment
|
||||||
|
println('this is a test')
|
||||||
|
// before if expr
|
||||||
|
// after if expr
|
||||||
|
if true {
|
||||||
|
println('if')
|
||||||
|
}
|
||||||
|
// before else if
|
||||||
|
// between else if
|
||||||
|
else if false {
|
||||||
|
println('else if')
|
||||||
|
}
|
||||||
|
// before else
|
||||||
|
// after else
|
||||||
|
else {
|
||||||
|
println('else')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,4 +10,16 @@ fn main() {
|
||||||
a := /* this is a comment */ 1
|
a := /* this is a comment */ 1
|
||||||
b, c := /* and another comment */ a, /* just to make it worse */ 2
|
b, c := /* and another comment */ a, /* just to make it worse */ 2
|
||||||
d := c // and an extra one
|
d := c // and an extra one
|
||||||
|
println(/* before arg comment */ 'this is a test' /* after arg comment */)
|
||||||
|
if /* before if expr */ true /* after if expr */ {
|
||||||
|
println('if')
|
||||||
|
}
|
||||||
|
// before else if
|
||||||
|
else /* between else if */ if false {
|
||||||
|
println('else if')
|
||||||
|
}
|
||||||
|
// before else
|
||||||
|
else /* after else */ {
|
||||||
|
println('else')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
if true {
|
if true {
|
||||||
}
|
}
|
||||||
|
// some comment after an if without else
|
||||||
n := sizeof(User)
|
n := sizeof(User)
|
||||||
// else
|
// else
|
||||||
// else {
|
// else {
|
||||||
|
|
|
@ -88,7 +88,7 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp
|
||||||
fn_name = registered.name
|
fn_name = registered.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node := ast.CallExpr{
|
return ast.CallExpr{
|
||||||
name: fn_name
|
name: fn_name
|
||||||
args: args
|
args: args
|
||||||
mod: fn_mod
|
mod: fn_mod
|
||||||
|
@ -101,7 +101,6 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp
|
||||||
}
|
}
|
||||||
generic_type: generic_type
|
generic_type: generic_type
|
||||||
}
|
}
|
||||||
return node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut p Parser) call_args() []ast.CallArg {
|
pub fn (mut p Parser) call_args() []ast.CallArg {
|
||||||
|
@ -113,11 +112,14 @@ pub fn (mut p Parser) call_args() []ast.CallArg {
|
||||||
if is_mut {
|
if is_mut {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
mut comments := p.eat_comments()
|
||||||
e := p.expr(0)
|
e := p.expr(0)
|
||||||
|
comments << p.eat_comments()
|
||||||
args << ast.CallArg{
|
args << ast.CallArg{
|
||||||
is_mut: is_mut
|
is_mut: is_mut
|
||||||
share: table.sharetype_from_flags(is_shared, is_atomic)
|
share: table.sharetype_from_flags(is_shared, is_atomic)
|
||||||
expr: e
|
expr: e
|
||||||
|
comments: comments
|
||||||
}
|
}
|
||||||
if p.tok.kind != .rpar {
|
if p.tok.kind != .rpar {
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
|
|
|
@ -24,8 +24,9 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
if p.tok.kind == .key_if {
|
if p.tok.kind == .key_if {
|
||||||
p.next()
|
p.next()
|
||||||
} else {
|
} else {
|
||||||
comments = p.eat_comments()
|
comments << p.eat_comments()
|
||||||
p.check(.key_else)
|
p.check(.key_else)
|
||||||
|
comments << p.eat_comments()
|
||||||
if p.tok.kind == .key_if {
|
if p.tok.kind == .key_if {
|
||||||
p.next()
|
p.next()
|
||||||
} else {
|
} else {
|
||||||
|
@ -65,13 +66,16 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
}
|
}
|
||||||
mut cond := ast.Expr{}
|
mut cond := ast.Expr{}
|
||||||
mut is_guard := false
|
mut is_guard := false
|
||||||
|
comments << p.eat_comments()
|
||||||
// `if x := opt() {`
|
// `if x := opt() {`
|
||||||
if p.peek_tok.kind == .decl_assign {
|
if p.peek_tok.kind == .decl_assign {
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
is_guard = true
|
is_guard = true
|
||||||
var_pos := p.tok.position()
|
var_pos := p.tok.position()
|
||||||
var_name := p.check_name()
|
var_name := p.check_name()
|
||||||
|
comments << p.eat_comments()
|
||||||
p.check(.decl_assign)
|
p.check(.decl_assign)
|
||||||
|
comments << p.eat_comments()
|
||||||
expr := p.expr(0)
|
expr := p.expr(0)
|
||||||
p.scope.register(var_name, ast.Var{
|
p.scope.register(var_name, ast.Var{
|
||||||
name: var_name
|
name: var_name
|
||||||
|
@ -87,6 +91,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
prev_guard = false
|
prev_guard = false
|
||||||
cond = p.expr(0)
|
cond = p.expr(0)
|
||||||
}
|
}
|
||||||
|
comments << p.eat_comments()
|
||||||
mut left_as_name := ''
|
mut left_as_name := ''
|
||||||
if cond is ast.InfixExpr as infix {
|
if cond is ast.InfixExpr as infix {
|
||||||
// if sum is T
|
// if sum is T
|
||||||
|
@ -117,13 +122,14 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
comments: comments
|
comments: comments
|
||||||
left_as_name: left_as_name
|
left_as_name: left_as_name
|
||||||
}
|
}
|
||||||
comments = []
|
comments = p.eat_comments()
|
||||||
if p.tok.kind != .key_else {
|
if p.tok.kind != .key_else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ast.IfExpr{
|
return ast.IfExpr{
|
||||||
branches: branches
|
branches: branches
|
||||||
|
post_comments: comments
|
||||||
pos: pos
|
pos: pos
|
||||||
has_else: has_else
|
has_else: has_else
|
||||||
}
|
}
|
||||||
|
@ -210,7 +216,8 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
|
||||||
expr := p.expr(0)
|
expr := p.expr(0)
|
||||||
p.inside_match_case = false
|
p.inside_match_case = false
|
||||||
if p.tok.kind == .dotdot {
|
if p.tok.kind == .dotdot {
|
||||||
p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)', p.tok.position())
|
p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)',
|
||||||
|
p.tok.position())
|
||||||
} else if p.tok.kind == .ellipsis {
|
} else if p.tok.kind == .ellipsis {
|
||||||
p.next()
|
p.next()
|
||||||
expr2 := p.expr(0)
|
expr2 := p.expr(0)
|
||||||
|
|
|
@ -626,10 +626,6 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
||||||
expr := p.expr(0)
|
expr := p.expr(0)
|
||||||
// mut call_expr := &ast.CallExpr(0) // TODO
|
// mut call_expr := &ast.CallExpr(0) // TODO
|
||||||
// { call_expr = it }
|
// { call_expr = it }
|
||||||
match expr {
|
|
||||||
ast.CallExpr {}
|
|
||||||
else {}
|
|
||||||
}
|
|
||||||
return ast.GoStmt{
|
return ast.GoStmt{
|
||||||
call_expr: expr
|
call_expr: expr
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue