Skip to content

Commit 6494da8

Browse files
committed
the code_format lib api provides a default configuration about #21
1 parent 565494b commit 6494da8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,43 @@ int diagnose_file(lua_State* L)
235235
}
236236

237237

238+
int set_default_config(lua_State* L)
239+
{
240+
int top = lua_gettop(L);
241+
242+
if (top < 1)
243+
{
244+
return 0;
245+
}
246+
247+
if (lua_isstring(L, 1))
248+
{
249+
try
250+
{
251+
std::string configPath = lua_tostring(L, 1);
252+
LuaCodeFormat::GetInstance().SetDefaultCodeStyle(configPath);
253+
lua_pushboolean(L, true);
254+
return 1;
255+
}
256+
catch (std::exception& e)
257+
{
258+
std::string err = e.what();
259+
lua_settop(L, top);
260+
lua_pushboolean(L, false);
261+
lua_pushlstring(L, err.c_str(), err.size());
262+
return 2;
263+
}
264+
}
265+
266+
return 0;
267+
}
268+
238269
static const luaL_Reg lib[] = {
239270
{"format", format},
240271
{"range_format", range_format},
241272
{"update_config", update_config},
242273
{"diagnose_file", diagnose_file},
274+
{"set_default_config", set_default_config},
243275
{nullptr, nullptr}
244276
};
245277

CodeFormatLib/src/LuaCodeFormat.cpp

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

56+
void LuaCodeFormat::SetDefaultCodeStyle(const std::string& configPath)
57+
{
58+
auto editorconfig = LuaEditorConfig::LoadFromFile(configPath);
59+
if (editorconfig == nullptr)
60+
{
61+
return;
62+
}
63+
64+
auto defaultCodeStyle = editorconfig->Generate("");
65+
if (defaultCodeStyle)
66+
{
67+
_defaultOptions = defaultCodeStyle;
68+
}
69+
}
70+
5671
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text)
5772
{
5873
auto parser = LuaParser::LoadFromBuffer(std::move(text));

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +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);
2122

2223
std::string Reformat(const std::string& uri, std::string&& text);
2324

0 commit comments

Comments
 (0)