You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Without the flag, the `and` chain does not fit, and the formatter
117
+
// falls back to breaking inside the nested `getStorageValue` call
118
+
// argument list instead of breaking at the chain's own operators.
119
+
let config = LuaFormatConfig{
120
+
layout:LayoutConfig{
121
+
max_line_width:120,
122
+
prefer_binary_chain_operand_per_line:true,
123
+
..Default::default()
124
+
},
125
+
..Default::default()
126
+
};
127
+
assert_format_with_config!(
128
+
r#"if player:getStorageValue(Storage.ExplorerSociety.TheIceMusic) >= 62 and player:getStorageValue(Storage.ExplorerSociety.QuestLine) >= 62 and player:removeItem(5022, 1) then
// Regression guard: with the flag off (the default), behavior is
148
+
// unchanged from before this option existed, including the
149
+
// less-than-ideal nested-call break.
150
+
let config = LuaFormatConfig{
151
+
layout:LayoutConfig{
152
+
max_line_width:120,
153
+
..Default::default()
154
+
},
155
+
..Default::default()
156
+
};
157
+
assert_format_with_config!(
158
+
r#"if player:getStorageValue(Storage.ExplorerSociety.TheIceMusic) >= 62 and player:getStorageValue(Storage.ExplorerSociety.QuestLine) >= 62 and player:removeItem(5022, 1) then
// When the existing fill/packed layout already fits without
205
+
// overflowing max_line_width in fewer lines than one-operand-per-line
206
+
// would need, the scorer keeps preferring the more compact layout.
207
+
let config = LuaFormatConfig{
208
+
layout:LayoutConfig{
209
+
max_line_width:100,
210
+
prefer_binary_chain_operand_per_line:true,
211
+
..Default::default()
212
+
},
213
+
..Default::default()
214
+
};
215
+
assert_format_with_config!(
216
+
r#"if get_value(namespace_alpha_beta) >= threshold_value and get_value(namespace_gamma_delta) >= threshold_value and remove_item(item_id_number, item_count) then
217
+
do_something()
218
+
end
219
+
"#,
220
+
r#"
221
+
if get_value(namespace_alpha_beta) >= threshold_value and get_value(namespace_gamma_delta)
222
+
>= threshold_value and remove_item(item_id_number, item_count) then
r#"if player:getStorageValue(Storage.QuestSystem.FirstPartCompleted) == 1 or player:getStorageValue(Storage.QuestSystem.SecondPartCompleted) == 1 or player:getStorageValue(Storage.QuestSystem.ThirdPartCompleted) == 1 then
242
+
work()
243
+
end
244
+
"#,
245
+
r#"
246
+
if player:getStorageValue(Storage.QuestSystem.FirstPartCompleted) == 1
247
+
or player:getStorageValue(Storage.QuestSystem.SecondPartCompleted) == 1
248
+
or player:getStorageValue(Storage.QuestSystem.ThirdPartCompleted) == 1 then
- 这里的 chain head 定义为:`root` 加上前导命名空间/字段访问,再加上第一个调用段;但如果 `root` 本身已经是一个调用,则 head 就停在这个调用本身。例如 `Builder:new():add():add()` 的 head 是 `Builder:new()`,`ConsoleFormattingBuilder():setColor():build()` 的 head 是 `ConsoleFormattingBuilder()`,`vim.api.nvim_set_keymap(...)` 的 head 是整个 `vim.api.nvim_set_keymap(...)`。
Copy file name to clipboardExpand all lines: docs/emmylua_formatter/options_EN.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,6 +59,7 @@ width = 4
59
59
-`prefer_call_args_layout_from_source`: prefer keeping the source layout goal for explicit multiline call argument lists
60
60
-`prefer_table_layout_from_source`: prefer keeping the source layout goal for explicit multiline pure-array tables
61
61
-`prefer_chain_break_on_statement_tail`: prefer multiline breaking for fluent chains in statement-tail position
62
+
-`prefer_binary_chain_operand_per_line`: offer a one-operand-per-line layout candidate for same-operator binary chains (3 or more operands), such as a run of `and`/`or` conditions
62
63
63
64
Default:
64
65
@@ -72,6 +73,7 @@ func_params_expand = "Auto"
72
73
prefer_call_args_layout_from_source = false
73
74
prefer_table_layout_from_source = false
74
75
prefer_chain_break_on_statement_tail = false
76
+
prefer_binary_chain_operand_per_line = false
75
77
```
76
78
77
79
Behavior notes:
@@ -87,6 +89,8 @@ Behavior notes:
87
89
-`prefer_chain_break_on_statement_tail = true` applies to the last direct expression in a statement, including standalone call statements, and also to keyed table-field values; when that expression is a long enough fluent chain, the formatter prefers chain-style breaking.
88
90
- The chain head is defined as the `root`, plus any leading namespace or field accesses, plus the first call segment; however, if the `root` itself already ends in a call, the head stops at that call. For example, the head of `Builder:new():add():add()` is `Builder:new()`, the head of `ConsoleFormattingBuilder():setColor():build()` is `ConsoleFormattingBuilder()`, and the head of `vim.api.nvim_set_keymap(...)` is the whole `vim.api.nvim_set_keymap(...)` call.
89
91
- The break start is defined as the first continuation segment after that head. In other words, only segments that continue after the first call are eligible for chain-style line breaking; a namespace-qualified terminal call is not treated as a fluent chain by this option.
92
+
-`prefer_binary_chain_operand_per_line = true` applies to a run of 3 or more operands joined by the same operator, most commonly a chain of `and`/`or` conditions in an `if`/`while` header. It only adds a candidate layout; the formatter still picks whichever candidate (flat, fill, packed, or one-operand-per-line) produces the fewest lines without overflowing `max_line_width`, so short chains and chains that already fit with fill/packed are unaffected.
93
+
- Without this option, a chain that does not fit can end up breaking inside one of its own operands (for example, expanding a nested call's argument list) instead of breaking at the chain's operators. Enabling it gives the formatter a clean alternative: one operand per line, with the operator leading each continuation line (`and`/`or` at the start of the line, matching this project's existing leading-operator style for binary expressions).
0 commit comments