Skip to content

Commit a8d6f88

Browse files
committed
格式化选项测试
1 parent 03c0d50 commit a8d6f88

File tree

16 files changed

+409
-19
lines changed

16 files changed

+409
-19
lines changed

CodeService/src/LuaEditorConfig.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
222222
options->do_statement_no_indent = configMap.at("do_statement_no_indent") == "true";
223223
}
224224

225+
if(configMap.count("if_condition_no_continuation_indent"))
226+
{
227+
options->if_condition_no_continuation_indent = configMap.at("if_condition_no_continuation_indent") == "true";
228+
}
229+
225230
if (configMap.count("align_table_field_to_first_field"))
226231
{
227232
options->align_table_field_to_first_field = configMap.at("align_table_field_to_first_field") == "true";

CodeService/src/LuaFormatter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
999999
}
10001000
case LuaAstNodeType::Expression:
10011001
{
1002-
env->AddChild(FormatNode(child));
1002+
std::shared_ptr<FormatElement> expression = nullptr;
1003+
if (_options.if_condition_no_continuation_indent)
1004+
{
1005+
expression = std::make_shared<LongExpressionLayoutElement>(0);
1006+
}
1007+
1008+
env->AddChild(FormatExpression(child, expression));
10031009
env->Add<KeepBlankElement>(1);
10041010
break;
10051011
}

Test/src/CodeFormatTest.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Util/format.h"
88
#include "Util/CommandLine.h"
99
#include "Util/StringUtil.h"
10+
#include "CodeService/LuaEditorConfig.h"
1011

1112

1213
bool TestFormatted(std::string input, const std::string& shouldBe, std::shared_ptr<LuaCodeStyleOptions> options)
@@ -57,10 +58,10 @@ bool TestGrammar(std::string input)
5758
{
5859
for (auto& error : formattedParser->GetErrors())
5960
std::cout << format("error after formatted: {}, from [{},{}] to [{},{}]", error.ErrorMessage,
60-
formattedParser->GetLine(error.ErrorRange.StartOffset),
61-
formattedParser->GetColumn(error.ErrorRange.StartOffset),
62-
formattedParser->GetLine(error.ErrorRange.EndOffset),
63-
formattedParser->GetColumn(error.ErrorRange.EndOffset)
61+
formattedParser->GetLine(error.ErrorRange.StartOffset),
62+
formattedParser->GetColumn(error.ErrorRange.StartOffset),
63+
formattedParser->GetLine(error.ErrorRange.EndOffset),
64+
formattedParser->GetColumn(error.ErrorRange.EndOffset)
6465
) << std::endl;
6566
}
6667

@@ -112,6 +113,7 @@ int main(int argc, char* argv[])
112113

113114
commandLine.AddTarget("CheckGrammar");
114115
commandLine.AddTarget("CheckFormatResult");
116+
commandLine.AddTarget("CheckFormatResultByOption");
115117

116118
commandLine.Add<std::string>("work-directory", "w", "special base work directory");
117119
commandLine.Add<std::string>("formatted-work-directory", "f", "special formatted work directory");
@@ -156,6 +158,41 @@ int main(int argc, char* argv[])
156158
std::cout << format("test format stability {} ... {}", path, passed ? "passed" : "false") << std::endl;
157159
}
158160
}
161+
else if (target == "CheckFormatResultByOption")
162+
{
163+
auto editorconfigPath = workRoot / ".editorconfig";
164+
auto editorConfig = LuaEditorConfig::LoadFromFile(editorconfigPath.string());
165+
editorConfig->SetWorkspace(workRoot.string());
166+
if (!editorConfig)
167+
{
168+
std::cerr << "can not find editorconfig file" << std::endl;
169+
return -1;
170+
}
171+
std::filesystem::path formattedRoot(commandLine.Get<std::string>("formatted-work-directory"));
172+
for (auto& path : luaFiles)
173+
{
174+
auto options = editorConfig->Generate(path);
175+
#ifndef _WIN32
176+
options->end_of_line = "\n";
177+
#endif
178+
179+
auto waitFormattingFilePath = workRoot / path;
180+
auto waitFormattingText = ReadFile(waitFormattingFilePath.string());
181+
182+
auto formattedFilePath = formattedRoot / path;
183+
184+
auto formattedText = ReadFile(formattedFilePath.string());
185+
186+
bool passed = TestFormatted(waitFormattingText, formattedText, options);
187+
188+
success &= passed;
189+
std::cout << format("test format {} ... {}", path, passed ? "passed" : "false") << std::endl;
190+
191+
passed = TestFormatted(formattedText, formattedText, options);
192+
success &= passed;
193+
std::cout << format("test format stability {} ... {}", path, passed ? "passed" : "false") << std::endl;
194+
}
195+
}
159196
else if (target == "CheckGrammar")
160197
{
161198
for (auto& path : luaFiles)

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ indent_style = space
6565
[{indent_style-eq-tab.lua}]
6666
# optional space/tab
6767
indent_style = tab
68-
align_table_field_to_first_field = true
68+
[{indent_size-eq-2.lua}]
69+
indent_style = space
70+
indent_size = 2
71+
[{indent_size-eq-4.lua}]
72+
indent_style = space
73+
indent_size = 4
74+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local t = 13
2+
print(function ()
3+
local c = 123
4+
local d = 131
5+
end)
6+
function f()
7+
print("sjfoiwjfow",afff,fwfw)
8+
local c = function()
9+
local d = 12313
10+
end
11+
end
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local t = 13
2+
print(function()
3+
local c = 123
4+
local d = 131
5+
end)
6+
function f()
7+
print("sjfoiwjfow", afff, fwfw)
8+
local c = function()
9+
local d = 12313
10+
end
11+
end

Test/test_script/format_text/wait_format_by_option/indent_style-eq-space.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- base
12
local t = function()
23
local a = 131
34
local b = function()
@@ -38,3 +39,37 @@ function ff()
3839
end
3940
end
4041
end
42+
43+
-- translate tab to space
44+
-- TODO
45+
-- pcall {
46+
-- function()
47+
-- local t = 13
48+
-- end,
49+
-- a, b, c,
50+
-- }
51+
52+
function fff()
53+
do
54+
return
55+
end
56+
57+
while true do
58+
local c = function ()
59+
for i=1,2,3 do
60+
print(i)
61+
end
62+
63+
for i,v in pairs{1,2,3,4,5,6} do
64+
print(i,v)
65+
end
66+
end
67+
end
68+
69+
if a then
70+
elseif b then
71+
local d =123
72+
else
73+
local c =123
74+
end
75+
end
Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,74 @@
1-
local t = {
2-
(function(a1, a2, a3, a4, a5, a6)
3-
return {
4-
[function()
5-
aaa, bbb, ccc = 1, 2, 3
6-
7-
return aa, bb, cc, dd, ee
8-
end] = {
9-
gggg
10-
}
11-
}
12-
end)(1, 2, 3, 4, 5, 6)
13-
}
1+
local t = function()
2+
local a = 131
3+
local b = function()
4+
local c = {
5+
d = function()
6+
local e = 13
7+
local f = print {
8+
{
9+
{
10+
{
11+
{
12+
{
13+
(function(a1, a2, a3, a4, a5, a6)
14+
return {
15+
[function()
16+
aaa, bbb, ccc = 1, 2, 3
17+
18+
return aa, bb, cc, dd, ee
19+
end] = {
20+
gggg
21+
}
22+
}
23+
end)(1, 2, 3, 4, 5, 6)
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
end
31+
}
32+
end
33+
end
34+
function ff()
35+
if aaa and bbbb then
36+
function dddd(aaa,
37+
bbbb, cccc, ddd)
38+
end
39+
end
40+
end
41+
42+
-- translate space to tab
43+
pcall {
44+
function ()
45+
local t = 13
46+
end,
47+
a,b,c,
48+
}
49+
50+
function fff()
51+
do
52+
return
53+
end
54+
55+
while true do
56+
local c = function()
57+
for i = 1, 2, 3 do
58+
print(i)
59+
end
60+
61+
for i, v in pairs { 1, 2, 3, 4, 5, 6 } do
62+
print(i, v)
63+
end
64+
end
65+
end
66+
67+
if a then
68+
elseif b then
69+
local d = 123
70+
else
71+
local c = 123
72+
end
73+
end
74+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
# see https://github.com/CppCXY/EmmyLuaCodeStyle
3+
[*.lua]
4+
# optional space/tab
5+
indent_style = space
6+
# if indent_style is space, this is valid
7+
indent_size = 4
8+
# if indent_style is tab, this is valid
9+
tab_width = 4
10+
continuation_indent_size = 4
11+
align_call_args = true
12+
keep_one_space_between_call_args_and_parentheses = false
13+
align_function_define_params = true
14+
keep_one_space_between_table_and_bracket = true
15+
align_table_field_to_first_field = false
16+
keep_one_space_between_namedef_and_attribute = false
17+
continuous_assign_statement_align_to_equal_sign = true
18+
continuous_assign_table_field_align_to_equal_sign = true
19+
label_no_indent = false
20+
do_statement_no_indent = false
21+
# optional crlf/lf
22+
end_of_line = crlf
23+
24+
# The following configuration supports three expressions
25+
# minLine:${n}
26+
# keepLine
27+
# KeepLine:${n}
28+
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
34+
keep_line_after_local_or_assign_statement = keepLine
35+
keep_line_after_function_define_statement = keepLine:1
36+
37+
# the following is code diagnostic options
38+
enable_check_codestyle = true
39+
# this mean utf8 length
40+
max_line_length = 120
41+
# this will check text end with new line(format always end with new line)
42+
insert_final_newline = true
43+
44+
45+
enable_name_style_check = false
46+
# the following is name style check rule
47+
# base option off/camel_case/snake_case/upper_snake_case/pascal_case/same(filename/first_param/'<const string>', snake_case/pascal_case/camel_case)
48+
# all option can use '|' represent or
49+
# for example:
50+
# snake_case | upper_snake_case
51+
# same(first_param, snake_case)
52+
# same('m')
53+
local_name_define_style = snake_case
54+
function_param_name_style = snake_case
55+
function_name_define_style = snake_case
56+
local_function_name_define_style = snake_case
57+
table_field_name_define_style = snake_case
58+
global_variable_name_define_style = snake_case|upper_snake_case
59+
module_name_define_style = same('m')|same(filename, snake_case)
60+
require_module_name_style = same(first_param, snake_case)
61+
class_name_define_style = same(filename, snake_case)
62+
[{indent_style-eq-space.lua}]
63+
# optional space/tab
64+
indent_style = space
65+
[{indent_style-eq-tab.lua}]
66+
# optional space/tab
67+
indent_style = tab
68+
[{indent_size-eq-2.lua}]
69+
indent_style = space
70+
indent_size = 2
71+
[{indent_size-eq-4.lua}]
72+
indent_style = space
73+
indent_size = 4
74+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local t = 13
2+
print(function()
3+
local c = 123
4+
local d = 131
5+
end)
6+
function f()
7+
print("sjfoiwjfow", afff, fwfw)
8+
local c = function()
9+
local d = 12313
10+
end
11+
end

0 commit comments

Comments
 (0)