Skip to content

Commit 41a22cb

Browse files
authored
Merge pull request #125 from qq792453582/master
add keep_indents_on_empty_lines option
2 parents 12925e1 + 7bf6a0c commit 41a22cb

File tree

5 files changed

+113
-16
lines changed

5 files changed

+113
-16
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

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

117117
bool never_indent_comment_on_if_branch = false;
118118

119+
/*
120+
* if true retains indents on empty lines as if they contained some code
121+
* if false deletes the tab characters and spaces on empty lines
122+
*/
123+
bool keep_indents_on_empty_lines = false;
124+
119125
// [line space]
120126

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

CodeFormatCore/include/CodeFormatCore/Format/FormatBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ class FormatBuilder {
1515
explicit FormatBuilder(LuaStyle &style);
1616

1717
virtual std::string GetFormatResult(const LuaSyntaxTree &t);
18+
1819
protected:
1920
void DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve);
2021

2122
virtual void WriteSyntaxNode(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
2223

2324
virtual void WriteSpace(std::size_t space);
2425

26+
void AddEndOfLine(std::size_t line);
27+
2528
virtual void WriteLine(std::size_t line);
2629

2730
virtual void WriteIndent();

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::AddEndOfLine(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+
AddEndOfLine(1);
311+
312+
for (int i = 1; i < line; ++i) {
313+
WriteIndent();
314+
AddEndOfLine(1);
315+
}
316+
} else {
317+
AddEndOfLine(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+
}

0 commit comments

Comments
 (0)