Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: fix formating non-unsafe blocks with break line (fix #22900) #22903

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.pref.building_v && f.should_insert_newline_before_node(stmt, prev_stmt) {
spytheman marked this conversation as resolved.
Show resolved Hide resolved
f.out.writeln('')
}
f.stmt(stmt)
Expand Down
16 changes: 16 additions & 0 deletions vlib/v/fmt/tests/blocks_keep.vv
Original file line number Diff line number Diff line change
@@ -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')
}
}
Loading