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
branches []MatchBranch
pos token.Position
comments []Comment // comments before the first branch
pub mut:
is_expr bool // returns a value
return_type table.Type
@ -671,9 +672,8 @@ pub:
ecmnts [][]Comment // inline comments for each left side expr
stmts []Stmt // right side
pos token.Position
comments []Comment // comment above `xxx {`
is_else bool
post_comments []Comment
post_comments []Comment // comments below ´... }´
pub mut:
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
if node.op == .amp && !right_type.is_ptr() {
match node.right {
ast.IntegerLiteral { 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',
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.IntegerLiteral {
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', 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 {}
}
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.indent++
f.comments(it.comments, {})
mut single_line := true
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
break
}
@ -1797,10 +1799,6 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
}
}
for branch in it.branches {
for cmnt in branch.comments {
f.comment(cmnt, inline: true)
f.writeln('')
}
if !branch.is_else {
// normal branch
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() {
a := 20
_ := 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() {
a := 20
_ := match a {

View File

@ -1,5 +1,4 @@
fn main() {
// Nested match
fn nested_match() {
match 100 {
0...1 {
println('0 to 1')
@ -11,7 +10,9 @@ fn main() {
}
}
}
// StructInit
}
fn branches_are_struct_inits() {
match 'a' {
'b' { SpamStruct{} }
}
@ -25,11 +26,13 @@ fn main() {
match 'a' {
'b' {
SpamStruct{
// only a comment
// comment inside init
}
}
}
// CallExpr with or block
}
fn branches_are_call_exprs_with_or_blocks() {
match 'a' {
'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 {
p.check(.lcbr)
}
comments := p.eat_comments() // comments before the first branch
mut branches := []ast.MatchBranch{}
for p.tok.kind != .eof {
branch_first_pos := p.tok.position()
comments := p.eat_comments() // comments before {}
mut exprs := []ast.Expr{}
mut ecmnts := [][]ast.Comment{}
p.open_scope()
@ -235,18 +235,13 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
branch_scope := p.scope
p.close_scope()
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()
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{
exprs: exprs
ecmnts: ecmnts
stmts: stmts
pos: pos
comments: comments
is_else: is_else
post_comments: post_comments
scope: branch_scope
@ -274,6 +269,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
cond: cond
is_sum_type: is_sum_type
pos: pos
comments: comments
}
}