Skip to content

Commit 9bf7c68

Browse files
committed
支持从lua中读取namestyle 配置信息
1 parent d577b77 commit 9bf7c68

File tree

3 files changed

+73
-29
lines changed

3 files changed

+73
-29
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ int type_format(lua_State *L) {
219219
}
220220
}
221221
auto typeFormatResult = LuaCodeFormat::GetInstance()
222-
.TypeFormat(filename,
223-
static_cast<std::size_t>(line), static_cast<std::size_t>(character),
224-
std::move(text), configMap, stringTypeOptions);
222+
.TypeFormat(filename,
223+
static_cast<std::size_t>(line), static_cast<std::size_t>(character),
224+
std::move(text), configMap, stringTypeOptions);
225225

226226
if (typeFormatResult.Type == ResultType::Err) {
227227
lua_pushboolean(L, false);
@@ -576,6 +576,42 @@ int spell_analysis(lua_State *L) {
576576
return 0;
577577
}
578578

579+
InfoNode CreateFromLua(InfoTree &t, lua_State *L, int idx) {
580+
if (lua_istable(L, idx)) {
581+
// lua 不区分 数组和table, 太难了
582+
auto len = luaL_len(L, idx);
583+
if (len != 0) {
584+
auto arr = t.CreateArray();
585+
for (auto i = 0; i < len; i++) {
586+
if (lua_geti(L, idx, i)) {
587+
arr.AddChild(CreateFromLua(t, L, -1));
588+
}
589+
lua_pop(L, 1);
590+
}
591+
return arr;
592+
} else {
593+
auto object = t.CreateObject();
594+
lua_pushnil(L);
595+
while (lua_next(L, idx) != 0) {
596+
if (lua_isstring(L, -2)) {
597+
auto key = luaToString(L, -2);
598+
object.AddChild(key, CreateFromLua(t, L, -1));
599+
}
600+
lua_pop(L, 1);
601+
}
602+
return object;
603+
}
604+
} else if (lua_isnumber(L, idx)) {
605+
return t.CreateNumber(lua_tonumber(L, idx));
606+
} else if (lua_isstring(L, idx)) {
607+
return t.CreateString(lua_tostring(L, idx));
608+
} else if (lua_isboolean(L, idx)) {
609+
return t.CreateBool(lua_toboolean(L, idx));
610+
} else {
611+
return t.CreateNone();
612+
}
613+
}
614+
579615
int update_name_style_config(lua_State *L) {
580616
int top = lua_gettop(L);
581617

@@ -585,20 +621,18 @@ int update_name_style_config(lua_State *L) {
585621

586622
if (lua_istable(L, 1)) {
587623
try {
624+
InfoTree tree;
625+
auto root = tree.GetRoot();
626+
lua_pushnil(L);
627+
while (lua_next(L, -2) != 0) {
628+
if (lua_isstring(L, -2)) {
629+
auto key = luaToString(L, -2);
630+
root.AddChild(key, CreateFromLua(tree, L, -1));
631+
}
632+
lua_pop(L, 1);
633+
}
588634

589-
// lua_pushnil(L);
590-
// while (lua_next(L, -2) != 0) {
591-
// auto key = luaToString(L, -2);
592-
// auto value = luaToString(L, -1);
593-
//
594-
// if (key != "nil") {
595-
// configMap.insert({key, value});
596-
// }
597-
//
598-
// lua_pop(L, 1);
599-
// }
600-
//
601-
// LuaCodeFormat::GetInstance().SetDefaultCodeStyle(configMap);
635+
LuaCodeFormat::GetInstance().UpdateDiagnosticStyle(tree);
602636
lua_pushboolean(L, true);
603637
return 1;
604638
} catch (std::exception &e) {
@@ -685,20 +719,20 @@ int spell_suggest(lua_State *L) {
685719
}
686720

687721
static const luaL_Reg lib[] = {
688-
{"format", format },
689-
{"range_format", range_format },
690-
{"type_format", type_format },
691-
{"update_config", update_config },
692-
{"diagnose_file", diagnose_file },
693-
{"set_default_config", set_default_config },
694-
{"spell_load_dictionary_from_path", spell_load_dictionary_from_path },
722+
{"format", format},
723+
{"range_format", range_format},
724+
{"type_format", type_format},
725+
{"update_config", update_config},
726+
{"diagnose_file", diagnose_file},
727+
{"set_default_config", set_default_config},
728+
{"spell_load_dictionary_from_path", spell_load_dictionary_from_path},
695729
{"spell_load_dictionary_from_buffer", spell_load_dictionary_from_buffer},
696-
{"spell_analysis", spell_analysis },
697-
{"spell_suggest", spell_suggest },
698-
{"set_nonstandard_symbol", set_nonstandard_symbol },
699-
{"name_style_analysis", name_style_analysis },
700-
{"update_name_style_config", update_name_style_config },
701-
{nullptr, nullptr }
730+
{"spell_analysis", spell_analysis},
731+
{"spell_suggest", spell_suggest},
732+
{"set_nonstandard_symbol", set_nonstandard_symbol},
733+
{"name_style_analysis", name_style_analysis},
734+
{"update_name_style_config", update_name_style_config},
735+
{nullptr, nullptr}
702736
};
703737

704738
extern "C" EXPORT int luaopen_code_format(lua_State *L) {

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ void LuaCodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::
3030
config.Editorconfig->Parse();
3131
}
3232

33+
void LuaCodeFormat::UpdateDiagnosticStyle(InfoTree &tree) {
34+
_diagnosticStyle.ParseTree(tree);
35+
_diagnosticStyle.code_style_check = true;
36+
_diagnosticStyle.name_style_check = true;
37+
_diagnosticStyle.spell_check = true;
38+
}
39+
3340
void LuaCodeFormat::RemoveCodeStyle(const std::string &workspaceUri) {
3441
for (auto it = _configs.begin(); it != _configs.end(); it++) {
3542
if (it->Workspace == workspaceUri) {
@@ -282,3 +289,4 @@ void LuaCodeFormat::CalculateTempStyle(LuaStyle &style, ConfigMap &configMap) {
282289
}
283290
}
284291
}
292+

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class LuaCodeFormat {
2323

2424
void UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath);
2525

26+
void UpdateDiagnosticStyle(InfoTree& tree);
27+
2628
void RemoveCodeStyle(const std::string &workspaceUri);
2729

2830
void SetDefaultCodeStyle(ConfigMap &configMap);

0 commit comments

Comments
 (0)