Skip to content

Commit acf6d01

Browse files
committed
除same规则外基本完成
1 parent 9ad89a3 commit acf6d01

File tree

13 files changed

+324
-229
lines changed

13 files changed

+324
-229
lines changed

CodeService/src/FormatElement/DiagnosisContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void DiagnosisContext::RecoverIndent()
6464
void DiagnosisContext::PushDiagnosis(std::string_view message, TextRange range)
6565
{
6666
LuaDiagnosisPosition start(GetLine(range.StartOffset), GetColumn(range.StartOffset));
67-
LuaDiagnosisPosition end(GetLine(range.EndOffset), GetColumn(range.EndOffset));
67+
LuaDiagnosisPosition end(GetLine(range.EndOffset), GetColumn(range.EndOffset) + 1);
6868
PushDiagnosis(message, start, end);
6969
}
7070

CodeService/src/FormatElement/TextElement.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,3 @@ void TextElement::SetTextDefineType(TextDefineType textDefineType)
4444
{
4545
_textDefineType = textDefineType;
4646
}
47-
48-
std::string TextElement::TranslateNameStyle(NameStyle style)
49-
{
50-
switch (style)
51-
{
52-
case NameStyle::CamelCase:
53-
{
54-
return "camel-case";
55-
}
56-
case NameStyle::PascalCase:
57-
{
58-
return "pascal-case";
59-
}
60-
case NameStyle::SnakeCase:
61-
{
62-
return "snake-case";
63-
}
64-
default:
65-
{
66-
return "unknwon";
67-
}
68-
}
69-
}

CodeService/src/LuaCodeStyleOptions.cpp

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,22 @@
66

77
LuaCodeStyleOptions::LuaCodeStyleOptions()
88
:
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),
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)),
1616

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)
17+
local_name_define_style(std::make_shared<NameStyleRuleMatcher>()),
18+
function_param_name_style(std::make_shared<NameStyleRuleMatcher>()),
19+
function_name_define_style(std::make_shared<NameStyleRuleMatcher>()),
20+
local_function_name_define_style(std::make_shared<NameStyleRuleMatcher>()),
21+
table_field_name_define_style(std::make_shared<NameStyleRuleMatcher>()),
22+
global_variable_name_define_style(std::make_shared<NameStyleRuleMatcher>()),
23+
module_name_define_style(std::make_shared<NameStyleRuleMatcher>()),
24+
require_module_name_style(std::make_shared<NameStyleRuleMatcher>()),
25+
class_name_define_style(std::make_shared<NameStyleRuleMatcher>())
2426
{
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>();
4527
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,14 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
298298
//
299299
std::vector<std::pair<std::string, std::shared_ptr<NameStyleRuleMatcher>&>> styleList = {
300300
{"local_name_define_style", options->local_name_define_style},
301+
{"function_param_name_style", options->function_param_name_style},
301302
{"function_name_define_style", options->function_name_define_style},
303+
{"local_function_name_define_style", options->local_function_name_define_style},
302304
{"table_field_name_define_style", options->table_field_name_define_style},
303-
{"global_variable_name_define_style", options->table_field_name_define_style}
305+
{"global_variable_name_define_style", options->global_variable_name_define_style},
306+
{"module_name_define_style", options->module_name_define_style},
307+
{"require_module_name_style", options->require_module_name_style},
308+
{"class_name_define_style", options->class_name_define_style},
304309
};
305310

306311
for (auto& styleOption : styleList)

CodeService/src/NameStyle/CheckElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "CodeService/NameStyle/CheckElement.h"
1+
#include "CodeService/NameStyle/CheckElement.h"
22

33

44
CheckElement::CheckElement(NameDefineType type, std::shared_ptr<LuaAstNode> node,

CodeService/src/NameStyle/NameStyleChecker.cpp

Lines changed: 8 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,42 @@ void NameStyleChecker::Analysis()
2222
}
2323
case NameDefineType::ModuleDefineName:
2424
{
25-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
25+
_ctx.GetOptions().module_name_define_style->Diagnosis(_ctx, checkElement);
2626
break;
2727
}
2828
case NameDefineType::LocalFunctionName:
2929
{
30-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
30+
_ctx.GetOptions().local_function_name_define_style->Diagnosis(_ctx, checkElement);
3131
break;
3232
}
3333
case NameDefineType::GlobalVariableDefineName:
3434
{
35-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
35+
_ctx.GetOptions().global_variable_name_define_style->Diagnosis(_ctx, checkElement);
3636
break;
3737
}
3838
case NameDefineType::ParamName:
3939
{
40-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
40+
_ctx.GetOptions().function_param_name_style->Diagnosis(_ctx, checkElement);
4141
break;
4242
}
4343
case NameDefineType::ImportModuleName:
4444
{
45-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
45+
_ctx.GetOptions().require_module_name_style->Diagnosis(_ctx, checkElement);
4646
break;
4747
}
4848
case NameDefineType::ClassVariableName:
4949
{
50-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
50+
_ctx.GetOptions().class_name_define_style->Diagnosis(_ctx, checkElement);
5151
break;
5252
}
5353
case NameDefineType::FunctionDefineName:
5454
{
55-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
55+
_ctx.GetOptions().function_name_define_style->Diagnosis(_ctx, checkElement);
5656
break;
5757
}
5858
case NameDefineType::TableFieldDefineName:
5959
{
60-
_ctx.GetOptions().local_name_define_style->Diagnosis(_ctx, checkElement);
60+
_ctx.GetOptions().table_field_name_define_style->Diagnosis(_ctx, checkElement);
6161
break;
6262
}
6363
}
@@ -357,111 +357,3 @@ bool NameStyleChecker::IsGlobal(std::shared_ptr<LuaAstNode> node)
357357

358358
return true;
359359
}
360-
361-
bool NameStyleChecker::SnakeCase(std::string_view source)
362-
{
363-
bool lowerCase = false;
364-
for (std::size_t index = 0; index != source.size(); index++)
365-
{
366-
char ch = source[index];
367-
368-
if (index == 0)
369-
{
370-
if (::islower(ch))
371-
{
372-
lowerCase = true;
373-
}
374-
else if (::isupper(ch))
375-
{
376-
lowerCase = false;
377-
}
378-
else if (ch == '_' && source.size() == 1)
379-
{
380-
return true;
381-
}
382-
else
383-
{
384-
return false;
385-
}
386-
}
387-
else if ((lowerCase && !::islower(ch)) || (!lowerCase && !::isupper(ch)))
388-
{
389-
if (ch == '_')
390-
{
391-
// 不允许双下划线
392-
if ((index < source.size() - 1) && source[index + 1] == '_')
393-
{
394-
return false;
395-
}
396-
}
397-
else if (!::isdigit(ch))
398-
{
399-
return false;
400-
}
401-
}
402-
}
403-
return true;
404-
}
405-
406-
bool NameStyleChecker::PascalCase(std::string_view source)
407-
{
408-
for (std::size_t index = 0; index != source.size(); index++)
409-
{
410-
char ch = source[index];
411-
412-
if (index == 0)
413-
{
414-
// 首字母必须大写
415-
if (!::isupper(ch))
416-
{
417-
// _ 亚元不受命名限制
418-
if (source.size() == 1 && ch == '_')
419-
{
420-
return true;
421-
}
422-
else
423-
{
424-
return false;
425-
}
426-
}
427-
}
428-
// 我又没办法分词简单处理下
429-
else if (!::isalnum(ch))
430-
{
431-
return false;
432-
}
433-
}
434-
return true;
435-
}
436-
437-
bool NameStyleChecker::CamelCase(std::string_view source)
438-
{
439-
for (std::size_t index = 0; index != source.size(); index++)
440-
{
441-
char ch = source[index];
442-
443-
if (index == 0)
444-
{
445-
// 首字母可以小写,也可以单下划线开头
446-
if (!::islower(ch))
447-
{
448-
if (ch == '_')
449-
{
450-
if (source.size() > 1 && !::islower(ch))
451-
{
452-
return false;
453-
}
454-
}
455-
else
456-
{
457-
return false;
458-
}
459-
}
460-
}
461-
else if (!::isalnum(ch))
462-
{
463-
return false;
464-
}
465-
}
466-
return true;
467-
}

0 commit comments

Comments
 (0)