Skip to content

Commit 32538b0

Browse files
committed
支持临时字典
1 parent fd56252 commit 32538b0

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,21 @@ int spell_analysis(lua_State* L)
441441
{
442442
std::string filename = lua_tostring(L, 1);
443443
std::string text = lua_tostring(L, 2);
444-
auto diagnosticInfos = LuaCodeFormat::GetInstance().SpellCheck(filename, std::move(text));
444+
445+
std::set<std::string> tempDict;
446+
447+
if (top == 3 && lua_istable(L, 3))
448+
{
449+
lua_pushnil(L);
450+
while (lua_next(L, -2) != 0)
451+
{
452+
auto value = luaToString(L, -1);
453+
tempDict.insert(value);
454+
lua_pop(L, 1);
455+
}
456+
}
457+
458+
auto diagnosticInfos = LuaCodeFormat::GetInstance().SpellCheck(filename, std::move(text), tempDict);
445459

446460
lua_pushboolean(L, true);
447461
int count = 1;

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ std::pair<bool, std::vector<LuaDiagnosisInfo>> LuaCodeFormat::Diagnose(const std
133133
return std::make_pair(true, ctx.GetDiagnosisInfos());
134134
}
135135

136-
std::vector<LuaDiagnosisInfo> LuaCodeFormat::SpellCheck(const std::string& uri, std::string&& text)
136+
std::vector<LuaDiagnosisInfo> LuaCodeFormat::SpellCheck(const std::string& uri, std::string&& text, std::set<std::string>& tempDict)
137137
{
138138
auto parser = LuaParser::LoadFromBuffer(std::move(text));
139139

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class LuaCodeFormat
3232

3333
std::pair<bool, std::vector<LuaDiagnosisInfo>> Diagnose(const std::string& uri, std::string&& text);
3434

35-
std::vector<LuaDiagnosisInfo> SpellCheck(const std::string& uri, std::string&& text);
35+
std::vector<LuaDiagnosisInfo> SpellCheck(const std::string& uri, std::string&& text, std::set<std::string>& tempDict);
3636

3737
std::vector<SuggestItem> SpellCorrect(const std::string& word);
3838

CodeService/src/Spell/CodeSpellChecker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void CodeSpellChecker::LoadDictionaryFromBuffer(std::string_view buffer)
2222
_symSpell->LoadWordDictionaryFromBuffer(buffer);
2323
}
2424

25-
void CodeSpellChecker::Analysis(DiagnosisContext& ctx)
25+
void CodeSpellChecker::Analysis(DiagnosisContext& ctx, const std::set<std::string>& tempDict)
2626
{
2727
auto parser = ctx.GetParser();
2828
auto tokenParser = parser->GetTokenParser();
@@ -31,7 +31,7 @@ void CodeSpellChecker::Analysis(DiagnosisContext& ctx)
3131
{
3232
if (token.TokenType == TK_NAME)
3333
{
34-
IdentifyAnalysis(ctx, token);
34+
IdentifyAnalysis(ctx, token, tempDict);
3535
}
3636
}
3737
}
@@ -150,7 +150,7 @@ std::vector<SuggestItem> CodeSpellChecker::GetSuggests(std::string word)
150150
return suggests;
151151
}
152152

153-
void CodeSpellChecker::IdentifyAnalysis(DiagnosisContext& ctx, LuaToken& token)
153+
void CodeSpellChecker::IdentifyAnalysis(DiagnosisContext& ctx, LuaToken& token, const std::set<std::string>& tempDict)
154154
{
155155
std::shared_ptr<IdentifyParser> parser = nullptr;
156156
std::string text(token.Text);
@@ -174,7 +174,7 @@ void CodeSpellChecker::IdentifyAnalysis(DiagnosisContext& ctx, LuaToken& token)
174174

175175
for (auto& word : words)
176176
{
177-
if (!word.Item.empty() && !_symSpell->IsCorrectWord(word.Item))
177+
if (!word.Item.empty() && !_symSpell->IsCorrectWord(word.Item) && tempDict.count(word.Item) == 0)
178178
{
179179
auto range = TextRange(token.Range.StartOffset + word.Range.Start,
180180
token.Range.StartOffset + word.Range.Start + word.Range.Count - 1

include/CodeService/Spell/CodeSpellChecker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class CodeSpellChecker
1616

1717
void LoadDictionaryFromBuffer(std::string_view buffer);
1818

19-
void Analysis(DiagnosisContext& ctx);
19+
void Analysis(DiagnosisContext& ctx, const std::set<std::string>& tempDict = std::set<std::string>());
2020

2121
// copy once
2222
std::vector<SuggestItem> GetSuggests(std::string word);
2323
private:
24-
void IdentifyAnalysis(DiagnosisContext& ctx, LuaToken& token);
24+
void IdentifyAnalysis(DiagnosisContext& ctx, LuaToken& token, const std::set<std::string>& tempDict);
2525

2626
std::shared_ptr<SymSpell> _symSpell;
2727
std::unordered_map<std::string, std::shared_ptr<IdentifyParser>> _caches;

0 commit comments

Comments
 (0)