Skip to content

Commit 674a0cc

Browse files
committed
更新一点格式化选项
1 parent 4103e79 commit 674a0cc

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ FormatContext::FormatContext(std::shared_ptr<LuaParser> parser, LuaFormatOptions
1111
void 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

4031
void 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+
4950
void 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

include/CodeService/FormatElement/FormatContext.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class FormatContext
1313
enum class IndentStateType
1414
{
1515
Normal,
16-
ActiveIfLineBreak,
16+
// ActiveIfLineBreak,
1717
};
1818

1919
struct IndentState
2020
{
2121
int Indent = 0;
22-
int WaitActiveIndent = 0;
22+
// int WaitActiveIndent = 0;
2323
std::string IndentString = "";
2424
IndentStateType Type = IndentStateType::Normal;
2525
};
@@ -32,6 +32,8 @@ class FormatContext
3232

3333
void PrintBlank(int blank);
3434

35+
void PrintIndent(int indent, const std::string& indentString);
36+
3537
void AddIndent(int specialIndent = -1, IndentStateType type = IndentStateType::Normal);
3638

3739
void RecoverIndent();
@@ -46,9 +48,6 @@ class FormatContext
4648

4749
std::string GetText();
4850

49-
void SetIndentIfLineBreak(int indent);
50-
51-
void ClearIndentIfLineBreak();
5251

5352
private:
5453
std::stack<IndentState, std::vector<IndentState>> _indentStack;

include/CodeService/LuaFormatOptions.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
class LuaFormatOptions
44
{
55
public:
6-
int Indent = 4;
7-
8-
std::string IndentString = " ";
6+
// 我反对这种风格
7+
// 但我依然实现他
8+
bool UseTabIndent = false;
99

10+
int Indent = 4;
11+
// 这只是初始化时的默认选项,在linux上依然可以保持 \r\n
12+
#ifdef _WINDOWS
1013
std::string LineSeperater = "\r\n";
11-
};
14+
#else
15+
std::string LineSeperater = "\r";
16+
#endif
17+
};

0 commit comments

Comments
 (0)