Skip to content

Commit 9119bbd

Browse files
committed
implement #83
1 parent 8b0dcdc commit 9119bbd

File tree

6 files changed

+157
-71
lines changed

6 files changed

+157
-71
lines changed

CodeService/src/Config/LuaStyle.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,28 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
103103

104104
NUMBER_OPTION(continuation_indent)
105105

106-
if (configMap.count("detect_end_of_line")) {
107-
detect_end_of_line = configMap.at("detect_end_of_line") == "true";
108-
}
106+
BOOL_OPTION(detect_end_of_line)
109107

110-
if (configMap.count("insert_final_newline")) {
111-
insert_final_newline = configMap.at("insert_final_newline") == "true";
112-
}
108+
BOOL_OPTION(insert_final_newline)
113109

114110
BOOL_OPTION(space_around_table_field_list)
115111

116112
BOOL_OPTION(space_before_attribute)
117113

118114
BOOL_OPTION(space_before_function_open_parenthesis)
119115

120-
BOOL_OPTION(space_inside_function_call_parentheses)
116+
BOOL_OPTION(space_before_function_call_open_parenthesis)
121117

122-
BOOL_OPTION(space_inside_function_param_list_parentheses)
118+
BOOL_OPTION(space_before_closure_open_parenthesis)
119+
120+
BOOL_OPTION(space_before_function_call_single_arg)
123121

124122
BOOL_OPTION(space_before_open_square_bracket)
125123

124+
BOOL_OPTION(space_inside_function_call_parentheses)
125+
126+
BOOL_OPTION(space_inside_function_param_list_parentheses)
127+
126128
BOOL_OPTION(space_inside_square_brackets)
127129

128130
BOOL_OPTION(space_around_table_append_operator)

CodeService/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -450,55 +450,5 @@ void AlignAnalyzer::PushNormalAlignGroup(std::size_t alignPos, std::vector<std::
450450

451451
void
452452
AlignAnalyzer::AnalyzeContinuousSimilarCallArgs(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
453-
// auto children = syntaxNode.GetChildren(t);
454-
// std::size_t lastLine = 0;
455-
// std::vector<std::size_t> group;
456-
// for (auto stmt: children) {
457-
// auto kind = stmt.GetSyntaxKind(t);
458-
// if (group.empty()) {
459-
// if (kind == LuaSyntaxNodeKind::ExpressionStatement) {
460-
// group.push_back(stmt.GetIndex());
461-
// lastLine = stmt.GetEndLine(t);
462-
// }
463-
// continue;
464-
// }
465-
//
466-
// if (stmt.GetTokenKind(t) == TK_SHORT_COMMENT) {
467-
// auto line = stmt.GetStartLine(t);
468-
// if (line - lastLine > 2) {
469-
// if (group.size() > 1) {
470-
// PushAlignGroup(AlignStrategy::AlignToEq, group);
471-
// }
472-
// group.clear();
473-
// } else {
474-
// lastLine = stmt.GetEndLine(t);
475-
// }
476-
// continue;
477-
// }
478-
//
479-
// if (kind == LuaSyntaxNodeKind::LocalStatement
480-
// || kind == LuaSyntaxNodeKind::AssignStatement) {
481-
// auto line = stmt.GetStartLine(t);
482-
// if (line - lastLine <= 2) {
483-
// group.push_back(stmt.GetIndex());
484-
// } else {
485-
// if (group.size() > 1) {
486-
// PushAlignGroup(AlignStrategy::AlignToEq, group);
487-
// }
488-
// group.clear();
489-
// group.push_back(stmt.GetIndex());
490-
// }
491-
//
492-
// lastLine = stmt.GetEndLine(t);
493-
// } else if (group.size() > 1) {
494-
// PushAlignGroup(AlignStrategy::AlignToEq, group);
495-
// group.clear();
496-
// } else {
497-
// group.clear();
498-
// }
499-
// }
500-
//
501-
// if (group.size() > 1) {
502-
// PushAlignGroup(AlignStrategy::AlignToEq, group);
503-
// }
453+
504454
}

CodeService/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
135135
auto rightBrace = syntaxNode.GetChildToken(')', t);
136136
SpaceRight(leftBrace, t, 1);
137137
SpaceLeft(rightBrace, t, 1);
138-
} else {
139-
SpaceLeft(syntaxNode, t, 0);
140138
}
141139
if (f.GetStyle().ignore_spaces_inside_function_call) {
142140
auto exprList = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::ExpressionList, t);
@@ -147,8 +145,17 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
147145
}
148146
}
149147
}
148+
if (f.GetStyle().space_before_function_call_open_parenthesis) {
149+
SpaceLeft(leftBrace, t, 1);
150+
} else {
151+
SpaceLeft(leftBrace, t, 0);
152+
}
150153
} else {
151-
SpaceLeft(syntaxNode, t, 1);
154+
if (f.GetStyle().space_before_function_call_single_arg) {
155+
SpaceLeft(syntaxNode, t, 1);
156+
} else {
157+
SpaceLeft(syntaxNode, t, 0);
158+
}
152159
}
153160
break;
154161
}
@@ -228,12 +235,19 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
228235
}
229236
case LuaSyntaxNodeKind::FunctionBody: {
230237
auto leftBrace = syntaxNode.GetChildToken('(', t);
231-
if (f.GetStyle().space_before_function_open_parenthesis) {
232-
SpaceLeft(leftBrace, t, 1);
238+
if (syntaxNode.GetParent(t).GetSyntaxKind(t) == LuaSyntaxNodeKind::ClosureExpression) {
239+
if (f.GetStyle().space_before_closure_open_parenthesis) {
240+
SpaceLeft(leftBrace, t, 1);
241+
} else {
242+
SpaceLeft(leftBrace, t, 0);
243+
}
233244
} else {
234-
SpaceLeft(leftBrace, t, 0);
245+
if (f.GetStyle().space_before_function_open_parenthesis) {
246+
SpaceLeft(leftBrace, t, 1);
247+
} else {
248+
SpaceLeft(leftBrace, t, 0);
249+
}
235250
}
236-
237251
if (f.GetStyle().space_inside_function_param_list_parentheses) {
238252
auto next = leftBrace.GetNextToken(t);
239253
if (next.GetTokenKind(t) != ')') {

Test/src/FormatStyle_unitest.cpp

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,112 @@ p({1231})
315315
p("456")
316316
p { 1231 }
317317
)", style));
318-
}
318+
}
319+
320+
TEST(FormatByStyleOption, detect_end_of_line_no_test_case) {
321+
}
322+
323+
TEST(FormatByStyleOption, insert_final_newline_no_test_case) {
324+
}
325+
326+
TEST(FormatByStyleOption, space_around_table_field_list) {
327+
LuaStyle style;
328+
329+
style.space_around_table_field_list = true;
330+
EXPECT_TRUE(TestHelper::TestFormatted(
331+
R"(
332+
local t = { aaa }
333+
local t2 = {
334+
}
335+
local t3 = {}
336+
)",
337+
R"(
338+
local t = { aaa }
339+
local t2 = {
340+
}
341+
local t3 = {}
342+
)", style));
343+
style.space_around_table_field_list = false;
344+
EXPECT_TRUE(TestHelper::TestFormatted(
345+
R"(
346+
local t = { aaa }
347+
local t2 = {
348+
}
349+
local t3 = {}
350+
)",
351+
R"(
352+
local t = {aaa}
353+
local t2 = {
354+
}
355+
local t3 = {}
356+
)", style));
357+
}
358+
359+
TEST(FormatByStyleOption, space_before_attribute) {
360+
LuaStyle style;
361+
362+
style.space_before_attribute = true;
363+
EXPECT_TRUE(TestHelper::TestFormatted(
364+
R"(
365+
local t <const> = 123
366+
local t2 <close> = {}
367+
)",
368+
R"(
369+
local t <const> = 123
370+
local t2 <close> = {}
371+
)", style));
372+
style.space_before_attribute = false;
373+
EXPECT_TRUE(TestHelper::TestFormatted(
374+
R"(
375+
local t <const> = 123
376+
local t2 <close> = {}
377+
)",
378+
R"(
379+
local t<const> = 123
380+
local t2<close> = {}
381+
)", style));
382+
}
383+
384+
TEST(FormatByStyleOption, space_before_function_open_parenthesis) {
385+
LuaStyle style;
386+
387+
style.space_before_function_open_parenthesis = true;
388+
EXPECT_TRUE(TestHelper::TestFormatted(
389+
R"(
390+
function f() end
391+
)",
392+
R"(
393+
function f ()
394+
end
395+
)", style));
396+
style.space_before_function_open_parenthesis = false;
397+
EXPECT_TRUE(TestHelper::TestFormatted(
398+
R"(
399+
function f() end
400+
)",
401+
R"(
402+
function f()
403+
end
404+
)", style));
405+
}
406+
407+
TEST(FormatByStyleOption, space_before_function_call_open_parenthesis) {
408+
LuaStyle style;
409+
410+
style.space_before_function_call_open_parenthesis = true;
411+
EXPECT_TRUE(TestHelper::TestFormatted(
412+
R"(
413+
print(1, 2, 3)
414+
)",
415+
R"(
416+
print (1, 2, 3)
417+
)", style));
418+
style.space_before_function_call_open_parenthesis = false;
419+
EXPECT_TRUE(TestHelper::TestFormatted(
420+
R"(
421+
print(1, 2, 3)
422+
)",
423+
R"(
424+
print(1, 2, 3)
425+
)", style));
426+
}

include/CodeService/Config/LuaStyle.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ class LuaStyle {
5757

5858
bool space_before_function_open_parenthesis = false;
5959

60-
bool space_inside_function_call_parentheses = false;
60+
bool space_before_function_call_open_parenthesis = false;
6161

62-
bool space_inside_function_param_list_parentheses = false;
62+
bool space_before_closure_open_parenthesis = false;
63+
64+
bool space_before_function_call_single_arg = true;
6365

6466
bool space_before_open_square_bracket = false;
6567

68+
bool space_inside_function_call_parentheses = false;
69+
70+
bool space_inside_function_param_list_parentheses = false;
71+
6672
bool space_inside_square_brackets = false;
6773

6874
// like t[#t+1] = 1

lua.template.editorconfig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ space_before_attribute = true
4040

4141
space_before_function_open_parenthesis = false
4242

43-
space_inside_function_call_parentheses = false
43+
space_before_function_call_open_parenthesis = false
4444

45-
space_inside_function_param_list_parentheses = false
45+
space_before_closure_open_parenthesis = true
46+
47+
space_before_function_call_single_arg = true
4648

4749
space_before_open_square_bracket = false
4850

51+
space_inside_function_call_parentheses = false
52+
53+
space_inside_function_param_list_parentheses = false
54+
4955
space_inside_square_brackets = false
5056

5157
# like t[#t+1] = 1

0 commit comments

Comments
 (0)