Skip to content

Commit 862c502

Browse files
committed
feat(formatter): ARK-282, add special case formatting for dictionnaries
1 parent 823d9d0 commit 862c502

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## [Unreleased version] - 20XX-XX-XX
4+
### Added
5+
6+
### Changed
7+
- the formatter properly formats dictionaries (key-value pairs on their own line, always)
8+
9+
### Removed
10+
311
## [4.0.0] - 2025-09-12
412
### Added
513
- more tests for the io builtins

src/arkscript/Formatter.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

322322
std::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)
484482
std::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
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(let a (dict "key" "value"
2+
1 12
3+
"yes?" true))
4+
(let b (dict "key" "value"))
5+
6+
(print a)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(let a (dict
2+
"key" "value"
3+
1 12
4+
"yes?" true))
5+
(let b (dict "key" "value"))
6+
7+
(print a)

0 commit comments

Comments
 (0)