Skip to content

Commit 82aca21

Browse files
committed
修复单词拼写检查对字符串的处理
1 parent 144a1a5 commit 82aca21

File tree

4 files changed

+23
-43
lines changed

4 files changed

+23
-43
lines changed

CodeService/src/Diagnostic/Spell/CodeSpellChecker.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ void CodeSpellChecker::IdentifyAnalyze(DiagnosticBuilder &d, LuaSyntaxNode &toke
152152
}
153153

154154
void CodeSpellChecker::TextAnalyze(DiagnosticBuilder &d, LuaSyntaxNode &token, const LuaSyntaxTree &t) {
155-
std::shared_ptr<spell::TextParser> parser = std::make_shared<spell::TextParser>(token.GetText(t));
156-
parser->Parse();
157-
auto &identifiers = parser->GetIdentifiers();
155+
auto identifiers = spell::TextParser::ParseToWords(token.GetText(t));
158156
if (identifiers.empty()) {
159157
return;
160158
}
@@ -182,16 +180,16 @@ void CodeSpellChecker::TextAnalyze(DiagnosticBuilder &d, LuaSyntaxNode &token, c
182180
continue;
183181
}
184182

183+
auto tokenRange = token.GetTextRange(t);
185184
for (auto &word: words) {
186185
if (!word.Item.empty() && !_symSpell->IsCorrectWord(word.Item) && customDict.count(word.Item) == 0) {
187-
auto tokenRange = token.GetTextRange(t);
188186
auto range = TextRange(tokenRange.StartOffset + identifier.Range.Start + word.Range.Start,
189-
word.Range.Count - 1
187+
word.Range.Count
190188
);
191189
std::string originText(
192190
token.GetText(t).substr(identifier.Range.Start + word.Range.Start, word.Range.Count));
193191
d.PushDiagnostic(DiagnosticType::Spell, range,
194-
util::format("Typo in identifier '{}'", originText), originText);
192+
util::format("Typo in string '{}'", originText), originText);
195193
}
196194
}
197195
}
Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
#include "CodeService/Diagnostic/Spell/TextParser.h"
2-
#include "CodeService/Diagnostic/Spell/IdentifyParser.h"
3-
42
using namespace spell;
53

64
bool IsIdentifier(char ch) {
75
return ch > 0 && (std::isalnum(ch) || ch == '_');
86
}
97

10-
TextParser::TextParser(std::string_view source)
11-
: _source(source) {
12-
}
13-
14-
void TextParser::Parse() {
8+
std::vector<Word> TextParser::ParseToWords(std::string_view source) {
159
enum class ParseState {
1610
Unknown,
1711
Identify
1812
} state = ParseState::Unknown;
19-
13+
std::vector<Word> words;
2014
std::size_t start = 0;
21-
for (std::size_t i = 0; i != _source.size(); i++) {
22-
char ch = _source[i];
15+
for (std::size_t i = 0; i != source.size(); i++) {
16+
char ch = source[i];
2317
switch (state) {
2418
case ParseState::Unknown: {
2519
if (IsIdentifier(ch)) {
@@ -31,26 +25,23 @@ void TextParser::Parse() {
3125
case ParseState::Identify: {
3226
if (!IsIdentifier(ch)) {
3327
state = ParseState::Unknown;
34-
PushIdentifier(WordRange(start, i - start));
28+
auto len = i - start;
29+
if (len <= 3) {
30+
break;
31+
}
32+
auto range = WordRange(start, len);
33+
words.emplace_back(range, source.substr(start, len));
3534
}
3635
break;
3736
}
3837
}
3938
}
4039
if (state == ParseState::Identify) {
41-
PushIdentifier(WordRange(start, _source.size() - start));
42-
}
43-
}
44-
45-
std::vector<Word> &TextParser::GetIdentifiers() {
46-
return _identifiers;
47-
}
48-
49-
void TextParser::PushIdentifier(spell::WordRange range) {
50-
if (range.Count <= 3) {
51-
return;
40+
auto len = source.size() - start;
41+
if (len > 3) {
42+
auto range = WordRange(start, len);
43+
words.emplace_back(range, source.substr(start, len));
44+
}
5245
}
53-
54-
std::string_view identifyView = _source.substr(range.Start, range.Count);
55-
_identifiers.emplace_back(range, std::string(identifyView));
46+
return words;
5647
}

include/CodeService/Diagnostic/Spell/BasicSpellParserType.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cinttypes>
44
#include <string>
5+
#include <string_view>
56

67
namespace spell {
78
struct WordRange {
@@ -18,7 +19,7 @@ struct Word {
1819
WordRange Range;
1920
std::string Item;
2021

21-
Word(const WordRange &range, const std::string &item)
22+
Word(const WordRange &range, std::string_view item)
2223
: Range(range),
2324
Item(item) {
2425
}

include/CodeService/Diagnostic/Spell/TextParser.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ namespace spell {
88
// TODO Write later
99
class TextParser {
1010
public:
11-
TextParser(std::string_view source);
12-
13-
void Parse();
14-
15-
std::vector<Word> &GetIdentifiers();
16-
17-
private:
18-
void PushIdentifier(spell::WordRange range);
19-
20-
std::string_view _source;
21-
std::vector<Word> _identifiers;
11+
static std::vector<Word> ParseToWords(std::string_view source);
2212
};
2313
}

0 commit comments

Comments
 (0)