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
2877void 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+
0 commit comments