Skip to content

Commit 9889f3d

Browse files
committed
implement #31
1 parent f01e4a1 commit 9889f3d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CodeService/src/LuaFormatter.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,24 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAssignLeftExpressionList(std:
662662

663663
std::shared_ptr<FormatElement> LuaFormatter::FormatComment(std::shared_ptr<LuaAstNode> comment)
664664
{
665-
return std::make_shared<TextElement>(comment);
665+
auto text = comment->GetText();
666+
if(!text.empty() && text.back() > 0 && ::isspace(text.back()) == 0)
667+
{
668+
return std::make_shared<TextElement>(comment);
669+
}
670+
int i = static_cast<int>(text.size()) - 1;
671+
for (; i >= 0; i--)
672+
{
673+
char ch = text[i];
674+
if(ch <= 0 || ::isspace(ch) == 0)
675+
{
676+
break;
677+
}
678+
}
679+
680+
text = text.substr(0, i + 1);
681+
682+
return std::make_shared<TextElement>(text, comment->GetTextRange());
666683
}
667684

668685
std::shared_ptr<FormatElement> LuaFormatter::FormatBreakStatement(std::shared_ptr<LuaAstNode> breakNode)

Test/test_script/format_text/wait_format/comment.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ end
2323

2424
return --fff
2525
aaa+ccc,bbb
26+
27+
--wjgiowjiojg
28+
--[[ ]]
29+
local t = -- [[[[[[]]]]]]
30+
wgwjgjwiog --[[
31+
32+
33+
]]

Test/test_script/format_text/wait_format_should_be/comment.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ end
2323

2424
return--fff
2525
aaa + ccc, bbb
26+
27+
--wjgiowjiojg
28+
--[[ ]]
29+
local t = -- [[[[[[]]]]]]
30+
wgwjgjwiog --[[
31+
32+
33+
]]

0 commit comments

Comments
 (0)