@@ -293,6 +293,8 @@ pub fn (f Fmt) imp_stmt_str(imp ast.Import) string {
293
293
return '$imp.mod$imp_alias_suffix '
294
294
}
295
295
296
+ // === Node helpers ===//
297
+
296
298
fn (f Fmt) should_insert_newline_before_node (node ast.Node, prev_node ast.Node) bool {
297
299
// No need to insert a newline if there is already one
298
300
if f.out.last_n (2 ) == '\n\n ' {
@@ -345,6 +347,22 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
345
347
return true
346
348
}
347
349
350
+ pub fn (mut f Fmt) node_str (node ast.Node) string {
351
+ was_empty_line := f.empty_line
352
+ prev_line_len := f.line_len
353
+ pos := f.out.len
354
+ match node {
355
+ ast.Stmt { f.stmt (node) }
356
+ ast.Expr { f.expr (node) }
357
+ else { panic ('´f.node_str()´ is not implemented for ${node} .' ) }
358
+ }
359
+ str := f.out.after (pos).trim_space ()
360
+ f.out.go_back_to (pos)
361
+ f.empty_line = was_empty_line
362
+ f.line_len = prev_line_len
363
+ return str
364
+ }
365
+
348
366
// === General Stmt-related methods and helpers ===//
349
367
350
368
pub fn (mut f Fmt) stmts (stmts []ast.Stmt) {
@@ -456,18 +474,6 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
456
474
}
457
475
}
458
476
459
- pub fn (mut f Fmt) stmt_str (node ast.Stmt) string {
460
- was_empty_line := f.empty_line
461
- prev_line_len := f.line_len
462
- pos := f.out.len
463
- f.stmt (node)
464
- str := f.out.after (pos).trim_space ()
465
- f.out.go_back_to (pos)
466
- f.empty_line = was_empty_line
467
- f.line_len = prev_line_len
468
- return str
469
- }
470
-
471
477
// === General Expr-related methods and helpers ===//
472
478
473
479
pub fn (mut f Fmt) expr (node ast.Expr) {
@@ -622,20 +628,14 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
622
628
623
629
fn expr_is_single_line (expr ast.Expr) bool {
624
630
match expr {
631
+ ast.Comment, ast.IfExpr, ast.MapInit, ast.MatchExpr {
632
+ return false
633
+ }
625
634
ast.AnonFn {
626
635
if ! expr.decl.no_body {
627
636
return false
628
637
}
629
638
}
630
- ast.IfExpr {
631
- return false
632
- }
633
- ast.Comment {
634
- return false
635
- }
636
- ast.MatchExpr {
637
- return false
638
- }
639
639
ast.StructInit {
640
640
if ! expr.is_short && (expr.fields.len > 0 || expr.pre_comments.len > 0 ) {
641
641
return false
@@ -1399,15 +1399,32 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
1399
1399
penalty--
1400
1400
}
1401
1401
}
1402
- is_new_line := f.wrap_long_line (penalty, ! inc_indent)
1402
+ mut is_new_line := f.wrap_long_line (penalty, ! inc_indent)
1403
1403
if is_new_line && ! inc_indent {
1404
1404
f.indent++
1405
1405
inc_indent = true
1406
1406
}
1407
- if ! is_new_line && i > 0 {
1408
- f.write (' ' )
1407
+ single_line_expr := expr_is_single_line (expr)
1408
+ if single_line_expr {
1409
+ estr := f.node_str (expr)
1410
+ if ! is_new_line && ! f.buffering && f.line_len + estr.len > fmt.max_len.last () {
1411
+ f.writeln ('' )
1412
+ is_new_line = true
1413
+ if ! inc_indent {
1414
+ f.indent++
1415
+ inc_indent = true
1416
+ }
1417
+ }
1418
+ if ! is_new_line && i > 0 {
1419
+ f.write (' ' )
1420
+ }
1421
+ f.write (estr)
1422
+ } else {
1423
+ if ! is_new_line && i > 0 {
1424
+ f.write (' ' )
1425
+ }
1426
+ f.expr (expr)
1409
1427
}
1410
- f.expr (expr)
1411
1428
if i < node.ecmnts.len && node.ecmnts[i].len > 0 {
1412
1429
expr_pos := expr.position ()
1413
1430
for cmt in node.ecmnts[i] {
@@ -2089,7 +2106,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
2089
2106
} else if node.stmts.len == 1 && stmt_is_single_line (node.stmts[0 ]) {
2090
2107
// the control stmts (return/break/continue...) print a newline inside them,
2091
2108
// so, since this'll all be on one line, trim any possible whitespace
2092
- str := f.stmt_str (node.stmts[0 ]).trim_space ()
2109
+ str := f.node_str (node.stmts[0 ]).trim_space ()
2093
2110
single_line := ' or { $str }'
2094
2111
if single_line.len + f.line_len < = fmt.max_len.last () {
2095
2112
f.write (single_line)
0 commit comments