Skip to content

Commit f580f0d

Browse files
committed
提供由编辑器配置覆盖的接口
1 parent 3271ec9 commit f580f0d

File tree

3 files changed

+123
-39
lines changed

3 files changed

+123
-39
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@
77
#define EXPORT
88
#endif
99

10+
std::string luaToString(lua_State* L, int idx)
11+
{
12+
if (lua_isstring(L, idx))
13+
{
14+
return lua_tostring(L, idx);
15+
}
16+
else if (lua_isinteger(L, idx))
17+
{
18+
return std::to_string(lua_tointeger(L, idx));
19+
}
20+
else if (lua_isnumber(L, idx))
21+
{
22+
return std::to_string(lua_tonumber(L, idx));
23+
}
24+
else if (lua_isboolean(L, idx))
25+
{
26+
return std::to_string(static_cast<bool>(lua_toboolean(L, idx)));
27+
}
28+
else
29+
{
30+
return "nil";
31+
}
32+
}
33+
34+
1035
int format(lua_State* L)
1136
{
1237
int top = lua_gettop(L);
@@ -22,7 +47,26 @@ int format(lua_State* L)
2247
{
2348
std::string filename = lua_tostring(L, 1);
2449
std::string text = lua_tostring(L, 2);
25-
auto formattedText = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text));
50+
LuaCodeFormat::ConfigMap configMap;
51+
52+
if(top == 3 && lua_istable(L, 3))
53+
{
54+
lua_pushnil(L);
55+
while (lua_next(L, -2) != 0)
56+
{
57+
auto key = luaToString(L, -2);
58+
auto value = luaToString(L, -1);
59+
60+
if (key != "nil")
61+
{
62+
configMap.insert({ key, value });
63+
}
64+
65+
lua_pop(L, 1);
66+
}
67+
}
68+
69+
auto formattedText = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text), configMap);
2670
if (formattedText.empty())
2771
{
2872
lua_pushboolean(L, false);
@@ -68,9 +112,27 @@ int range_format(lua_State* L)
68112
std::string text = lua_tostring(L, 2);
69113
int startLine = lua_tointeger(L, 3);
70114
int endLine = lua_tointeger(L, 4);
115+
LuaCodeFormat::ConfigMap configMap;
116+
117+
if (top == 5 && lua_istable(L, 5))
118+
{
119+
lua_pushnil(L);
120+
while (lua_next(L, -2) != 0)
121+
{
122+
auto key = luaToString(L, -2);
123+
auto value = luaToString(L, -1);
124+
125+
if (key != "nil")
126+
{
127+
configMap.insert({ key, value });
128+
}
129+
130+
lua_pop(L, 1);
131+
}
132+
}
71133

72134
LuaFormatRange range(startLine, endLine);
73-
auto formattedText = LuaCodeFormat::GetInstance().RangeFormat(filename, range, std::move(text));
135+
auto formattedText = LuaCodeFormat::GetInstance().RangeFormat(filename, range, std::move(text), configMap);
74136
if (formattedText.empty())
75137
{
76138
lua_pushboolean(L, false);
@@ -234,30 +296,6 @@ int diagnose_file(lua_State* L)
234296
return 0;
235297
}
236298

237-
std::string luaToString(lua_State* L, int idx)
238-
{
239-
if (lua_isstring(L, idx))
240-
{
241-
return lua_tostring(L, idx);
242-
}
243-
else if (lua_isinteger(L, idx))
244-
{
245-
return std::to_string(lua_tointeger(L, idx));
246-
}
247-
else if (lua_isnumber(L, idx))
248-
{
249-
return std::to_string(lua_tonumber(L, idx));
250-
}
251-
else if (lua_isboolean(L, idx))
252-
{
253-
return std::to_string(static_cast<bool>(lua_toboolean(L, idx)));
254-
}
255-
else
256-
{
257-
return "nil";
258-
}
259-
}
260-
261299
int set_default_config(lua_State* L)
262300
{
263301
int top = lua_gettop(L);
@@ -271,7 +309,7 @@ int set_default_config(lua_State* L)
271309
{
272310
try
273311
{
274-
std::map<std::string, std::string, std::less<>> configMap;
312+
LuaCodeFormat::ConfigMap configMap;
275313

276314
lua_pushnil(L);
277315
while (lua_next(L, - 2) != 0)

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ void LuaCodeFormat::RemoveCodeStyle(const std::string& workspaceUri)
5353
}
5454
}
5555

56-
void LuaCodeFormat::SetDefaultCodeStyle(std::map<std::string, std::string, std::less<>>& configMap)
56+
void LuaCodeFormat::SetDefaultCodeStyle(ConfigMap& configMap)
5757
{
5858
if (!configMap.empty())
5959
{
6060
LuaEditorConfig::ParseFromSection(_defaultOptions, configMap);
6161
}
6262
}
6363

64-
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text)
64+
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text, ConfigMap& configMap)
6565
{
6666
auto parser = LuaParser::LoadFromBuffer(std::move(text));
6767
parser->BuildAstWithComment();
@@ -71,13 +71,34 @@ std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text)
7171
return "";
7272
}
7373
auto options = GetOptions(uri);
74-
LuaFormatter formatter(parser, *options);
75-
formatter.BuildFormattedElement();
74+
if (configMap.empty())
75+
{
76+
LuaFormatter formatter(parser, *options);
77+
formatter.BuildFormattedElement();
78+
return formatter.GetFormattedText();
79+
}
80+
else
81+
{
82+
LuaCodeStyleOptions tempOptions = *options;
83+
if (configMap.count("insertSpaces"))
84+
{
85+
tempOptions.indent_style = configMap.at("insertSpaces") == "true"
86+
? IndentStyle::Space
87+
: IndentStyle::Tab;
88+
}
89+
if(configMap.count("tabSize"))
90+
{
91+
tempOptions.tab_width = std::stoi(configMap.at("tabSize"));
92+
}
7693

77-
return formatter.GetFormattedText();
94+
LuaFormatter formatter(parser, tempOptions);
95+
formatter.BuildFormattedElement();
96+
return formatter.GetFormattedText();
97+
}
7898
}
7999

80-
std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text)
100+
std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text,
101+
ConfigMap& configMap)
81102
{
82103
auto parser = LuaParser::LoadFromBuffer(std::move(text));
83104
parser->BuildAstWithComment();
@@ -87,10 +108,34 @@ std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& r
87108
return "";
88109
}
89110
auto options = GetOptions(uri);
90-
LuaFormatter formatter(parser, *options);
91-
formatter.BuildFormattedElement();
92111

93-
return formatter.GetRangeFormattedText(range);
112+
if (configMap.empty())
113+
{
114+
LuaFormatter formatter(parser, *options);
115+
formatter.BuildFormattedElement();
116+
117+
return formatter.GetRangeFormattedText(range);
118+
}
119+
else
120+
{
121+
LuaCodeStyleOptions tempOptions = *options;
122+
if (configMap.count("insertSpaces"))
123+
{
124+
tempOptions.indent_style = configMap.at("insertSpaces") == "true"
125+
? IndentStyle::Space
126+
: IndentStyle::Tab;
127+
}
128+
if (configMap.count("tabSize"))
129+
{
130+
tempOptions.tab_width = std::stoi(configMap.at("tabSize"));
131+
}
132+
133+
LuaFormatter formatter(parser, tempOptions);
134+
formatter.BuildFormattedElement();
135+
136+
return formatter.GetRangeFormattedText(range);
137+
}
138+
94139
}
95140

96141
std::pair<bool, std::vector<LuaDiagnosisInfo>> LuaCodeFormat::Diagnose(const std::string& uri, std::string&& text)

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
class LuaCodeFormat
1313
{
1414
public:
15+
using ConfigMap = std::map<std::string, std::string, std::less<>>;
1516
static LuaCodeFormat& GetInstance();
1617

1718
LuaCodeFormat();
1819

1920
void UpdateCodeStyle(const std::string& workspaceUri, const std::string& configPath);
2021
void RemoveCodeStyle(const std::string& workspaceUri);
21-
void SetDefaultCodeStyle(std::map<std::string, std::string, std::less<>>& configMap);
22+
void SetDefaultCodeStyle(ConfigMap& configMap);
2223

23-
std::string Reformat(const std::string& uri, std::string&& text);
24+
std::string Reformat(const std::string& uri, std::string&& text,ConfigMap& configMap);
2425

25-
std::string RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text);
26+
std::string RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text, ConfigMap& configMap);
2627

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

0 commit comments

Comments
 (0)