Skip to content

Commit f01e4a1

Browse files
committed
implement #30
1 parent e133f8e commit f01e4a1

File tree

8 files changed

+127
-3
lines changed

8 files changed

+127
-3
lines changed

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
319319
configMap.at("if_condition_align_with_each_other") == "true";
320320
}
321321

322+
if(configMap.count("if_branch_comments_after_block_no_indent"))
323+
{
324+
options->if_branch_comments_after_block_no_indent =
325+
configMap.at("if_branch_comments_after_block_no_indent") == "true";
326+
}
327+
322328
if(configMap.count("long_chain_expression_allow_one_space_after_colon"))
323329
{
324330
options->long_chain_expression_allow_one_space_after_colon =

CodeService/src/LuaFormatter.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,12 +1852,23 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
18521852
{
18531853
std::shared_ptr<LuaAstNode> block = nullptr;
18541854
std::vector<std::shared_ptr<LuaAstNode>> comments;
1855-
1855+
std::vector<std::shared_ptr<LuaAstNode>> afterBlockComments;
18561856
for (; it != children.end(); ++it)
18571857
{
18581858
if (nextMatch(it, LuaAstNodeType::Comment, children))
18591859
{
1860-
comments.push_back(nextNode(it, children));
1860+
auto next = nextNode(it, children);
1861+
if (block)
1862+
{
1863+
if (_parser->GetLine(block->GetTextRange().EndOffset) != _parser->GetLine(
1864+
next->GetTextRange().StartOffset))
1865+
{
1866+
afterBlockComments.push_back(next);
1867+
continue;
1868+
}
1869+
}
1870+
1871+
comments.push_back(next);
18611872
}
18621873
else if (nextMatch(it, LuaAstNodeType::Block, children))
18631874
{
@@ -1878,7 +1889,32 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
18781889
copyBlock->AddComment(comment);
18791890
}
18801891

1881-
return FormatBlock(copyBlock);
1892+
auto nextKey = nextNode(it, children);
1893+
if (_options.if_branch_comments_after_block_no_indent
1894+
&& nextKey && nextKey->GetType() == LuaAstNodeType::KeyWord
1895+
&& (nextKey->GetText() == "elseif" || nextKey->GetText() == "else"))
1896+
{
1897+
auto blockEnv = FormatBlock(copyBlock);
1898+
auto noIndentEnv = std::make_shared<NoIndentElement>();
1899+
for (auto comment : afterBlockComments)
1900+
{
1901+
auto commentStatement = std::make_shared<StatementElement>();
1902+
commentStatement->AddChild(FormatComment(comment));
1903+
noIndentEnv->AddChild(commentStatement);
1904+
noIndentEnv->Add<KeepLineElement>();
1905+
}
1906+
blockEnv->AddChild(noIndentEnv);
1907+
1908+
return blockEnv;
1909+
}
1910+
else
1911+
{
1912+
for (auto comment : afterBlockComments)
1913+
{
1914+
copyBlock->AddComment(comment);
1915+
}
1916+
return FormatBlock(copyBlock);
1917+
}
18821918
}
18831919
else
18841920
{

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ call_arg_parentheses = remove_table_only
155155
call_arg_parentheses = remove_string_only
156156
[{call_arg_parentheses-eq-unambiguous_remove_string_only.lua}]
157157
call_arg_parentheses = unambiguous_remove_string_only
158+
[{if_branch_comments_after_block_no_indent-eq-true.lua}]
159+
if_branch_comments_after_block_no_indent = true
158160
[{minLine-eq-1.lua}]
159161
keep_line_after_if_statement = minLine:1
160162
keep_line_after_do_statement = minLine:1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
if a then
2+
--aaa
3+
local c =123
4+
--bbb
5+
else --cccc
6+
--dddddd
7+
end
8+
9+
if cccc then
10+
11+
---ddddd
12+
elseif aaa then
13+
---hhhhh
14+
end
15+
16+
if dddd then
17+
18+
local cc =123
19+
---kwgfowkjgow
20+
local t =123
21+
--afafafa
22+
elseif ccccc then
23+
local t = 123
24+
--fnwojfoiw
25+
end
26+
27+
if eeee then ddd() --[[aaaa]] else end
28+
29+
if hhhh then dddd() ---jfoiwjfiwjifw
30+
elseif hhhhh then
31+
end
32+
33+
if hhhh then
34+
dddd()
35+
--hhhh
36+
else
37+
end

Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ call_arg_parentheses = remove_table_only
153153
call_arg_parentheses = remove_string_only
154154
[{call_arg_parentheses-eq-unambiguous_remove_string_only.lua}]
155155
call_arg_parentheses = unambiguous_remove_string_only
156+
[{if_branch_comments_after_block_no_indent-eq-true.lua}]
157+
if_branch_comments_after_block_no_indent = true
156158
[{minLine-eq-1.lua}]
157159
keep_line_after_if_statement = minLine:1
158160
keep_line_after_do_statement = minLine:1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
if a then
2+
--aaa
3+
local c = 123
4+
--bbb
5+
else --cccc
6+
--dddddd
7+
end
8+
9+
if cccc then
10+
11+
---ddddd
12+
elseif aaa then
13+
---hhhhh
14+
end
15+
16+
if dddd then
17+
18+
local cc = 123
19+
---kwgfowkjgow
20+
local t = 123
21+
--afafafa
22+
elseif ccccc then
23+
local t = 123
24+
--fnwojfoiw
25+
end
26+
27+
if eeee then ddd() --[[aaaa]] else end
28+
29+
if hhhh then dddd() ---jfoiwjfiwjifw
30+
elseif hhhhh then
31+
end
32+
33+
if hhhh then
34+
dddd()
35+
--hhhh
36+
else
37+
end

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class LuaCodeStyleOptions
8484
*/
8585
bool if_condition_no_continuation_indent = false;
8686

87+
bool if_branch_comments_after_block_no_indent = false;
8788
/*
8889
* 表内每一个表项对齐到第一个表项
8990
*/
@@ -103,6 +104,7 @@ class LuaCodeStyleOptions
103104
bool table_append_expression_no_space = false;
104105

105106
bool long_chain_expression_allow_one_space_after_colon = false;
107+
106108
#ifndef _WINDOWS
107109
std::string end_of_line = "\n";
108110
#else

lua.template.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ label_no_indent = false
4040
do_statement_no_indent = false
4141
# if true, the conditional expression of the if statement will not be a continuation line indent
4242
if_condition_no_continuation_indent = false
43+
#
44+
if_branch_comments_after_block_no_indent = false
4345
# if true, t[#t+1] will not space wrapper '+'
4446
table_append_expression_no_space = false
4547
# if statement will align like switch case

0 commit comments

Comments
 (0)