Skip to content

Commit 39ad873

Browse files
committed
修复对齐规则下的bug,
1 parent 8d9e13b commit 39ad873

File tree

7 files changed

+31
-16
lines changed

7 files changed

+31
-16
lines changed

CodeService/src/FormatElement/AlignmentLayoutElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int AlignmentLayoutElement::GetAlignOffsetWithWeakRule(FormatContext& ctx)
158158

159159
const auto lastPosition = ctx.GetColumn(lastStatChild->GetTextRange().EndOffset);
160160

161-
if (signPosition - lastPosition >= 2)
161+
if (signPosition - lastPosition > 2)
162162
{
163163
canAlignToSign = true;
164164
}

CodeService/src/LuaFormatter.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,12 +1089,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionStatement(std::shar
10891089
env->AddChild(FormatNode(child));
10901090
break;
10911091
}
1092-
// case LuaAstNodeType::Expression:
1093-
// {
1094-
// FormatExpression(child, env);
1095-
// break;
1096-
// }
1097-
// default 一般只有一个分号
1092+
// case LuaAstNodeType::Expression:
1093+
// {
1094+
// FormatExpression(child, env);
1095+
// break;
1096+
// }
1097+
// default 一般只有一个分号
10981098
default:
10991099
{
11001100
DefaultHandle(child, env);
@@ -1417,6 +1417,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
14171417
if (child->GetText() == "{")
14181418
{
14191419
env->Add<TextElement>(child);
1420+
auto operatorLine = _parser->GetLine(child->GetTextRange().StartOffset);
14201421
auto next = nextNode(it, children);
14211422
if (next)
14221423
{
@@ -1428,7 +1429,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
14281429

14291430
env->Add<KeepElement>(_options.keep_one_space_between_table_and_bracket ? 1 : 0);
14301431
++it;
1431-
env->AddChild(FormatAlignTableField(it, children));
1432+
bool allowAlign = _options.continuous_assign_table_field_align_to_equal_sign
1433+
&& _parser->GetLine((*it)->GetTextRange().StartOffset) != operatorLine;
1434+
env->AddChild(FormatAlignTableField(it, allowAlign ,children));
14321435
env->Add<KeepElement>(_options.keep_one_space_between_table_and_bracket ? 1 : 0);
14331436
}
14341437
}
@@ -1617,6 +1620,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
16171620
}
16181621

16191622
std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::ChildIterator& it,
1623+
bool allowAlignToEq,
16201624
const LuaAstNode::ChildrenContainer& children)
16211625
{
16221626
std::shared_ptr<FormatElement> env = nullptr;
@@ -1657,8 +1661,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
16571661
env->Add<KeepBlankElement>(1);
16581662
env->Add<TextElement>(nextChild);
16591663
}
1660-
else if ((currentChild->GetType() == LuaAstNodeType::TableFieldSep) && nextChild->GetType() ==
1661-
LuaAstNodeType::TableField)
1664+
else if ((currentChild->GetType() == LuaAstNodeType::TableFieldSep)
1665+
&& nextChild->GetType() == LuaAstNodeType::TableField)
16621666
{
16631667
// 此时认为table 不应该考虑对齐到等号
16641668
alignToEq = false;
@@ -1693,7 +1697,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
16931697
}
16941698

16951699
// 认为tableField 可以(但不是必须这样做)按照等号对齐
1696-
if (alignToEq && _options.continuous_assign_table_field_align_to_equal_sign)
1700+
if (alignToEq && allowAlignToEq)
16971701
{
16981702
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>("=");
16991703
alignmentLayoutElement->CopyFrom(env);
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
21
local t = 131
32
local t2 = 2
43
local t3 = 4
4+
5+
6+
local t4 = { aaa= 123 }
7+
8+
p { aaa= 123,
9+
aa = 123 }

Test/test_script/format_text/wait_format_by_option_should_be/keep_one_space_between_table_and_bracket-eq-false.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ local t2 = {
55
local t3 = {}
66
local t4 =
77
{aaa,
8-
bbbb,
9-
ccc}
8+
bbbb,
9+
ccc}
1010

1111
local t5 = {aaa,
1212
bbb, ccc, ddd, eee

Test/test_script/format_text/wait_format_by_option_should_be/keep_one_space_between_table_and_bracket-eq-true.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ local t2 = {
55
local t3 = {}
66
local t4 =
77
{ aaa,
8-
bbbb,
9-
ccc }
8+
bbbb,
9+
ccc }
1010

1111
local t5 = { aaa,
1212
bbb, ccc, ddd, eee
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
local t = 131
22
local t2 = 2
33
local t3 = 4
4+
5+
local t4 = { aaa = 123 }
6+
7+
p { aaa = 123,
8+
aa = 123 }

include/CodeService/LuaFormatter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class LuaFormatter
9999
const LuaAstNode::ChildrenContainer& children);
100100

101101
std::shared_ptr<FormatElement> FormatAlignTableField(LuaAstNode::ChildIterator& it,
102+
bool allowAlignToEq,
102103
const LuaAstNode::ChildrenContainer& children);
103104
//意思其实是格式化任意节点加上Block和可选的接受一个end
104105
std::shared_ptr<FormatElement> FormatNodeAndBlockOrEnd(LuaAstNode::ChildIterator& it,

0 commit comments

Comments
 (0)