Skip to content

Commit 208a4c5

Browse files
committed
修改缩进算法
1 parent dd3dae0 commit 208a4c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+377
-285
lines changed

CodeService/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ target_sources(CodeService
4343
${CodeService_SOURCE_DIR}/src/FormatElement/KeepBlankElement.cpp
4444
${CodeService_SOURCE_DIR}/src/FormatElement/FormatElement.cpp
4545
${CodeService_SOURCE_DIR}/src/FormatElement/LineElement.cpp
46+
${CodeService_SOURCE_DIR}/src/FormatElement/NoIndentElement.cpp
4647
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentElement.cpp
47-
${CodeService_SOURCE_DIR}/src/FormatElement/ExpressionElement.cpp
48+
${CodeService_SOURCE_DIR}/src/FormatElement/ExpressionElement.cpp
4849
${CodeService_SOURCE_DIR}/src/FormatElement/FormatContext.cpp
50+
${CodeService_SOURCE_DIR}/src/FormatElement/SerializeContext.cpp
4951
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentLayoutElement.cpp
5052
${CodeService_SOURCE_DIR}/src/FormatElement/LongExpressionLayoutElement.cpp
5153
${CodeService_SOURCE_DIR}/src/FormatElement/AlignToFirstElement.cpp
@@ -59,6 +61,7 @@ target_sources(CodeService
5961
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleChecker.cpp
6062
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleRuleMatcher.cpp
6163
${CodeService_SOURCE_DIR}/src/NameStyle/CheckElement.cpp
64+
6265
)
6366

6467
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")

CodeService/src/FormatElement/AlignToFirstElement.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ FormatElementType AlignToFirstElement::GetType()
1010
return FormatElementType::AlignToFirstElement;
1111
}
1212

13-
void AlignToFirstElement::Serialize(FormatContext& ctx, ChildIterator selfIt, FormatElement& parent)
13+
void AlignToFirstElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
1414
{
1515
for (auto it = _children.begin(); it != _children.end(); ++it)
1616
{
@@ -21,27 +21,28 @@ void AlignToFirstElement::Serialize(FormatContext& ctx, ChildIterator selfIt, Fo
2121
auto indentCount = ctx.GetCurrentIndent();
2222
if (writeCount > indentCount)
2323
{
24-
ctx.AddIndent(static_cast<int>(writeCount));
24+
ctx.AddIndent(writeCount, ctx.GetOptions().indent_style);
2525
}
2626
else
2727
{
2828
indentCount += _lowestIndent;
2929
const auto column = ctx.GetColumn(child->GetTextRange().StartOffset);
3030
if (column > static_cast<int>(indentCount))
3131
{
32-
ctx.AddIndent(column);
32+
ctx.AddIndent(column, ctx.GetOptions().indent_style);
3333
}
3434
else
3535
{
36-
ctx.AddIndent(static_cast<int>(indentCount));
36+
ctx.AddIndent(indentCount, ctx.GetOptions().indent_style);
3737
}
3838
}
3939
}
4040

4141
child->Serialize(ctx, it, *this);
4242
}
4343

44-
if (!_children.empty()) {
44+
if (!_children.empty())
45+
{
4546
ctx.RecoverIndent();
4647
}
4748
}
@@ -57,27 +58,28 @@ void AlignToFirstElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt,
5758
auto indentCount = ctx.GetCurrentIndent();
5859
if (writeCount > indentCount)
5960
{
60-
ctx.AddIndent(static_cast<int>(writeCount));
61+
ctx.AddIndent(writeCount, ctx.GetOptions().indent_style);
6162
}
6263
else
6364
{
6465
indentCount += _lowestIndent;
6566
const auto column = ctx.GetColumn(child->GetTextRange().StartOffset);
6667
if (column > static_cast<int>(indentCount))
6768
{
68-
ctx.AddIndent(column);
69+
ctx.AddIndent(column, ctx.GetOptions().indent_style);
6970
}
7071
else
7172
{
72-
ctx.AddIndent(static_cast<int>(indentCount));
73+
ctx.AddIndent(indentCount, ctx.GetOptions().indent_style);
7374
}
7475
}
7576
}
7677

7778
child->Diagnosis(ctx, it, *this);
7879
}
7980

80-
if (!_children.empty()) {
81+
if (!_children.empty())
82+
{
8183
ctx.RecoverIndent();
8284
}
8385
}

CodeService/src/FormatElement/AlignmentElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ FormatElementType AlignmentElement::GetType()
1212
return FormatElementType::AlignmentElement;
1313
}
1414

15-
void AlignmentElement::Serialize(FormatContext& ctx, ChildIterator selfIt, FormatElement& parent)
15+
void AlignmentElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
1616
{
1717
const int blank = _alignmentPosition - static_cast<int>(ctx.GetCharacterCount());
1818
if (blank > 0)

CodeService/src/FormatElement/AlignmentLayoutElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ FormatElementType AlignmentLayoutElement::GetType()
2222
return FormatElementType::AlignmentLayoutElement;
2323
}
2424

25-
void AlignmentLayoutElement::Serialize(FormatContext& ctx, ChildIterator selfIt,
25+
void AlignmentLayoutElement::Serialize(SerializeContext& ctx, ChildIterator selfIt,
2626
FormatElement& parent)
2727
{
2828
const auto eqPosition = GetAlignPosition(ctx.GetParser());
@@ -103,7 +103,7 @@ int AlignmentLayoutElement::GetAlignPosition(std::shared_ptr<LuaParser> luaParse
103103
return eqAlignedPosition;
104104
}
105105

106-
void AlignmentLayoutElement::AlignmentSerialize(FormatContext& ctx, ChildIterator selfIt, int eqPosition,
106+
void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int eqPosition,
107107
FormatElement& parent)
108108
{
109109
for (const auto& statChild : _children)

CodeService/src/FormatElement/DiagnosisContext.cpp

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,10 @@
33
#include "Util/format.h"
44

55
DiagnosisContext::DiagnosisContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options)
6-
: _options(options),
7-
_parser(parser)
6+
: FormatContext(parser, options)
87
{
98
}
109

11-
void DiagnosisContext::AddIndent(int specialIndent)
12-
{
13-
if (_options.indent_style == IndentStyle::Space)
14-
{
15-
int newIndent = 0;
16-
if (specialIndent == -1)
17-
{
18-
if (_indentStack.empty())
19-
{
20-
_indentStack.push(0);
21-
return;
22-
}
23-
24-
auto topIndent = _indentStack.top();
25-
newIndent = _options.indent_size + topIndent;
26-
}
27-
else
28-
{
29-
newIndent = specialIndent;
30-
}
31-
_indentStack.push(newIndent);
32-
}
33-
else //这个算法可能会有问题
34-
{
35-
int newIndent = 0;
36-
if (specialIndent == -1)
37-
{
38-
if (_indentStack.empty())
39-
{
40-
_indentStack.push(0);
41-
return;
42-
}
43-
44-
auto topIndent = _indentStack.top();
45-
// 一次只增一个\t
46-
newIndent = 1 + topIndent;
47-
}
48-
else
49-
{
50-
// 我会认为存在一个换算
51-
// 当你制定了一个缩进,则我会认为保底有一个缩进
52-
newIndent = std::max(1, specialIndent / _options.tab_width);
53-
}
54-
_indentStack.push(newIndent);
55-
}
56-
}
57-
58-
void DiagnosisContext::RecoverIndent()
59-
{
60-
_indentStack.pop();
61-
}
62-
6310

6411
void DiagnosisContext::PushDiagnosis(std::string_view message, TextRange range)
6512
{
@@ -73,21 +20,6 @@ void DiagnosisContext::PushDiagnosis(std::string_view message, LuaDiagnosisPosit
7320
_diagnosisInfos.push_back(LuaDiagnosisInfo{std::string(message), LuaDiagnosisRange(start, end)});
7421
}
7522

76-
int DiagnosisContext::GetLine(int offset)
77-
{
78-
return _parser->GetLine(offset);
79-
}
80-
81-
int DiagnosisContext::GetColumn(int offset)
82-
{
83-
return _parser->GetColumn(offset);
84-
}
85-
86-
std::size_t DiagnosisContext::GetCharacterCount() const
87-
{
88-
return _characterCount;
89-
}
90-
9123
void DiagnosisContext::SetCharacterCount(int character)
9224
{
9325
_characterCount = character;
@@ -98,11 +30,6 @@ void DiagnosisContext::SetLineMaxLength(int line, int character)
9830
_lineMaxLengthMap[line] = character;
9931
}
10032

101-
std::size_t DiagnosisContext::GetCurrentIndent() const
102-
{
103-
return _indentStack.top();
104-
}
105-
10633
std::vector<LuaDiagnosisInfo> DiagnosisContext::GetDiagnosisInfos()
10734
{
10835
if (!_lineMaxLengthMap.empty())
@@ -125,13 +52,3 @@ std::vector<LuaDiagnosisInfo> DiagnosisContext::GetDiagnosisInfos()
12552
}
12653
return _diagnosisInfos;
12754
}
128-
129-
const LuaCodeStyleOptions& DiagnosisContext::GetOptions()
130-
{
131-
return _options;
132-
}
133-
134-
std::shared_ptr<LuaParser> DiagnosisContext::GetParser()
135-
{
136-
return _parser;
137-
}

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -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

10559
int 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

13594
std::shared_ptr<LuaParser> FormatContext::GetParser()
13695
{
13796
return _parser;
13897
}
98+
99+
LuaCodeStyleOptions& FormatContext::GetOptions()
100+
{
101+
return _options;
102+
}

0 commit comments

Comments
 (0)