| 62 | } |
| 63 | |
| 64 | int format(lua_State *L) { |
| 65 | int top = lua_gettop(L); |
| 66 | |
| 67 | if (top < 2) { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | if (lua_isstring(L, 1) && lua_isstring(L, 2)) { |
| 72 | try { |
| 73 | std::string filename = lua_tostring(L, 1); |
| 74 | std::string text = lua_tostring(L, 2); |
| 75 | LuaCodeFormat::ConfigMap configMap; |
| 76 | |
| 77 | if (top == 3 && lua_istable(L, 3)) { |
| 78 | lua_pushnil(L); |
| 79 | while (lua_next(L, -2) != 0) { |
| 80 | auto key = luaToString(L, -2); |
| 81 | auto value = luaToString(L, -1); |
| 82 | |
| 83 | if (key != "nil") { |
| 84 | configMap.insert({key, value}); |
| 85 | } |
| 86 | |
| 87 | lua_pop(L, 1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | auto formattedTextResult = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text), configMap); |
| 92 | if (formattedTextResult.Type == ResultType::Err) { |
| 93 | lua_pushboolean(L, false); |
| 94 | return 1; |
| 95 | } |
| 96 | auto &formattedText = formattedTextResult.Data; |
| 97 | lua_pushboolean(L, true); |
| 98 | lua_pushlstring(L, formattedText.c_str(), formattedText.size()); |
| 99 | return 2; |
| 100 | } catch (std::exception &e) { |
| 101 | std::string err = e.what(); |
| 102 | lua_settop(L, top); |
| 103 | lua_pushboolean(L, false); |
| 104 | lua_pushlstring(L, err.c_str(), err.size()); |
| 105 | return 2; |
| 106 | } |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | enum class UpdateType { |
| 112 | Created = 1, |
no test coverage detected