Skip to content

Commit 10112df

Browse files
committed
支持非标准token
1 parent 1a93b5c commit 10112df

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,47 @@ int set_default_config(lua_State* L)
361361
return 0;
362362
}
363363

364+
int set_nonstandard_symbol(lua_State* L)
365+
{
366+
int top = lua_gettop(L);
367+
368+
if (top != 2)
369+
{
370+
return 0;
371+
}
372+
373+
if (lua_isstring(L, 1) && lua_istable(L, 2))
374+
{
375+
try
376+
{
377+
std::string type = lua_tostring(L, 1);
378+
std::vector<std::string> tokens;
379+
lua_pushnil(L);
380+
while (lua_next(L, -2) != 0)
381+
{
382+
auto value = luaToString(L, -1);
383+
tokens.push_back(value);
384+
lua_pop(L, 1);
385+
}
386+
lua_settop(L, top);
387+
388+
LuaCodeFormat::GetInstance().SetSupportNonStandardSymbol(type, tokens);
389+
lua_pushboolean(L, true);
390+
return 1;
391+
}
392+
catch (std::exception& e)
393+
{
394+
std::string err = e.what();
395+
lua_settop(L, top);
396+
lua_pushboolean(L, false);
397+
lua_pushlstring(L, err.c_str(), err.size());
398+
return 2;
399+
}
400+
}
401+
402+
return 0;
403+
}
404+
364405
int spell_load_dictionary_from_path(lua_State* L)
365406
{
366407
int top = lua_gettop(L);

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ LuaCodeFormat& LuaCodeFormat::GetInstance()
1414

1515
LuaCodeFormat::LuaCodeFormat()
1616
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>()),
17-
_codeSpellChecker(std::make_shared<CodeSpellChecker>())
17+
_codeSpellChecker(std::make_shared<CodeSpellChecker>()),
18+
_customParser(std::make_shared<LuaCustomParser>())
1819
{
1920
}
2021

@@ -63,6 +64,14 @@ void LuaCodeFormat::SetDefaultCodeStyle(ConfigMap& configMap)
6364
}
6465
}
6566

67+
void LuaCodeFormat::SetSupportNonStandardSymbol(const std::string& tokenType,const std::vector<std::string>& tokens)
68+
{
69+
if (tokenType.size() == 1)
70+
{
71+
_customParser->SetTokens(tokenType.front(), tokens);
72+
}
73+
}
74+
6675
void LuaCodeFormat::LoadSpellDictionary(const std::string& path)
6776
{
6877
_codeSpellChecker->LoadDictionary(path);
@@ -76,6 +85,9 @@ void LuaCodeFormat::LoadSpellDictionaryFromBuffer(const std::string& buffer)
7685
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text, ConfigMap& configMap)
7786
{
7887
auto parser = LuaParser::LoadFromBuffer(std::move(text));
88+
if (_customParser->IsSupportCustomTokens()) {
89+
parser->GetTokenParser()->SetCustomParser(_customParser);
90+
}
7991
parser->BuildAstWithComment();
8092

8193
if (!parser->GetErrors().empty())
@@ -93,6 +105,9 @@ std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& r
93105
ConfigMap& configMap)
94106
{
95107
auto parser = LuaParser::LoadFromBuffer(std::move(text));
108+
if (_customParser->IsSupportCustomTokens()) {
109+
parser->GetTokenParser()->SetCustomParser(_customParser);
110+
}
96111
parser->BuildAstWithComment();
97112

98113
if (!parser->GetErrors().empty())
@@ -111,6 +126,9 @@ std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& r
111126
std::pair<bool, std::vector<LuaDiagnosisInfo>> LuaCodeFormat::Diagnose(const std::string& uri, std::string&& text)
112127
{
113128
auto parser = LuaParser::LoadFromBuffer(std::move(text));
129+
if (_customParser->IsSupportCustomTokens()) {
130+
parser->GetTokenParser()->SetCustomParser(_customParser);
131+
}
114132
parser->BuildAstWithComment();
115133

116134
if (!parser->GetErrors().empty())
@@ -137,6 +155,10 @@ std::vector<LuaDiagnosisInfo> LuaCodeFormat::SpellCheck(const std::string& uri,
137155
const CodeSpellChecker::CustomDictionary& tempDict)
138156
{
139157
auto parser = LuaParser::LoadFromBuffer(std::move(text));
158+
if (_customParser->IsSupportCustomTokens()) {
159+
parser->GetTokenParser()->SetCustomParser(_customParser);
160+
}
161+
parser->GetTokenParser()->Parse();
140162

141163
auto options = GetOptions(uri);
142164

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class LuaCodeFormat
2121
void UpdateCodeStyle(const std::string& workspaceUri, const std::string& configPath);
2222
void RemoveCodeStyle(const std::string& workspaceUri);
2323
void SetDefaultCodeStyle(ConfigMap& configMap);
24+
void SetSupportNonStandardSymbol(const std::string& tokenType, const std::vector<std::string>& tokens);
2425

2526
void LoadSpellDictionary(const std::string& path);
2627

@@ -46,4 +47,6 @@ class LuaCodeFormat
4647
std::shared_ptr<LuaCodeStyleOptions> _defaultOptions;
4748

4849
std::shared_ptr<CodeSpellChecker> _codeSpellChecker;
50+
51+
std::shared_ptr<LuaCustomParser> _customParser;
4952
};

LuaParser/src/LuaCustomParser.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,25 @@ LuaTokenType LuaCustomParser::Lex(std::string_view source, std::size_t start, st
9898
return it->second;
9999
}
100100

101-
void LuaCustomParser::SetEqToken(std::vector<std::string>& customTokens)
101+
void LuaCustomParser::SetTokens(LuaTokenType type, const std::vector<std::string>& customTokens)
102102
{
103+
for (auto it = _customTokens.begin(); it != _customTokens.end();)
104+
{
105+
if (it->second == type)
106+
{
107+
_customTokens.erase(it++);
108+
continue;
109+
}
110+
++it;
111+
}
112+
103113
for (auto& token : customTokens)
104114
{
105-
_customTokens.insert({token, '='});
115+
_customTokens.insert({token, type});
106116
}
107117
}
118+
119+
bool LuaCustomParser::IsSupportCustomTokens() const
120+
{
121+
return !_customTokens.empty();
122+
}

LuaParser/src/LuaParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ std::shared_ptr<LuaParser> LuaParser::LoadFromFile(std::string_view filename)
3333
std::shared_ptr<LuaParser> LuaParser::LoadFromBuffer(std::string&& buffer)
3434
{
3535
auto file = std::make_shared<LuaFile>("chunk", std::move(buffer));
36-
36+
3737
auto tokenParser = std::make_shared<LuaTokenParser>(file);
38-
tokenParser->Parse();
3938

4039
return std::make_shared<LuaParser>(tokenParser);
4140
}
4241

4342
void LuaParser::BuildAst()
4443
{
44+
_tokenParser->Parse();
4545
_chunkAstNode = CreateAstNode(LuaAstNodeType::Chunk);
4646
try
4747
{

include/LuaParser/LuaCustomParser.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#include <string_view>
66
#include "LuaTokenType.h"
77
#include <map>
8+
#include <set>
89

910
class LuaCustomParser
1011
{
1112
public:
1213
LuaTokenType Lex(std::string_view source, std::size_t start, std::size_t& consumeSize);
1314

14-
void SetEqToken(std::vector<std::string>& customTokens);
15-
private:
15+
void SetTokens(LuaTokenType type, const std::vector<std::string>& customTokens);
1616

17+
bool IsSupportCustomTokens() const;
18+
private:
1719
std::map<std::string, LuaTokenType, std::less<>> _customTokens;
1820
};

0 commit comments

Comments
 (0)