@@ -11,9 +11,9 @@ FormatContext::FormatContext(std::shared_ptr<LuaParser> parser, LuaFormatOptions
1111void FormatContext::Print (std::string_view text)
1212{
1313 auto & indentState = _indentStack.top ();
14- if (_characterCount < indentState.Indent )
14+ if (static_cast < int >( _characterCount) < indentState.Indent )
1515 {
16- PrintBlank (indentState.Indent - static_cast <int >(_characterCount));
16+ PrintIndent (indentState.Indent - static_cast <int >(_characterCount), indentState. IndentString );
1717 }
1818 _os << text;
1919 _characterCount += text.size ();
@@ -26,36 +26,37 @@ void FormatContext::PrintLine(int line)
2626 _os << _options.LineSeperater ;
2727 _characterCount = 0 ;
2828 }
29-
30- if (_indentStack.top ().Type == IndentStateType::ActiveIfLineBreak)
31- {
32- // ¸´ÖÆ
33- auto topIndent = _indentStack.top ();
34- _indentStack.pop ();
35-
36- AddIndent (topIndent.WaitActiveIndent );
37- }
3829}
3930
4031void FormatContext::PrintBlank (int blank)
4132{
4233 for (int i = 0 ; i < blank; i ++)
4334 {
44- _os << " " ;
35+ _os << ' ' ;
4536 _characterCount++;
4637 }
4738}
4839
40+ void FormatContext::PrintIndent (int indent, const std::string& indentString)
41+ {
42+ // 我始终认为使\t来排版就是垃圾
43+ for (int i = 0 ; i < indent; i++)
44+ {
45+ _os << indentString;
46+ _characterCount += indentString.size ();
47+ }
48+ }
49+
4950void FormatContext::AddIndent (int specialIndent, IndentStateType type)
5051{
51- if (type == IndentStateType::Normal )
52+ if (!_options. UseTabIndent )
5253 {
5354 int newIndent = 0 ;
5455 if (specialIndent == -1 )
5556 {
5657 if (_indentStack.empty ())
5758 {
58- _indentStack.push ({0 , 0 , " " , type});
59+ _indentStack.push ({0 , " " , type});
5960 return ;
6061 }
6162
@@ -66,21 +67,31 @@ void FormatContext::AddIndent(int specialIndent, IndentStateType type)
6667 {
6768 newIndent = specialIndent;
6869 }
69- std::string indentString;
70- indentString.reserve (_options.IndentString .size () * newIndent);
71- for (int i = 0 ; i != newIndent; i++)
72- {
73- indentString.append (_options.IndentString );
74- }
75- _indentStack.push ({newIndent, 0 , std::move (indentString), type});
70+ _indentStack.push ({newIndent, " " , type});
7671 }
77- else if (type == IndentStateType::ActiveIfLineBreak)
72+ else // 这个算法可能会有问题
7873 {
79- // ¸´ÖÆ
80- auto topIndent = _indentStack.top ();
81- topIndent.WaitActiveIndent = specialIndent;
82- topIndent.Type = type;
83- _indentStack.push (topIndent);
74+ int newIndent = 0 ;
75+ if (specialIndent == -1 )
76+ {
77+ if (_indentStack.empty ())
78+ {
79+ _indentStack.push ({0 , " " , type});
80+ return ;
81+ }
82+
83+ auto & topIndent = _indentStack.top ();
84+ // 一次只增一个\t
85+ newIndent = 1 + topIndent.Indent ;
86+ }
87+ else
88+ {
89+ // 我会认为存在一个换算
90+ // 当你制定了一个缩进,则我会认为保底有一个缩进
91+ newIndent = std::max (1 , specialIndent / 8 );
92+ }
93+ std::string indentString = " \t " ;
94+ _indentStack.push ({newIndent, " \t " , type});
8495 }
8596}
8697
0 commit comments