Skip to content

Commit 3d06311

Browse files
committed
resolve #12
1 parent ddc8c39 commit 3d06311

File tree

9 files changed

+149
-29
lines changed

9 files changed

+149
-29
lines changed

CodeService/src/FormatElement/AlignIfLayoutElement.cpp

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
1+
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
22
#include "CodeService/FormatElement/TextElement.h"
33
#include "CodeService/FormatElement/KeepBlankElement.h"
44

@@ -23,10 +23,60 @@ void AlignIfElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, Form
2323
AlignElement(ctx);
2424

2525
FormatElement::Diagnosis(ctx, selfIt, parent);
26+
// WeakDiagnosis(ctx, selfIt, parent);
27+
}
28+
29+
bool AlignIfElement::ElseifFounded()
30+
{
31+
for (auto it = _children.begin(); it != _children.end(); ++it)
32+
{
33+
auto child = *it;
34+
if (child->GetType() == FormatElementType::TextElement)
35+
{
36+
auto textElement = std::dynamic_pointer_cast<TextElement>(child);
37+
if (textElement->GetText() == "elseif")
38+
{
39+
return true;
40+
}
41+
}
42+
}
43+
return false;
44+
}
45+
46+
bool AlignIfElement::OnlyEmptyCharBeforeAnd(FormatContext& ctx, FormatElement& expressionLayout)
47+
{
48+
auto& children = expressionLayout.GetChildren();
49+
for (auto it = children.begin(); it != children.end(); ++it)
50+
{
51+
auto child = *it;
52+
53+
if (child->GetType() == FormatElementType::SubExpressionElement)
54+
{
55+
if (OnlyEmptyCharBeforeAnd(ctx, *child))
56+
{
57+
return true;
58+
}
59+
}
60+
else if (child->GetType() == FormatElementType::TextElement)
61+
{
62+
auto textElement = std::dynamic_pointer_cast<TextElement>(child);
63+
auto text = textElement->GetText();
64+
if (text == "and")
65+
{
66+
if (ctx.OnlyEmptyCharBefore(textElement->GetTextRange().StartOffset))
67+
{
68+
return true;
69+
}
70+
}
71+
}
72+
}
73+
74+
return false;
2675
}
2776

2877
void AlignIfElement::AlignElement(FormatContext& ctx)
2978
{
79+
bool elseIfFounded = ElseifFounded();
3080
for (auto it = _children.begin(); it != _children.end(); ++it)
3181
{
3282
auto child = *it;
@@ -36,22 +86,57 @@ void AlignIfElement::AlignElement(FormatContext& ctx)
3686
if (textElement->GetText() == "if")
3787
{
3888
++it;
39-
if (it != _children.end())
89+
auto controlIt = it;
90+
if (controlIt == _children.end())
4091
{
41-
*it = std::make_shared<KeepBlankElement>(5);
4292
continue;
4393
}
94+
95+
++it;
96+
if (it != _children.end()
97+
&& (*it)->GetType() == FormatElementType::LongExpressionLayoutElement
98+
&& (*controlIt)->GetType() == FormatElementType::KeepBlankElement)
99+
{
100+
auto expressionLayout = *it;
101+
if (elseIfFounded)
102+
{
103+
int ifOffset = child->GetTextRange().EndOffset;
104+
int expressionOffset = expressionLayout->GetTextRange().StartOffset;
105+
106+
int spaceAfterIf = expressionOffset - ifOffset - 1;
107+
if(spaceAfterIf == 2 || spaceAfterIf == 1)
108+
{
109+
*controlIt = std::make_shared<KeepBlankElement>(2);
110+
AlignConditionExpression(ctx, *expressionLayout, 4);
111+
}
112+
else {
113+
// if(expressionOffset - ifOffset >= )
114+
// if<5 space>condition
115+
// elseif<1 space>condition
116+
*controlIt = std::make_shared<KeepBlankElement>(5);
117+
AlignConditionExpression(ctx, *expressionLayout, 7);
118+
}
119+
}
120+
else if (OnlyEmptyCharBeforeAnd(ctx, *expressionLayout))
121+
{
122+
// if<2 space>condition
123+
// and<1 space>condition
124+
*controlIt = std::make_shared<KeepBlankElement>(2);
125+
AlignConditionExpression(ctx, *expressionLayout, 4);
126+
}
127+
}
44128
}
45129
}
46130

47131
if (child->GetType() == FormatElementType::LongExpressionLayoutElement)
48132
{
49-
AlignConditionExpression(ctx, *child);
133+
AlignConditionExpression(ctx, *child, 7);
50134
}
51135
}
52136
}
53137

54-
void AlignIfElement::AlignConditionExpression(FormatContext& ctx, FormatElement& parent)
138+
void AlignIfElement::AlignConditionExpression(FormatContext& ctx, FormatElement& parent,
139+
int spacePositionAfterIndent)
55140
{
56141
auto& children = parent.GetChildren();
57142
for (auto it = children.begin(); it != children.end(); ++it)
@@ -60,7 +145,7 @@ void AlignIfElement::AlignConditionExpression(FormatContext& ctx, FormatElement&
60145

61146
if (child->GetType() == FormatElementType::SubExpressionElement)
62147
{
63-
AlignConditionExpression(ctx, *child);
148+
AlignConditionExpression(ctx, *child, spacePositionAfterIndent);
64149
}
65150
else if (child->GetType() == FormatElementType::TextElement)
66151
{
@@ -74,11 +159,12 @@ void AlignIfElement::AlignConditionExpression(FormatContext& ctx, FormatElement&
74159
if (it != children.end())
75160
{
76161
*it = std::make_shared<KeepBlankElement>(
77-
text == "and" ? 4 : 5
162+
static_cast<int>(spacePositionAfterIndent - text.size())
78163
);
79164
}
80165
}
81166
}
82167
}
83168
}
84169
}
170+

CodeService/src/LuaFormatter.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRepeatStatement(std::shared_p
10301030
std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<LuaAstNode> ifStatement)
10311031
{
10321032
auto env = std::make_shared<StatementElement>();
1033-
bool canAlignCondition = false;
10341033
auto& children = ifStatement->GetChildren();
10351034

10361035
std::vector<std::shared_ptr<PlaceholderElement>> placeholderExpressions;
@@ -1052,10 +1051,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10521051
{
10531052
env->Add<TextElement>(child);
10541053
env->Add<KeepBlankElement>(1);
1055-
if (_options.if_condition_align_with_each_other && child->GetText() == "elseif")
1056-
{
1057-
canAlignCondition = true;
1058-
}
10591054
}
10601055
else // 然而end是在 FormatNodeAndBlockOrEnd 中完成的
10611056
{
@@ -1085,7 +1080,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10851080
}
10861081
}
10871082

1088-
if(canAlignCondition)
1083+
if(_options.if_condition_align_with_each_other)
10891084
{
10901085
auto ifAlignLayout = std::make_shared<AlignIfElement>();
10911086
ifAlignLayout->CopyFrom(env);

Test/test_script/format_text/wait_format_by_option/if_condition_align_with_each_other-eq-true.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,25 @@ if aaa == 13
22
and bbb == 123 then
33
elseif bbb == 123 then
44

5-
elseif fuck == 131 then
5+
elseif fff == 131 then
6+
else
7+
8+
end
9+
10+
if aaa == 13
11+
and bbb == 123 then
12+
elseif bbb == 123 then
13+
14+
elseif fff == 131 then
15+
else
16+
17+
end
18+
19+
if aaa == 13
20+
and bbb == 123 then
21+
elseif bbb == 123 then
22+
23+
elseif fff == 131 then
624
else
725

826
end
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
if aaa == 13
2+
and bbb == 123 then
3+
elseif bbb == 123 then
4+
5+
elseif fff == 131 then
6+
else
7+
8+
end
9+
10+
if aaa == 13
11+
and bbb == 123 then
12+
elseif bbb == 123 then
13+
14+
elseif fff == 131 then
15+
else
16+
17+
end
18+
119
if aaa == 13
220
and bbb == 123 then
321
elseif bbb == 123 then
422

5-
elseif fuck == 131 then
23+
elseif fff == 131 then
624
else
725

826
end

docs/format_action.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ end
116116
```
117117
会被格式化为:
118118
```lua
119-
if aa.isDDDD()
120-
and bb == fwfwfw
121-
or hi == 123 then
119+
if aa.isDDDD()
120+
and bb == fwfwfw
121+
or hi == 123 then
122122
print(1313)
123123
elseif cc == 123
124124
or dd == 13131 and ddd == 123 then

docs/format_action_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ end
117117
```
118118
will be formatted as:
119119
```lua
120-
if aa.isDDDD()
121-
and bb == fwfwfw
122-
or hi == 123 then
120+
if aa.isDDDD()
121+
and bb == fwfwfw
122+
or hi == 123 then
123123
print(1313)
124124
elseif cc == 123
125125
or dd == 13131 and ddd == 123 then

docs/format_config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ end
485485
```
486486
会被格式化为:
487487
```lua
488-
if aa.isDDDD()
489-
and bb == fwfwfw
490-
or hi == 123 then
488+
if aa.isDDDD()
489+
and bb == fwfwfw
490+
or hi == 123 then
491491
print(1313)
492492
elseif cc == 123
493493
or dd == 13131 and ddd == 123 then

docs/format_config_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ end
487487
```
488488
after formatting:
489489
```lua
490-
if aa.isDDDD()
491-
and bb == fwfwfw
492-
or hi == 123 then
490+
if aa.isDDDD()
491+
and bb == fwfwfw
492+
or hi == 123 then
493493
print(1313)
494494
elseif cc == 123
495495
or dd == 13131 and ddd == 123 then

include/CodeService/FormatElement/AlignIfLayoutElement.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#pragma once
1+
#pragma once
22

33
#include "FormatElement.h"
44

@@ -11,6 +11,9 @@ class AlignIfElement : public FormatElement
1111
void Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent) override;
1212
void Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent) override;
1313
private:
14+
bool ElseifFounded();
15+
bool OnlyEmptyCharBeforeAnd(FormatContext& ctx, FormatElement& expressionLayout);
1416
void AlignElement(FormatContext& ctx);
15-
void AlignConditionExpression(FormatContext& ctx, FormatElement& parent);
17+
void AlignConditionExpression(FormatContext& ctx, FormatElement& parent, int spacePositionAfterIndent);
18+
1619
};

0 commit comments

Comments
 (0)