Skip to content

Commit af4cbbd

Browse files
committed
支持修改对默认配置的支持
1 parent febe122 commit af4cbbd

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,60 @@ int diagnose_file(lua_State* L)
234234
return 0;
235235
}
236236

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+
}
237260

238261
int set_default_config(lua_State* L)
239262
{
240263
int top = lua_gettop(L);
241264

242-
if (top < 1)
265+
if (top != 1)
243266
{
244267
return 0;
245268
}
246269

247-
if (lua_isstring(L, 1))
270+
if (lua_istable(L, 1))
248271
{
249272
try
250273
{
251-
std::string configPath = lua_tostring(L, 1);
252-
LuaCodeFormat::GetInstance().SetDefaultCodeStyle(configPath);
274+
std::map<std::string, std::string, std::less<>> configMap;
275+
276+
lua_pushnil(L);
277+
while (lua_next(L, - 2) != 0)
278+
{
279+
auto key = luaToString(L, -2);
280+
auto value = luaToString(L, -1);
281+
282+
if (key != "nil")
283+
{
284+
configMap.insert({key, value});
285+
}
286+
287+
lua_pop(L, 1);
288+
}
289+
290+
LuaCodeFormat::GetInstance().SetDefaultCodeStyle(configMap);
253291
lua_pushboolean(L, true);
254292
return 1;
255293
}

CodeFormatLib/src/LuaCodeFormat.cpp

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

56-
void LuaCodeFormat::SetDefaultCodeStyle(const std::string& configPath)
56+
void LuaCodeFormat::SetDefaultCodeStyle(std::map<std::string, std::string, std::less<>>& configMap)
5757
{
58-
auto editorconfig = LuaEditorConfig::LoadFromFile(configPath);
59-
if (editorconfig == nullptr)
60-
{
61-
return;
62-
}
63-
64-
auto defaultCodeStyle = editorconfig->Generate("");
65-
if (defaultCodeStyle)
58+
if (!configMap.empty())
6659
{
67-
_defaultOptions = defaultCodeStyle;
60+
LuaEditorConfig::ParseFromSection(_defaultOptions, configMap);
6861
}
6962
}
7063

CodeFormatLib/src/LuaCodeFormat.h

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

1919
void UpdateCodeStyle(const std::string& workspaceUri, const std::string& configPath);
2020
void RemoveCodeStyle(const std::string& workspaceUri);
21-
void SetDefaultCodeStyle(const std::string& configPath);
21+
void SetDefaultCodeStyle(std::map<std::string, std::string, std::less<>>& configMap);
2222

2323
std::string Reformat(const std::string& uri, std::string&& text);
2424

include/CodeService/LuaEditorConfig.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class LuaEditorConfig
1616

1717
static std::shared_ptr<LuaEditorConfig> LoadFromFile(const std::string& path);
1818

19+
static void ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> options,
20+
std::map<std::string, std::string, std::less<>>& configMap);
21+
1922
LuaEditorConfig(std::string&& source);
2023

2124
void Parse();
@@ -25,10 +28,7 @@ class LuaEditorConfig
2528
void SetWorkspace(std::string_view workspace);
2629

2730
void SetRootWorkspace(std::string_view rootWorkspace);
28-
private:
29-
void ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> options,
30-
std::map<std::string, std::string, std::less<>>& configMap);
31-
31+
private:
3232
std::string _workspace;
3333
std::string _rootWorkspace;
3434
std::string _source;

0 commit comments

Comments
 (0)