Skip to content

Commit 5e2218b

Browse files
committed
修改函数调用时对齐到首个参数的行为,实现local语句对齐到首个表达式
1 parent a8d6f88 commit 5e2218b

File tree

6 files changed

+107
-54
lines changed

6 files changed

+107
-54
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
[*]
33

44
# ReSharper properties
5-
resharper_namespace_indentation = none
5+
resharper_namespace_indentation=none
6+
indent_style=tab
7+
tab_width=4
8+

CodeService/src/FormatElement/AlignmentLayoutElement.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ std::shared_ptr<FormatElement> FindLastValidChild(FormatElement::ChildIterator i
1616
return nullptr;
1717
}
1818

19+
AlignmentLayoutElement::AlignmentLayoutElement(std::string_view alignSign)
20+
: _alignSign(alignSign)
21+
{
22+
}
1923

2024
FormatElementType AlignmentLayoutElement::GetType()
2125
{
@@ -25,11 +29,11 @@ FormatElementType AlignmentLayoutElement::GetType()
2529
void AlignmentLayoutElement::Serialize(SerializeContext& ctx, ChildIterator selfIt,
2630
FormatElement& parent)
2731
{
28-
const auto eqPosition = GetAlignPosition(ctx.GetParser());
32+
const auto alignSignPosition = GetAlignPosition(ctx);
2933

30-
if (eqPosition != -1)
34+
if (alignSignPosition != -1)
3135
{
32-
return AlignmentSerialize(ctx, selfIt, eqPosition, parent);
36+
return AlignmentSerialize(ctx, selfIt, alignSignPosition, parent);
3337
}
3438
else
3539
{
@@ -40,7 +44,7 @@ void AlignmentLayoutElement::Serialize(SerializeContext& ctx, ChildIterator self
4044
void AlignmentLayoutElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt,
4145
FormatElement& parent)
4246
{
43-
const auto eqPosition = GetAlignPosition(ctx.GetParser());
47+
const auto eqPosition = GetAlignPosition(ctx);
4448

4549
if (eqPosition != -1)
4650
{
@@ -52,10 +56,11 @@ void AlignmentLayoutElement::Diagnosis(DiagnosisContext& ctx, ChildIterator self
5256
}
5357
}
5458

55-
int AlignmentLayoutElement::GetAlignPosition(std::shared_ptr<LuaParser> luaParser)
59+
int AlignmentLayoutElement::GetAlignPosition(FormatContext& ctx)
5660
{
57-
int eqAlignedPosition = 0;
58-
bool firstContainEq = true;
61+
auto indentState = ctx.GetCurrentIndent();
62+
int alignSignOffset = 0;
63+
bool firstContainAlignSign = true;
5964
// 先寻找等号对齐的位置,并且判断连续的带等号的语句是否应该对齐到等号
6065
// 连续的带等号的语句是否应该对齐到等号,这个行为应该由连续语句的首行决定
6166
// 如果被子节点内的其他语句共同决定则很难将连续对齐还原为普通排版
@@ -71,39 +76,39 @@ int AlignmentLayoutElement::GetAlignPosition(std::shared_ptr<LuaParser> luaParse
7176
if (textChild->GetType() == FormatElementType::TextElement)
7277
{
7378
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
74-
if (textElement->GetText() == "=")
79+
if (textElement->GetText() == _alignSign)
7580
{
76-
const auto eqPosition = luaParser->GetColumn(textElement->GetTextRange().StartOffset);
77-
if (firstContainEq && it != statementChildren.begin())
81+
const auto signPosition = ctx.GetColumn(textElement->GetTextRange().StartOffset);
82+
if (firstContainAlignSign && it != statementChildren.begin())
7883
{
79-
firstContainEq = false;
84+
firstContainAlignSign = false;
8085
auto lastStatChild = FindLastValidChild(it, statementChildren);
8186
if (lastStatChild == nullptr)
8287
{
8388
return -1;
8489
}
8590

86-
const auto lastPosition = luaParser->GetColumn(lastStatChild->GetTextRange().EndOffset);
91+
const auto lastPosition = ctx.GetColumn(lastStatChild->GetTextRange().EndOffset);
8792

88-
if (eqPosition - lastPosition <= 2)
93+
if (signPosition - lastPosition <= 2)
8994
{
9095
return -1;
9196
}
9297
}
9398

94-
if (eqAlignedPosition < eqPosition)
95-
{
96-
eqAlignedPosition = eqPosition;
97-
}
99+
100+
alignSignOffset = std::max(alignSignOffset,
101+
signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset)
102+
);
98103
}
99104
}
100105
}
101106
}
102107

103-
return eqAlignedPosition;
108+
return alignSignOffset + indentState.SpaceIndent + indentState.TabIndent;
104109
}
105110

106-
void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int eqPosition,
111+
void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int alignSignPosition,
107112
FormatElement& parent)
108113
{
109114
for (const auto& statChild : _children)
@@ -116,12 +121,12 @@ void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIter
116121
if (textChild->GetType() == FormatElementType::TextElement)
117122
{
118123
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
119-
if (textElement->GetText() == "=" && it != statChildren.begin())
124+
if (textElement->GetText() == _alignSign && it != statChildren.begin())
120125
{
121126
auto lastIt = it;
122127
--lastIt;
123128
// 将控制元素变更为对齐元素
124-
*lastIt = std::make_shared<AlignmentElement>(eqPosition);
129+
*lastIt = std::make_shared<AlignmentElement>(alignSignPosition);
125130
break;
126131
}
127132
}
@@ -133,7 +138,7 @@ void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIter
133138

134139
void AlignmentLayoutElement::AlignmentDiagnosis(DiagnosisContext& ctx,
135140
ChildIterator selfIt,
136-
int eqPosition, FormatElement& parent)
141+
int alignSignPosition, FormatElement& parent)
137142
{
138143
for (const auto& statChild : _children)
139144
{
@@ -145,12 +150,12 @@ void AlignmentLayoutElement::AlignmentDiagnosis(DiagnosisContext& ctx,
145150
if (textChild->GetType() == FormatElementType::TextElement)
146151
{
147152
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
148-
if (textElement->GetText() == "=" && it != statChildren.begin())
153+
if (textElement->GetText() == _alignSign && it != statChildren.begin())
149154
{
150155
auto lastIt = it;
151156
--lastIt;
152157
// 将控制元素变更为对齐元素
153-
*lastIt = std::make_shared<AlignmentElement>(eqPosition);
158+
*lastIt = std::make_shared<AlignmentElement>(alignSignPosition);
154159
break;
155160
}
156161
}

CodeService/src/LuaFormatter.cpp

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,32 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatLocalStatement(std::shared_pt
406406
}
407407
case LuaAstNodeType::ExpressionList:
408408
{
409-
env->AddChild(FormatNode(node));
409+
std::shared_ptr<FormatElement> layout = nullptr;
410+
if (_options.local_assign_continuation_align_to_first_expression)
411+
{
412+
bool canAligned = true;
413+
// 但是如果表达式列表中出现跨行表达式,则采用长表达式对齐
414+
for (auto& expression : node->GetChildren())
415+
{
416+
if (expression->GetType() == LuaAstNodeType::Expression)
417+
{
418+
auto startLine = _parser->GetLine(expression->GetTextRange().StartOffset);
419+
auto endLine = _parser->GetLine(expression->GetTextRange().EndOffset);
420+
421+
if (startLine != endLine)
422+
{
423+
canAligned = false;
424+
break;
425+
}
426+
}
427+
}
428+
if (canAligned)
429+
{
430+
layout = std::make_shared<AlignToFirstElement>();
431+
}
432+
}
433+
434+
env->AddChild(FormatExpressionList(node, layout));
410435
break;
411436
}
412437
case LuaAstNodeType::Comment:
@@ -522,9 +547,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNameDefList(std::shared_ptr<L
522547
* cccc,eeee
523548
* ,fff
524549
*/
525-
std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionList(std::shared_ptr<LuaAstNode> expressionList)
550+
std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionList(std::shared_ptr<LuaAstNode> expressionList,
551+
std::shared_ptr<FormatElement> env)
526552
{
527-
auto env = std::make_shared<LongExpressionLayoutElement>(_options.continuation_indent_size);
553+
if (env == nullptr)
554+
{
555+
env = std::make_shared<LongExpressionLayoutElement>(_options.continuation_indent_size);
556+
}
528557

529558
for (auto& node : expressionList->GetChildren())
530559
{
@@ -1033,12 +1062,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionStatement(std::shar
10331062
env->AddChild(FormatNode(child));
10341063
break;
10351064
}
1036-
// case LuaAstNodeType::Expression:
1037-
// {
1038-
// FormatExpression(child, env);
1039-
// break;
1040-
// }
1041-
// default 一般只有一个分号
1065+
// case LuaAstNodeType::Expression:
1066+
// {
1067+
// FormatExpression(child, env);
1068+
// break;
1069+
// }
1070+
// default 一般只有一个分号
10421071
default:
10431072
{
10441073
DefaultHandle(child, env);
@@ -1065,22 +1094,34 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
10651094
}
10661095
case LuaAstNodeType::ExpressionList:
10671096
{
1068-
auto exprListEnv = FormatNode(child);
1097+
std::shared_ptr<FormatElement> layout = nullptr;
10691098
if (_options.align_call_args)
10701099
{
1071-
auto alignToFirstEnv = std::make_shared<AlignToFirstElement>();
1072-
alignToFirstEnv->AddChildren(exprListEnv->GetChildren());
1073-
env->AddChild(alignToFirstEnv);
1074-
}
1075-
else
1076-
{
1077-
auto& exprListChildren = exprListEnv->GetChildren();
1078-
auto keepElement = std::make_shared<KeepElement>(0);
1079-
exprListChildren.insert(exprListChildren.begin(), keepElement);
1100+
bool canAligned = true;
1101+
// 但是如果表达式列表中出现跨行表达式,则采用长表达式对齐
1102+
for (auto& expression : child->GetChildren())
1103+
{
1104+
if (expression->GetType() == LuaAstNodeType::Expression)
1105+
{
1106+
auto startLine = _parser->GetLine(expression->GetTextRange().StartOffset);
1107+
auto endLine = _parser->GetLine(expression->GetTextRange().EndOffset);
1108+
1109+
if (startLine != endLine)
1110+
{
1111+
canAligned = false;
1112+
break;
1113+
}
1114+
}
1115+
}
10801116

1081-
env->AddChild(exprListEnv);
1117+
if (canAligned)
1118+
{
1119+
layout = std::make_shared<AlignToFirstElement>();
1120+
}
10821121
}
10831122

1123+
env->AddChild(FormatExpressionList(child, layout));
1124+
10841125
if (_options.keep_one_space_between_call_args_and_parentheses)
10851126
{
10861127
env->Add<KeepElement>(1);
@@ -1434,7 +1475,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
14341475
std::shared_ptr<FormatElement> env = nullptr;
14351476
if (_options.continuous_assign_statement_align_to_equal_sign)
14361477
{
1437-
env = std::make_shared<AlignmentLayoutElement>();
1478+
env = std::make_shared<AlignmentLayoutElement>("=");
14381479
}
14391480
else
14401481
{
@@ -1596,7 +1637,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
15961637
// 认为tableField 可以(但不是必须这样做)按照等号对齐
15971638
if (alignToEq && _options.continuous_assign_table_field_align_to_equal_sign)
15981639
{
1599-
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>();
1640+
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>("=");
16001641
alignmentLayoutElement->CopyFrom(env);
16011642
env->Reset();
16021643
env->AddChild(alignmentLayoutElement);

include/CodeService/FormatElement/AlignmentLayoutElement.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
class AlignmentLayoutElement : public FormatElement
66
{
77
public:
8+
AlignmentLayoutElement(std::string_view alignSign);
89
FormatElementType GetType() override;
910

1011
void Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent) override;
1112
void Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent) override;
1213
private:
13-
int GetAlignPosition(std::shared_ptr<LuaParser> luaParser);
14-
void AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int eqPosition,
15-
FormatElement& parent);
16-
void AlignmentDiagnosis(DiagnosisContext& ctx, ChildIterator selfIt, int eqPosition,
17-
FormatElement& parent);
14+
int GetAlignPosition(FormatContext& ctx);
15+
void AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int alignSignPosition,
16+
FormatElement& parent);
17+
void AlignmentDiagnosis(DiagnosisContext& ctx, ChildIterator selfIt, int alignSignPosition,
18+
FormatElement& parent);
19+
std::string _alignSign;
1820
};

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ class LuaCodeStyleOptions
3434
/*
3535
* tab的width
3636
*/
37-
int tab_width = 8;
37+
int tab_width = 4;
3838

3939
/*
4040
* 延续行缩进
4141
*/
4242
int continuation_indent_size = 4;
4343

44+
bool local_assign_continuation_align_to_first_expression = true;
4445
/*
4546
* 调用参数对齐到第一个参数,经过实际体验这种风格在vscode上会因为垂直对齐线的标注而显得极为难看
4647
*

include/CodeService/LuaFormatter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class LuaFormatter
3434

3535
std::shared_ptr<FormatElement> FormatNameDefList(std::shared_ptr<LuaAstNode> nameDefList);
3636

37-
std::shared_ptr<FormatElement> FormatExpressionList(std::shared_ptr<LuaAstNode> expressionList);
37+
std::shared_ptr<FormatElement> FormatExpressionList(std::shared_ptr<LuaAstNode> expressionList,
38+
std::shared_ptr<FormatElement> env = nullptr);
3839

3940
std::shared_ptr<FormatElement> FormatAssignLeftExpressionList(std::shared_ptr<LuaAstNode> expressionList);
4041

0 commit comments

Comments
 (0)