33#include " CodeService/FormatElement/TextElement.h"
44#include " CodeService/FormatElement/AlignmentElement.h"
55
6- int FindLastValidChildIndex (int position, std::vector<std::shared_ptr<FormatElement>>& vec)
6+ std::shared_ptr<FormatElement> FindLastValidChild (FormatElement::ChildIterator it,
7+ FormatElement::ChildContainer& children)
78{
8- for (int index = position - 1 ; position >= 0 ; index-- )
9+ for (auto rIt = std::reverse_iterator< decltype (it)>(it); rIt != children. rend (); ++rIt )
910 {
10- if (vec[index] ->HasValidTextRange ())
11+ if ((*rIt) ->HasValidTextRange ())
1112 {
12- return index ;
13+ return *rIt ;
1314 }
1415 }
15- return - 1 ;
16+ return nullptr ;
1617}
1718
1819
@@ -21,67 +22,68 @@ FormatElementType AlignmentLayoutElement::GetType()
2122 return FormatElementType::AlignmentLayoutElement;
2223}
2324
24- void AlignmentLayoutElement::Serialize (FormatContext& ctx, int position, FormatElement& parent)
25+ void AlignmentLayoutElement::Serialize (FormatContext& ctx, std::optional<FormatElement::ChildIterator> selfIt,
26+ FormatElement& parent)
2527{
26- auto eqPosition = getAlignPosition (ctx.GetParser ());
28+ const auto eqPosition = GetAlignPosition (ctx.GetParser ());
2729
2830 if (eqPosition != -1 )
2931 {
30- return alignmentSerialize (ctx, position, parent, eqPosition );
32+ return AlignmentSerialize (ctx, selfIt, eqPosition, parent );
3133 }
3234 else
3335 {
34- FormatElement::Serialize (ctx, position , parent);
36+ FormatElement::Serialize (ctx, selfIt , parent);
3537 }
3638}
3739
38- void AlignmentLayoutElement::Diagnosis (DiagnosisContext& ctx, int position, FormatElement& parent)
40+ void AlignmentLayoutElement::Diagnosis (DiagnosisContext& ctx, std::optional<FormatElement::ChildIterator> selfIt,
41+ FormatElement& parent)
3942{
40- auto eqPosition = getAlignPosition (ctx.GetParser ());
43+ const auto eqPosition = GetAlignPosition (ctx.GetParser ());
4144
4245 if (eqPosition != -1 )
4346 {
44- return alignmentDiagnosis (ctx, position, parent, eqPosition );
47+ return AlignmentDiagnosis (ctx, selfIt, eqPosition, parent );
4548 }
4649 else
4750 {
48- FormatElement::Diagnosis (ctx, position , parent);
51+ FormatElement::Diagnosis (ctx, selfIt , parent);
4952 }
5053}
5154
52- int AlignmentLayoutElement::getAlignPosition (std::shared_ptr<LuaParser> luaParser)
55+ int AlignmentLayoutElement::GetAlignPosition (std::shared_ptr<LuaParser> luaParser)
5356{
5457 int eqAlignedPosition = 0 ;
5558 bool firstContainEq = true ;
5659 // 先寻找等号对齐的位置,并且判断连续的带等号的语句是否应该对齐到等号
5760 // 连续的带等号的语句是否应该对齐到等号,这个行为应该由连续语句的首行决定
5861 // 如果被子节点内的其他语句共同决定则很难将连续对齐还原为普通排版
59- for (int statIndex = 0 ; statIndex != static_cast < int >( _children.size ()); statIndex++ )
62+ for (auto statIt = _children. begin (); statIt != _children.end (); ++statIt )
6063 {
61- auto statChild = _children[statIndex] ;
64+ const auto statement = *statIt ;
6265
63- auto & statChildren = statChild ->GetChildren ();
66+ auto & statementChildren = statement ->GetChildren ();
6467
65- for (int index = 0 ; index != static_cast < int >(statChildren. size ()); index++ )
68+ for (auto it = statementChildren. begin (); it != statementChildren. end (); ++it )
6669 {
67- auto textChild = statChildren[index] ;
70+ auto textChild = *it ;
6871 if (textChild->GetType () == FormatElementType::TextElement)
6972 {
70- auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
73+ const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
7174 if (textElement->GetText () == " =" )
7275 {
73- int eqPosition = luaParser->GetColumn (textElement->GetTextRange ().StartOffset );
74- if (firstContainEq && index > 0 )
76+ const auto eqPosition = luaParser->GetColumn (textElement->GetTextRange ().StartOffset );
77+ if (firstContainEq && it != statementChildren. begin () )
7578 {
7679 firstContainEq = false ;
77- auto lastValidIndex = FindLastValidChildIndex (index, statChildren );
78- if (lastValidIndex == - 1 )
80+ auto lastStatChild = FindLastValidChild (it, statementChildren );
81+ if (lastStatChild == nullptr )
7982 {
8083 return -1 ;
8184 }
82- auto lastStatChild = statChildren[lastValidIndex];
83- auto lastPosition = luaParser->GetColumn (lastStatChild->GetTextRange ().EndOffset );
8485
86+ const auto lastPosition = luaParser->GetColumn (lastStatChild->GetTextRange ().EndOffset );
8587
8688 if (eqPosition - lastPosition <= 2 )
8789 {
@@ -101,53 +103,59 @@ int AlignmentLayoutElement::getAlignPosition(std::shared_ptr<LuaParser> luaParse
101103 return eqAlignedPosition;
102104}
103105
104- void AlignmentLayoutElement::alignmentSerialize (FormatContext& ctx, int position, FormatElement& parent, int eqPosition)
106+ void AlignmentLayoutElement::AlignmentSerialize (FormatContext& ctx, std::optional<ChildIterator> selfIt, int eqPosition,
107+ FormatElement& parent)
105108{
106- for (auto statChild : _children)
109+ for (const auto & statChild : _children)
107110 {
108111 auto & statChildren = statChild->GetChildren ();
109112
110- for (std:: size_t i = 0 ; i < statChildren.size (); i++ )
113+ for (auto it = statChildren. begin (); it != statChildren.end (); ++it )
111114 {
112- auto textChild = statChildren[i] ;
115+ auto textChild = *it ;
113116 if (textChild->GetType () == FormatElementType::TextElement)
114117 {
115- auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
116- if (textElement->GetText () == " =" && i > 0 )
118+ const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
119+ if (textElement->GetText () == " =" && it != statChildren. begin () )
117120 {
121+ auto lastIt = it;
122+ --lastIt;
118123 // 将控制元素变更为对齐元素
119- statChildren[i - 1 ] = std::make_shared<AlignmentElement>(eqPosition);
124+ *lastIt = std::make_shared<AlignmentElement>(eqPosition);
120125 break ;
121126 }
122127 }
123128 }
124129 }
125130
126- return FormatElement::Serialize (ctx, position , parent);
131+ return FormatElement::Serialize (ctx, selfIt , parent);
127132}
128133
129- void AlignmentLayoutElement::alignmentDiagnosis (DiagnosisContext& ctx, int position, FormatElement& parent,
130- int eqPosition)
134+ void AlignmentLayoutElement::AlignmentDiagnosis (DiagnosisContext& ctx,
135+ std::optional<FormatElement::ChildIterator> selfIt,
136+ int eqPosition, FormatElement& parent)
131137{
132- for (auto statChild : _children)
138+ for (const auto & statChild : _children)
133139 {
134140 auto & statChildren = statChild->GetChildren ();
135141
136- for (std:: size_t i = 0 ; i < statChildren.size (); i++ )
142+ for (auto it = statChildren. begin (); it != statChildren.end (); ++it )
137143 {
138- auto textChild = statChildren[i] ;
144+ auto textChild = *it ;
139145 if (textChild->GetType () == FormatElementType::TextElement)
140146 {
141- auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
142- if (textElement->GetText () == " =" && i > 0 )
147+ const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
148+ if (textElement->GetText () == " =" && it != statChildren. begin () )
143149 {
150+ auto lastIt = it;
151+ --lastIt;
144152 // 将控制元素变更为对齐元素
145- statChildren[i - 1 ] = std::make_shared<AlignmentElement>(eqPosition);
153+ *lastIt = std::make_shared<AlignmentElement>(eqPosition);
146154 break ;
147155 }
148156 }
149157 }
150158 }
151159
152- return FormatElement::Diagnosis (ctx, position , parent);
160+ return FormatElement::Diagnosis (ctx, selfIt , parent);
153161}
0 commit comments