From 7a4b3abd07fcb20ee5845e79e2ea42bf2ce4ab7f Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 19 Nov 2024 03:57:36 +0800 Subject: [PATCH] fmt: fix formating non-unsafe blocks with break line (fix #22900) (#22903) --- vlib/v/fmt/fmt.v | 13 ++++++++++--- vlib/v/fmt/tests/blocks_keep.vv | 16 ++++++++++++++++ vlib/v/gen/golang/golang.v | 6 +++--- vlib/v/tests/structs/struct_local_test.v | 2 -- vlib/x/json2/decoder2/decode_test.v | 1 - 5 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 vlib/v/fmt/tests/blocks_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 1250c106d..db403e300 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -443,6 +443,13 @@ fn (f &Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node) ast.Import { return false } + ast.Block { + if node is ast.Block && !node.is_unsafe && node.pos.line_nr - prev_line_nr > 0 { + return true + } else { + return false + } + } ast.ConstDecl { if node !is ast.ConstDecl && !(node is ast.ExprStmt && node.expr is ast.Comment) { return true @@ -489,10 +496,10 @@ pub fn (mut f Fmt) node_str(node ast.Node) string { //=== General Stmt-related methods and helpers ===// pub fn (mut f Fmt) stmts(stmts []ast.Stmt) { - mut prev_stmt := if stmts.len > 0 { stmts[0] } else { ast.empty_stmt } + mut prev_stmt := ast.empty_stmt f.indent++ - for stmt in stmts { - if !f.pref.building_v && f.should_insert_newline_before_node(stmt, prev_stmt) { + for i, stmt in stmts { + if i > 0 && f.should_insert_newline_before_node(stmt, prev_stmt) { f.out.writeln('') } f.stmt(stmt) diff --git a/vlib/v/fmt/tests/blocks_keep.vv b/vlib/v/fmt/tests/blocks_keep.vv new file mode 100644 index 000000000..b53ad8df7 --- /dev/null +++ b/vlib/v/fmt/tests/blocks_keep.vv @@ -0,0 +1,16 @@ +fn main() { + { + println('(1/3) My first small scope') + // + } + + { + println('(2/3) My second small scope') + // + } + // A line below prevents the line removal + + { + println('(3/3) My Third small scope') + } +} diff --git a/vlib/v/gen/golang/golang.v b/vlib/v/gen/golang/golang.v index ed30117cd..8076a12bf 100644 --- a/vlib/v/gen/golang/golang.v +++ b/vlib/v/gen/golang/golang.v @@ -387,10 +387,10 @@ pub fn (mut f Gen) node_str(node ast.Node) string { //=== General Stmt-related methods and helpers ===// pub fn (mut f Gen) stmts(stmts []ast.Stmt) { - mut prev_stmt := if stmts.len > 0 { stmts[0] } else { ast.empty_stmt } + mut prev_stmt := ast.empty_stmt f.indent++ - for stmt in stmts { - if !f.pref.building_v && f.should_insert_newline_before_node(stmt, prev_stmt) { + for i, stmt in stmts { + if i > 0 && f.should_insert_newline_before_node(stmt, prev_stmt) { f.out.writeln('') } f.stmt(stmt) diff --git a/vlib/v/tests/structs/struct_local_test.v b/vlib/v/tests/structs/struct_local_test.v index 562a62c39..361ca0526 100644 --- a/vlib/v/tests/structs/struct_local_test.v +++ b/vlib/v/tests/structs/struct_local_test.v @@ -1,5 +1,4 @@ fn test_main() { - struct Foobar { foo int } @@ -10,7 +9,6 @@ fn test_main() { } fn x() { - struct Foobar { baz string } diff --git a/vlib/x/json2/decoder2/decode_test.v b/vlib/x/json2/decoder2/decode_test.v index 85fd000e6..d7012b2ca 100644 --- a/vlib/x/json2/decoder2/decode_test.v +++ b/vlib/x/json2/decoder2/decode_test.v @@ -181,7 +181,6 @@ fn test_check_json_format() { } fn test_get_value_kind() { - struct Object_ { byte_ u8 value_kind ValueKind -- 2.39.5