Skip to content

Commit 33d3692

Browse files
committed
Impl #9
1 parent ea3ceb2 commit 33d3692

File tree

13 files changed

+165
-29
lines changed

13 files changed

+165
-29
lines changed

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ target_sources(CodeService
5151
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentLayoutElement.cpp
5252
${CodeService_SOURCE_DIR}/src/FormatElement/LongExpressionLayoutElement.cpp
5353
${CodeService_SOURCE_DIR}/src/FormatElement/AlignToFirstElement.cpp
54+
${CodeService_SOURCE_DIR}/src/FormatElement/AlignIfLayoutElement.cpp
5455
${CodeService_SOURCE_DIR}/src/FormatElement/IndentOnLineBreakElement.cpp
5556
${CodeService_SOURCE_DIR}/src/FormatElement/KeepElement.cpp
5657
${CodeService_SOURCE_DIR}/src/FormatElement/PlaceholderElement.cpp
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
2+
#include "CodeService/FormatElement/TextElement.h"
3+
#include "CodeService/FormatElement/KeepBlankElement.h"
4+
5+
AlignIfElement::AlignIfElement()
6+
{
7+
}
8+
9+
FormatElementType AlignIfElement::GetType()
10+
{
11+
return FormatElementType::AlignIfLayoutElement;
12+
}
13+
14+
void AlignIfElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
15+
{
16+
AlignElement(ctx);
17+
18+
FormatElement::Serialize(ctx, selfIt, parent);
19+
}
20+
21+
void AlignIfElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
22+
{
23+
AlignElement(ctx);
24+
25+
FormatElement::Diagnosis(ctx, selfIt, parent);
26+
}
27+
28+
void AlignIfElement::AlignElement(FormatContext& ctx)
29+
{
30+
for (auto it = _children.begin(); it != _children.end(); ++it)
31+
{
32+
auto child = *it;
33+
if (child->GetType() == FormatElementType::TextElement)
34+
{
35+
auto textElement = std::dynamic_pointer_cast<TextElement>(child);
36+
if (textElement->GetText() == "if")
37+
{
38+
++it;
39+
if (it != _children.end())
40+
{
41+
*it = std::make_shared<KeepBlankElement>(5);
42+
continue;
43+
}
44+
}
45+
}
46+
47+
if (child->GetType() == FormatElementType::LongExpressionLayoutElement)
48+
{
49+
AlignConditionExpression(ctx, *child);
50+
}
51+
}
52+
}
53+
54+
void AlignIfElement::AlignConditionExpression(FormatContext& ctx, FormatElement& parent)
55+
{
56+
auto& children = parent.GetChildren();
57+
for (auto it = children.begin(); it != children.end(); ++it)
58+
{
59+
auto child = *it;
60+
61+
if (child->GetType() == FormatElementType::SubExpressionElement)
62+
{
63+
AlignConditionExpression(ctx, *child);
64+
}
65+
else if (child->GetType() == FormatElementType::TextElement)
66+
{
67+
auto textElement = std::dynamic_pointer_cast<TextElement>(child);
68+
auto text = textElement->GetText();
69+
if (text == "and" || text == "or")
70+
{
71+
if (ctx.OnlyEmptyCharBefore(textElement->GetTextRange().StartOffset))
72+
{
73+
++it;
74+
if (it != children.end())
75+
{
76+
*it = std::make_shared<KeepBlankElement>(
77+
text == "and" ? 4 : 5
78+
);
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ FormatContext::IndentState FormatContext::GetLastIndent() const
131131
return _indentStack[_indentStack.size() - 2];
132132
}
133133

134+
bool FormatContext::OnlyEmptyCharBefore(int offset)
135+
{
136+
auto file = _parser->GetLuaFile();
137+
auto source = file->GetSource();
138+
auto line = file->GetLine(offset);
139+
auto start = file->GetOffsetFromPosition(line, 0);
140+
141+
for (; start < offset; start++)
142+
{
143+
auto ch = source[start];
144+
if (ch != '\t' && ch != ' ')
145+
{
146+
return false;
147+
}
148+
}
149+
150+
return true;
151+
}
152+
134153
std::shared_ptr<LuaParser> FormatContext::GetParser()
135154
{
136155
return _parser;

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
290290
configMap.at("table_append_expression_no_space") == "true";
291291
}
292292

293+
if (configMap.count("if_condition_align_with_each_other"))
294+
{
295+
options->if_condition_align_with_each_other =
296+
configMap.at("if_condition_align_with_each_other") == "true";
297+
}
298+
293299
if (configMap.count("end_of_line"))
294300
{
295301
auto lineSeparatorSymbol = configMap.at("end_of_line");

CodeService/src/LuaFormatter.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "CodeService/FormatElement/SerializeContext.h"
1919
#include "CodeService/FormatElement/IndentOnLineBreakElement.h"
2020
#include "CodeService/FormatElement/PlaceholderElement.h"
21+
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
2122
#include "Util/StringUtil.h"
2223

2324
bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
@@ -1029,6 +1030,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRepeatStatement(std::shared_p
10291030
std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<LuaAstNode> ifStatement)
10301031
{
10311032
auto env = std::make_shared<StatementElement>();
1033+
bool canAlignCondition = false;
10321034
auto& children = ifStatement->GetChildren();
10331035

10341036
std::vector<std::shared_ptr<PlaceholderElement>> placeholderExpressions;
@@ -1050,6 +1052,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10501052
{
10511053
env->Add<TextElement>(child);
10521054
env->Add<KeepBlankElement>(1);
1055+
if (_options.if_condition_align_with_each_other && child->GetText() == "elseif")
1056+
{
1057+
canAlignCondition = true;
1058+
}
10531059
}
10541060
else // 然而end是在 FormatNodeAndBlockOrEnd 中完成的
10551061
{
@@ -1060,21 +1066,14 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10601066
}
10611067
case LuaAstNodeType::Expression:
10621068
{
1063-
if (_options.if_condition_align_with_each_other)
1069+
std::shared_ptr<FormatElement> expression = nullptr;
1070+
if (_options.if_condition_no_continuation_indent
1071+
|| _options.if_condition_align_with_each_other)
10641072
{
1065-
placeholderExpressions.push_back(std::make_shared<PlaceholderElement>(env, child));
1066-
env->AddChild(placeholderExpressions.back());
1073+
expression = std::make_shared<LongExpressionLayoutElement>(0);
10671074
}
1068-
else
1069-
{
1070-
std::shared_ptr<FormatElement> expression = nullptr;
1071-
if (_options.if_condition_no_continuation_indent)
1072-
{
1073-
expression = std::make_shared<LongExpressionLayoutElement>(0);
1074-
}
10751075

1076-
env->AddChild(FormatExpression(child, expression));
1077-
}
1076+
env->AddChild(FormatExpression(child, expression));
10781077
env->Add<KeepBlankElement>(1);
10791078
break;
10801079
}
@@ -1086,9 +1085,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10861085
}
10871086
}
10881087

1089-
if (!placeholderExpressions.empty())
1088+
if(canAlignCondition)
10901089
{
1091-
FormatIfConditionAlign(placeholderExpressions);
1090+
auto ifAlignLayout = std::make_shared<AlignIfElement>();
1091+
ifAlignLayout->CopyFrom(env);
1092+
env->Reset();
1093+
env->AddChild(ifAlignLayout);
10921094
}
10931095

10941096
return env;
@@ -2171,20 +2173,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableAppendExpression(std::sh
21712173
}
21722174

21732175
return FormatExpression(expression, env);
2174-
}
2175-
2176-
void LuaFormatter::FormatIfConditionAlign(std::vector<std::shared_ptr<PlaceholderElement>>& placeholders)
2177-
{
2178-
bool canAlign = true;
2179-
for(auto placeholder: placeholders)
2180-
{
2181-
2182-
}
2183-
2184-
2185-
2186-
2187-
21882176
}
21892177

21902178
std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode,

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ max_continuous_line_distance = 2
144144
weak_alignment_rule = true
145145
[{table_append_expression_no_space-eq-true.lua}]
146146
table_append_expression_no_space = true
147+
[{if_condition_align_with_each_other-eq-true.lua}]
148+
if_condition_align_with_each_other = true
147149
[{minLine-eq-1.lua}]
148150
keep_line_after_if_statement = minLine:1
149151
keep_line_after_do_statement = minLine:1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if aaa == 13
2+
and bbb == 123 then
3+
elseif bbb == 123 then
4+
5+
elseif fuck == 131 then
6+
else
7+
8+
end

Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ max_continuous_line_distance = 2
139139
weak_alignment_rule = true
140140
[{table_append_expression_no_space-eq-true.lua}]
141141
table_append_expression_no_space = true
142+
[{if_condition_align_with_each_other-eq-true.lua}]
143+
if_condition_align_with_each_other = true
142144
[{minLine-eq-1.lua}]
143145
keep_line_after_if_statement = minLine:1
144146
keep_line_after_do_statement = minLine:1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if aaa == 13
2+
and bbb == 123 then
3+
elseif bbb == 123 then
4+
5+
elseif fuck == 131 then
6+
else
7+
8+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "FormatElement.h"
4+
5+
class AlignIfElement : public FormatElement
6+
{
7+
public:
8+
AlignIfElement();
9+
FormatElementType GetType() override;
10+
11+
void Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent) override;
12+
void Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent) override;
13+
private:
14+
void AlignElement(FormatContext& ctx);
15+
void AlignConditionExpression(FormatContext& ctx, FormatElement& parent);
16+
};

0 commit comments

Comments
 (0)