MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / format

Function format

CodeFormatLib/src/CodeFormatLib.cpp:64–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

62}
63
64int 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
111enum class UpdateType {
112 Created = 1,

Callers 15

CheckMethod · 0.50
DiagnosticInspectionFunction · 0.50
CheckSingleFileMethod · 0.50
CheckWorkspaceMethod · 0.50
ReformatWorkspaceMethod · 0.50
IdentifyAnalyzeMethod · 0.50
TextAnalyzeMethod · 0.50
MakeDiagnosticInfoMethod · 0.50
GetAdditionalNoteMethod · 0.50
GetIndentNoteMethod · 0.50
CompleteMissTokenMethod · 0.50

Calls 12

lua_gettopFunction · 0.85
lua_isstringFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
luaToStringFunction · 0.85
lua_pushbooleanFunction · 0.85
lua_pushlstringFunction · 0.85
lua_settopFunction · 0.85
insertMethod · 0.45
ReformatMethod · 0.45
sizeMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected