2525#include " Util/StringUtil.h"
2626#include " CodeService/AstUtil.h"
2727
28- bool nextMatch (LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
28+ bool NextMatch (LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
2929{
3030 if (it != container.end () && (++it) != container.end ())
3131 {
@@ -35,7 +35,7 @@ bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNo
3535 return false ;
3636}
3737
38- std::shared_ptr<LuaAstNode> nextNode (LuaAstNode::ChildIterator it, const LuaAstNode::ChildrenContainer& container)
38+ std::shared_ptr<LuaAstNode> NextNode (LuaAstNode::ChildIterator it, const LuaAstNode::ChildrenContainer& container)
3939{
4040 if (it != container.end () && (++it) != container.end ())
4141 {
@@ -278,9 +278,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlock(std::shared_ptr<LuaAstN
278278 case LuaAstNodeType::AssignStatement:
279279 case LuaAstNodeType::LocalStatement:
280280 {
281- if (nextMatch (it, LuaAstNodeType::AssignStatement, statements)
282- || nextMatch (it, LuaAstNodeType::LocalStatement, statements)
283- || nextMatch (it, LuaAstNodeType::Comment, statements))
281+ if (NextMatch (it, LuaAstNodeType::AssignStatement, statements)
282+ || NextMatch (it, LuaAstNodeType::LocalStatement, statements)
283+ || NextMatch (it, LuaAstNodeType::Comment, statements))
284284 {
285285 indentEnv->AddChild (FormatAlignStatement (it, statements));
286286 }
@@ -351,9 +351,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlock(std::shared_ptr<LuaAstN
351351 case LuaAstNodeType::ExpressionStatement:
352352 {
353353 auto statEnv = FormatNode (statement);
354- if (nextMatch (it, LuaAstNodeType::Comment, statements))
354+ if (NextMatch (it, LuaAstNodeType::Comment, statements))
355355 {
356- auto next = nextNode (it, statements);
356+ auto next = NextNode (it, statements);
357357 int currentLine = _parser->GetLine (statement->GetTextRange ().EndOffset );
358358 int nextLine = _parser->GetLine (next->GetTextRange ().StartOffset );
359359
@@ -1310,7 +1310,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNameExpression(std::shared_pt
13101310
13111311 for (auto & child : nameExpression->GetChildren ())
13121312 {
1313- FormatSubExpressionNode (child, env);
1313+ FormatSubExpression (child, env);
13141314 }
13151315
13161316 return env;
@@ -1364,7 +1364,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatParamList(std::shared_ptr<Lua
13641364 case LuaAstNodeType::Param:
13651365 {
13661366 paramListLayoutEnv->Add <TextElement>(child);
1367- if (nextMatch (it, LuaAstNodeType::Comment, children))
1367+ if (NextMatch (it, LuaAstNodeType::Comment, children))
13681368 {
13691369 paramListLayoutEnv->Add <KeepElement>(1 );
13701370 }
@@ -1649,7 +1649,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
16491649
16501650 env->AddChild (FormatNode (*it));
16511651
1652- auto nextChild = nextNode (it, children);
1652+ auto nextChild = NextNode (it, children);
16531653 if (nextChild == nullptr )
16541654 {
16551655 return env->GetChildren ().front ();
@@ -1708,7 +1708,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
17081708
17091709 ++it;
17101710
1711- nextChild = nextNode (it, children);
1711+ nextChild = NextNode (it, children);
17121712 if (nextChild == nullptr )
17131713 {
17141714 break ;
@@ -1737,7 +1737,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
17371737 for (; it != siblings.end (); ++it)
17381738 {
17391739 auto current = *it;
1740- auto nextSibling = nextNode (it, siblings);
1740+ auto nextSibling = NextNode (it, siblings);
17411741
17421742 if (nextSibling == nullptr )
17431743 {
@@ -1824,11 +1824,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode:
18241824{
18251825 auto env = std::make_shared<ExpressionElement>();
18261826 auto keyNode = *it;
1827+ auto parentNode = keyNode->GetParent ();
18271828 env->AddChild (FormatNode (keyNode));
18281829
1829- if (nextMatch (it, LuaAstNodeType::Comment, children))
1830+ if (NextMatch (it, LuaAstNodeType::Comment, children))
18301831 {
1831- auto comment = nextNode (it, children);
1832+ auto comment = NextNode (it, children);
18321833 int currentLine = _parser->GetLine (keyNode->GetTextRange ().EndOffset );
18331834 int nextLine = _parser->GetLine (comment->GetTextRange ().StartOffset );
18341835
@@ -1849,10 +1850,18 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode:
18491850 blockExist = true ;
18501851 if (_parser->GetLine (keyNode->GetTextRange ().StartOffset ) != _parser->GetLine (block->GetTextRange ().EndOffset ))
18511852 {
1852- env->Add <KeepElement>(1 );
1853-
1854- env->AddChild (block);
1855- env->Add <KeepElement>(1 , true );
1853+ if (_options.remove_empty_header_and_footer_lines_in_function
1854+ && parentNode && parentNode->GetType () == LuaAstNodeType::FunctionBody)
1855+ {
1856+ env->Add <LineElement>();
1857+ env->AddChild (block);
1858+ }
1859+ else
1860+ {
1861+ env->Add <KeepElement>(1 );
1862+ env->AddChild (block);
1863+ env->Add <KeepElement>(1 , true );
1864+ }
18561865 }
18571866 else
18581867 {
@@ -1876,9 +1885,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode:
18761885 env->Add <KeepElement>(1 );
18771886 }
18781887
1879- if (nextMatch (it, LuaAstNodeType::KeyWord, children))
1888+ if (NextMatch (it, LuaAstNodeType::KeyWord, children))
18801889 {
1881- auto next = nextNode (it, children);
1890+ auto next = NextNode (it, children);
18821891 if (next->GetText () == " end" )
18831892 {
18841893 env->Add <TextElement>(next);
@@ -1898,16 +1907,16 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode:
18981907}
18991908
19001909std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent (LuaAstNode::ChildIterator& it,
1901- const LuaAstNode::ChildrenContainer& children )
1910+ const LuaAstNode::ChildrenContainer& siblings )
19021911{
19031912 std::shared_ptr<LuaAstNode> block = nullptr ;
19041913 std::vector<std::shared_ptr<LuaAstNode>> comments;
19051914 std::vector<std::shared_ptr<LuaAstNode>> afterBlockComments;
1906- for (; it != children .end (); ++it)
1915+ for (; it != siblings .end (); ++it)
19071916 {
1908- if (nextMatch (it, LuaAstNodeType::Comment, children ))
1917+ if (NextMatch (it, LuaAstNodeType::Comment, siblings ))
19091918 {
1910- auto next = nextNode (it, children );
1919+ auto next = NextNode (it, siblings );
19111920 if (block)
19121921 {
19131922 if (_parser->GetLine (block->GetTextRange ().EndOffset ) != _parser->GetLine (
@@ -1920,9 +1929,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
19201929
19211930 comments.push_back (next);
19221931 }
1923- else if (nextMatch (it, LuaAstNodeType::Block, children ))
1932+ else if (NextMatch (it, LuaAstNodeType::Block, siblings ))
19241933 {
1925- block = nextNode (it, children );
1934+ block = NextNode (it, siblings );
19261935 }
19271936 else
19281937 {
@@ -1939,7 +1948,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
19391948 copyBlock->AddComment (comment);
19401949 }
19411950
1942- auto nextKey = nextNode (it, children );
1951+ auto nextKey = NextNode (it, siblings );
19431952 if (_options.if_branch_comments_after_block_no_indent
19441953 && nextKey && nextKey->GetType () == LuaAstNodeType::KeyWord
19451954 && (nextKey->GetText () == " elseif" || nextKey->GetText () == " else" ))
@@ -1982,8 +1991,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
19821991}
19831992
19841993
1985- void LuaFormatter::FormatSubExpressionNode (std::shared_ptr<LuaAstNode> expression,
1986- std::shared_ptr<FormatElement> env)
1994+ void LuaFormatter::FormatSubExpression (std::shared_ptr<LuaAstNode> expression,
1995+ std::shared_ptr<FormatElement> env)
19871996{
19881997 switch (expression->GetType ())
19891998 {
@@ -2038,7 +2047,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpression(std::shared_ptr<Lu
20382047 {
20392048 const auto current = *it;
20402049
2041- FormatSubExpressionNode (current, env);
2050+ FormatSubExpression (current, env);
20422051 env->Add <KeepElement>(0 );
20432052 }
20442053
@@ -2053,7 +2062,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBinaryExpression(std::shared_
20532062 {
20542063 const auto current = *it;
20552064
2056- FormatSubExpressionNode (current, env);
2065+ FormatSubExpression (current, env);
20572066 env->Add <KeepElement>(1 );
20582067 }
20592068
@@ -2078,7 +2087,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatUnaryExpression(std::shared_p
20782087 }
20792088 else
20802089 {
2081- auto next = nextNode (it, children);
2090+ auto next = NextNode (it, children);
20822091 if (next && (next->GetType () == LuaAstNodeType::UnaryExpression || next->GetType () ==
20832092 LuaAstNodeType::Comment))
20842093 {
@@ -2091,7 +2100,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatUnaryExpression(std::shared_p
20912100 }
20922101 default :
20932102 {
2094- FormatSubExpressionNode (child, env);
2103+ FormatSubExpression (child, env);
20952104 env->Add <KeepElement>(1 );
20962105 }
20972106 }
@@ -2184,13 +2193,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
21842193 }
21852194 }
21862195
2187- FormatSubExpressionNode (child, env);
2196+ FormatSubExpression (child, env);
21882197 env->Add <KeepElement>(0 );
21892198 break ;
21902199 }
21912200 default :
21922201 {
2193- FormatSubExpressionNode (child, env);
2202+ FormatSubExpression (child, env);
21942203 env->Add <KeepElement>(0 );
21952204 }
21962205 }
@@ -2212,9 +2221,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
22122221 case LuaAstNodeType::IndexExpression:
22132222 case LuaAstNodeType::PrimaryExpression:
22142223 {
2215- FormatSubExpressionNode (child, env);
2224+ FormatSubExpression (child, env);
22162225
2217- auto next = nextNode (it, children);
2226+ auto next = NextNode (it, children);
22182227 if (next && next->GetType () == LuaAstNodeType::CallArgList)
22192228 {
22202229 if (!ast_util::WillCallArgHaveParentheses (next, _options.call_arg_parentheses ))
@@ -2239,7 +2248,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
22392248 env->AddChild (FormatNode (child));
22402249
22412250 auto currentCallArgList = child->FindFirstOf (LuaAstNodeType::CallArgList);
2242- auto nextCallArgList = nextNode (it, children);
2251+ auto nextCallArgList = NextNode (it, children);
22432252 bool needSpace = true ;
22442253 if (currentCallArgList && nextCallArgList)
22452254 {
@@ -2291,7 +2300,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableAppendExpression(std::sh
22912300 && binaryExpressionChildren[1 ]->GetText () == " +" )
22922301 && binaryExpressionChildren[2 ]->GetText () == " 1" )
22932302 {
2294- FormatSubExpressionNode (binaryExpressionChildren[0 ], env);
2303+ FormatSubExpression (binaryExpressionChildren[0 ], env);
22952304 env->Add <KeepElement>(0 );
22962305 env->Add <TextElement>(binaryExpressionChildren[1 ]);
22972306 env->Add <KeepElement>(0 );
@@ -2332,11 +2341,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<Lu
23322341 {
23332342 auto lastLine = _parser->GetLine (textRange.EndOffset );
23342343 auto newIt = it;
2335- while (nextMatch (newIt, LuaAstNodeType::AssignStatement, statements)
2336- || nextMatch (newIt, LuaAstNodeType::LocalStatement, statements)
2337- || nextMatch (newIt, LuaAstNodeType::Comment, statements))
2344+ while (NextMatch (newIt, LuaAstNodeType::AssignStatement, statements)
2345+ || NextMatch (newIt, LuaAstNodeType::LocalStatement, statements)
2346+ || NextMatch (newIt, LuaAstNodeType::Comment, statements))
23382347 {
2339- auto next = nextNode (newIt, statements);
2348+ auto next = NextNode (newIt, statements);
23402349 auto nextTextRange = next->GetTextRange ();
23412350
23422351 auto nextTextLine = _parser->GetLine (nextTextRange.StartOffset );
@@ -2394,9 +2403,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<Lu
23942403 case LuaAstNodeType::AssignStatement:
23952404 case LuaAstNodeType::LocalStatement:
23962405 {
2397- if (nextMatch (it, LuaAstNodeType::AssignStatement, statements)
2398- || nextMatch (it, LuaAstNodeType::LocalStatement, statements)
2399- || nextMatch (it, LuaAstNodeType::Comment, statements))
2406+ if (NextMatch (it, LuaAstNodeType::AssignStatement, statements)
2407+ || NextMatch (it, LuaAstNodeType::LocalStatement, statements)
2408+ || NextMatch (it, LuaAstNodeType::Comment, statements))
24002409 {
24012410 indentEnv->AddChild (FormatAlignStatement (it, statements));
24022411 }
@@ -2455,9 +2464,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<Lu
24552464 case LuaAstNodeType::ExpressionStatement:
24562465 {
24572466 auto statEnv = FormatNode (statement);
2458- if (nextMatch (it, LuaAstNodeType::Comment, statements))
2467+ if (NextMatch (it, LuaAstNodeType::Comment, statements))
24592468 {
2460- auto next = nextNode (it, statements);
2469+ auto next = NextNode (it, statements);
24612470 int currentLine = _parser->GetLine (statement->GetTextRange ().EndOffset );
24622471 int nextLine = _parser->GetLine (next->GetTextRange ().StartOffset );
24632472
0 commit comments