Skip to content

Commit 2154329

Browse files
committed
施工中...
1 parent f4771e0 commit 2154329

File tree

10 files changed

+174
-53
lines changed

10 files changed

+174
-53
lines changed

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ target_sources(CodeService
5757
${CodeService_SOURCE_DIR}/src/LuaEditorConfig.cpp
5858
${CodeService_SOURCE_DIR}/src/LanguageTranslator.cpp
5959
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleChecker.cpp
60+
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleRuleMatcher.cpp
6061
)
6162

6263
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
#include "CodeService/LuaCodeStyleOptions.h"
22
#include <filesystem>
3-
#include <regex>
43
#include "CodeService/FormatElement/MinLineElement.h"
54
#include "CodeService/FormatElement/KeepLineElement.h"
5+
#include "CodeService/NameStyle/NameStyleRuleMatcher.h"
66

77
LuaCodeStyleOptions::LuaCodeStyleOptions()
88
:
9-
keep_line_after_if_statement(std::make_shared<MinLineElement>(1)),
10-
keep_line_after_do_statement(std::make_shared<MinLineElement>(1)),
11-
keep_line_after_while_statement(std::make_shared<MinLineElement>(1)),
12-
keep_line_after_repeat_statement(std::make_shared<MinLineElement>(1)),
13-
keep_line_after_for_statement(std::make_shared<MinLineElement>(1)),
14-
keep_line_after_local_or_assign_statement(std::make_shared<KeepLineElement>()),
15-
keep_line_after_function_define_statement(std::make_shared<KeepLineElement>(1))
9+
keep_line_after_if_statement(nullptr),
10+
keep_line_after_do_statement(nullptr),
11+
keep_line_after_while_statement(nullptr),
12+
keep_line_after_repeat_statement(nullptr),
13+
keep_line_after_for_statement(nullptr),
14+
keep_line_after_local_or_assign_statement(nullptr),
15+
keep_line_after_function_define_statement(nullptr),
16+
17+
local_name_define_style(nullptr),
18+
function_name_define_style(nullptr),
19+
table_field_name_define_style(nullptr),
20+
global_variable_name_define_style(nullptr),
21+
module_name_define_style(nullptr),
22+
require_module_name_style(nullptr),
23+
class_name_define_style(nullptr)
1624
{
25+
SetDefault();
26+
}
27+
28+
void LuaCodeStyleOptions::SetDefault()
29+
{
30+
keep_line_after_if_statement = std::make_shared<MinLineElement>(1);
31+
keep_line_after_do_statement = std::make_shared<MinLineElement>(1);
32+
keep_line_after_while_statement = std::make_shared<MinLineElement>(1);
33+
keep_line_after_repeat_statement = std::make_shared<MinLineElement>(1);
34+
keep_line_after_for_statement = std::make_shared<MinLineElement>(1);
35+
keep_line_after_local_or_assign_statement = std::make_shared<KeepLineElement>();
36+
keep_line_after_function_define_statement = std::make_shared<KeepLineElement>(1);
37+
38+
local_name_define_style = std::make_shared<NameStyleRuleMatcher>();
39+
function_name_define_style = std::make_shared<NameStyleRuleMatcher>();
40+
table_field_name_define_style = std::make_shared<NameStyleRuleMatcher>();
41+
global_variable_name_define_style = std::make_shared<NameStyleRuleMatcher>();
42+
module_name_define_style = std::make_shared<NameStyleRuleMatcher>();
43+
require_module_name_style = std::make_shared<NameStyleRuleMatcher>();
44+
class_name_define_style = std::make_shared<NameStyleRuleMatcher>();
1745
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -294,34 +294,35 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
294294
{
295295
options->enable_name_style_check = configMap.at("enable_name_style_check") == "true";
296296
}
297-
298-
std::vector<std::pair<std::string, NameStyle&>> styleList = {
299-
{"local_name_define_style", options->local_name_define_style},
300-
{"function_name_define_style", options->function_name_define_style},
301-
{"table_field_name_define_style", options->table_field_name_define_style},
302-
};
303-
304-
for (auto& styleOption : styleList)
305-
{
306-
if (configMap.count(styleOption.first))
307-
{
308-
std::string value = configMap.at(styleOption.first);
309-
if (value == "off")
310-
{
311-
styleOption.second = NameStyle::Off;
312-
}
313-
else if (value == "snake_case")
314-
{
315-
styleOption.second = NameStyle::SnakeCase;
316-
}
317-
else if (value == "camel_case")
318-
{
319-
styleOption.second = NameStyle::CamelCase;
320-
}
321-
else if (value == "pascal_case")
322-
{
323-
styleOption.second = NameStyle::PascalCase;
324-
}
325-
}
326-
}
297+
//
298+
// std::vector<std::pair<std::string, NameStyle&>> styleList = {
299+
// {"local_name_define_style", options->local_name_define_style},
300+
// {"function_name_define_style", options->function_name_define_style},
301+
// {"table_field_name_define_style", options->table_field_name_define_style},
302+
// {"global_variable_name_define_style", options->table_field_name_define_style}
303+
// };
304+
//
305+
// for (auto& styleOption : styleList)
306+
// {
307+
// if (configMap.count(styleOption.first))
308+
// {
309+
// std::string value = configMap.at(styleOption.first);
310+
// if (value == "off")
311+
// {
312+
// styleOption.second = NameStyle::Off;
313+
// }
314+
// else if (value == "snake_case")
315+
// {
316+
// styleOption.second = NameStyle::SnakeCase;
317+
// }
318+
// else if (value == "camel_case")
319+
// {
320+
// styleOption.second = NameStyle::CamelCase;
321+
// }
322+
// else if (value == "pascal_case")
323+
// {
324+
// styleOption.second = NameStyle::PascalCase;
325+
// }
326+
// }
327+
// }
327328
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "CodeService/NameStyle/NameStyleRuleMatcher.h"
2+
#include "LuaParser/LuaTokenTypeDetail.h"
3+
#include <algorithm>
4+
5+
6+
std::shared_ptr<NameStyleRuleMatcher> NameStyleRuleMatcher::ParseFrom(std::string_view rule)
7+
{
8+
auto matcher = std::make_shared<NameStyleRuleMatcher>();
9+
matcher->SetRule(rule);
10+
matcher->ParseRule();
11+
return matcher;
12+
}
13+
14+
NameStyleRuleMatcher::NameStyleRuleMatcher()
15+
: _tokenParser(nullptr)
16+
{
17+
}
18+
19+
void NameStyleRuleMatcher::SetRule(std::string_view rule)
20+
{
21+
_tokenParser = std::make_shared<LuaTokenParser>(std::string(rule));
22+
}
23+
24+
void NameStyleRuleMatcher::ParseRule()
25+
{
26+
enum class ParseState
27+
{
28+
NamedRule,
29+
FunctionRule
30+
} state = ParseState::NamedRule;
31+
32+
while (_tokenParser->Current().TokenType != TK_EOS)
33+
{
34+
auto type = _tokenParser->Current().TokenType;
35+
switch (state)
36+
{
37+
case ParseState::NamedRule:
38+
{
39+
if(type == TK_NAME)
40+
{
41+
42+
}
43+
else if(type == '|')
44+
{
45+
break;
46+
}
47+
else if(type == '(')
48+
{
49+
state = ParseState::FunctionRule;
50+
51+
}
52+
break;
53+
}
54+
case ParseState::FunctionRule:
55+
{
56+
break;
57+
}
58+
}
59+
_tokenParser->Next();
60+
}
61+
}

LuaParser/src/LuaParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <sstream>
44
#include <filesystem>
55
#include "LuaDefine.h"
6-
#include "LuaTokenTypeDetail.h"
6+
#include "LuaParser/LuaTokenTypeDetail.h"
77
#include "LuaParser/LuaOperatorType.h"
88
#include "Util/format.h"
99
#include "LuaParser/LuaParseException.h"

LuaParser/src/LuaTokenParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "LuaParser/LuaTokenParser.h"
22
#include <limits>
33
#include "LuaDefine.h"
4-
#include "LuaTokenTypeDetail.h"
4+
#include "LuaParser/LuaTokenTypeDetail.h"
55
#include "Util/format.h"
66
#include "utf8.h"
77

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <memory>
66

77
class FormatElement;
8+
class NameStyleRuleMatcher;
89

910
enum class IndentStyle
1011
{
@@ -24,6 +25,7 @@ class LuaCodeStyleOptions
2425
{
2526
public:
2627
LuaCodeStyleOptions();
28+
2729
/*
2830
* 缩进风格
2931
*/
@@ -104,10 +106,15 @@ class LuaCodeStyleOptions
104106

105107
// 命名风格检查
106108
bool enable_name_style_check = false;
107-
NameStyle local_name_define_style = NameStyle::Off;
108-
NameStyle function_name_define_style = NameStyle::Off;
109-
NameStyle table_field_name_define_style = NameStyle::Off;
110-
// global_variable_name_define_style = off
111-
// local_require_module_name_define_style = off
112-
// local_module_or_class_name_define = off
109+
std::shared_ptr<NameStyleRuleMatcher> local_name_define_style;
110+
std::shared_ptr<NameStyleRuleMatcher> function_name_define_style;
111+
std::shared_ptr<NameStyleRuleMatcher> table_field_name_define_style;
112+
std::shared_ptr<NameStyleRuleMatcher> global_variable_name_define_style;
113+
std::shared_ptr<NameStyleRuleMatcher> module_name_define_style;
114+
std::shared_ptr<NameStyleRuleMatcher> require_module_name_style;
115+
std::shared_ptr<NameStyleRuleMatcher> class_name_define_style;
116+
117+
private:
118+
119+
void SetDefault();
113120
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
#include <memory>
5+
#include "LuaParser/LuaTokenParser.h"
6+
7+
class NameStyleRuleMatcher
8+
{
9+
public:
10+
static std::shared_ptr<NameStyleRuleMatcher> ParseFrom(std::string_view rule);
11+
12+
NameStyleRuleMatcher();
13+
14+
void SetRule(std::string_view rule);
15+
16+
void ParseRule();
17+
private:
18+
19+
std::shared_ptr<LuaTokenParser> _tokenParser;
20+
std::string _rule;
21+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <climits>
4+
35
const int FIRST_RESERVED = UCHAR_MAX + 1;
46

57
enum LuaTokenTypeDetail : int

lua.template.editorconfig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ enable_name_style_check = false
4545
local_name_define_style = snake_case
4646
function_name_define_style = snake_case
4747
table_field_name_define_style = snake_case
48-
global_variable_name_define_style = snake_case
49-
# the following has extra option: same:filename/first_param/'<const string>', snake_case/pascal_case/camel_case
48+
global_variable_name_define_style = snake_case|pascal_case
49+
# the following has extra option: same(filename/first_param/'<const string>', snake_case/pascal_case/camel_case)
5050
# for example:
51-
# same:first_param, snake_case
52-
# same:'m'
53-
module_name_define_style = same:filename, snake_case
54-
require_module_name_sytle = same:first_param, snake_case
55-
class_name_define_style = same:first_param
51+
# same(first_param, snake_case)
52+
# same('m')
53+
module_name_define_style = same(filename, snake_case)|same('m')
54+
require_module_name_sytle = same(first_param, snake_case)
55+
class_name_define_style = same(first_param)

0 commit comments

Comments
 (0)