Skip to content

Commit aa891f0

Browse files
committed
重构诊断信息
1 parent db5f6f3 commit aa891f0

20 files changed

+644
-424
lines changed

CodeService/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ target_sources(CodeService
2121
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/KeepLineElement.h
2222
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/FormatElementType.h
2323
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/StatementElement.h
24-
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/KeepBlankElement.h
24+
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/SpaceElement.h
2525
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/LineElement.h
2626
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/AlignmentElement.h
2727
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/AlignmentLayoutElement.h
@@ -40,7 +40,9 @@ target_sources(CodeService
4040
${CodeService_SOURCE_DIR}/src/FormatElement/MinLineElement.cpp
4141
${CodeService_SOURCE_DIR}/src/FormatElement/StatementElement.cpp
4242
${CodeService_SOURCE_DIR}/src/FormatElement/TextElement.cpp
43-
${CodeService_SOURCE_DIR}/src/FormatElement/KeepBlankElement.cpp
43+
${CodeService_SOURCE_DIR}/src/FormatElement/OperatorElement.cpp
44+
${CodeService_SOURCE_DIR}/src/FormatElement/KeyWordElement.cpp
45+
${CodeService_SOURCE_DIR}/src/FormatElement/SpaceElement.cpp
4446
${CodeService_SOURCE_DIR}/src/FormatElement/FormatElement.cpp
4547
${CodeService_SOURCE_DIR}/src/FormatElement/LineElement.cpp
4648
${CodeService_SOURCE_DIR}/src/FormatElement/NoIndentElement.cpp

CodeService/src/FormatElement/AlignIfLayoutElement.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
22
#include "CodeService/FormatElement/TextElement.h"
3-
#include "CodeService/FormatElement/KeepBlankElement.h"
3+
#include "CodeService/FormatElement/SpaceElement.h"
44

55
AlignIfElement::AlignIfElement()
66
{
@@ -95,7 +95,7 @@ void AlignIfElement::AlignElement(FormatContext& ctx)
9595
++it;
9696
if (it != _children.end()
9797
&& (*it)->GetType() == FormatElementType::LongExpressionLayoutElement
98-
&& (*controlIt)->GetType() == FormatElementType::KeepBlankElement)
98+
&& (*controlIt)->GetType() == FormatElementType::SpaceElement)
9999
{
100100
auto expressionLayout = *it;
101101
if (elseIfFounded)
@@ -106,22 +106,22 @@ void AlignIfElement::AlignElement(FormatContext& ctx)
106106
int spaceAfterIf = expressionOffset - ifOffset - 1;
107107
if(spaceAfterIf == 2 || spaceAfterIf == 1)
108108
{
109-
*controlIt = std::make_shared<KeepBlankElement>(2);
109+
*controlIt = std::make_shared<SpaceElement>(2);
110110
AlignConditionExpression(ctx, *expressionLayout, 4);
111111
}
112112
else {
113113
// if(expressionOffset - ifOffset >= )
114114
// if<5 space>condition
115115
// elseif<1 space>condition
116-
*controlIt = std::make_shared<KeepBlankElement>(5);
116+
*controlIt = std::make_shared<SpaceElement>(5);
117117
AlignConditionExpression(ctx, *expressionLayout, 7);
118118
}
119119
}
120120
else if (OnlyEmptyCharBeforeAnd(ctx, *expressionLayout))
121121
{
122122
// if<2 space>condition
123123
// and<1 space>condition
124-
*controlIt = std::make_shared<KeepBlankElement>(2);
124+
*controlIt = std::make_shared<SpaceElement>(2);
125125
AlignConditionExpression(ctx, *expressionLayout, 4);
126126
}
127127
}
@@ -158,7 +158,7 @@ void AlignIfElement::AlignConditionExpression(FormatContext& ctx, FormatElement&
158158
++it;
159159
if (it != children.end())
160160
{
161-
*it = std::make_shared<KeepBlankElement>(
161+
*it = std::make_shared<SpaceElement>(
162162
static_cast<int>(spacePositionAfterIndent - text.size())
163163
);
164164
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "CodeService/FormatElement/BreakableElement.h"
2+
3+
BreakableElement::BreakableElement()
4+
{
5+
}
6+
7+
void BreakableElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
8+
{
9+
10+
}

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void FormatElement::Reset()
219219
_children.clear();
220220
}
221221

222-
std::shared_ptr<FormatElement> FormatElement::GetNextValidElement(ChildIterator& it, FormatElement& parent)
222+
std::shared_ptr<FormatElement> FormatElement::GetNextValidElement(ChildIterator it, FormatElement& parent)
223223
{
224224
auto& siblings = parent.GetChildren();
225225
++it;
@@ -235,7 +235,7 @@ std::shared_ptr<FormatElement> FormatElement::GetNextValidElement(ChildIterator&
235235
return nullptr;
236236
}
237237

238-
int FormatElement::GetLastValidOffset(ChildIterator& it, FormatElement& parent)
238+
std::shared_ptr<FormatElement> FormatElement::GetLastValidElement(ChildIterator it, FormatElement& parent)
239239
{
240240
auto& siblings = parent.GetChildren();
241241
auto rIt = std::reverse_iterator<std::remove_reference_t<decltype(it)>>(it);
@@ -245,9 +245,19 @@ int FormatElement::GetLastValidOffset(ChildIterator& it, FormatElement& parent)
245245
auto sibling = *rIt;
246246
if (sibling->HasValidTextRange())
247247
{
248-
return sibling->GetTextRange().EndOffset;
248+
return sibling;
249249
}
250250
}
251+
return nullptr;
252+
}
253+
254+
int FormatElement::GetLastValidOffset(ChildIterator& it, FormatElement& parent)
255+
{
256+
auto lastElement = GetLastValidElement(it, parent);
257+
if(lastElement)
258+
{
259+
return lastElement->GetTextRange().EndOffset;
260+
}
251261

252262
// 那么一定是往上找不到有效范围元素
253263
return parent.GetTextRange().StartOffset;

CodeService/src/FormatElement/KeepBlankElement.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

CodeService/src/FormatElement/KeepElement.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "CodeService/FormatElement/KeepElement.h"
22
#include "Util/format.h"
33

4-
KeepElement::KeepElement(int keepBlank, bool hasLinebreak, bool allowContinueIndent)
5-
: _keepBlank(keepBlank),
4+
KeepElement::KeepElement(int keepSpace, bool hasLinebreak, bool allowContinueIndent)
5+
: SpaceElement(keepSpace),
66
_hasLinebreak(hasLinebreak),
77
_allowContinueIndent(allowContinueIndent)
88
{
@@ -33,7 +33,7 @@ void KeepElement::Serialize(SerializeContext& ctx, ChildIterator selfIt,
3333
// 这个条件的意思是如果上一个元素和下一个元素没有实质的换行则保持一定的空格
3434
if (nextElementLine == lastElementLine && ctx.GetCharacterCount() != 0)
3535
{
36-
ctx.PrintBlank(_keepBlank);
36+
ctx.PrintBlank(_space);
3737
}
3838
else
3939
{
@@ -68,11 +68,7 @@ void KeepElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt,
6868

6969
if (nextElementLine == lastElementLine)
7070
{
71-
if (nextOffset - lastOffset - 1 != _keepBlank)
72-
{
73-
ctx.PushDiagnosis(Util::format(LText("here need keep {} space"), _keepBlank),
74-
TextRange(lastOffset, nextOffset), DiagnosisType::Blank);
75-
}
71+
PushSpaceDiagnostic(ctx, GetLastValidElement(selfIt, parent), GetNextValidElement(selfIt, parent));
7672
}
7773
else
7874
{
@@ -82,7 +78,7 @@ void KeepElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt,
8278

8379
void KeepElement::AllowBreakLineSerialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
8480
{
85-
if(!_allowContinueIndent)
81+
if (!_allowContinueIndent)
8682
{
8783
return Serialize(ctx, selfIt, parent);
8884
}
@@ -109,7 +105,7 @@ void KeepElement::AllowBreakLineSerialize(SerializeContext& ctx, ChildIterator s
109105
ctx.PrintLine(1);
110106
return;
111107
}
112-
ctx.PrintBlank(_keepBlank);
108+
ctx.PrintBlank(_space);
113109
}
114110
else
115111
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "CodeService/FormatElement/KeyWordElement.h"
2+
3+
KeyWordElement::KeyWordElement(std::shared_ptr<LuaAstNode> node)
4+
: TextElement(node)
5+
{
6+
}
7+
8+
FormatElementType KeyWordElement::GetType()
9+
{
10+
return FormatElementType::KeyWordElement;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "CodeService/FormatElement/OperatorElement.h"
2+
3+
OperatorElement::OperatorElement(std::shared_ptr<LuaAstNode> node)
4+
:TextElement(node)
5+
{
6+
}
7+
8+
FormatElementType OperatorElement::GetType()
9+
{
10+
return FormatElementType::OperatorElement;
11+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "CodeService/FormatElement/SpaceElement.h"
2+
3+
#include "CodeService/FormatElement/KeyWordElement.h"
4+
#include "CodeService/FormatElement/OperatorElement.h"
5+
#include "Util/format.h"
6+
7+
SpaceElement::SpaceElement(int space)
8+
: FormatElement(),
9+
_space(space)
10+
{
11+
}
12+
13+
FormatElementType SpaceElement::GetType()
14+
{
15+
return FormatElementType::SpaceElement;
16+
}
17+
18+
void SpaceElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
19+
{
20+
int nextOffset = GetNextValidOffset(selfIt, parent);
21+
if (nextOffset != -1)
22+
{
23+
ctx.PrintBlank(_space);
24+
}
25+
}
26+
27+
void SpaceElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
28+
{
29+
const int lastOffset = GetLastValidOffset(selfIt, parent);
30+
const int nextOffset = GetNextValidOffset(selfIt, parent);
31+
32+
if (nextOffset == -1 && lastOffset != -1)
33+
{
34+
return;
35+
}
36+
37+
const int lastElementLine = ctx.GetLine(lastOffset);
38+
const int nextElementLine = ctx.GetLine(nextOffset);
39+
40+
if (nextElementLine == lastElementLine)
41+
{
42+
PushSpaceDiagnostic(ctx, GetLastValidElement(selfIt, parent), GetNextValidElement(selfIt, parent));
43+
}
44+
else
45+
{
46+
auto character = ctx.GetColumn(lastOffset);
47+
ctx.PushDiagnosis(LText("should be whitespace"),
48+
LuaDiagnosisPosition(lastElementLine, character + 1),
49+
LuaDiagnosisPosition(lastElementLine, character + 1),
50+
DiagnosisType::Blank);
51+
}
52+
}
53+
54+
void SpaceElement::PushSpaceDiagnostic(DiagnosisContext& ctx,
55+
std::shared_ptr<FormatElement> left,
56+
std::shared_ptr<FormatElement> right)
57+
{
58+
if (left == nullptr || right == nullptr)
59+
{
60+
return;
61+
}
62+
63+
auto leftOffset = left->GetTextRange().EndOffset;
64+
auto rightOffset = right->GetTextRange().StartOffset;
65+
int diff = rightOffset - leftOffset - 1;
66+
67+
if (diff < 0 || _space < 0 || diff == _space)
68+
{
69+
return;
70+
}
71+
72+
auto additional = GetAdditionalNote(left, right);
73+
switch (_space)
74+
{
75+
case 0:
76+
{
77+
ctx.PushDiagnosis(Util::format(LText("unnecessary whitespace {}"), additional),
78+
TextRange(leftOffset, rightOffset),
79+
DiagnosisType::Blank);
80+
break;
81+
}
82+
case 1:
83+
{
84+
if (diff == 0)
85+
{
86+
ctx.PushDiagnosis(Util::format(LText("missing whitespace {}"), additional),
87+
TextRange(leftOffset, rightOffset),
88+
DiagnosisType::Blank);
89+
}
90+
else // diff > 1
91+
{
92+
ctx.PushDiagnosis(Util::format(LText("multiple spaces {}"), additional),
93+
TextRange(leftOffset + 1, rightOffset - 1),
94+
DiagnosisType::Blank);
95+
}
96+
break;
97+
}
98+
default:
99+
{
100+
if (diff < _space)
101+
{
102+
ctx.PushDiagnosis(Util::format(LText("expected {} whitespace, found {} {}"), _space, diff, additional),
103+
TextRange(leftOffset, rightOffset),
104+
DiagnosisType::Blank);
105+
}
106+
else // diff > _space
107+
{
108+
ctx.PushDiagnosis(Util::format(LText("expected {} whitespace, found {} {}"), _space, diff, additional),
109+
TextRange(leftOffset + 1, rightOffset - 1),
110+
DiagnosisType::Blank);
111+
}
112+
break;
113+
}
114+
}
115+
}
116+
117+
std::string SpaceElement::GetAdditionalNote(std::shared_ptr<FormatElement> left, std::shared_ptr<FormatElement> right)
118+
{
119+
if (left->Is(FormatElementType::KeyWordElement))
120+
{
121+
auto keyWord = std::dynamic_pointer_cast<KeyWordElement>(left);
122+
return Util::format(LText("after keyword '{}'"), keyWord->GetText());
123+
}
124+
else if (left->Is(FormatElementType::OperatorElement))
125+
{
126+
auto operatorElement = std::dynamic_pointer_cast<OperatorElement>(left);
127+
return Util::format(LText("after operator '{}'"), operatorElement->GetText());
128+
}
129+
else if (right->Is(FormatElementType::KeyWordElement))
130+
{
131+
auto keyWord = std::dynamic_pointer_cast<KeyWordElement>(right);
132+
return Util::format(LText("before keyword '{}'"), keyWord->GetText());
133+
}
134+
else if (right->Is(FormatElementType::OperatorElement))
135+
{
136+
auto operatorElement = std::dynamic_pointer_cast<OperatorElement>(right);
137+
return Util::format(LText("before operator '{}'"), operatorElement->GetText());
138+
}
139+
else
140+
{
141+
return "";
142+
}
143+
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#endif
99
#include "Util/StringUtil.h"
1010
#include "CodeService/FormatElement/KeepElement.h"
11+
#include "CodeService/FormatElement/KeepLineElement.h"
1112
#include "CodeService/FormatElement/MinLineElement.h"
1213
#include "CodeService/FormatElement/MaxLineElement.h"
1314
#include "CodeService/NameStyle/NameStyleRuleMatcher.h"

0 commit comments

Comments
 (0)