Skip to content

Commit 0236e36

Browse files
authored
formatter: fix multi-line form consolidation (#3336)
1 parent 0ea718a commit 0236e36

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

common/formatter/formatter.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,12 @@ std::vector<std::string> apply_formatting(const FormatterTreeNode& curr_node,
288288
}
289289

290290
// Consolidate any lines if the configuration requires it
291+
// TODO there is a hack here so that multi-line forms that are consolidated still line up properly
292+
// i have to make consolidate a more first-class feature of the config
291293
if (curr_node.formatting_config.inline_until_index(form_lines)) {
292294
std::vector<std::string> new_form_lines = {};
295+
const auto original_form_head_width = str_util::split(form_lines.at(0), '\n').at(0).length();
296+
bool consolidating_lines = true;
293297
for (int i = 0; i < (int)form_lines.size(); i++) {
294298
if (i < curr_node.formatting_config.inline_until_index(form_lines)) {
295299
if (new_form_lines.empty()) {
@@ -298,7 +302,13 @@ std::vector<std::string> apply_formatting(const FormatterTreeNode& curr_node,
298302
new_form_lines.at(0) += fmt::format(" {}", form_lines.at(i));
299303
}
300304
} else {
301-
new_form_lines.push_back(form_lines.at(i));
305+
if (str_util::starts_with(form_lines.at(i), " ") && consolidating_lines) {
306+
new_form_lines.push_back(fmt::format(
307+
"{}{}", str_util::repeat(original_form_head_width, " "), form_lines.at(i)));
308+
} else {
309+
consolidating_lines = false;
310+
new_form_lines.push_back(form_lines.at(i));
311+
}
302312
}
303313
}
304314
form_lines = new_form_lines;

test/common/formatter/corpus/conditions.test.gc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ Non-Inlinable If
2525
(if arg1
2626
(symbol->string (-> arg0 type symbol) (-> arg0 type symbol) (-> arg0 type symbol) (-> arg0 type symbol))
2727
(symbol->string (-> arg0 type symbol) (-> arg0 type symbol) (-> arg0 type symbol) (-> arg0 type symbol)))
28+
29+
===
30+
Multiline condition hang
31+
===
32+
33+
(when (and (-> this ignore-menu-toggle?)
34+
(or (not (cpad-hold? 0 l1)) (not (cpad-hold? 0 r1)))
35+
(or (and (-> this opened-with-start?) (not (cpad-hold? 0 start)) (not (cpad-hold? 0 start))) (and (not (-> this opened-with-start?)) (not (cpad-hold? 0 select)))))
36+
(set! (-> this ignore-menu-toggle?) #f))
37+
38+
---
39+
40+
(when (and (-> this ignore-menu-toggle?)
41+
(or (not (cpad-hold? 0 l1)) (not (cpad-hold? 0 r1)))
42+
(or (and (-> this opened-with-start?) (not (cpad-hold? 0 start)) (not (cpad-hold? 0 start)))
43+
(and (not (-> this opened-with-start?)) (not (cpad-hold? 0 select)))))
44+
(set! (-> this ignore-menu-toggle?) #f))

0 commit comments

Comments
 (0)