Skip to content

Commit bcf55bf

Browse files
committed
add keep_indents_on_empty_lines option
1 parent 12925e1 commit bcf55bf

File tree

6 files changed

+123
-24
lines changed

6 files changed

+123
-24
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

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

117117
bool never_indent_comment_on_if_branch = false;
118118

119+
bool keep_indents_on_empty_lines = false; // 空行保持缩进
120+
119121
// [line space]
120122

121123
LineSpace line_space_after_if_statement = LineSpace(LineSpaceType::Keep);

CodeFormatCore/include/CodeFormatCore/Format/FormatBuilder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class FormatBuilder {
2222

2323
virtual void WriteSpace(std::size_t space);
2424

25+
void BreakLine(std::size_t line);
26+
2527
virtual void WriteLine(std::size_t line);
26-
28+
2729
virtual void WriteIndent();
2830

2931
virtual void WriteChar(char ch);

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
220220

221221
BOOL_OPTION(never_indent_comment_on_if_branch)
222222

223+
BOOL_OPTION(keep_indents_on_empty_lines)
224+
223225
std::vector<std::pair<std::string, LineSpace &>> fieldList = {
224226
{"line_space_after_if_statement", line_space_after_if_statement },
225227
{"line_space_after_do_statement", line_space_after_do_statement },

CodeFormatCore/src/Format/FormatBuilder.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,7 @@ void FormatBuilder::WriteSpace(std::size_t space) {
251251
_state.CurrentWidth() += space;
252252
}
253253

254-
void FormatBuilder::WriteLine(std::size_t line) {
255-
if (line == 0) {
256-
return;
257-
}
258-
// trim end space
259-
auto lastIndex = _formattedText.size();
260-
std::size_t reduce = 0;
261-
while (_formattedText[lastIndex - reduce - 1] == ' ') {
262-
reduce++;
263-
if (lastIndex <= reduce + 1) {
264-
break;
265-
}
266-
}
267-
if (reduce > 0) {
268-
_formattedText.resize(_formattedText.size() - reduce);
269-
}
254+
void FormatBuilder::BreakLine(std::size_t line) {
270255
auto endOfLine = _state.GetEndOfLine();
271256
switch (endOfLine) {
272257
case EndOfLine::CRLF: {
@@ -304,6 +289,35 @@ void FormatBuilder::WriteLine(std::size_t line) {
304289
_state.CurrentWidth() = 0;
305290
}
306291

292+
void FormatBuilder::WriteLine(std::size_t line) {
293+
if (line == 0) {
294+
return;
295+
}
296+
// trim end space
297+
auto lastIndex = _formattedText.size();
298+
std::size_t reduce = 0;
299+
while (_formattedText[lastIndex - reduce - 1] == ' ') {
300+
reduce++;
301+
if (lastIndex <= reduce + 1) {
302+
break;
303+
}
304+
}
305+
if (reduce > 0) {
306+
_formattedText.resize(_formattedText.size() - reduce);
307+
}
308+
309+
if (line > 1 && _state.GetStyle().keep_indents_on_empty_lines) {
310+
BreakLine(1);
311+
312+
for (int i = 1; i < line; ++i) {
313+
WriteIndent();
314+
BreakLine(1);
315+
}
316+
} else {
317+
BreakLine(line);
318+
}
319+
}
320+
307321
void FormatBuilder::WriteIndent() {
308322
auto topLevelIndent = _state.GetCurrentIndent();
309323
switch (_state.GetStyle().indent_style) {

Test/src/FormatStyle_unitest.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,3 +1680,75 @@ local table1 = { 1, 2, 3 }
16801680
)",
16811681
style));
16821682
}
1683+
1684+
TEST(FormatByStyleOption, keep_indents_on_empty_lines) {
1685+
LuaStyle style;
1686+
1687+
style.keep_indents_on_empty_lines = true;
1688+
EXPECT_TRUE(TestHelper::TestFormatted(
1689+
R"(
1690+
local t = function() local a = 131
1691+
1692+
1693+
local b = function()
1694+
local c = {
1695+
}
1696+
end
1697+
end
1698+
1699+
local t2 = function()
1700+
local c = 1
1701+
end
1702+
)",
1703+
R"(
1704+
local t = function()
1705+
local a = 131
1706+
1707+
1708+
local b = function()
1709+
local c = {
1710+
}
1711+
end
1712+
end
1713+
1714+
local t2 = function()
1715+
local c = 1
1716+
end
1717+
1718+
)",
1719+
style));
1720+
1721+
style.keep_indents_on_empty_lines = false;
1722+
EXPECT_TRUE(TestHelper::TestFormatted(
1723+
R"(
1724+
local t = function() local a = 131
1725+
1726+
1727+
local b = function()
1728+
local c = {
1729+
}
1730+
end
1731+
end
1732+
1733+
local t2 = function()
1734+
local c = 1
1735+
end
1736+
)",
1737+
R"(
1738+
local t = function()
1739+
local a = 131
1740+
1741+
1742+
local b = function()
1743+
local c = {
1744+
}
1745+
end
1746+
end
1747+
1748+
local t2 = function()
1749+
local c = 1
1750+
end
1751+
1752+
)",
1753+
style));
1754+
}

Test2/src/FormatTest2.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88

99
int main() {
1010
std::string buffer = R"(
11-
local t = { function ()
12-
local t = 123
13-
end }
11+
local t = function() local a = 131
1412
15-
local t = { {
16-
okokok = 123
17-
} }
13+
14+
local b = function()
15+
local c = {
16+
}
17+
end
18+
end
19+
20+
local t2 = function()
21+
local c = 1
22+
end
1823
)";
1924

2025
auto file = std::make_shared<LuaFile>(std::move(buffer));
@@ -30,6 +35,8 @@ local t = { {
3035
std::cout << t.GetDebugView() << std::endl;
3136

3237
LuaStyle s;
38+
s.indent_style = IndentStyle::Tab;
39+
s.keep_indents_on_empty_lines = true;
3340
FormatBuilder b(s);
3441
auto text = b.GetFormatResult(t);
3542
std::cout<< text << std::endl;
@@ -40,4 +47,4 @@ local t = { {
4047
// std::cout << diag.Message << std::endl;
4148
// }
4249
return 0;
43-
}
50+
}

0 commit comments

Comments
 (0)