Skip to content

Commit c96f884

Browse files
committed
ci开始进行选项测试
1 parent 5e2218b commit c96f884

29 files changed

+443
-46
lines changed

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ target_sources(CodeService
5151
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentLayoutElement.cpp
5252
${CodeService_SOURCE_DIR}/src/FormatElement/LongExpressionLayoutElement.cpp
5353
${CodeService_SOURCE_DIR}/src/FormatElement/AlignToFirstElement.cpp
54+
${CodeService_SOURCE_DIR}/src/FormatElement/IndentOnLineBreakElement.cpp
5455
${CodeService_SOURCE_DIR}/src/FormatElement/KeepElement.cpp
5556
${CodeService_SOURCE_DIR}/src/FormatElement/SubExpressionElement.cpp
5657
${CodeService_SOURCE_DIR}/src/FormatElement/DiagnosisContext.cpp
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "CodeService/FormatElement/IndentOnLineBreakElement.h"
2+
3+
IndentOnLineBreakElement::IndentOnLineBreakElement()
4+
: FormatElement(),
5+
_hasLineBreak(false)
6+
{
7+
}
8+
9+
FormatElementType IndentOnLineBreakElement::GetType()
10+
{
11+
return FormatElementType::IndentOnLineBreakElement;
12+
}
13+
14+
void IndentOnLineBreakElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
15+
{
16+
for (auto it = _children.begin(); it != _children.end(); ++it)
17+
{
18+
if (ctx.GetCharacterCount() == 0 && !_hasLineBreak)
19+
{
20+
_hasLineBreak = true;
21+
ctx.AddIndent();
22+
}
23+
(*it)->Serialize(ctx, it, *this);
24+
}
25+
if (_hasLineBreak)
26+
{
27+
ctx.RecoverIndent();
28+
}
29+
}
30+
31+
void IndentOnLineBreakElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
32+
{
33+
for (auto it = _children.begin(); it != _children.end(); ++it)
34+
{
35+
if (ctx.GetCharacterCount() == 0 && !_hasLineBreak)
36+
{
37+
_hasLineBreak = true;
38+
ctx.AddIndent();
39+
}
40+
(*it)->Diagnosis(ctx, it, *this);
41+
}
42+
if (_hasLineBreak)
43+
{
44+
ctx.RecoverIndent();
45+
}
46+
}

CodeService/src/FormatElement/LongExpressionLayoutElement.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ void LongExpressionLayoutElement::SerializeSubExpression(SerializeContext& ctx,
4949
{
5050
if (ctx.GetCharacterCount() == 0 && !_hasContinuation)
5151
{
52-
_hasContinuation = true;
53-
auto state = ctx.GetCurrentIndent();
54-
state.SpaceIndent += _continuationIndent;
55-
ctx.AddIndent(state);
52+
IndentSubExpression(ctx);
5653
}
5754
}
5855
}
@@ -77,11 +74,31 @@ void LongExpressionLayoutElement::DiagnosisSubExpression(DiagnosisContext& ctx,
7774
{
7875
if (ctx.GetCharacterCount() == 0 && !_hasContinuation)
7976
{
80-
_hasContinuation = true;
81-
auto state = ctx.GetCurrentIndent();
82-
state.SpaceIndent += _continuationIndent;
83-
ctx.AddIndent(state);
77+
IndentSubExpression(ctx);
8478
}
8579
}
8680
}
8781
}
82+
83+
void LongExpressionLayoutElement::IndentSubExpression(FormatContext& ctx)
84+
{
85+
_hasContinuation = true;
86+
if (_continuationIndent != -1) {
87+
auto& options = ctx.GetOptions();
88+
auto state = ctx.GetCurrentIndent();
89+
if (options.indent_style == IndentStyle::Space)
90+
{
91+
state.SpaceIndent += _continuationIndent;
92+
}
93+
else
94+
{
95+
state.TabIndent += _continuationIndent / options.tab_width;
96+
state.SpaceIndent += _continuationIndent % options.tab_width;
97+
}
98+
ctx.AddIndent(state);
99+
}
100+
else
101+
{
102+
ctx.AddIndent();
103+
}
104+
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
189189
options->continuation_indent_size = std::stoi(configMap.at("continuation_indent_size"));
190190
}
191191

192+
if(configMap.count("local_assign_continuation_align_to_first_expression"))
193+
{
194+
options->local_assign_continuation_align_to_first_expression =
195+
configMap.at("local_assign_continuation_align_to_first_expression") == "true";
196+
}
197+
192198
if (configMap.count("align_call_args"))
193199
{
194200
options->align_call_args = configMap.at("align_call_args") == "true";

CodeService/src/LuaFormatter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "CodeService/NameStyle/NameStyleChecker.h"
1717
#include "CodeService/FormatElement/NoIndentElement.h"
1818
#include "CodeService/FormatElement/SerializeContext.h"
19+
#include "CodeService/FormatElement/IndentOnLineBreakElement.h"
1920

2021
bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
2122
{
@@ -1568,7 +1569,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
15681569
}
15691570
else
15701571
{
1571-
env = std::make_shared<IndentElement>();
1572+
env = std::make_shared<IndentOnLineBreakElement>();
15721573
}
15731574

15741575
env->AddChild(FormatNode(*it));

Test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ target_sources(CodeFormatTest
1919
target_link_libraries(CodeFormatTest CodeService Util)
2020

2121
add_test(NAME GrammarTest COMMAND CodeFormatTest CheckGrammar -w ${CodeFormatTest_SOURCE_DIR}/test_script/grammar)
22-
add_test(NAME FormatTest COMMAND CodeFormatTest CheckFormatResult -w ${CodeFormatTest_SOURCE_DIR}/test_script/format_tex/wait_format -f ${CodeFormatTest_SOURCE_DIR}/test_script/format_text/wait_format_should_be)
23-
#add_test(NAME FormatByOptionTest COMMAND CodeFormatTest CheckFormatResultByOption -w ${CodeFormatTest_SOURCE_DIR}/test_script/format_tex/wait_format_by_option -f ${CodeFormatTest_SOURCE_DIR}/test_script/format_text/wait_format_by_option_should_be)
22+
add_test(NAME FormatTest COMMAND CodeFormatTest CheckFormatResult -w ${CodeFormatTest_SOURCE_DIR}/test_script/format_text/wait_format -f ${CodeFormatTest_SOURCE_DIR}/test_script/format_text/wait_format_should_be)
23+
add_test(NAME FormatByOptionTest COMMAND CodeFormatTest CheckFormatResultByOption -w ${CodeFormatTest_SOURCE_DIR}/test_script/format_text/wait_format_by_option -f ${CodeFormatTest_SOURCE_DIR}/test_script/format_text/wait_format_by_option_should_be)

Test/src/CodeFormatTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ int main(int argc, char* argv[])
162162
{
163163
auto editorconfigPath = workRoot / ".editorconfig";
164164
auto editorConfig = LuaEditorConfig::LoadFromFile(editorconfigPath.string());
165-
editorConfig->SetWorkspace(workRoot.string());
166165
if (!editorConfig)
167166
{
168167
std::cerr << "can not find editorconfig file" << std::endl;
169168
return -1;
170169
}
170+
editorConfig->SetWorkspace(workRoot.string());
171171
std::filesystem::path formattedRoot(commandLine.Get<std::string>("formatted-work-directory"));
172172
for (auto& path : luaFiles)
173173
{

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,68 @@
11

22
# see https://github.com/CppCXY/EmmyLuaCodeStyle
33
[*.lua]
4+
# [basic code reformat option]
45
# optional space/tab
56
indent_style = space
67
# if indent_style is space, this is valid
78
indent_size = 4
89
# if indent_style is tab, this is valid
910
tab_width = 4
11+
# only support number
1012
continuation_indent_size = 4
13+
# if true, continuation_indent_size for local or assign statement is invalid
14+
# however, if the expression list has cross row expression, it will not be aligned to the first expression
15+
local_assign_continuation_align_to_first_expression = false
16+
# function call expression's args will align to first arg
17+
# however, if the args has cross row arg, it will not be aligned to the first arg
1118
align_call_args = false
19+
# if true, format like this "print( "123", 456 )"
1220
keep_one_space_between_call_args_and_parentheses = false
21+
# if true, all function define params will align to first param
1322
align_function_define_params = true
23+
# if true, format like this "local t = { 1, 2, 3 }"
1424
keep_one_space_between_table_and_bracket = true
15-
align_table_field_to_first_field = true
25+
# if indent_style is tab, this option is invalid
26+
align_table_field_to_first_field = false
27+
# if true, ormat like this "local t <const> = 1"
1628
keep_one_space_between_namedef_and_attribute = false
29+
# see document for detail
1730
continuous_assign_statement_align_to_equal_sign = true
31+
# see document for detail
1832
continuous_assign_table_field_align_to_equal_sign = true
33+
# if true, the label loses its current indentation
1934
label_no_indent = false
35+
# if true, there will be no indentation in the do statement
2036
do_statement_no_indent = false
37+
# if true, the conditional expression of the if statement will not be a continuation line indent
38+
if_condition_no_continuation_indent = false
39+
2140
# optional crlf/lf
2241
end_of_line = crlf
2342

43+
# [Row layout]
2444
# The following configuration supports three expressions
2545
# minLine:${n}
2646
# keepLine
2747
# KeepLine:${n}
2848

29-
keep_line_after_if_statement = minLine:1
30-
keep_line_after_do_statement = minLine:1
31-
keep_line_after_while_statement = minLine:1
32-
keep_line_after_repeat_statement = minLine:1
33-
keep_line_after_for_statement = minLine:1
49+
keep_line_after_if_statement = minLine:0
50+
keep_line_after_do_statement = minLine:0
51+
keep_line_after_while_statement = minLine:0
52+
keep_line_after_repeat_statement = minLine:0
53+
keep_line_after_for_statement = minLine:0
3454
keep_line_after_local_or_assign_statement = keepLine
3555
keep_line_after_function_define_statement = keepLine:1
3656

57+
# [diagnostic]
3758
# the following is code diagnostic options
3859
enable_check_codestyle = true
3960
# this mean utf8 length
4061
max_line_length = 120
4162
# this will check text end with new line(format always end with new line)
4263
insert_final_newline = true
4364

44-
65+
# [name style check]
4566
enable_name_style_check = false
4667
# the following is name style check rule
4768
# base option off/camel_case/snake_case/upper_snake_case/pascal_case/same(filename/first_param/'<const string>', snake_case/pascal_case/camel_case)
@@ -59,6 +80,7 @@ global_variable_name_define_style = snake_case|upper_snake_case
5980
module_name_define_style = same('m')|same(filename, snake_case)
6081
require_module_name_style = same(first_param, snake_case)
6182
class_name_define_style = same(filename, snake_case)
83+
6284
[{indent_style-eq-space.lua}]
6385
# optional space/tab
6486
indent_style = space
@@ -71,4 +93,15 @@ indent_size = 2
7193
[{indent_size-eq-4.lua}]
7294
indent_style = space
7395
indent_size = 4
74-
96+
[{tab_width-eq-4.lua}]
97+
indent_style=tab
98+
tab_width=4
99+
[{tab_width-eq-8.lua}]
100+
indent_style=tab
101+
tab_width=8
102+
[{continuation_indent_size-eq-4.lua}]
103+
continuation_indent_size = 4
104+
[{continuation_indent_size-eq-8.lua}]
105+
continuation_indent_size = 8
106+
[{local_assign_continuation_align_to_first_expression-eq-true.lua}]
107+
local_assign_continuation_align_to_first_expression = true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local t = 123,
2+
456
3+
4+
if aaa
5+
and bbb or ccc then
6+
7+
end
8+
9+
return aaa,bbb,
10+
eeee,fff
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local t = 123,
2+
456
3+
4+
if aaa
5+
and bbb or ccc then
6+
7+
end
8+
9+
return aaa,bbb,
10+
eeee,fff

0 commit comments

Comments
 (0)