@@ -153,7 +153,7 @@ bool Formatter::shouldAddNewLineBetweenNodes(const Node& node, const std::size_t
153
153
return false ;
154
154
155
155
const auto & list = node.constList ();
156
- std::size_t previous_line = lineOfLastNodeIn (list[at - 1 ]);
156
+ const std::size_t previous_line = lineOfLastNodeIn (list[at - 1 ]);
157
157
158
158
const auto & child = list[at];
159
159
@@ -321,16 +321,14 @@ std::string Formatter::formatFunction(const Node& node, const std::size_t indent
321
321
322
322
std::string Formatter::formatVariable (const Node& node, const std::size_t indent)
323
323
{
324
- std::string keyword = std::string (keywords[static_cast <std::size_t >(node.constList ()[0 ].keyword ())]);
324
+ const auto keyword = std::string (keywords[static_cast <std::size_t >(node.constList ()[0 ].keyword ())]);
325
325
326
326
const Node body_node = node.constList ()[2 ];
327
327
const std::string formatted_bind = format (node.constList ()[1 ], indent, false );
328
328
329
329
// we don't want to add another indentation level here, because it would result in a (let a (fun ()\n{indent+=4}...))
330
- if (isFuncDef (body_node))
330
+ if (isFuncDef (body_node) || ! shouldSplitOnNewline (body_node) )
331
331
return fmt::format (" ({} {} {})" , keyword, formatted_bind, format (body_node, indent, false ));
332
- if (!shouldSplitOnNewline (body_node))
333
- return fmt::format (" ({} {} {})" , keyword, formatted_bind, format (body_node, indent + 1 , false ));
334
332
return fmt::format (" ({} {}\n {})" , keyword, formatted_bind, format (body_node, indent + 1 , true ));
335
333
}
336
334
@@ -484,12 +482,17 @@ std::string Formatter::formatDel(const Node& node, const std::size_t indent)
484
482
std::string Formatter::formatCall (const Node& node, const std::size_t indent)
485
483
{
486
484
bool is_list = false ;
487
- if (!node.constList ().empty () && node.constList ().front ().nodeType () == NodeType::Symbol &&
488
- node.constList ().front ().string () == " list" )
489
- is_list = true ;
490
-
485
+ bool is_dict = false ;
491
486
bool is_multiline = false ;
492
487
488
+ if (!node.constList ().empty () && node.constList ().front ().nodeType () == NodeType::Symbol)
489
+ {
490
+ if (node.constList ().front ().string () == " list" )
491
+ is_list = true ;
492
+ else if (node.constList ().front ().string () == " dict" )
493
+ is_dict = true ;
494
+ }
495
+
493
496
std::vector<std::string> formatted_args;
494
497
for (std::size_t i = 1 , end = node.constList ().size (); i < end; ++i)
495
498
{
@@ -502,14 +505,24 @@ std::string Formatter::formatCall(const Node& node, const std::size_t indent)
502
505
std::string result = is_list ? " [" : (" (" + format (node.constList ()[0 ], indent, false ));
503
506
for (std::size_t i = 0 , end = formatted_args.size (); i < end; ++i)
504
507
{
505
- const std::string formatted_node = formatted_args[i];
506
- if (is_multiline)
508
+ const std::string& formatted_node = formatted_args[i];
509
+ if (is_dict)
510
+ {
511
+ if (i % 2 == 0 && formatted_args.size () > 2 ) // one pair per line if we have at least 2 key-value pairs
512
+ result += " \n " + format (node.constList ()[i + 1 ], indent + 1 , true );
513
+ else
514
+ result += " " + formatted_node;
515
+ }
516
+ else if (is_multiline)
507
517
result += " \n " + format (node.constList ()[i + 1 ], indent + 1 , true );
508
- else
509
- result += (is_list && i == 0 ? " " : " " ) + formatted_node;
518
+ else if (is_list && i == 0 )
519
+ result += formatted_node;
520
+ else // put all arguments on the same line
521
+ result += " " + formatted_node;
510
522
}
511
523
if (!node.constList ().back ().commentAfter ().empty ())
512
524
result += " \n " + prefix (indent);
525
+
513
526
result += is_list ? " ]" : " )" ;
514
527
return result;
515
528
}
0 commit comments