Skip to content

Commit 4e3155f

Browse files
committed
先上传代码
1 parent 21c2e5f commit 4e3155f

File tree

2 files changed

+98
-82
lines changed

2 files changed

+98
-82
lines changed

CodeService/src/LuaFormatter.cpp

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,19 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
14081408
{
14091409
auto env = std::make_shared<ExpressionElement>();
14101410
auto& children = tableExpression->GetChildren();
1411+
1412+
std::shared_ptr<FormatElement> tableFieldLayout = nullptr;
1413+
if (_options.align_table_field_to_first_field)
1414+
{
1415+
tableFieldLayout = std::make_shared<AlignToFirstElement>();
1416+
}
1417+
else
1418+
{
1419+
tableFieldLayout = std::make_shared<IndentOnLineBreakElement>();
1420+
}
1421+
1422+
int leftBraceLine = 0;
1423+
14111424
for (auto it = children.begin(); it != children.end(); ++it)
14121425
{
14131426
const auto child = *it;
@@ -1418,34 +1431,37 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
14181431
if (child->GetText() == "{")
14191432
{
14201433
env->Add<TextElement>(child);
1421-
auto operatorLine = _parser->GetLine(child->GetTextRange().StartOffset);
1422-
auto next = nextNode(it, children);
1423-
if (next)
1434+
leftBraceLine = _parser->GetLine(child->GetTextRange().EndOffset);
1435+
}
1436+
else if (child->GetText() == "}")
1437+
{
1438+
if (tableFieldLayout->GetChildren().empty())
1439+
{
1440+
env->Add<KeepElement>(0);
1441+
}
1442+
else
14241443
{
1425-
if (next->GetType() == LuaAstNodeType::GeneralOperator)
1426-
{
1427-
env->Add<KeepElement>(0);
1428-
break;
1429-
}
1430-
14311444
env->Add<KeepElement>(_options.keep_one_space_between_table_and_bracket ? 1 : 0);
1432-
++it;
1433-
bool allowAlign = _options.continuous_assign_table_field_align_to_equal_sign
1434-
&& _parser->GetLine((*it)->GetTextRange().StartOffset) != operatorLine;
1435-
env->AddChild(FormatAlignTableField(it, allowAlign, children));
1445+
env->AddChild(tableFieldLayout);
1446+
tableFieldLayout = nullptr;
14361447
env->Add<KeepElement>(_options.keep_one_space_between_table_and_bracket ? 1 : 0);
14371448
}
1438-
}
1439-
else if (child->GetText() == "}")
1440-
{
14411449
env->Add<TextElement>(child);
14421450
}
14431451

14441452
break;
14451453
}
14461454
default:
14471455
{
1448-
DefaultHandle(child, env);
1456+
if (tableFieldLayout)
1457+
{
1458+
tableFieldLayout->AddChild(FormatAlignTableField(it, leftBraceLine, children));
1459+
tableFieldLayout->Add<KeepElement>(1);
1460+
}
1461+
else
1462+
{
1463+
DefaultHandle(child, env);
1464+
}
14491465
}
14501466
}
14511467
}
@@ -1621,92 +1637,93 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
16211637
}
16221638

16231639
std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::ChildIterator& it,
1624-
bool allowAlignToEq,
1625-
const LuaAstNode::ChildrenContainer& children)
1640+
int leftBraceLine,
1641+
const LuaAstNode::ChildrenContainer& siblings)
16261642
{
1627-
std::shared_ptr<FormatElement> env = nullptr;
1628-
if (_options.align_table_field_to_first_field)
1629-
{
1630-
env = std::make_shared<AlignToFirstElement>();
1631-
}
1632-
else
1643+
bool canAlign = true;
1644+
std::shared_ptr<FormatElement> layout = std::make_shared<ExpressionElement>();
1645+
if (leftBraceLine == _parser->GetLine((*it)->GetTextRange().StartOffset))
16331646
{
1634-
env = std::make_shared<IndentOnLineBreakElement>();
1647+
canAlign = false;
16351648
}
16361649

1637-
env->AddChild(FormatNode(*it));
1638-
1639-
auto nextChild = nextNode(it, children);
1640-
if (nextChild == nullptr)
1650+
for (; it != siblings.end(); ++it)
16411651
{
1642-
return env;
1643-
}
1652+
auto current = *it;
1653+
auto nextSibling = nextNode(it, siblings);
16441654

1645-
bool alignToEq = true;
1646-
while (nextChild->GetType() == LuaAstNodeType::TableField
1647-
|| nextChild->GetType() == LuaAstNodeType::TableFieldSep
1648-
|| nextChild->GetType() == LuaAstNodeType::Comment)
1649-
{
1650-
auto currentChild = *it;
1651-
int currentLine = _parser->GetLine(currentChild->GetTextRange().EndOffset);
1652-
int nextLine = _parser->GetLine(nextChild->GetTextRange().StartOffset);
1653-
if (nextLine == currentLine)
1655+
if (nextSibling == nullptr)
16541656
{
1655-
// 检查是否会是内联注释
1656-
// 比如 t = 123, -- inline comment
1657-
// 或者 c = 456 --fff
1658-
// 或者 ddd = 123 --[[ffff]] ,
1659-
if ((currentChild->GetType() == LuaAstNodeType::TableField || currentChild->GetType() ==
1660-
LuaAstNodeType::TableFieldSep) && nextChild->GetType() == LuaAstNodeType::Comment)
1661-
{
1662-
env->Add<KeepBlankElement>(1);
1663-
env->Add<TextElement>(nextChild);
1664-
}
1665-
else if ((currentChild->GetType() == LuaAstNodeType::TableFieldSep)
1666-
&& nextChild->GetType() == LuaAstNodeType::TableField)
1657+
layout->AddChild(FormatNode(current));
1658+
return layout;
1659+
}
1660+
1661+
if (nextSibling->GetType() == LuaAstNodeType::TableField
1662+
|| nextSibling->GetType() == LuaAstNodeType::TableFieldSep
1663+
|| nextSibling->GetType() == LuaAstNodeType::Comment)
1664+
{
1665+
int currentLine = _parser->GetLine(current->GetTextRange().EndOffset);
1666+
int nextLine = _parser->GetLine(nextSibling->GetTextRange().StartOffset);
1667+
1668+
if (nextLine == currentLine)
16671669
{
1668-
// 此时认为table 不应该考虑对齐到等号
1669-
alignToEq = false;
1670-
env->Add<KeepBlankElement>(1);
1671-
env->AddChild(FormatNode(nextChild));
1670+
// 检查是否会是内联注释
1671+
// 比如 t = 123, -- inline comment
1672+
// 或者 c = 456 --fff
1673+
// 或者 ddd = 123 --[[ffff]] ,
1674+
if ((current->GetType() == LuaAstNodeType::TableField
1675+
|| current->GetType() == LuaAstNodeType::TableFieldSep)
1676+
&& nextSibling->GetType() == LuaAstNodeType::Comment)
1677+
{
1678+
layout->AddChild(FormatNode(current));
1679+
layout->Add<KeepBlankElement>(1);
1680+
}
1681+
else if (current->GetType() == LuaAstNodeType::TableFieldSep
1682+
&& nextSibling->GetType() == LuaAstNodeType::TableField)
1683+
{
1684+
canAlign = false;
1685+
layout->AddChild(FormatNode(current));
1686+
// 此时认为table 不应该考虑对齐到等号
1687+
layout->Add<KeepBlankElement>(1);
1688+
}
1689+
else if (current->GetType() == LuaAstNodeType::TableField
1690+
&& nextSibling->GetType() == LuaAstNodeType::TableFieldSep)
1691+
{
1692+
layout->AddChild(FormatNode(current));
1693+
layout->Add<KeepBlankElement>(0);
1694+
}
1695+
else
1696+
{
1697+
layout->AddChild(FormatNode(current));
1698+
layout->Add<KeepElement>(1);
1699+
}
16721700
}
1673-
else if ((currentChild->GetType() == LuaAstNodeType::TableField) && nextChild->GetType() ==
1674-
LuaAstNodeType::TableFieldSep)
1701+
else if (nextLine - currentLine <= _options.max_continuous_line_distance)
16751702
{
1676-
env->AddChild(FormatNode(nextChild));
1703+
layout->AddChild(FormatNode(current));
1704+
layout->Add<KeepElement>(1);
16771705
}
16781706
else
16791707
{
1680-
env->Add<KeepBlankElement>(1);
1681-
env->AddChild(FormatNode(nextChild));
1708+
layout->AddChild(FormatNode(current));
1709+
break;
16821710
}
16831711
}
1684-
else
1685-
{
1686-
env->Add<LineElement>();
1687-
env->Add<KeepLineElement>();
1688-
env->AddChild(FormatNode(nextChild));
1689-
}
1690-
1691-
++it;
1692-
1693-
nextChild = nextNode(it, children);
1694-
if (nextChild == nullptr)
1712+
else if (nextSibling->GetType() == LuaAstNodeType::GeneralOperator)
16951713
{
1714+
layout->AddChild(FormatNode(current));
16961715
break;
16971716
}
16981717
}
16991718

1700-
// 认为tableField 可以(但不是必须这样做)按照等号对齐
1701-
if (alignToEq && allowAlignToEq)
1719+
if (canAlign && _options.continuous_assign_table_field_align_to_equal_sign && layout->GetChildren().size() > 1)
17021720
{
17031721
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>("=");
1704-
alignmentLayoutElement->CopyFrom(env);
1705-
env->Reset();
1706-
env->AddChild(alignmentLayoutElement);
1722+
alignmentLayoutElement->CopyFrom(layout);
1723+
layout = alignmentLayoutElement;
17071724
}
17081725

1709-
return env;
1726+
return layout;
17101727
}
17111728

17121729
std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode::ChildIterator& it,

include/CodeService/LuaFormatter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ class LuaFormatter
9999
const LuaAstNode::ChildrenContainer& children);
100100

101101
std::shared_ptr<FormatElement> FormatAlignTableField(LuaAstNode::ChildIterator& it,
102-
bool allowAlignToEq,
103-
const LuaAstNode::ChildrenContainer& children);
102+
int leftBraceLine,
103+
const LuaAstNode::ChildrenContainer& siblings);
104104
//意思其实是格式化任意节点加上Block和可选的接受一个end
105105
std::shared_ptr<FormatElement> FormatNodeAndBlockOrEnd(LuaAstNode::ChildIterator& it,
106106
bool& singleLineBlock,
@@ -134,4 +134,3 @@ class LuaFormatter
134134
LuaCodeStyleOptions& _options;
135135
std::shared_ptr<FormatElement> _env;
136136
};
137-

0 commit comments

Comments
 (0)