From 522eac200fa516304ad1c549701a04d0b5568d39 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Sun, 24 Jan 2021 08:02:35 +0100 Subject: [PATCH] fmt: keep `arr << if x {} else {}` on a single line (#8289) --- cmd/tools/vdoc/html.v | 6 +----- vlib/v/fmt/fmt.v | 7 ++++++- vlib/v/fmt/tests/assign_keep.vv | 3 --- vlib/v/fmt/tests/if_keep.vv | 1 - vlib/v/fmt/tests/if_single_line_keep.vv | 4 ++++ vlib/v/parser/struct.v | 12 ++---------- 6 files changed, 13 insertions(+), 20 deletions(-) delete mode 100644 vlib/v/fmt/tests/assign_keep.vv create mode 100644 vlib/v/fmt/tests/if_single_line_keep.vv diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index 41f09f73b6..a885db973a 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -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) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index b722f88f7a..462ff342c3 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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 { diff --git a/vlib/v/fmt/tests/assign_keep.vv b/vlib/v/fmt/tests/assign_keep.vv deleted file mode 100644 index 230d3a8bd4..0000000000 --- a/vlib/v/fmt/tests/assign_keep.vv +++ /dev/null @@ -1,3 +0,0 @@ -a, b := if true { 'a', 'b' } else { 'b', 'a' } -println(a) -println(b) diff --git a/vlib/v/fmt/tests/if_keep.vv b/vlib/v/fmt/tests/if_keep.vv index 6522847e87..1aad57fe8b 100644 --- a/vlib/v/fmt/tests/if_keep.vv +++ b/vlib/v/fmt/tests/if_keep.vv @@ -17,5 +17,4 @@ fn main() { x: 5 } } - _ := if false { Foo{} } else { Foo{5, 6} } } diff --git a/vlib/v/fmt/tests/if_single_line_keep.vv b/vlib/v/fmt/tests/if_single_line_keep.vv new file mode 100644 index 0000000000..d9ba70a22e --- /dev/null +++ b/vlib/v/fmt/tests/if_single_line_keep.vv @@ -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 } diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 822187216f..5fe8ed0108 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -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 }