Skip to content

Commit 3363bcf

Browse files
committed
提供新的选项
1 parent c96f884 commit 3363bcf

38 files changed

+194
-24
lines changed

CodeService/src/FormatElement/AlignmentLayoutElement.cpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ void AlignmentLayoutElement::Diagnosis(DiagnosisContext& ctx, ChildIterator self
5959
int AlignmentLayoutElement::GetAlignPosition(FormatContext& ctx)
6060
{
6161
auto indentState = ctx.GetCurrentIndent();
62+
int alignOffset = 0;
63+
64+
if (!ctx.GetOptions().weak_alignment_rule)
65+
{
66+
alignOffset = GetAlignOffset(ctx);
67+
}
68+
else
69+
{
70+
alignOffset = GetAlignOffsetWithWeakRule(ctx);
71+
}
72+
73+
if (alignOffset == -1)
74+
{
75+
return alignOffset;
76+
}
77+
78+
return alignOffset + static_cast<int>(indentState.SpaceIndent + indentState.TabIndent);
79+
}
80+
81+
int AlignmentLayoutElement::GetAlignOffset(FormatContext& ctx)
82+
{
6283
int alignSignOffset = 0;
6384
bool firstContainAlignSign = true;
6485
// 先寻找等号对齐的位置,并且判断连续的带等号的语句是否应该对齐到等号
@@ -98,14 +119,59 @@ int AlignmentLayoutElement::GetAlignPosition(FormatContext& ctx)
98119

99120

100121
alignSignOffset = std::max(alignSignOffset,
101-
signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset)
102-
);
122+
signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset)
123+
);
124+
}
125+
}
126+
}
127+
}
128+
return alignSignOffset;
129+
}
130+
131+
int AlignmentLayoutElement::GetAlignOffsetWithWeakRule(FormatContext& ctx)
132+
{
133+
int alignSignOffset = 0;
134+
bool canAlignToSign = false;
135+
// 先寻找等号对齐的位置,并且判断连续的带等号的语句是否应该对齐到等号
136+
// 连续的带等号的语句是否应该对齐到等号,这个行为由所有连续语句共同决定
137+
for (auto statIt = _children.begin(); statIt != _children.end(); ++statIt)
138+
{
139+
const auto statement = *statIt;
140+
141+
auto& statementChildren = statement->GetChildren();
142+
143+
for (auto it = statementChildren.begin(); it != statementChildren.end(); ++it)
144+
{
145+
auto textChild = *it;
146+
if (textChild->GetType() == FormatElementType::TextElement)
147+
{
148+
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
149+
if (textElement->GetText() == _alignSign)
150+
{
151+
const auto signPosition = ctx.GetColumn(textElement->GetTextRange().StartOffset);
152+
153+
auto lastStatChild = FindLastValidChild(it, statementChildren);
154+
if (lastStatChild == nullptr)
155+
{
156+
return -1;
157+
}
158+
159+
const auto lastPosition = ctx.GetColumn(lastStatChild->GetTextRange().EndOffset);
160+
161+
if (signPosition - lastPosition >= 2)
162+
{
163+
canAlignToSign = true;
164+
}
165+
166+
alignSignOffset = std::max(alignSignOffset,
167+
signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset)
168+
);
103169
}
104170
}
105171
}
106172
}
107173

108-
return alignSignOffset + indentState.SpaceIndent + indentState.TabIndent;
174+
return canAlignToSign ? alignSignOffset : -1;
109175
}
110176

111177
void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int alignSignPosition,

CodeService/src/LuaEditorConfig.cpp

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
#include "CodeService/FormatElement/MinLineElement.h"
1010
#include "CodeService/NameStyle/NameStyleRuleMatcher.h"
1111

12+
bool isNumber(std::string_view source)
13+
{
14+
for (auto c : source)
15+
{
16+
if (c > '9' || c < '0')
17+
{
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
1224
std::shared_ptr<LuaEditorConfig> LuaEditorConfig::LoadFromFile(const std::string& path)
1325
{
1426
std::fstream fin(path, std::ios::in);
@@ -85,14 +97,14 @@ std::shared_ptr<LuaCodeStyleOptions> LuaEditorConfig::Generate(std::string_view
8597
patternKey.append("#").append(sectionPattern);
8698
luaSections.push_back(section);
8799
}
88-
// [{test.lua,lib.lua}]
89-
else if (StringUtil::StartWith(sectionPattern,"{") && StringUtil::EndWith(sectionPattern, "}"))
100+
// [{test.lua,lib.lua}]
101+
else if (StringUtil::StartWith(sectionPattern, "{") && StringUtil::EndWith(sectionPattern, "}"))
90102
{
91103
auto fileListText = sectionPattern.substr(1, sectionPattern.size() - 2);
92104
auto fileList = StringUtil::Split(fileListText, ",");
93105
for (auto fileMatchUri : fileList)
94106
{
95-
if (StringUtil::EndWith(fileUri,StringUtil::TrimSpace(fileMatchUri)))
107+
if (StringUtil::EndWith(fileUri, StringUtil::TrimSpace(fileMatchUri)))
96108
{
97109
patternKey.append("#").append(sectionPattern);
98110
luaSections.push_back(section);
@@ -101,7 +113,7 @@ std::shared_ptr<LuaCodeStyleOptions> LuaEditorConfig::Generate(std::string_view
101113
}
102114
}
103115
#ifndef NOT_SURPPORT_FILE_SYSTEM
104-
// [lib/**.lua]
116+
// [lib/**.lua]
105117
else if (StringUtil::EndWith(sectionPattern, "**.lua"))
106118
{
107119
std::string prefix = sectionPattern.substr(0, sectionPattern.size() - 6);
@@ -111,13 +123,13 @@ std::shared_ptr<LuaCodeStyleOptions> LuaEditorConfig::Generate(std::string_view
111123
std::filesystem::path file(fileUri);
112124
auto dirNormal = dirname.lexically_normal();
113125
auto fileNormal = file.lexically_normal();
114-
if (StringUtil::StartWith(fileNormal.string(),dirNormal.string()))
126+
if (StringUtil::StartWith(fileNormal.string(), dirNormal.string()))
115127
{
116128
patternKey.append("#").append(sectionPattern);
117129
luaSections.push_back(section);
118130
}
119131
}
120-
//[aaa/bbb.lua]
132+
//[aaa/bbb.lua]
121133
else
122134
{
123135
std::filesystem::path workspace(_workspace);
@@ -129,7 +141,6 @@ std::shared_ptr<LuaCodeStyleOptions> LuaEditorConfig::Generate(std::string_view
129141
}
130142
}
131143
#endif
132-
133144
}
134145

135146
if (_optionPatternMap.count(patternKey) > 0)
@@ -174,27 +185,37 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
174185
}
175186
}
176187

177-
if (configMap.count("indent_size"))
188+
if (configMap.count("indent_size")
189+
&& isNumber(configMap.at("indent_size"))
190+
)
178191
{
179192
options->indent_size = std::stoi(configMap.at("indent_size"));
180193
}
181194

182-
if (configMap.count("tab_width"))
195+
if (configMap.count("tab_width")
196+
&& isNumber(configMap.at("tab_width")))
183197
{
184198
options->tab_width = std::stoi(configMap.at("tab_width"));
185199
}
186200

187-
if (configMap.count("continuation_indent_size"))
201+
if (configMap.count("continuation_indent_size")
202+
&& isNumber(configMap.at("continuation_indent_size")))
188203
{
189204
options->continuation_indent_size = std::stoi(configMap.at("continuation_indent_size"));
190205
}
191206

192-
if(configMap.count("local_assign_continuation_align_to_first_expression"))
207+
if (configMap.count("local_assign_continuation_align_to_first_expression"))
193208
{
194209
options->local_assign_continuation_align_to_first_expression =
195210
configMap.at("local_assign_continuation_align_to_first_expression") == "true";
196211
}
197212

213+
if (configMap.count("table_field_continuation_align_to_first_sub_expression"))
214+
{
215+
options->table_field_continuation_align_to_first_sub_expression =
216+
configMap.at("table_field_continuation_align_to_first_sub_expression") == "true";
217+
}
218+
198219
if (configMap.count("align_call_args"))
199220
{
200221
options->align_call_args = configMap.at("align_call_args") == "true";
@@ -212,23 +233,23 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
212233
configMap.at("keep_one_space_between_table_and_bracket") == "true";
213234
}
214235

215-
if(configMap.count("keep_one_space_between_namedef_and_attribute"))
236+
if (configMap.count("keep_one_space_between_namedef_and_attribute"))
216237
{
217238
options->keep_one_space_between_namedef_and_attribute =
218239
configMap.at("keep_one_space_between_namedef_and_attribute") == "true";
219240
}
220241

221-
if(configMap.count("label_no_indent"))
242+
if (configMap.count("label_no_indent"))
222243
{
223244
options->label_no_indent = configMap.at("label_no_indent") == "true";
224245
}
225246

226-
if(configMap.count("do_statement_no_indent"))
247+
if (configMap.count("do_statement_no_indent"))
227248
{
228249
options->do_statement_no_indent = configMap.at("do_statement_no_indent") == "true";
229250
}
230251

231-
if(configMap.count("if_condition_no_continuation_indent"))
252+
if (configMap.count("if_condition_no_continuation_indent"))
232253
{
233254
options->if_condition_no_continuation_indent = configMap.at("if_condition_no_continuation_indent") == "true";
234255
}
@@ -238,6 +259,19 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
238259
options->align_table_field_to_first_field = configMap.at("align_table_field_to_first_field") == "true";
239260
}
240261

262+
if (configMap.count("max_continuous_line_distance")
263+
&& isNumber(configMap.at("max_continuous_line_distance")))
264+
{
265+
options->max_continuous_line_distance =
266+
std::stoi(configMap.at("max_continuous_line_distance"));
267+
}
268+
269+
if(configMap.count("weak_alignment_rule"))
270+
{
271+
options->weak_alignment_rule =
272+
configMap.at("weak_alignment_rule") == "true";
273+
}
274+
241275
if (configMap.count("continuous_assign_statement_align_to_equal_sign"))
242276
{
243277
options->continuous_assign_statement_align_to_equal_sign =
@@ -263,7 +297,8 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
263297
}
264298
}
265299

266-
if (configMap.count("max_line_length"))
300+
if (configMap.count("max_line_length")
301+
&& isNumber(configMap.at("max_line_length")))
267302
{
268303
options->max_line_length = std::stoi(configMap.at("max_line_length"));
269304
}
@@ -340,7 +375,7 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
340375
}
341376
}
342377

343-
if(options->indent_style == IndentStyle::Tab)
378+
if (options->indent_style == IndentStyle::Tab)
344379
{
345380
options->align_table_field_to_first_field = false;
346381
}

CodeService/src/LuaFormatter.cpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,32 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAssignment(std::shared_ptr<Lu
474474
}
475475
else
476476
{
477-
env->AddChild(FormatNode(node));
477+
std::shared_ptr<FormatElement> layout = nullptr;
478+
if (_options.local_assign_continuation_align_to_first_expression)
479+
{
480+
bool canAligned = true;
481+
// 但是如果表达式列表中出现跨行表达式,则采用长表达式对齐
482+
for (auto& expression : node->GetChildren())
483+
{
484+
if (expression->GetType() == LuaAstNodeType::Expression)
485+
{
486+
auto startLine = _parser->GetLine(expression->GetTextRange().StartOffset);
487+
auto endLine = _parser->GetLine(expression->GetTextRange().EndOffset);
488+
489+
if (startLine != endLine)
490+
{
491+
canAligned = false;
492+
break;
493+
}
494+
}
495+
}
496+
if (canAligned)
497+
{
498+
layout = std::make_shared<AlignToFirstElement>();
499+
}
500+
}
501+
502+
env->AddChild(FormatExpressionList(node, layout));
478503
}
479504

480505
break;
@@ -1425,7 +1450,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
14251450
std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<LuaAstNode> tableField)
14261451
{
14271452
auto env = std::make_shared<ExpressionElement>();
1428-
1453+
auto eqSignFounded = false;
14291454
for (auto& child : tableField->GetChildren())
14301455
{
14311456
switch (child->GetType())
@@ -1434,6 +1459,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
14341459
{
14351460
if (child->GetText() == "=")
14361461
{
1462+
eqSignFounded = true;
14371463
env->Add<KeepBlankElement>(1);
14381464
env->Add<TextElement>(child);
14391465
env->Add<KeepBlankElement>(1);
@@ -1455,6 +1481,36 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
14551481
env->Add<KeepElement>(1);
14561482
break;
14571483
}
1484+
case LuaAstNodeType::Expression:
1485+
{
1486+
std::shared_ptr<FormatElement> layout = nullptr;
1487+
if (_options.table_field_continuation_align_to_first_sub_expression && eqSignFounded)
1488+
{
1489+
bool canAligned = true;
1490+
// 但是如果表达式列表中出现跨行表达式,则采用长表达式对齐
1491+
for (auto& expression : child->GetChildren())
1492+
{
1493+
if (expression->GetType() == LuaAstNodeType::Expression)
1494+
{
1495+
auto startLine = _parser->GetLine(expression->GetTextRange().StartOffset);
1496+
auto endLine = _parser->GetLine(expression->GetTextRange().EndOffset);
1497+
1498+
if (startLine != endLine)
1499+
{
1500+
canAligned = false;
1501+
break;
1502+
}
1503+
}
1504+
}
1505+
if (canAligned)
1506+
{
1507+
layout = std::make_shared<AlignToFirstElement>();
1508+
}
1509+
}
1510+
1511+
env->AddChild(FormatExpression(child, layout));
1512+
break;
1513+
}
14581514
default:
14591515
{
14601516
DefaultHandle(child, env);
@@ -1499,7 +1555,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
14991555
int currentLine = _parser->GetLine(currentChild->GetTextRange().EndOffset);
15001556
int nextLine = _parser->GetLine(nextChild->GetTextRange().StartOffset);
15011557
// 这个规则是下一个连续的赋值/local/注释语句如果和上一个赋值/local/注释语句 间距2行以上,则不认为是连续
1502-
if (nextLine - currentLine > 2)
1558+
if (nextLine - currentLine > _options.max_continuous_line_distance)
15031559
{
15041560
break;
15051561
}

Test/test_script/format_text/waitt_format/.editorconfig renamed to Test/test_script/format_text/wait_format/.editorconfig

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Test/test_script/format_text/waitt_format/expression.lua renamed to Test/test_script/format_text/wait_format/expression.lua

File renamed without changes.

Test/test_script/format_text/waitt_format/expressionList.lua renamed to Test/test_script/format_text/wait_format/expressionList.lua

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)