fmt: keep user choice for newlines in match branches (#8418)

pull/8441/head
Lukas Neubert 2021-01-30 11:57:57 +01:00 committed by GitHub
parent 77b3d40f46
commit 2774db077d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 48 deletions

View File

@ -657,6 +657,7 @@ pub:
cond Expr cond Expr
branches []MatchBranch branches []MatchBranch
pos token.Position pos token.Position
comments []Comment // comments before the first branch
pub mut: pub mut:
is_expr bool // returns a value is_expr bool // returns a value
return_type table.Type return_type table.Type
@ -671,9 +672,8 @@ pub:
ecmnts [][]Comment // inline comments for each left side expr ecmnts [][]Comment // inline comments for each left side expr
stmts []Stmt // right side stmts []Stmt // right side
pos token.Position pos token.Position
comments []Comment // comment above `xxx {`
is_else bool is_else bool
post_comments []Comment post_comments []Comment // comments below ´... }´
pub mut: pub mut:
scope &Scope scope &Scope
} }

View File

@ -4830,13 +4830,21 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) table.Type {
// TODO: testing ref/deref strategy // TODO: testing ref/deref strategy
if node.op == .amp && !right_type.is_ptr() { if node.op == .amp && !right_type.is_ptr() {
match node.right { match node.right {
ast.IntegerLiteral { c.error('cannot take the address of an int literal', ast.IntegerLiteral {
node.pos) } c.error('cannot take the address of an int literal', node.pos)
ast.BoolLiteral { c.error('cannot take the address of a bool literal', node.pos) } }
ast.StringLiteral, ast.StringInterLiteral { c.error('cannot take the address of a string literal', ast.BoolLiteral {
node.pos) } c.error('cannot take the address of a bool literal', node.pos)
ast.FloatLiteral { c.error('cannot take the address of a float literal', node.pos) } }
ast.CharLiteral { c.error('cannot take the address of a char literal', node.pos) } ast.StringLiteral, ast.StringInterLiteral {
c.error('cannot take the address of a string literal', node.pos)
}
ast.FloatLiteral {
c.error('cannot take the address of a float literal', node.pos)
}
ast.CharLiteral {
c.error('cannot take the address of a char literal', node.pos)
}
else {} else {}
} }
if mut node.right is ast.IndexExpr { if mut node.right is ast.IndexExpr {

View File

@ -1782,9 +1782,11 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
} }
f.writeln(' {') f.writeln(' {')
f.indent++ f.indent++
f.comments(it.comments, {})
mut single_line := true mut single_line := true
for branch in it.branches { for branch in it.branches {
if branch.stmts.len > 1 { if branch.stmts.len > 1 || branch.pos.line_nr < branch.pos.last_line {
println(branch)
single_line = false single_line = false
break break
} }
@ -1797,10 +1799,6 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
} }
} }
for branch in it.branches { for branch in it.branches {
for cmnt in branch.comments {
f.comment(cmnt, inline: true)
f.writeln('')
}
if !branch.is_else { if !branch.is_else {
// normal branch // normal branch
f.is_mbranch_expr = true f.is_mbranch_expr = true

View File

@ -1,12 +1,3 @@
fn match_expr() {
a := 10
match a {
10 { println('10') }
20 { println('20') }
else {}
}
}
fn match_expr_assignment() { fn match_expr_assignment() {
a := 20 a := 20
_ := match a { _ := match a {

View File

@ -1,16 +1,3 @@
fn match_expr() {
a := 10
match a {
10 {
println('10')
}
20 {
println('20')
}
else {}
}
}
fn match_expr_assignment() { fn match_expr_assignment() {
a := 20 a := 20
_ := match a { _ := match a {

View File

@ -1,5 +1,4 @@
fn main() { fn nested_match() {
// Nested match
match 100 { match 100 {
0...1 { 0...1 {
println('0 to 1') println('0 to 1')
@ -11,7 +10,9 @@ fn main() {
} }
} }
} }
// StructInit }
fn branches_are_struct_inits() {
match 'a' { match 'a' {
'b' { SpamStruct{} } 'b' { SpamStruct{} }
} }
@ -25,11 +26,13 @@ fn main() {
match 'a' { match 'a' {
'b' { 'b' {
SpamStruct{ SpamStruct{
// only a comment // comment inside init
} }
} }
} }
// CallExpr with or block }
fn branches_are_call_exprs_with_or_blocks() {
match 'a' { match 'a' {
'b' { foo() or { panic(err) } } 'b' { foo() or { panic(err) } }
} }
@ -50,3 +53,24 @@ fn main() {
} }
} }
} }
fn keep_branch_linebreaks() {
a := 10
match a {
// first comment
10 {
println('10')
}
20 {
println('20')
}
else {}
}
match a {
// first comment
10 { println('10') }
// post_comment of the first branch
20 { println('20') }
else {}
}
}

View File

@ -166,10 +166,10 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
if !no_lcbr { if !no_lcbr {
p.check(.lcbr) p.check(.lcbr)
} }
comments := p.eat_comments() // comments before the first branch
mut branches := []ast.MatchBranch{} mut branches := []ast.MatchBranch{}
for p.tok.kind != .eof { for p.tok.kind != .eof {
branch_first_pos := p.tok.position() branch_first_pos := p.tok.position()
comments := p.eat_comments() // comments before {}
mut exprs := []ast.Expr{} mut exprs := []ast.Expr{}
mut ecmnts := [][]ast.Comment{} mut ecmnts := [][]ast.Comment{}
p.open_scope() p.open_scope()
@ -235,18 +235,13 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
branch_scope := p.scope branch_scope := p.scope
p.close_scope() p.close_scope()
p.inside_match_body = false p.inside_match_body = false
mut pos := branch_first_pos.extend(branch_last_pos) pos := branch_first_pos.extend_with_last_line(branch_last_pos, p.prev_tok.line_nr)
post_comments := p.eat_comments() post_comments := p.eat_comments()
pos.update_last_line(p.prev_tok.line_nr)
if post_comments.len > 0 {
pos.last_line = post_comments.last().pos.last_line
}
branches << ast.MatchBranch{ branches << ast.MatchBranch{
exprs: exprs exprs: exprs
ecmnts: ecmnts ecmnts: ecmnts
stmts: stmts stmts: stmts
pos: pos pos: pos
comments: comments
is_else: is_else is_else: is_else
post_comments: post_comments post_comments: post_comments
scope: branch_scope scope: branch_scope
@ -274,6 +269,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
cond: cond cond: cond
is_sum_type: is_sum_type is_sum_type: is_sum_type
pos: pos pos: pos
comments: comments
} }
} }