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

Function range_format

CodeFormatLib/src/CodeFormatLib.cpp:117–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115};
116
117int 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);

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.85
lua_isstringFunction · 0.85
lua_pushbooleanFunction · 0.85
lua_pushstringFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
luaToStringFunction · 0.85
lua_pushlstringFunction · 0.85
lua_pushintegerFunction · 0.85
lua_settopFunction · 0.85
lua_isintegerFunction · 0.70
insertMethod · 0.45

Tested by

no test coverage detected