Skip to content

Commit 2d43a90

Browse files
committed
使诊断更有可读性
1 parent c202fb3 commit 2d43a90

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

CodeFormat/src/LuaFormat.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ bool IsSubRelative(std::filesystem::path& path, std::filesystem::path base)
4141
{
4242
auto relative = std::filesystem::relative(path, base);
4343
auto relativeString = relative.string();
44-
if(relativeString.empty())
44+
if (relativeString.empty())
4545
{
4646
return false;
4747
}
48-
if(relativeString == ".")
48+
if (relativeString == ".")
4949
{
5050
return true;
5151
}
@@ -54,30 +54,29 @@ bool IsSubRelative(std::filesystem::path& path, std::filesystem::path base)
5454

5555
void LuaFormat::AutoDetectConfigRoot(std::string_view root)
5656
{
57-
if(_inputFile.empty())
57+
if (_inputFile.empty())
5858
{
5959
return;
6060
}
6161
std::filesystem::path workspace(root);
6262
std::filesystem::path inputPath(_inputFile);
63-
if(!IsSubRelative(inputPath, workspace))
63+
if (!IsSubRelative(inputPath, workspace))
6464
{
6565
return;
6666
}
6767

6868
auto directory = inputPath.parent_path();
69-
while(IsSubRelative(directory, workspace))
69+
while (IsSubRelative(directory, workspace))
7070
{
7171
auto editorconfigPath = directory / ".editorconfig";
72-
if(std::filesystem::exists(editorconfigPath))
72+
if (std::filesystem::exists(editorconfigPath))
7373
{
7474
SetConfigPath(editorconfigPath.string());
7575
return;
7676
}
7777

7878
directory = directory.parent_path();
7979
}
80-
8180
}
8281

8382
void LuaFormat::SetConfigPath(std::string_view config)
@@ -115,7 +114,7 @@ bool LuaFormat::Reformat()
115114
}
116115
}
117116

118-
if(_outFile.empty())
117+
if (_outFile.empty())
119118
{
120119
_options->end_of_line = "\n";
121120
}
@@ -143,6 +142,16 @@ bool LuaFormat::Check()
143142

144143
if (_parser->HasError())
145144
{
145+
auto errors = _parser->GetErrors();
146+
147+
std::cerr << format("Check {}\t{} error", _inputFile, errors.size()) << std::endl;
148+
149+
for (auto& error : errors)
150+
{
151+
auto luaFile = _parser->GetLuaFile();
152+
DiagnosisInspection(error.ErrorMessage, error.ErrorRange, luaFile);
153+
}
154+
146155
return false;
147156
}
148157

@@ -161,12 +170,34 @@ bool LuaFormat::Check()
161170
auto diagnosis = formatter.GetDiagnosisInfos();
162171
if (!diagnosis.empty())
163172
{
173+
std::cerr << format("Check {}\t{} warning", _inputFile, diagnosis.size()) << std::endl;
174+
164175
for (auto& d : diagnosis)
165176
{
166-
std::cout << format("{} from {}:{} to {}:{}", d.Message, d.Range.Start.Line, d.Range.Start.Character,
167-
d.Range.End.Line, d.Range.End.Character) << std::endl;
177+
auto luaFile = _parser->GetLuaFile();
178+
179+
DiagnosisInspection(d.Message, TextRange(
180+
luaFile->GetOffsetFromPosition(d.Range.Start.Line, d.Range.Start.Character),
181+
luaFile->GetOffsetFromPosition(d.Range.End.Line, d.Range.End.Character)
182+
), luaFile);
168183
}
184+
169185
return false;
170186
}
187+
std::cout << format("Check {} OK", _inputFile) << std::endl;
171188
return true;
172189
}
190+
191+
void LuaFormat::DiagnosisInspection(std::string_view message, TextRange range, std::shared_ptr<LuaFile> file)
192+
{
193+
std::string_view source = file->GetSource();
194+
auto startLine = file->GetLine(range.StartOffset);
195+
auto startChar = file->GetColumn(range.StartOffset);
196+
auto endLine = file->GetLine(range.EndOffset);
197+
auto endChar = file->GetColumn(range.EndOffset);
198+
std::cerr << format("{}({}:{} to {}:{}): {}", file->GetFilename(), startLine + 1, startChar, endLine + 1, endChar,
199+
message) << std::endl;
200+
201+
202+
203+
}

CodeFormat/src/LuaFormat.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "LuaParser/LuaParser.h"
99
#include "CodeService/LuaCodeStyleOptions.h"
10+
#include "CodeService/LuaDiagnosisInfo.h"
1011

1112
class LuaFormat
1213
{
@@ -23,12 +24,15 @@ class LuaFormat
2324

2425
void SetConfigPath(std::string_view config);
2526

26-
void SetDefaultOptions(std::map<std::string, std::string,std::less<>>& keyValues);
27+
void SetDefaultOptions(std::map<std::string, std::string, std::less<>>& keyValues);
2728

2829
bool Reformat();
2930

3031
bool Check();
3132

33+
void DiagnosisInspection(std::string_view message, TextRange range, std::shared_ptr<LuaFile> file);
34+
35+
3236
private:
3337
std::string _inputFile;
3438
std::string _outFile;

0 commit comments

Comments
 (0)