Skip to content

Commit 43befaf

Browse files
committed
FIX #38 处理调用参数的排版问题
1 parent 15de43a commit 43befaf

14 files changed

+348
-35
lines changed

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ target_sources(CodeService
5959
${CodeService_SOURCE_DIR}/src/FormatElement/PlaceholderElement.cpp
6060
${CodeService_SOURCE_DIR}/src/FormatElement/SubExpressionElement.cpp
6161
${CodeService_SOURCE_DIR}/src/FormatElement/MaxLineElement.cpp
62+
${CodeService_SOURCE_DIR}/src/FormatElement/CallArgsListLayoutElement.cpp
6263
${CodeService_SOURCE_DIR}/src/FormatElement/DiagnosisContext.cpp
6364
${CodeService_SOURCE_DIR}/src/FormatElement/RangeFormatContext.cpp
6465
${CodeService_SOURCE_DIR}/src/LuaCodeStyleOptions.cpp
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#include "CodeService/FormatElement/CallArgsListLayoutElement.h"
2+
3+
CallArgsListLayoutElement::CallArgsListLayoutElement()
4+
: _hasLineBreak(false)
5+
{
6+
}
7+
8+
FormatElementType CallArgsListLayoutElement::GetType()
9+
{
10+
return FormatElementType::CallArgsListLayoutElement;
11+
}
12+
13+
void CallArgsListLayoutElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
14+
{
15+
if (_children.empty())
16+
{
17+
return;
18+
}
19+
20+
if (_children.size() == 1
21+
&& (_children.front()->Is(FormatElementType::StringLiteralElement)
22+
|| _children.front()->Is(FormatElementType::ExpressionElement)
23+
))
24+
{
25+
auto node = _children.front();
26+
node->Serialize(ctx, _children.begin(), *this);
27+
return;
28+
}
29+
30+
for (auto it = _children.begin(); it != _children.end(); ++it)
31+
{
32+
if (ctx.GetCharacterCount() == 0 && !_hasLineBreak)
33+
{
34+
_hasLineBreak = true;
35+
ctx.AddIndent();
36+
}
37+
auto arg = *it;
38+
if (arg->Is(FormatElementType::SubExpressionElement))
39+
{
40+
SerializeSubExpression(ctx, arg, true);
41+
}
42+
else
43+
{
44+
arg->Serialize(ctx, it, *this);
45+
}
46+
}
47+
if (_hasLineBreak)
48+
{
49+
ctx.RecoverIndent();
50+
}
51+
}
52+
53+
void CallArgsListLayoutElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
54+
{
55+
if (_children.empty())
56+
{
57+
return;
58+
}
59+
60+
if (_children.size() == 1
61+
&& (_children.front()->Is(FormatElementType::StringLiteralElement)
62+
|| _children.front()->Is(FormatElementType::ExpressionElement)
63+
))
64+
{
65+
auto node = _children.front();
66+
node->Diagnosis(ctx, _children.begin(), *this);
67+
return;
68+
}
69+
70+
for (auto it = _children.begin(); it != _children.end(); ++it)
71+
{
72+
if (ctx.GetCharacterCount() == 0 && !_hasLineBreak)
73+
{
74+
_hasLineBreak = true;
75+
ctx.AddIndent();
76+
}
77+
auto arg = *it;
78+
if (arg->Is(FormatElementType::SubExpressionElement))
79+
{
80+
DiagnoseSubExpression(ctx, arg, true);
81+
}
82+
else
83+
{
84+
arg->Diagnosis(ctx, it, *this);
85+
}
86+
}
87+
if (_hasLineBreak)
88+
{
89+
ctx.RecoverIndent();
90+
}
91+
}
92+
93+
void CallArgsListLayoutElement::SerializeSubExpression(SerializeContext& ctx, std::shared_ptr<FormatElement> parent,
94+
bool topLevelSubexpression)
95+
{
96+
if (parent->GetChildren().empty())
97+
{
98+
return;
99+
}
100+
auto& children = parent->GetChildren();
101+
auto it = children.begin();
102+
103+
if (topLevelSubexpression && _hasLineBreak && IsLastArg(parent))
104+
{
105+
auto expression = *it;
106+
if (expression->Is(FormatElementType::ExpressionElement))
107+
{
108+
auto textRange = expression->GetTextRange();
109+
// 如果跨行
110+
auto startLine = ctx.GetLine(textRange.StartOffset);
111+
if (startLine != ctx.GetLine(textRange.EndOffset))
112+
{
113+
ctx.PrintIndentOnly(startLine);
114+
_hasLineBreak = false;
115+
ctx.RecoverIndent();
116+
}
117+
}
118+
else if (expression->Is(FormatElementType::StringLiteralElement) && IsFirstArg(parent))
119+
{
120+
_hasLineBreak = false;
121+
ctx.RecoverIndent();
122+
}
123+
}
124+
125+
for (; it != children.end(); ++it)
126+
{
127+
auto child = *it;
128+
129+
if (child->Is(FormatElementType::SubExpressionElement))
130+
{
131+
SerializeSubExpression(ctx, child, false);
132+
}
133+
else
134+
{
135+
child->Serialize(ctx, it, *parent);
136+
}
137+
if (child->Is(FormatElementType::KeepElement))
138+
{
139+
if (ctx.GetCharacterCount() == 0 && !_hasLineBreak)
140+
{
141+
_hasLineBreak = true;
142+
ctx.AddIndent();
143+
}
144+
}
145+
}
146+
}
147+
148+
void CallArgsListLayoutElement::DiagnoseSubExpression(DiagnosisContext& ctx, std::shared_ptr<FormatElement> parent,
149+
bool topLevelSubexpression)
150+
{
151+
auto& children = parent->GetChildren();
152+
if (parent->GetChildren().empty())
153+
{
154+
return;
155+
}
156+
auto it = children.begin();
157+
158+
if (topLevelSubexpression && _hasLineBreak && IsLastArg(parent))
159+
{
160+
auto expression = *it;
161+
if (expression->Is(FormatElementType::ExpressionElement))
162+
{
163+
auto textRange = expression->GetTextRange();
164+
// 如果跨行
165+
auto startLine = ctx.GetLine(textRange.StartOffset);
166+
if (startLine != ctx.GetLine(textRange.EndOffset))
167+
{
168+
_hasLineBreak = false;
169+
ctx.RecoverIndent();
170+
}
171+
}
172+
else if (expression->Is(FormatElementType::StringLiteralElement) && IsFirstArg(parent))
173+
{
174+
_hasLineBreak = false;
175+
ctx.RecoverIndent();
176+
}
177+
}
178+
179+
for (; it != children.end(); ++it)
180+
{
181+
auto child = *it;
182+
183+
if (child->Is(FormatElementType::SubExpressionElement))
184+
{
185+
DiagnoseSubExpression(ctx, child, false);
186+
}
187+
else
188+
{
189+
child->Diagnosis(ctx, it, *parent);
190+
}
191+
if (child->Is(FormatElementType::KeepElement))
192+
{
193+
if (ctx.GetCharacterCount() == 0 && !_hasLineBreak)
194+
{
195+
// IndentSubExpression(ctx);
196+
ctx.AddIndent();
197+
_hasLineBreak = true;
198+
}
199+
}
200+
}
201+
}
202+
203+
bool CallArgsListLayoutElement::IsLastArg(std::shared_ptr<FormatElement> node)
204+
{
205+
for (auto it = _children.rbegin(); it != _children.rend(); ++it)
206+
{
207+
if ((*it)->HasValidTextRange())
208+
{
209+
return *it == node;
210+
}
211+
}
212+
213+
return false;
214+
}
215+
216+
bool CallArgsListLayoutElement::IsFirstArg(std::shared_ptr<FormatElement> node)
217+
{
218+
for (auto it = _children.begin(); it != _children.end(); ++it)
219+
{
220+
if ((*it)->HasValidTextRange())
221+
{
222+
return *it == node;
223+
}
224+
}
225+
226+
return false;
227+
}

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void FormatElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, Forma
7878
}
7979
}
8080

81+
bool FormatElement::Is(FormatElementType type)
82+
{
83+
return type == GetType();
84+
}
85+
8186
void FormatElement::Format(SerializeContext& ctx)
8287
{
8388
// workaround

CodeService/src/FormatElement/LongExpressionLayoutElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ void LongExpressionLayoutElement::SerializeSubExpression(SerializeContext& ctx,
3737
{
3838
auto child = *it;
3939

40-
if (child->GetType() == FormatElementType::SubExpressionElement)
40+
if (child->Is(FormatElementType::SubExpressionElement))
4141
{
4242
SerializeSubExpression(ctx, *child);
4343
}
4444
else
4545
{
4646
child->Serialize(ctx, it, parent);
4747
}
48-
if (child->GetType() == FormatElementType::KeepElement)
48+
if (child->Is(FormatElementType::KeepElement))
4949
{
5050
if (ctx.GetCharacterCount() == 0 && !_hasContinuation)
5151
{

CodeService/src/FormatElement/RangeFormatContext.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,40 @@ void RangeFormatContext::Print(char ch, int offset)
113113
}
114114
}
115115

116+
void RangeFormatContext::PrintIndentOnly(int line)
117+
{
118+
if (line > _validRange.EndLine || line < _validRange.StartLine)
119+
{
120+
_inValidRange = false;
121+
return;
122+
}
123+
124+
if (!_indentStack.empty())
125+
{
126+
auto& indentState = _indentStack.back();
127+
if (indentState.Style == IndentStyle::Space)
128+
{
129+
if (_characterCount < indentState.SpaceIndent)
130+
{
131+
PrintIndent(indentState.SpaceIndent - _characterCount, indentState.Style);
132+
}
133+
}
134+
else
135+
{
136+
if (_characterCount == 0)
137+
{
138+
PrintIndent(indentState.TabIndent, indentState.Style);
139+
PrintIndent(indentState.SpaceIndent, IndentStyle::Space);
140+
}
141+
else if (_characterCount >= indentState.TabIndent && (indentState.SpaceIndent + indentState.TabIndent >
142+
_characterCount))
143+
{
144+
PrintIndent(indentState.SpaceIndent - (_characterCount - indentState.TabIndent), IndentStyle::Space);
145+
}
146+
}
147+
}
148+
}
149+
116150
void RangeFormatContext::PrintBlank(int blank)
117151
{
118152
for (int i = 0; i < blank; i++)

CodeService/src/FormatElement/SerializeContext.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,7 @@ SerializeContext::SerializeContext(std::shared_ptr<LuaParser> parser, LuaCodeSty
88

99
void SerializeContext::Print(std::string_view text, TextRange range)
1010
{
11-
if (!_indentStack.empty())
12-
{
13-
auto& indentState = _indentStack.back();
14-
if (indentState.Style == IndentStyle::Space)
15-
{
16-
if (_characterCount < indentState.SpaceIndent)
17-
{
18-
PrintIndent(indentState.SpaceIndent - _characterCount, indentState.Style);
19-
}
20-
}
21-
else
22-
{
23-
if (_characterCount == 0)
24-
{
25-
PrintIndent(indentState.TabIndent, indentState.Style);
26-
PrintIndent(indentState.SpaceIndent, IndentStyle::Space);
27-
}
28-
else if (_characterCount >= indentState.TabIndent && (indentState.SpaceIndent + indentState.TabIndent >
29-
_characterCount))
30-
{
31-
PrintIndent(indentState.SpaceIndent - (_characterCount - indentState.TabIndent), IndentStyle::Space);
32-
}
33-
}
34-
}
11+
PrintIndentOnly();
3512
InnerPrintText(text, range);
3613
}
3714

@@ -96,6 +73,36 @@ std::string SerializeContext::GetText()
9673
return std::move(_buffer);
9774
}
9875

76+
void SerializeContext::PrintIndentOnly(int line)
77+
{
78+
(void)line;
79+
80+
if (!_indentStack.empty())
81+
{
82+
auto& indentState = _indentStack.back();
83+
if (indentState.Style == IndentStyle::Space)
84+
{
85+
if (_characterCount < indentState.SpaceIndent)
86+
{
87+
PrintIndent(indentState.SpaceIndent - _characterCount, indentState.Style);
88+
}
89+
}
90+
else
91+
{
92+
if (_characterCount == 0)
93+
{
94+
PrintIndent(indentState.TabIndent, indentState.Style);
95+
PrintIndent(indentState.SpaceIndent, IndentStyle::Space);
96+
}
97+
else if (_characterCount >= indentState.TabIndent && (indentState.SpaceIndent + indentState.TabIndent >
98+
_characterCount))
99+
{
100+
PrintIndent(indentState.SpaceIndent - (_characterCount - indentState.TabIndent), IndentStyle::Space);
101+
}
102+
}
103+
}
104+
}
105+
99106
void SerializeContext::SetReadySize(std::size_t size)
100107
{
101108
_buffer.reserve(size);

0 commit comments

Comments
 (0)