@@ -12,94 +12,48 @@ FormatContext::~FormatContext()
1212{
1313}
1414
15- void FormatContext::Print (TextElement& textElement )
15+ void FormatContext::AddIndent ( )
1616{
17- auto & indentState = _indentStack.top ();
18- if (static_cast <int >(_characterCount) < indentState.Indent )
17+ if (_options.indent_style == IndentStyle::Space)
1918 {
20- PrintIndent (indentState. Indent - static_cast < int >(_characterCount), indentState. IndentString );
21- }
22- _os << textElement. GetText ( );
23- _characterCount += textElement. GetText (). size () ;
24- }
19+ if (_indentStack. empty ())
20+ {
21+ _indentStack. push_back ({ 0 , IndentStyle::Space} );
22+ return ;
23+ }
2524
26- void FormatContext::PrintLine (int line)
27- {
28- for (int i = 0 ; i < line; i++)
29- {
30- _os << _options.end_of_line ;
31- _characterCount = 0 ;
32- }
33- }
25+ auto & topIndent = _indentStack.back ();
26+ std::size_t newIndent = _options.indent_size + topIndent.Indent ;
3427
35- void FormatContext::PrintBlank ( int blank)
36- {
37- for ( int i = 0 ; i < blank; i ++)
28+ _indentStack. push_back ({newIndent, IndentStyle::Space});
29+ }
30+ else
3831 {
39- _os << ' ' ;
40- _characterCount++;
32+ if (_indentStack.empty ())
33+ {
34+ _indentStack.push_back ({0 , IndentStyle::Tab});
35+ return ;
36+ }
37+
38+ auto & topIndent = _indentStack.back ();
39+
40+ _indentStack.push_back ({1 + topIndent.Indent , IndentStyle::Tab});
4141 }
4242}
4343
44- void FormatContext::PrintIndent ( int indent, const std::string& indentString )
44+ void FormatContext::AddIndent ( std::size_t specialIndent, IndentStyle style )
4545{
46- for (int i = 0 ; i < indent; i++)
47- {
48- _os << indentString;
49- _characterCount += indentString.size ();
50- }
46+ _indentStack.push_back ({specialIndent, style});
5147}
5248
53- void FormatContext::AddIndent ( int specialIndent )
49+ void FormatContext::RecoverIndent ( )
5450{
55- if (_options.indent_style == IndentStyle::Space)
56- {
57- int newIndent = 0 ;
58- if (specialIndent == -1 )
59- {
60- if (_indentStack.empty ())
61- {
62- _indentStack.push ({0 , " " });
63- return ;
64- }
65-
66- auto & topIndent = _indentStack.top ();
67- newIndent = _options.indent_size + topIndent.Indent ;
68- }
69- else
70- {
71- newIndent = specialIndent;
72- }
73- _indentStack.push ({newIndent, " " });
74- }
75- else // 这个算法可能会有问题
51+ if (_indentStack.empty ())
7652 {
77- int newIndent = 0 ;
78- if (specialIndent == -1 )
79- {
80- if (_indentStack.empty ())
81- {
82- _indentStack.push ({0 , " " });
83- return ;
84- }
85-
86- auto & topIndent = _indentStack.top ();
87- // 一次只增一个\t
88- newIndent = 1 + topIndent.Indent ;
89- }
90- else
91- {
92- // 我会认为存在一个换算
93- // 当你制定了一个缩进,则我会认为保底有一个缩进
94- newIndent = std::max (1 , specialIndent / _options.tab_width );
95- }
96- _indentStack.push ({newIndent, " \t " });
53+ return ;
9754 }
98- }
9955
100- void FormatContext::RecoverIndent ()
101- {
102- _indentStack.pop ();
56+ _indentStack.resize (_indentStack.size () - 1 );
10357}
10458
10559int FormatContext::GetLine (int offset)
@@ -124,15 +78,25 @@ std::size_t FormatContext::GetCurrentIndent() const
12478 return 0 ;
12579 }
12680
127- return _indentStack.top ().Indent ;
81+ return _indentStack.back ().Indent ;
12882}
12983
130- std::string FormatContext::GetText ()
84+ std::size_t FormatContext::GetLastIndent () const
13185{
132- return _os.str ();
86+ if (_indentStack.size () < 2 )
87+ {
88+ return 0 ;
89+ }
90+
91+ return _indentStack[_indentStack.size () - 2 ].Indent ;
13392}
13493
13594std::shared_ptr<LuaParser> FormatContext::GetParser ()
13695{
13796 return _parser;
13897}
98+
99+ LuaCodeStyleOptions& FormatContext::GetOptions ()
100+ {
101+ return _options;
102+ }
0 commit comments