| 115 | }; |
| 116 | |
| 117 | int range_format(lua_State *L) { |
| 118 | int top = lua_gettop(L); |
| 119 | |
| 120 | if (top < 4) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | if (lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isinteger(L, 3) && lua_isinteger(L, 4)) { |
| 125 | try { |
| 126 | std::string filename = lua_tostring(L, 1); |
| 127 | std::string text = lua_tostring(L, 2); |
| 128 | auto startLine = lua_tointeger(L, 3); |
| 129 | auto endLine = lua_tointeger(L, 4); |
| 130 | |
| 131 | if (startLine < 0 || endLine < 0) { |
| 132 | lua_pushboolean(L, false); |
| 133 | lua_pushstring(L, "start line or end line < 0"); |
| 134 | return 2; |
| 135 | } |
| 136 | |
| 137 | LuaCodeFormat::ConfigMap configMap; |
| 138 | |
| 139 | if (top == 5 && lua_istable(L, 5)) { |
| 140 | lua_pushnil(L); |
| 141 | while (lua_next(L, -2) != 0) { |
| 142 | auto key = luaToString(L, -2); |
| 143 | auto value = luaToString(L, -1); |
| 144 | |
| 145 | if (key != "nil") { |
| 146 | configMap.insert({key, value}); |
| 147 | } |
| 148 | |
| 149 | lua_pop(L, 1); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | FormatRange range(static_cast<std::size_t>(startLine), static_cast<std::size_t>(endLine)); |
| 154 | auto formattedTextResult = LuaCodeFormat::GetInstance().RangeFormat(filename, range, std::move(text), |
| 155 | configMap); |
| 156 | if (formattedTextResult.Type == ResultType::Err) { |
| 157 | lua_pushboolean(L, false); |
| 158 | return 1; |
| 159 | } |
| 160 | auto &formattedText = formattedTextResult.Data; |
| 161 | if (formattedText.empty()) { |
| 162 | lua_pushboolean(L, false); |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | lua_pushboolean(L, true); |
| 167 | lua_pushlstring(L, formattedText.c_str(), formattedText.size()); |
| 168 | lua_pushinteger(L, range.StartLine); |
| 169 | lua_pushinteger(L, range.EndLine); |
| 170 | |
| 171 | return 4; |
| 172 | } catch (std::exception &e) { |
| 173 | std::string err = e.what(); |
| 174 | lua_settop(L, top); |
nothing calls this directly
no test coverage detected