|
1 | 1 | #include "lua.hpp" |
2 | | -#include "CodeService/LuaFormatter.h" |
| 2 | +#include "LuaCodeFormat.h" |
3 | 3 |
|
4 | 4 | #ifdef MSVC |
5 | 5 | #define EXPORT __declspec(dllexport) |
6 | 6 | #else |
7 | 7 | #define EXPORT |
8 | 8 | #endif |
9 | 9 |
|
10 | | - |
11 | 10 | int format(lua_State* L) |
12 | 11 | { |
13 | 12 | int top = lua_gettop(L); |
14 | 13 |
|
15 | | - if (top == 0) |
| 14 | + if (top < 2) |
16 | 15 | { |
17 | 16 | return 0; |
18 | 17 | } |
19 | 18 |
|
20 | | - if (lua_isstring(L, 1)) |
| 19 | + if (lua_isstring(L, 1) && lua_isstring(L, 2)) |
21 | 20 | { |
22 | | - std::string buffer = lua_tostring(L, 1); |
23 | | - auto parser = LuaParser::LoadFromBuffer(std::move(buffer)); |
24 | | - parser->BuildAstWithComment(); |
25 | | - |
26 | | - if (!parser->GetErrors().empty()) |
| 21 | + try |
| 22 | + { |
| 23 | + std::string filename = lua_tostring(L, 1); |
| 24 | + std::string text = lua_tostring(L, 2); |
| 25 | + auto formattedText = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text)); |
| 26 | + if (formattedText.empty()) |
| 27 | + { |
| 28 | + lua_pushboolean(L, false); |
| 29 | + return 1; |
| 30 | + } |
| 31 | + lua_pushboolean(L, true); |
| 32 | + lua_pushlstring(L, formattedText.c_str(), formattedText.size()); |
| 33 | + return 2; |
| 34 | + } |
| 35 | + catch (std::exception& e) |
27 | 36 | { |
| 37 | + std::string err = e.what(); |
| 38 | + lua_settop(L, top); |
28 | 39 | lua_pushboolean(L, false); |
| 40 | + lua_pushlstring(L, err.c_str(), err.size()); |
| 41 | + return 2; |
| 42 | + } |
| 43 | + } |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +enum class UpdateType |
| 48 | +{ |
| 49 | + Created = 1, |
| 50 | + Changed = 2, |
| 51 | + Deleted = 3 |
| 52 | +}; |
| 53 | + |
| 54 | +int range_format(lua_State* L) |
| 55 | +{ |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +int update_config(lua_State* L) |
| 60 | +{ |
| 61 | + int top = lua_gettop(L); |
| 62 | + |
| 63 | + if (top < 3) |
| 64 | + { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + if (lua_isinteger(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3)) |
| 69 | + { |
| 70 | + try { |
| 71 | + auto type = static_cast<UpdateType>(lua_tointeger(L, 1)); |
| 72 | + std::string workspaceUri = lua_tostring(L, 2); |
| 73 | + std::string configPath = lua_tostring(L, 3); |
| 74 | + switch (type) |
| 75 | + { |
| 76 | + case UpdateType::Created: |
| 77 | + case UpdateType::Changed: |
| 78 | + { |
| 79 | + LuaCodeFormat::GetInstance().UpdateCodeStyle(workspaceUri, configPath); |
| 80 | + break; |
| 81 | + } |
| 82 | + case UpdateType::Deleted: |
| 83 | + { |
| 84 | + LuaCodeFormat::GetInstance().RemoveCodeStyle(workspaceUri); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + lua_pushboolean(L, true); |
29 | 90 | return 1; |
30 | 91 | } |
31 | | - LuaFormatOptions options; |
32 | | - LuaFormatter formatter(parser, options); |
33 | | - formatter.BuildFormattedElement(); |
34 | | - |
35 | | - std::string formattedText = formatter.GetFormattedText(); |
36 | | - lua_pushboolean(L, true); |
37 | | - lua_pushlstring(L, formattedText.c_str(), formattedText.size()); |
38 | | - return 2; |
| 92 | + catch (std::exception& e) |
| 93 | + { |
| 94 | + std::string err = e.what(); |
| 95 | + lua_settop(L, top); |
| 96 | + lua_pushboolean(L, false); |
| 97 | + lua_pushlstring(L, err.c_str(), err.size()); |
| 98 | + return 2; |
| 99 | + } |
39 | 100 | } |
| 101 | + |
40 | 102 | return 0; |
41 | 103 | } |
42 | 104 |
|
43 | 105 | static const luaL_Reg lib[] = { |
44 | | - {"format",format}, |
| 106 | + {"format", format}, |
| 107 | + {"update_config", update_config}, |
45 | 108 | {nullptr, nullptr} |
46 | 109 | }; |
47 | 110 |
|
48 | | - |
49 | 111 | extern "C" |
50 | 112 | EXPORT int luaopen_code_format(lua_State* L) |
51 | 113 | { |
|
0 commit comments