Skip to content

Commit aeb3b0f

Browse files
committed
codeformatlib新增诊断
1 parent f14bdcd commit aeb3b0f

File tree

3 files changed

+116
-3
lines changed

3 files changed

+116
-3
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "lua.hpp"
1+
#include "lua.hpp"
22
#include "LuaCodeFormat.h"
33

44
#ifdef MSVC
@@ -142,10 +142,104 @@ int update_config(lua_State* L)
142142
return 0;
143143
}
144144

145+
int diagnose_file(lua_State* L)
146+
{
147+
int top = lua_gettop(L);
148+
149+
if (top < 2)
150+
{
151+
return 0;
152+
}
153+
154+
if (lua_isstring(L, 1) && lua_isstring(L, 2))
155+
{
156+
try
157+
{
158+
std::string filename = lua_tostring(L, 1);
159+
std::string text = lua_tostring(L, 2);
160+
auto result = LuaCodeFormat::GetInstance().Diagnose(filename, std::move(text));
161+
if (!result.first)
162+
{
163+
lua_pushboolean(L, false);
164+
return 1;
165+
}
166+
auto& diagnosticInfos = result.second;
167+
168+
lua_pushboolean(L, true);
169+
int count = 1;
170+
lua_newtable(L);
171+
for (auto& diagnosticInfo : diagnosticInfos)
172+
{
173+
// 诊断信息表
174+
lua_newtable(L);
175+
176+
//message
177+
{
178+
lua_pushstring(L, "message");
179+
lua_pushlstring(L, diagnosticInfo.Message.c_str(), diagnosticInfo.Message.size());
180+
lua_rawset(L, -3);
181+
}
182+
183+
// range
184+
{
185+
lua_pushstring(L, "range");
186+
//range table
187+
lua_newtable(L);
188+
189+
lua_pushstring(L, "start");
190+
// start table
191+
lua_newtable(L);
192+
lua_pushstring(L, "line");
193+
lua_pushinteger(L, diagnosticInfo.Range.Start.Line);
194+
lua_rawset(L, -3);
195+
196+
lua_pushstring(L, "character");
197+
lua_pushinteger(L, diagnosticInfo.Range.Start.Character);
198+
lua_rawset(L, -3);
199+
200+
lua_rawset(L, -3); // set start = {}
201+
202+
lua_pushstring(L, "end");
203+
// end table
204+
lua_newtable(L);
205+
lua_pushstring(L, "line");
206+
lua_pushinteger(L, diagnosticInfo.Range.Start.Line);
207+
lua_rawset(L, -3);
208+
209+
lua_pushstring(L, "character");
210+
lua_pushinteger(L, diagnosticInfo.Range.Start.Character);
211+
lua_rawset(L, -3);
212+
213+
lua_rawset(L, -3); // set end = {}
214+
215+
lua_rawset(L, -3); // set range = {}
216+
}
217+
218+
// 不确认lua会不会把他改成宏,所以不要在这里用++count
219+
lua_rawseti(L, -2, count);
220+
count++;
221+
}
222+
223+
return 2;
224+
}
225+
catch (std::exception& e)
226+
{
227+
std::string err = e.what();
228+
lua_settop(L, top);
229+
lua_pushboolean(L, false);
230+
lua_pushlstring(L, err.c_str(), err.size());
231+
return 2;
232+
}
233+
}
234+
return 0;
235+
}
236+
237+
145238
static const luaL_Reg lib[] = {
146239
{"format", format},
147240
{"range_format", range_format},
148241
{"update_config", update_config},
242+
{"diagnose_file", diagnose_file},
149243
{nullptr, nullptr}
150244
};
151245

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text)
6969
return formatter.GetFormattedText();
7070
}
7171

72-
std::string LuaCodeFormat::RangeFormat(const std::string uri, LuaFormatRange& range, std::string&& text)
72+
std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text)
7373
{
7474
auto parser = LuaParser::LoadFromBuffer(std::move(text));
7575
parser->BuildAstWithComment();
@@ -85,6 +85,22 @@ std::string LuaCodeFormat::RangeFormat(const std::string uri, LuaFormatRange& ra
8585
return formatter.GetRangeFormattedText(range);
8686
}
8787

88+
std::pair<bool, std::vector<LuaDiagnosisInfo>> LuaCodeFormat::Diagnose(const std::string& uri, std::string&& text)
89+
{
90+
auto parser = LuaParser::LoadFromBuffer(std::move(text));
91+
parser->BuildAstWithComment();
92+
93+
if (!parser->GetErrors().empty())
94+
{
95+
return std::make_pair(false, std::vector<LuaDiagnosisInfo>());
96+
}
97+
auto options = GetOptions(uri);
98+
LuaFormatter formatter(parser, *options);
99+
formatter.BuildFormattedElement();
100+
101+
return std::make_pair(true, formatter.GetDiagnosisInfos());
102+
}
103+
88104
std::shared_ptr<LuaCodeStyleOptions> LuaCodeFormat::GetOptions(const std::string& uri)
89105
{
90106
std::size_t matchLength = 0;

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string_view>
66

77
#include "CodeService/LuaCodeStyleOptions.h"
8+
#include "CodeService/LuaDiagnosisInfo.h"
89
#include "CodeService/LuaEditorConfig.h"
910
#include "CodeService/LuaFormatRange.h"
1011

@@ -20,7 +21,9 @@ class LuaCodeFormat
2021

2122
std::string Reformat(const std::string& uri, std::string&& text);
2223

23-
std::string RangeFormat(const std::string uri, LuaFormatRange& range, std::string&& text);
24+
std::string RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text);
25+
26+
std::pair<bool, std::vector<LuaDiagnosisInfo>> Diagnose(const std::string& uri, std::string&& text);
2427

2528
std::shared_ptr<LuaCodeStyleOptions> GetOptions(const std::string& uri);
2629
private:

0 commit comments

Comments
 (0)