Skip to content

Commit 87836c8

Browse files
committed
修复CodeFormatLib没有处理错误的bug
1 parent f88dcb0 commit 87836c8

File tree

4 files changed

+89
-32
lines changed

4 files changed

+89
-32
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ int format(lua_State *L) {
7575
}
7676
}
7777

78-
auto formattedText = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text), configMap);
79-
if (formattedText.empty()) {
78+
auto formattedTextResult = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text), configMap);
79+
if (formattedTextResult.Type == ResultType::Err) {
8080
lua_pushboolean(L, false);
8181
return 1;
8282
}
83+
auto &formattedText = formattedTextResult.Data;
8384
lua_pushboolean(L, true);
8485
lua_pushlstring(L, formattedText.c_str(), formattedText.size());
8586
return 2;
@@ -138,11 +139,13 @@ int range_format(lua_State *L) {
138139
}
139140

140141
FormatRange range(static_cast<std::size_t>(startLine), static_cast<std::size_t>(endLine));
141-
auto formattedText = LuaCodeFormat::GetInstance().RangeFormat(filename, range, std::move(text), configMap);
142-
if (formattedText.empty()) {
142+
auto formattedTextResult = LuaCodeFormat::GetInstance().RangeFormat(filename, range, std::move(text),
143+
configMap);
144+
if (formattedTextResult.Type == ResultType::Err) {
143145
lua_pushboolean(L, false);
144146
return 1;
145147
}
148+
auto &formattedText = formattedTextResult.Data;
146149
lua_pushboolean(L, true);
147150
lua_pushlstring(L, formattedText.c_str(), formattedText.size());
148151
lua_pushinteger(L, range.StartLine);
@@ -217,13 +220,14 @@ int type_format(lua_State *L) {
217220
static_cast<std::size_t>(line), static_cast<std::size_t>(character),
218221
std::move(text), configMap, stringTypeOptions);
219222

220-
if (typeFormatResult.empty()) {
223+
if (typeFormatResult.Type == ResultType::Err) {
221224
lua_pushboolean(L, false);
222225
return 1;
223226
} else {
224227
lua_pushboolean(L, true);
225-
if (!typeFormatResult.empty()) {
226-
auto &result = typeFormatResult.front();
228+
auto &typeFormats = typeFormatResult.Data;
229+
if (!typeFormats.empty()) {
230+
auto &result = typeFormats.front();
227231
// 结果
228232
lua_newtable(L);
229233

@@ -393,14 +397,15 @@ int diagnose_file(lua_State *L) {
393397
try {
394398
std::string filename = lua_tostring(L, 1);
395399
std::string text = lua_tostring(L, 2);
396-
auto results = LuaCodeFormat::GetInstance().Diagnostic(filename, std::move(text));
397-
if (!results.empty()) {
400+
auto diagnosticResult = LuaCodeFormat::GetInstance().Diagnostic(filename, std::move(text));
401+
if (diagnosticResult.Type == ResultType::Err) {
398402
lua_pushboolean(L, false);
399403
return 1;
400404
}
401405

406+
auto& diagnostics = diagnosticResult.Data;
402407
lua_pushboolean(L, true);
403-
PushDiagnosticToLua(L, results);
408+
PushDiagnosticToLua(L, diagnostics);
404409

405410
return 2;
406411
}
@@ -551,10 +556,15 @@ int spell_analysis(lua_State *L) {
551556
}
552557
}
553558

554-
auto results = LuaCodeFormat::GetInstance().SpellCheck(filename, std::move(text), tempDict);
559+
auto spellResult = LuaCodeFormat::GetInstance().SpellCheck(filename, std::move(text), tempDict);
560+
if(spellResult.Type == ResultType::Err){
561+
lua_pushboolean(L, false);
562+
return 1;
563+
}
555564

565+
auto& diagnostics = spellResult.Data;
556566
lua_pushboolean(L, true);
557-
PushDiagnosticToLua(L, results);
567+
PushDiagnosticToLua(L, diagnostics);
558568

559569
return 2;
560570
}
@@ -581,10 +591,15 @@ int name_style_analysis(lua_State *L) {
581591
std::string filename = lua_tostring(L, 1);
582592
std::string text = lua_tostring(L, 2);
583593

584-
auto results = LuaCodeFormat::GetInstance().NameStyleCheck(filename, std::move(text));
594+
auto nameStyleResult = LuaCodeFormat::GetInstance().NameStyleCheck(filename, std::move(text));
595+
if(nameStyleResult.Type == ResultType::Err){
596+
lua_pushboolean(L, false);
597+
return 1;
598+
}
585599

600+
auto& diagnostics = nameStyleResult.Data;
586601
lua_pushboolean(L, true);
587-
PushDiagnosticToLua(L, results);
602+
PushDiagnosticToLua(L, diagnostics);
588603

589604
return 2;
590605
}

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@ void LuaCodeFormat::LoadSpellDictionaryFromBuffer(const std::string &buffer) {
5959
_spellChecker.LoadDictionaryFromBuffer(buffer);
6060
}
6161

62-
std::string LuaCodeFormat::Reformat(const std::string &uri, std::string &&text, ConfigMap &configMap) {
62+
Result<std::string> LuaCodeFormat::Reformat(const std::string &uri, std::string &&text, ConfigMap &configMap) {
6363
auto file = std::make_shared<LuaFile>(std::move(text));
6464
LuaLexer luaLexer(file);
6565
luaLexer.Parse();
6666

6767
LuaParser p(file, std::move(luaLexer.GetTokens()));
6868
p.Parse();
6969

70+
if (p.HasError()) {
71+
return ResultType::Err;
72+
}
73+
7074
LuaSyntaxTree t;
7175
t.BuildTree(p);
7276

@@ -78,16 +82,20 @@ std::string LuaCodeFormat::Reformat(const std::string &uri, std::string &&text,
7882
return f.GetFormatResult(t);
7983
}
8084

81-
std::string LuaCodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
82-
std::string &&text,
83-
ConfigMap &configMap) {
85+
Result<std::string> LuaCodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
86+
std::string &&text,
87+
ConfigMap &configMap) {
8488
auto file = std::make_shared<LuaFile>(std::move(text));
8589
LuaLexer luaLexer(file);
8690
luaLexer.Parse();
8791

8892
LuaParser p(file, std::move(luaLexer.GetTokens()));
8993
p.Parse();
9094

95+
if (p.HasError()) {
96+
return ResultType::Err;
97+
}
98+
9199
LuaSyntaxTree t;
92100
t.BuildTree(p);
93101

@@ -101,7 +109,7 @@ std::string LuaCodeFormat::RangeFormat(const std::string &uri, FormatRange &rang
101109
return formattedText;
102110
}
103111

104-
std::vector<LuaTypeFormat::Result>
112+
Result<std::vector<LuaTypeFormat::Result>>
105113
LuaCodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text,
106114
ConfigMap &configMap, ConfigMap &stringTypeOptions) {
107115
auto file = std::make_shared<LuaFile>(std::move(text));
@@ -111,6 +119,10 @@ LuaCodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t
111119
LuaParser p(file, std::move(luaLexer.GetTokens()));
112120
p.Parse();
113121

122+
if (p.HasError()) {
123+
return ResultType::Err;
124+
}
125+
114126
LuaSyntaxTree t;
115127
t.BuildTree(p);
116128

@@ -124,14 +136,18 @@ LuaCodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t
124136
return tf.GetResult();
125137
}
126138

127-
std::vector<LuaDiagnosticInfo> LuaCodeFormat::Diagnostic(const std::string &uri, std::string &&text) {
139+
Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::Diagnostic(const std::string &uri, std::string &&text) {
128140
auto file = std::make_shared<LuaFile>(std::move(text));
129141
LuaLexer luaLexer(file);
130142
luaLexer.Parse();
131143

132144
LuaParser p(file, std::move(luaLexer.GetTokens()));
133145
p.Parse();
134146

147+
if (p.HasError()) {
148+
return ResultType::Err;
149+
}
150+
135151
LuaSyntaxTree t;
136152
t.BuildTree(p);
137153

@@ -142,15 +158,19 @@ std::vector<LuaDiagnosticInfo> LuaCodeFormat::Diagnostic(const std::string &uri,
142158
return MakeDiagnosticInfo(diagnosticBuilder.GetDiagnosticResults(t), file);
143159
}
144160

145-
std::vector<LuaDiagnosticInfo> LuaCodeFormat::SpellCheck(const std::string &uri, std::string &&text,
146-
const CodeSpellChecker::CustomDictionary &tempDict) {
161+
Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::SpellCheck(const std::string &uri, std::string &&text,
162+
const CodeSpellChecker::CustomDictionary &tempDict) {
147163
auto file = std::make_shared<LuaFile>(std::move(text));
148164
LuaLexer luaLexer(file);
149165
luaLexer.Parse();
150166

151167
LuaParser p(file, std::move(luaLexer.GetTokens()));
152168
p.Parse();
153169

170+
if (p.HasError()) {
171+
return ResultType::Err;
172+
}
173+
154174
LuaSyntaxTree t;
155175
t.BuildTree(p);
156176

@@ -163,14 +183,18 @@ std::vector<LuaDiagnosticInfo> LuaCodeFormat::SpellCheck(const std::string &uri,
163183
return MakeDiagnosticInfo(diagnosticBuilder.GetDiagnosticResults(t), file);
164184
}
165185

166-
std::vector<LuaDiagnosticInfo> LuaCodeFormat::NameStyleCheck(const std::string &uri, std::string &&text) {
186+
Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::NameStyleCheck(const std::string &uri, std::string &&text) {
167187
auto file = std::make_shared<LuaFile>(std::move(text));
168188
LuaLexer luaLexer(file);
169189
luaLexer.Parse();
170190

171191
LuaParser p(file, std::move(luaLexer.GetTokens()));
172192
p.Parse();
173193

194+
if (p.HasError()) {
195+
return ResultType::Err;
196+
}
197+
174198
LuaSyntaxTree t;
175199
t.BuildTree(p);
176200

@@ -247,8 +271,8 @@ void LuaCodeFormat::CalculateTempStyle(LuaStyle &style, ConfigMap &configMap) {
247271

248272
if (configMap.count("insertSpaces")) {
249273
style.indent_style = configMap.at("insertSpaces") == "true"
250-
? IndentStyle::Space
251-
: IndentStyle::Tab;
274+
? IndentStyle::Space
275+
: IndentStyle::Tab;
252276
}
253277
if (configMap.count("tabSize")) {
254278
if (style.indent_style == IndentStyle::Tab) {

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ class LuaCodeFormat {
3333

3434
void LoadSpellDictionaryFromBuffer(const std::string &buffer);
3535

36-
std::string Reformat(const std::string &uri, std::string &&text, ConfigMap &configMap);
36+
Result<std::string> Reformat(const std::string &uri, std::string &&text, ConfigMap &configMap);
3737

38-
std::string RangeFormat(const std::string &uri, FormatRange &range, std::string &&text, ConfigMap &configMap);
38+
Result<std::string> RangeFormat(const std::string &uri, FormatRange &range, std::string &&text, ConfigMap &configMap);
3939

40-
std::vector<LuaTypeFormat::Result>
40+
Result<std::vector<LuaTypeFormat::Result>>
4141
TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text,
4242
ConfigMap &configMap, ConfigMap &stringTypeOptions);
4343

44-
std::vector<LuaDiagnosticInfo> Diagnostic(const std::string &uri, std::string &&text);
44+
Result<std::vector<LuaDiagnosticInfo>> Diagnostic(const std::string &uri, std::string &&text);
4545

46-
std::vector<LuaDiagnosticInfo> SpellCheck(const std::string &uri, std::string &&text,
46+
Result<std::vector<LuaDiagnosticInfo>> SpellCheck(const std::string &uri, std::string &&text,
4747
const CodeSpellChecker::CustomDictionary &tempDict);
4848

49-
std::vector<LuaDiagnosticInfo> NameStyleCheck(const std::string &uri, std::string &&text);
49+
Result<std::vector<LuaDiagnosticInfo>> NameStyleCheck(const std::string &uri, std::string &&text);
5050

5151
std::vector<SuggestItem> SpellCorrect(const std::string &word);
5252

CodeFormatLib/src/Types.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,22 @@ struct LuaDiagnosticInfo {
1919
Position End;
2020
std::string Message;
2121
std::string Data;
22-
};
22+
};
23+
24+
enum class ResultType {
25+
Ok,
26+
Err
27+
};
28+
29+
template<class T>
30+
class Result {
31+
public:
32+
Result(T &&d)
33+
: Data(d) {}
34+
35+
Result(ResultType type)
36+
: Type(type) {}
37+
38+
ResultType Type;
39+
T Data;
40+
};

0 commit comments

Comments
 (0)