@@ -153,7 +153,7 @@ bool Formatter::shouldAddNewLineBetweenNodes(const Node& node, const std::size_t
153153 return false ;
154154
155155 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 ]);
157157
158158 const auto & child = list[at];
159159
@@ -321,16 +321,14 @@ std::string Formatter::formatFunction(const Node& node, const std::size_t indent
321321
322322std::string Formatter::formatVariable (const Node& node, const std::size_t indent)
323323{
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 ())]);
325325
326326 const Node body_node = node.constList ()[2 ];
327327 const std::string formatted_bind = format (node.constList ()[1 ], indent, false );
328328
329329 // 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) )
331331 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 ));
334332 return fmt::format (" ({} {}\n {})" , keyword, formatted_bind, format (body_node, indent + 1 , true ));
335333}
336334
@@ -484,12 +482,17 @@ std::string Formatter::formatDel(const Node& node, const std::size_t indent)
484482std::string Formatter::formatCall (const Node& node, const std::size_t indent)
485483{
486484 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 ;
491486 bool is_multiline = false ;
492487
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+
493496 std::vector<std::string> formatted_args;
494497 for (std::size_t i = 1 , end = node.constList ().size (); i < end; ++i)
495498 {
@@ -502,14 +505,24 @@ std::string Formatter::formatCall(const Node& node, const std::size_t indent)
502505 std::string result = is_list ? " [" : (" (" + format (node.constList ()[0 ], indent, false ));
503506 for (std::size_t i = 0 , end = formatted_args.size (); i < end; ++i)
504507 {
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)
507517 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;
510522 }
511523 if (!node.constList ().back ().commentAfter ().empty ())
512524 result += " \n " + prefix (indent);
525+
513526 result += is_list ? " ]" : " )" ;
514527 return result;
515528}
0 commit comments