fmt: keep `arr << if x {} else {}` on a single line (#8289)

pull/8265/head
Lukas Neubert 2021-01-24 08:02:35 +01:00 committed by GitHub
parent 79b4b0e6c8
commit 522eac200f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 20 deletions

View File

@ -201,11 +201,7 @@ fn (mut vd VDoc) create_search_results(mod string, dn doc.DocNode, out Output) {
dn_description := trim_doc_node_description(comments)
vd.search_index << dn.name
vd.search_data << SearchResult{
prefix: if dn.parent_name != '' {
'$dn.kind ($dn.parent_name)'
} else {
'$dn.kind '
}
prefix: if dn.parent_name != '' { '$dn.kind ($dn.parent_name)' } else { '$dn.kind ' }
description: dn_description
badge: mod
link: vd.get_file_name(mod, out) + '#' + get_node_id(dn)

View File

@ -35,6 +35,7 @@ pub mut:
file ast.File
did_imports bool
is_assign bool
is_arr_push bool
auto_imports []string // automatically inserted imports that the user forgot to specify
import_pos int // position of the imports in the resulting string for later autoimports insertion
used_imports []string // to remove unused imports
@ -1430,6 +1431,9 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
if !f.buffering {
f.buffering = true
}
if node.op == .left_shift {
f.is_arr_push = true
}
infix_start := f.out.len
start_len := f.line_len
f.expr(node.left)
@ -1454,6 +1458,7 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
f.wrap_infix(infix_start, start_len)
}
}
f.is_arr_push = false
f.or_expr(node.or_block)
}
@ -1518,7 +1523,7 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int) {
pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
dollar := if it.is_comptime { '$' } else { '' }
mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0])
&& branch_is_single_line(it.branches[1])&& (it.is_expr || f.is_assign)
&& branch_is_single_line(it.branches[1])&& (it.is_expr || f.is_assign || f.is_arr_push)
f.single_line_if = single_line
if_start := f.line_len
for {

View File

@ -1,3 +0,0 @@
a, b := if true { 'a', 'b' } else { 'b', 'a' }
println(a)
println(b)

View File

@ -17,5 +17,4 @@ fn main() {
x: 5
}
}
_ := if false { Foo{} } else { Foo{5, 6} }
}

View File

@ -0,0 +1,4 @@
a, b := if true { 'a', 'b' } else { 'b', 'a' }
_ := if false { Foo{} } else { Foo{5, 6} }
arr := [0, 1]
arr << if true { 2 } else { 3 }

View File

@ -273,16 +273,8 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
typ: typ
default_expr: ast.ex2fe(default_expr)
has_default_expr: has_default_expr
is_pub: if is_embed {
true
} else {
is_field_pub
}
is_mut: if is_embed {
true
} else {
is_field_mut
}
is_pub: if is_embed { true } else { is_field_pub }
is_mut: if is_embed { true } else { is_field_mut }
is_global: is_field_global
attrs: p.attrs
}