Skip to content

Commit ccfd166

Browse files
committed
close #111
1 parent 82aca21 commit ccfd166

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

CodeService/src/Format/Analyzer/LineBreakAnalyzer.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ void LineBreakAnalyzer::BreakBefore(LuaSyntaxNode n, const LuaSyntaxTree &t, std
246246
}
247247
}
248248

249+
void LineBreakAnalyzer::BreakBefore(LuaSyntaxNode n, const LuaSyntaxTree &t, LineSpace lineSpace) {
250+
auto prev = n.GetPrevSibling(t);
251+
if (!prev.IsNull(t)) {
252+
BreakAfter(prev, t, lineSpace);
253+
}
254+
}
255+
249256
void LineBreakAnalyzer::AnalyzeExprList(FormatState &f, LuaSyntaxNode &exprList, const LuaSyntaxTree &t) {
250257
auto exprs = exprList.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
251258
if (exprs.empty()) {
@@ -361,7 +368,13 @@ void LineBreakAnalyzer::AnalyzeExpr(FormatState &f, LuaSyntaxNode &expr, const L
361368
});
362369
}
363370
if (forceBreak) {
364-
BreakAfter(expr.GetChildToken('{', t), t);
371+
auto leftBrace = expr.GetChildToken('{', t);
372+
if (leftBrace.GetNextSibling(t).GetTokenKind(t) == TK_SHORT_COMMENT || leftBrace.GetNextSibling(t).GetTokenKind(t) == TK_LONG_COMMENT) {
373+
BreakAfter(leftBrace.GetNextSibling(t), t, LineSpace(LineSpaceType::Keep));
374+
} else {
375+
BreakAfter(leftBrace, t, LineSpace(LineSpaceType::Keep));
376+
}
377+
365378
for (auto field: fields) {
366379
BreakAfter(field, t, LineSpace(LineSpaceType::Keep));
367380
}

Test/src/FormatResult_unitest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,4 +924,20 @@ local t = 13 + 123
924924
925925
local t = 13 + 123
926926
)"));
927+
}
928+
929+
TEST(Format, feature_111) {
930+
EXPECT_TRUE(TestHelper::TestFormatted(
931+
R"(
932+
local table = { -- My comment
933+
elem1 = {},
934+
elem2 = {}
935+
}
936+
)",
937+
R"(
938+
local table = { -- My comment
939+
elem1 = {},
940+
elem2 = {}
941+
}
942+
)"));
927943
}

Test2/src/FormatTest2.cpp

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

99
int main() {
1010
std::string buffer = R"(
11-
--Some comment about the file
12-
13-
14-
tbl[i], tbl[j] = tbl[j], tbl[i]
11+
local t = { --131
12+
}
1513
)";
1614

1715
auto file = std::make_shared<LuaFile>(std::move(buffer));
@@ -26,7 +24,7 @@ tbl[i], tbl[j] = tbl[j], tbl[i]
2624
std::cout << t.GetDebugView() << std::endl;
2725

2826
LuaStyle s;
29-
s.trailing_table_separator = TrailingTableSeparator::Always;
27+
s.break_all_list_when_line_exceed = true;
3028
FormatBuilder b(s);
3129
auto text = b.GetFormatResult(t);
3230
std::cout<< text << std::endl;

include/CodeService/Format/Analyzer/LineBreakAnalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class LineBreakAnalyzer : public FormatAnalyzer {
2323

2424
void BreakBefore(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t line = 1);
2525

26+
void BreakBefore(LuaSyntaxNode n, const LuaSyntaxTree &t, LineSpace lineSpace);
27+
2628
void MarkLazyBreak(LuaSyntaxNode n, const LuaSyntaxTree &t, LineBreakStrategy strategy);
2729

2830
void MarkNotBreak(LuaSyntaxNode n, const LuaSyntaxTree &t);

0 commit comments

Comments
 (0)