Skip to content

Commit a154bb9

Browse files
committed
修改范围空判断
1 parent 27741e4 commit a154bb9

File tree

11 files changed

+43
-34
lines changed

11 files changed

+43
-34
lines changed

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ FormatElement::ChildContainer& FormatElement::GetChildren()
4545

4646
bool FormatElement::HasValidTextRange() const
4747
{
48-
return !(_textRange.StartOffset == 0 && _textRange.EndOffset == 0);
48+
return !_textRange.IsEmpty();
4949
}
5050

5151
std::shared_ptr<FormatElement> FormatElement::LastValidElement() const
@@ -91,7 +91,7 @@ void FormatElement::DiagnosisCodeStyle(DiagnosisContext& ctx)
9191

9292
void FormatElement::AddTextRange(TextRange range)
9393
{
94-
if (_textRange.StartOffset == 0 && _textRange.EndOffset == 0)
94+
if (_textRange.IsEmpty())
9595
{
9696
_textRange = range;
9797
}

CodeService/src/FormatElement/IndentElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "CodeService/FormatElement/FormatElement.h"
44

55
IndentElement::IndentElement(int specialIndent)
6-
: FormatElement(TextRange(0, 0)),
6+
: FormatElement(),
77
_specialIndent(specialIndent)
88
{
99
}

CodeService/src/FormatElement/KeepBlankElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "Util/format.h"
33

44
KeepBlankElement::KeepBlankElement(int blank)
5-
: FormatElement(TextRange(0, 0)),
5+
: FormatElement(),
66
_blank(blank)
77
{
88
}

CodeService/src/FormatElement/LineElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "CodeService/FormatElement/LineElement.h"
22

33
LineElement::LineElement()
4-
: FormatElement(TextRange())
4+
: FormatElement()
55
{
66
}
77

CodeService/src/FormatElement/MinLineElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "Util/format.h"
33

44
MinLineElement::MinLineElement(int line)
5-
: FormatElement(TextRange()),
5+
: FormatElement(),
66
_line(line)
77
{
88
}

LuaParser/src/LuaAstNode.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,24 @@ void LuaAstNode::AddChild(std::shared_ptr<LuaAstNode> child)
7272
{
7373
return;
7474
}
75-
auto source = GetSource();
75+
76+
if (child->_textRange.IsEmpty())
77+
{
78+
return;
79+
}
80+
81+
const auto source = GetSource();
7682
if (child->GetSource() != source)
7783
{
7884
return;
7985
}
8086

81-
if (_textRange.StartOffset == 0 && _textRange.EndOffset == 0)
87+
if (_textRange.IsEmpty())
8288
{
8389
_textRange = child->_textRange;
8490
}
85-
8691
else
8792
{
88-
if (child->_textRange.StartOffset == 0 && child->_textRange.EndOffset == 0)
89-
{
90-
return;
91-
}
92-
9393
if (_textRange.StartOffset > child->_textRange.StartOffset)
9494
{
9595
_textRange.StartOffset = child->_textRange.StartOffset;
@@ -173,23 +173,23 @@ void LuaAstNode::AddChildBefore(ChildIterator it, std::shared_ptr<LuaAstNode> ch
173173
{
174174
if (it == _children.begin())
175175
{
176-
auto source = GetSource();
176+
if (child->_textRange.IsEmpty())
177+
{
178+
return;
179+
}
180+
181+
const auto source = GetSource();
177182
if (child->GetSource() != source)
178183
{
179184
return;
180185
}
181186

182-
if (_textRange.StartOffset == 0 && _textRange.EndOffset == 0)
187+
if (_textRange.IsEmpty())
183188
{
184189
_textRange = child->_textRange;
185190
}
186191
else
187192
{
188-
if (child->_textRange.StartOffset == 0 && child->_textRange.EndOffset == 0)
189-
{
190-
return;
191-
}
192-
193193
if (_textRange.StartOffset > child->_textRange.StartOffset)
194194
{
195195
_textRange.StartOffset = child->_textRange.StartOffset;
@@ -203,7 +203,7 @@ void LuaAstNode::AddChildBefore(ChildIterator it, std::shared_ptr<LuaAstNode> ch
203203
_text = std::string_view(source + _textRange.StartOffset,
204204
_textRange.EndOffset - _textRange.StartOffset + 1);
205205
}
206-
else if(it == _children.end())
206+
else if (it == _children.end())
207207
{
208208
return AddChild(child);
209209
}

LuaParser/src/LuaParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ void LuaParser::CodeName(std::shared_ptr<LuaAstNode> parent)
11481148

11491149
std::shared_ptr<LuaAstNode> LuaParser::CreateAstNode(LuaAstNodeType type)
11501150
{
1151-
return std::make_shared<LuaAstNode>(type, std::string_view(GetSource().data(), 0), TextRange(0, 0));
1151+
return std::make_shared<LuaAstNode>(type, std::string_view(GetSource().data(), 0), TextRange());
11521152
}
11531153

11541154
std::shared_ptr<LuaAstNode> LuaParser::CreateAstNodeFromToken(LuaAstNodeType type, LuaToken& token)

LuaParser/src/LuaTokenParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ bool LuaTokenParser::Parse()
6666
{
6767
if (type == TK_LONG_COMMENT || type == TK_SHEBANG || type == TK_SHORT_COMMENT)
6868
{
69-
_commentTokens.emplace_back(type, text, TextRange(_buffStart, _buffIndex));
69+
_commentTokens.emplace_back(type, text, TextRange(
70+
static_cast<int>(_buffStart), static_cast<int>(_buffIndex)));
7071
}
7172
else
7273
{
73-
_tokens.emplace_back(type, text, TextRange(_buffStart, _buffIndex));
74+
_tokens.emplace_back(type, text, TextRange(
75+
static_cast<int>(_buffStart), static_cast<int>(_buffIndex)));
7476
}
7577
}
7678
else

LuaParser/src/TextRange.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#include "LuaParser/TextRange.h"
22

3-
TextRange::TextRange(std::size_t startOffset, std::size_t endOffset)
4-
: StartOffset(static_cast<int>(startOffset)),
5-
EndOffset(static_cast<int>(endOffset))
3+
TextRange::TextRange(int startOffset, int endOffset)
4+
: StartOffset(startOffset),
5+
EndOffset(endOffset)
66
{
77
}
88

9-
bool TextRange::Contain(TextRange& range)
9+
bool TextRange::IsEmpty() const
10+
{
11+
return StartOffset == 0 && EndOffset == -1;
12+
}
13+
14+
bool TextRange::Contain(TextRange& range) const
1015
{
1116
return this->StartOffset <= range.StartOffset && this->EndOffset >= range.EndOffset;
1217
}
1318

14-
bool TextRange::Between(TextRange& leftRange, TextRange& rightRange)
19+
bool TextRange::Between(TextRange& leftRange, TextRange& rightRange) const
1520
{
1621
return this->StartOffset > leftRange.EndOffset && this->EndOffset < rightRange.StartOffset;
1722
}

Test/src/CodeFormatTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ std::string ReadFile(const std::string& path)
8181
return "";
8282
}
8383

84-
// 第一个参数是待格式化文本目录,第二个参数是格式化预期结果
84+
8585
int main(int argc, char* argv[])
8686
{
8787
CommandLine commandLine;

0 commit comments

Comments
 (0)