Skip to content

Commit 803c100

Browse files
committed
提供树形信息工具
1 parent 94e4866 commit 803c100

File tree

13 files changed

+271
-159
lines changed

13 files changed

+271
-159
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ AllowShortLambdasOnASingleLine: All
1515
AllowShortLoopsOnASingleLine: true
1616
AlwaysBreakAfterReturnType: None
1717
AlwaysBreakTemplateDeclarations: Yes
18+
AlignArrayOfStructures: Left
1819
BreakBeforeBraces: Custom
1920
BraceWrapping:
2021
AfterCaseLabel: false
Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,80 @@
11
#include "CodeService/Config/LuaDiagnosticStyle.h"
2+
#include "Util/StringUtil.h"
23

3-
#define BOOL_OPTION(op) \
4+
#define BOOL_OPTION(op) \
45
if (auto n = root.GetValue(#op); n.IsBool()) { \
5-
op = n.AsBool(); \
6+
op = n.AsBool(); \
67
}
78

9+
NameStyleRule MakeNameStyle(InfoNode n) {
10+
if (n.IsString()) {
11+
auto type = n.AsString();
12+
if (type == "snake_case") {
13+
return NameStyleRule(NameStyleType::SnakeCase);
14+
} else if (type == "upper_snake_case") {
15+
return NameStyleRule(NameStyleType::UpperSnakeCase);
16+
} else if (type == "pascal_case") {
17+
return NameStyleRule(NameStyleType::PascalCase);
18+
} else if (type == "camel_case") {
19+
return NameStyleRule(NameStyleType::CamelCase);
20+
}
21+
22+
} else if (n.IsObject()) {
23+
auto typeNode = n.GetValue("type");
24+
if (typeNode.IsString()) {
25+
auto type = typeNode.AsString();
26+
if (type == "snake_case") {
27+
return NameStyleRule(NameStyleType::SnakeCase);
28+
} else if (type == "upper_snake_case") {
29+
return NameStyleRule(NameStyleType::UpperSnakeCase);
30+
} else if (type == "pascal_case") {
31+
return NameStyleRule(NameStyleType::PascalCase);
32+
} else if (type == "camel_case") {
33+
return NameStyleRule(NameStyleType::CamelCase);
34+
} else if (type == "same") {
35+
auto paramNode = n.GetValue("param");
36+
if (paramNode.IsString()) {
37+
return NameStyleRule(NameStyleType::Same, std::make_shared<SameNameStyleData>(paramNode.AsString()));
38+
}
39+
} else if (type == "pattern") {
40+
auto paramNode = n.GetValue("param");
41+
if (paramNode.IsString()) {
42+
43+
auto patternData = std::make_shared<PatternNameStyleData>();
44+
auto patternString = paramNode.AsString();
45+
try {
46+
patternData->Re = std::regex(patternString, std::regex_constants::ECMAScript);
47+
for (auto pair: n.AsMap()) {
48+
if (string_util::StartWith(pair.first, "$")) {
49+
auto istr = pair.first.substr(1);
50+
std::size_t groupId = std::stoull(istr);
51+
auto rule = pair.second.AsString();
52+
53+
if (rule == "snake_case") {
54+
patternData->GroupRules.emplace_back(groupId, NameStyleType::SnakeCase);
55+
} else if (rule == "upper_snake_case") {
56+
patternData->GroupRules.emplace_back(groupId, NameStyleType::UpperSnakeCase);
57+
} else if (rule == "pascal_case") {
58+
patternData->GroupRules.emplace_back(groupId, NameStyleType::PascalCase);
59+
} else if (rule == "camel_case") {
60+
patternData->GroupRules.emplace_back(groupId, NameStyleType::CamelCase);
61+
}
62+
}
63+
}
64+
65+
return NameStyleRule(NameStyleType::Pattern, patternData);
66+
} catch (std::exception &) {
67+
return NameStyleRule(NameStyleType::Off);
68+
}
69+
}
70+
} else {
71+
return NameStyleRule(NameStyleType::Off);
72+
}
73+
}
74+
}
75+
return NameStyleRule(NameStyleType::Off);
76+
}
77+
878
void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
979
auto root = tree.GetRoot();
1080

@@ -14,12 +84,27 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
1484

1585
BOOL_OPTION(spell_check);
1686

17-
// if (auto n = root.GetValue("local_name_style"); n.IsObject()) {
18-
// if (n.IsArray()) {
19-
// local_name_style.clear();
20-
// for (auto c: n.GetChildren()) {
21-
// if (c.IsNull())
22-
// }
23-
// }
24-
// }
87+
std::vector<std::pair<std::vector<NameStyleRule> &, std::string>> name_styles = {
88+
{local_name_style, "local_name_style" },
89+
{function_param_name_style, "function_param_name_style" },
90+
{function_name_style, "function_name_style" },
91+
{local_function_name_style, "local_function_name_style" },
92+
{table_field_name_style, "table_field_name_style" },
93+
{global_variable_name_style, "global_variable_name_style"},
94+
{module_name_style, "module_name_style" },
95+
{require_module_name_style, "require_module_name_style" },
96+
{class_name_style, "class_name_style" }
97+
};
98+
for (auto &pair: name_styles) {
99+
if (auto n = root.GetValue(pair.second); !n.IsNull()) {
100+
pair.first.clear();
101+
if (n.IsArray()) {
102+
for (auto c: n.AsArray()) {
103+
pair.first.push_back(MakeNameStyle(c));
104+
}
105+
} else {
106+
pair.first.push_back(MakeNameStyle(n));
107+
}
108+
}
109+
}
25110
}

CodeService/src/Diagnostic/NameStyle/NameStyleChecker.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#include "CodeService/Diagnostic/NameStyle/NameStyleChecker.h"
2-
#include <algorithm>
3-
#include "CodeService/Diagnostic/DiagnosticBuilder.h"
4-
#include "Util/format.h"
52
#include "CodeService/Config/LanguageTranslator.h"
3+
#include "CodeService/Diagnostic/DiagnosticBuilder.h"
64
#include "CodeService/Diagnostic/NameStyle/NameStyleRuleMatcher.h"
75
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
6+
#include "Util/format.h"
7+
#include <algorithm>
88

99
std::set<std::string, std::less<>> NameStyleChecker::TableFieldSpecialName = {
1010
"__add", "__sub", "__mul", "__div", "__mod", "__pow",
1111
"__unm", "__idiv", "__band", "__bor", "__bxor", "__bnot", "__shl",
1212
"__shr", "__concat", "__len", "__eq", "__lt", "__index", "__newindex",
13-
"__call", "__gc", "__close", "__mode", "__name"
14-
};
13+
"__call", "__gc", "__close", "__mode", "__name"};
1514

1615
std::set<std::string, std::less<>> NameStyleChecker::GlobalSpecialName = {
17-
"_G", "_ENV"
18-
};
16+
"_G", "_ENV"};
1917

2018
NameStyleChecker::NameStyleChecker() {
2119
}
@@ -333,8 +331,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
333331
d.PushDiagnostic(DiagnosticType::NameStyle,
334332
n.GetTextRange(t),
335333
MakeDiagnosticInfo("LocalVariableName", n, t,
336-
state.GetDiagnosticStyle().local_name_style)
337-
);
334+
state.GetDiagnosticStyle().local_name_style));
338335
}
339336
break;
340337
}
@@ -343,8 +340,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
343340
d.PushDiagnostic(DiagnosticType::NameStyle,
344341
n.GetTextRange(t),
345342
MakeDiagnosticInfo("ModuleName", n, t,
346-
state.GetDiagnosticStyle().module_name_style)
347-
);
343+
state.GetDiagnosticStyle().module_name_style));
348344
}
349345
break;
350346
}
@@ -356,8 +352,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
356352
d.PushDiagnostic(DiagnosticType::NameStyle,
357353
n.GetTextRange(t),
358354
MakeDiagnosticInfo("LocalFunctionName", n, t,
359-
state.GetDiagnosticStyle().local_function_name_style)
360-
);
355+
state.GetDiagnosticStyle().local_function_name_style));
361356
}
362357
break;
363358
}
@@ -369,8 +364,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
369364
d.PushDiagnostic(DiagnosticType::NameStyle,
370365
n.GetTextRange(t),
371366
MakeDiagnosticInfo("GlobalVariableDefineName", n, t,
372-
state.GetDiagnosticStyle().global_variable_name_style)
373-
);
367+
state.GetDiagnosticStyle().global_variable_name_style));
374368
}
375369
break;
376370
}
@@ -379,8 +373,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
379373
d.PushDiagnostic(DiagnosticType::NameStyle,
380374
n.GetTextRange(t),
381375
MakeDiagnosticInfo("ParamName", n, t,
382-
state.GetDiagnosticStyle().function_param_name_style)
383-
);
376+
state.GetDiagnosticStyle().function_param_name_style));
384377
}
385378
break;
386379
}
@@ -389,8 +382,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
389382
d.PushDiagnostic(DiagnosticType::NameStyle,
390383
n.GetTextRange(t),
391384
MakeDiagnosticInfo("ImportModuleName", n, t,
392-
state.GetDiagnosticStyle().require_module_name_style)
393-
);
385+
state.GetDiagnosticStyle().require_module_name_style));
394386
}
395387
break;
396388
}
@@ -399,8 +391,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
399391
d.PushDiagnostic(DiagnosticType::NameStyle,
400392
n.GetTextRange(t),
401393
MakeDiagnosticInfo("ClassVariableName", n, t,
402-
state.GetDiagnosticStyle().class_name_style)
403-
);
394+
state.GetDiagnosticStyle().class_name_style));
404395
}
405396
break;
406397
}
@@ -413,8 +404,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
413404
d.PushDiagnostic(DiagnosticType::NameStyle,
414405
n.GetTextRange(t),
415406
MakeDiagnosticInfo("FunctionDefineName", n, t,
416-
state.GetDiagnosticStyle().function_name_style)
417-
);
407+
state.GetDiagnosticStyle().function_name_style));
418408
}
419409
break;
420410
}
@@ -427,8 +417,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
427417
d.PushDiagnostic(DiagnosticType::NameStyle,
428418
n.GetTextRange(t),
429419
MakeDiagnosticInfo("TableFieldDefineName", n, t,
430-
state.GetDiagnosticStyle().table_field_name_style)
431-
);
420+
state.GetDiagnosticStyle().table_field_name_style));
432421
}
433422
break;
434423
}
@@ -456,11 +445,11 @@ std::string NameStyleChecker::MakeDiagnosticInfo(std::string_view ruleName, LuaS
456445
break;
457446
}
458447
case NameStyleType::Same: {
459-
ruleMessage.append(util::format("same('{}')", rule.Param));
448+
ruleMessage.append(util::format("same"));
460449
break;
461450
}
462451
case NameStyleType::Pattern: {
463-
ruleMessage.append(util::format("pattern('{}')", rule.Param));
452+
ruleMessage.append(util::format("pattern"));
464453
break;
465454
}
466455
case NameStyleType::UpperSnakeCase: {

0 commit comments

Comments
 (0)