Skip to content

Commit 3962c1f

Browse files
committed
support break_before_braces
1 parent ee5c11b commit 3962c1f

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class LuaStyle {
141141

142142
bool auto_collapse_lines = false;
143143

144+
bool break_before_braces = false;
145+
144146
// [preference]
145147
bool ignore_space_after_colon = false;
146148

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
263263

264264
BOOL_OPTION(auto_collapse_lines)
265265

266+
BOOL_OPTION(break_before_braces)
267+
266268
BOOL_OPTION(ignore_space_after_colon)
267269

268270
BOOL_OPTION(remove_call_expression_list_finish_comma)

CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ void LineBreakAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
1919
BreakAfter(syntaxNode, t, LineSpace(LineSpaceType::Keep));
2020
break;
2121
}
22+
case '{': {
23+
if (f.GetStyle().break_before_braces) {
24+
auto parent = syntaxNode.GetParent(t);
25+
if (parent.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression && !parent.IsSingleLineNode(t)) {
26+
BreakBefore(syntaxNode, t, LineSpace(LineSpaceType::Keep));
27+
}
28+
}
29+
break;
30+
}
2231
default: {
2332
break;
2433
}
@@ -240,14 +249,14 @@ void LineBreakAnalyzer::BreakAfter(LuaSyntaxNode n, const LuaSyntaxTree &t, Line
240249
}
241250

242251
void LineBreakAnalyzer::BreakBefore(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t line) {
243-
auto prev = n.GetPrevSibling(t);
252+
auto prev = n.GetPrevToken(t);
244253
if (!prev.IsNull(t)) {
245254
BreakAfter(prev, t, line);
246255
}
247256
}
248257

249258
void LineBreakAnalyzer::BreakBefore(LuaSyntaxNode n, const LuaSyntaxTree &t, LineSpace lineSpace) {
250-
auto prev = n.GetPrevSibling(t);
259+
auto prev = n.GetPrevToken(t);
251260
if (!prev.IsNull(t)) {
252261
BreakAfter(prev, t, lineSpace);
253262
}

Test/src/FormatStyle_unitest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,38 @@ map("n", "<C-j>", quickfix_step("below"), { buffer = true })
14971497
map("n", "<C-k>", quickfix_step("above"), { buffer = true })
14981498
map("n", "<Space>", "<CR><C-w>p", { buffer = true })
14991499
map({ "n", "x" }, "<CR>", "<CR>", { buffer = true })
1500+
)",
1501+
style));
1502+
}
1503+
1504+
TEST(FormatByStyleOption, break_before_braces) {
1505+
LuaStyle style;
1506+
1507+
style.break_before_braces = false;
1508+
EXPECT_TRUE(TestHelper::TestFormatted(
1509+
R"(
1510+
local t = {
1511+
a = 123
1512+
}
1513+
)",
1514+
R"(
1515+
local t = {
1516+
a = 123
1517+
}
1518+
)",
1519+
style));
1520+
style.break_before_braces = true;
1521+
EXPECT_TRUE(TestHelper::TestFormatted(
1522+
R"(
1523+
local t = {
1524+
a = 123
1525+
}
1526+
)",
1527+
R"(
1528+
local t =
1529+
{
1530+
a = 123
1531+
}
15001532
)",
15011533
style));
15021534
}

Test2/src/FormatTest2.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
int main() {
1010
std::string buffer = R"(
11-
map(1231, 3131, 12331113131,1 )
12-
map(213112, 1, 12313131,1313113131)
11+
local d = {
12+
dddd = 23131;
13+
}
1314
)";
1415

1516
auto file = std::make_shared<LuaFile>(std::move(buffer));
@@ -24,7 +25,7 @@ map(213112, 1, 12313131,1313113131)
2425
std::cout << t.GetDebugView() << std::endl;
2526

2627
LuaStyle s;
27-
s.align_continuous_similar_call_args = true;
28+
s.break_before_braces = true;
2829
FormatBuilder b(s);
2930
auto text = b.GetFormatResult(t);
3031
std::cout<< text << std::endl;

0 commit comments

Comments
 (0)