|
1 | 1 | #include "lua.hpp" |
2 | 2 | #include "LuaCodeFormat.h" |
| 3 | +#include "CodeService/LuaTypeFormat.h" |
3 | 4 |
|
4 | 5 | #ifdef _MSC_VER |
5 | 6 | #define EXPORT __declspec(dllexport) |
@@ -172,6 +173,117 @@ int range_format(lua_State* L) |
172 | 173 | return 0; |
173 | 174 | } |
174 | 175 |
|
| 176 | + |
| 177 | +int type_format(lua_State* L) |
| 178 | +{ |
| 179 | + int top = lua_gettop(L); |
| 180 | + |
| 181 | + if (top < 4) |
| 182 | + { |
| 183 | + return 0; |
| 184 | + } |
| 185 | + |
| 186 | + if (lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isinteger(L, 3) && lua_isinteger(L, 4)) |
| 187 | + { |
| 188 | + try |
| 189 | + { |
| 190 | + std::string filename = lua_tostring(L, 1); |
| 191 | + std::string text = lua_tostring(L, 2); |
| 192 | + int line = lua_tointeger(L, 3); |
| 193 | + int character = lua_tointeger(L, 4); |
| 194 | + |
| 195 | + LuaCodeFormat::ConfigMap configMap; |
| 196 | + |
| 197 | + if (top == 5 && lua_istable(L, 5)) |
| 198 | + { |
| 199 | + lua_pushnil(L); |
| 200 | + while (lua_next(L, -2) != 0) |
| 201 | + { |
| 202 | + auto key = luaToString(L, -2); |
| 203 | + auto value = luaToString(L, -1); |
| 204 | + |
| 205 | + if (key != "nil") |
| 206 | + { |
| 207 | + configMap.insert({key, value}); |
| 208 | + } |
| 209 | + |
| 210 | + lua_pop(L, 1); |
| 211 | + } |
| 212 | + } |
| 213 | + auto typeFormat = LuaCodeFormat::GetInstance().TypeFormat(filename, line, character, std::move(text), |
| 214 | + configMap); |
| 215 | + |
| 216 | + if(!typeFormat.HasFormatResult()) |
| 217 | + { |
| 218 | + lua_pushboolean(L, false); |
| 219 | + return 1; |
| 220 | + } |
| 221 | + else |
| 222 | + { |
| 223 | + lua_pushboolean(L, false); |
| 224 | + auto result = typeFormat.GetResult(); |
| 225 | + |
| 226 | + // 结果 |
| 227 | + lua_newtable(L); |
| 228 | + |
| 229 | + //message |
| 230 | + { |
| 231 | + lua_pushstring(L, "newText"); |
| 232 | + lua_pushlstring(L, result.Text.c_str(), result.Text.size()); |
| 233 | + lua_rawset(L, -3); |
| 234 | + } |
| 235 | + |
| 236 | + // range |
| 237 | + { |
| 238 | + lua_pushstring(L, "range"); |
| 239 | + //range table |
| 240 | + lua_newtable(L); |
| 241 | + |
| 242 | + lua_pushstring(L, "start"); |
| 243 | + // start table |
| 244 | + lua_newtable(L); |
| 245 | + lua_pushstring(L, "line"); |
| 246 | + lua_pushinteger(L, result.Range.StartLine); |
| 247 | + lua_rawset(L, -3); |
| 248 | + |
| 249 | + lua_pushstring(L, "character"); |
| 250 | + lua_pushinteger(L, result.Range.StartCharacter); |
| 251 | + lua_rawset(L, -3); |
| 252 | + |
| 253 | + lua_rawset(L, -3); // set start = {} |
| 254 | + |
| 255 | + lua_pushstring(L, "end"); |
| 256 | + // end table |
| 257 | + lua_newtable(L); |
| 258 | + lua_pushstring(L, "line"); |
| 259 | + lua_pushinteger(L, result.Range.EndLine); |
| 260 | + lua_rawset(L, -3); |
| 261 | + |
| 262 | + lua_pushstring(L, "character"); |
| 263 | + lua_pushinteger(L, result.Range.EndCharacter); |
| 264 | + lua_rawset(L, -3); |
| 265 | + |
| 266 | + lua_rawset(L, -3); // set end = {} |
| 267 | + |
| 268 | + lua_rawset(L, -3); // set range = {} |
| 269 | + } |
| 270 | + |
| 271 | + return 2; |
| 272 | + } |
| 273 | + } |
| 274 | + catch (std::exception& e) |
| 275 | + { |
| 276 | + std::string err = e.what(); |
| 277 | + lua_settop(L, top); |
| 278 | + lua_pushboolean(L, false); |
| 279 | + lua_pushlstring(L, err.c_str(), err.size()); |
| 280 | + return 2; |
| 281 | + } |
| 282 | + } |
| 283 | + return 0; |
| 284 | +} |
| 285 | + |
| 286 | + |
175 | 287 | int update_config(lua_State* L) |
176 | 288 | { |
177 | 289 | int top = lua_gettop(L); |
|
0 commit comments