Skip to content

Commit d9e8c5e

Browse files
committed
FIX #89
1 parent afc4de7 commit d9e8c5e

File tree

6 files changed

+260
-57
lines changed

6 files changed

+260
-57
lines changed

CodeService/src/Config/LuaStyle.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,8 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
224224
BOOL_OPTION(ignore_space_after_colon)
225225

226226
BOOL_OPTION(remove_call_expression_list_finish_comma)
227+
228+
BOOL_OPTION(table_use_continuation_indent)
229+
230+
BOOL_OPTION(function_call_use_continuation_indent)
227231
}

CodeService/src/Format/Analyzer/IndentationAnalyzer.cpp

Lines changed: 138 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,21 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
5858
if (syntaxNode.GetChildToken('(', t).IsToken(t)) {
5959
auto exprList = syntaxNode.GetChildSyntaxNode(NodeKind::ExpressionList, t);
6060
if (exprList.IsNode(t)) {
61-
for (auto expr: exprList.GetChildren(t)) {
62-
Indenter(expr, t, IndentData(
63-
IndentType::WhenLineBreak,
64-
f.GetStyle().indent_style == IndentStyle::Space ?
65-
f.GetStyle().indent_size : f.GetStyle().tab_width
66-
));
67-
}
61+
// for (auto expr: exprList.GetChildren(t)) {
62+
// if (f.GetStyle().function_call_use_continuation_indent) {
63+
// Indenter(expr, t, IndentData(
64+
// IndentType::WhenLineBreak,
65+
// f.GetStyle().continuation_indent
66+
// ));
67+
// } else {
68+
// Indenter(expr, t, IndentData(
69+
// IndentType::WhenLineBreak,
70+
// f.GetStyle().indent_style == IndentStyle::Space ?
71+
// f.GetStyle().indent_size : f.GetStyle().tab_width
72+
// ));
73+
// }
74+
// }
75+
AnalyzeCallExprList(f, exprList, t);
6876
}
6977
}
7078
break;
@@ -122,11 +130,18 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
122130
case LuaSyntaxNodeKind::TableExpression: {
123131
auto tableFieldList = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldList, t);
124132
for (auto field: tableFieldList.GetChildren(t)) {
125-
Indenter(field, t, IndentData(
126-
IndentType::WhenLineBreak,
127-
f.GetStyle().indent_style == IndentStyle::Space ?
128-
f.GetStyle().indent_size : f.GetStyle().tab_width
129-
));
133+
if (f.GetStyle().table_use_continuation_indent) {
134+
Indenter(field, t, IndentData(
135+
IndentType::WhenLineBreak,
136+
f.GetStyle().continuation_indent
137+
));
138+
} else {
139+
Indenter(field, t, IndentData(
140+
IndentType::WhenLineBreak,
141+
f.GetStyle().indent_style == IndentStyle::Space ?
142+
f.GetStyle().indent_size : f.GetStyle().tab_width
143+
));
144+
}
130145
}
131146

132147
break;
@@ -139,44 +154,6 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
139154
}
140155
}
141156

142-
void IndentationAnalyzer::AnalyzeExprList(FormatState &f, LuaSyntaxNode &exprList, const LuaSyntaxTree &t) {
143-
auto exprs = exprList.GetChildSyntaxNodes(MultiKind::Expression, t);
144-
if (exprs.size() == 1) {
145-
auto expr = exprs.front();
146-
auto syntaxKind = expr.GetSyntaxKind(t);
147-
if (syntaxKind == LuaSyntaxNodeKind::ClosureExpression
148-
|| syntaxKind == LuaSyntaxNodeKind::TableExpression
149-
|| syntaxKind == LuaSyntaxNodeKind::StringLiteralExpression) {
150-
return;
151-
}
152-
if (syntaxKind == LuaSyntaxNodeKind::SuffixedExpression) {
153-
auto subExprs = expr.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
154-
auto symbolLine = expr.GetPrevToken(t).GetEndLine(t);
155-
bool sameLine = true;
156-
for (auto subExpr: subExprs) {
157-
sameLine = sameLine && subExpr.GetStartLine(t) == symbolLine;
158-
}
159-
if (sameLine) {
160-
return;
161-
}
162-
}
163-
} else {
164-
auto symbolLine = exprList.GetPrevToken(t).GetEndLine(t);
165-
bool sameLine = true;
166-
for (auto expr: exprs) {
167-
sameLine = sameLine && expr.GetStartLine(t) == symbolLine;
168-
}
169-
if (sameLine) {
170-
return;
171-
}
172-
}
173-
174-
Indenter(exprList, t, IndentData(
175-
IndentType::Standard,
176-
f.GetStyle().continuation_indent
177-
));
178-
}
179-
180157
void
181158
IndentationAnalyzer::Query(FormatState &f, LuaSyntaxNode &n, const LuaSyntaxTree &t, FormatResolve &resolve) {
182159
auto it = _indent.find(n.GetIndex());
@@ -225,3 +202,114 @@ void IndentationAnalyzer::MarkIndent(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
225202
p = p.GetParent(t);
226203
}
227204
}
205+
206+
void IndentationAnalyzer::AnalyzeExprList(FormatState &f, LuaSyntaxNode &exprList, const LuaSyntaxTree &t) {
207+
auto exprs = exprList.GetChildSyntaxNodes(MultiKind::Expression, t);
208+
if (exprs.size() == 1) {
209+
auto expr = exprs.front();
210+
auto syntaxKind = expr.GetSyntaxKind(t);
211+
// special deal with
212+
if (syntaxKind == LuaSyntaxNodeKind::TableExpression
213+
|| syntaxKind == LuaSyntaxNodeKind::StringLiteralExpression) {
214+
return;
215+
}
216+
217+
if (!IsExprShouldIndent(expr, t)) {
218+
return;
219+
}
220+
221+
} else {
222+
bool shouldIndent = false;
223+
for (auto expr: exprs) {
224+
if (IsExprShouldIndent(expr, t)) {
225+
shouldIndent = true;
226+
break;
227+
}
228+
}
229+
230+
if (!shouldIndent) {
231+
return;
232+
}
233+
}
234+
235+
Indenter(exprList, t, IndentData(
236+
IndentType::Standard,
237+
f.GetStyle().continuation_indent
238+
));
239+
}
240+
241+
void IndentationAnalyzer::AnalyzeCallExprList(FormatState &f, LuaSyntaxNode &exprList, const LuaSyntaxTree &t) {
242+
auto exprs = exprList.GetChildSyntaxNodes(MultiKind::Expression, t);
243+
244+
bool shouldIndent = false;
245+
for (auto expr: exprs) {
246+
if (IsExprShouldIndent(expr, t)) {
247+
shouldIndent = true;
248+
break;
249+
}
250+
}
251+
252+
if (shouldIndent) {
253+
auto indent = f.GetStyle().function_call_use_continuation_indent
254+
? f.GetStyle().continuation_indent
255+
: (f.GetStyle().indent_style == IndentStyle::Space
256+
? f.GetStyle().indent_size
257+
: f.GetStyle().tab_width);
258+
Indenter(exprList, t, IndentData(
259+
IndentType::Standard,
260+
indent
261+
));
262+
}
263+
}
264+
265+
bool IndentationAnalyzer::IsExprShouldIndent(LuaSyntaxNode &expr, const LuaSyntaxTree &t) {
266+
auto symbolLine = expr.GetPrevToken(t).GetEndLine(t);
267+
if (expr.GetStartLine(t) != symbolLine) {
268+
return true;
269+
}
270+
// 快速收敛
271+
if (expr.IsSingleLineNode(t)) {
272+
return false;
273+
}
274+
275+
auto syntaxKind = expr.GetSyntaxKind(t);
276+
switch (syntaxKind) {
277+
case LuaSyntaxNodeKind::ClosureExpression:
278+
case LuaSyntaxNodeKind::TableExpression:
279+
case LuaSyntaxNodeKind::StringLiteralExpression:
280+
case LuaSyntaxNodeKind::NameExpression: {
281+
break;
282+
}
283+
case LuaSyntaxNodeKind::SuffixedExpression: {
284+
auto subExprs = expr.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
285+
for (auto subExpr: subExprs) {
286+
if (subExpr.GetStartLine(t) != symbolLine) {
287+
return true;
288+
}
289+
}
290+
break;
291+
}
292+
case LuaSyntaxNodeKind::ParExpression:
293+
case LuaSyntaxNodeKind::UnaryExpression: {
294+
auto subExpr = expr.GetChildSyntaxNode(LuaSyntaxMultiKind::Expression, t);
295+
if (subExpr.IsNode(t)) {
296+
return IsExprShouldIndent(subExpr, t);
297+
}
298+
break;
299+
}
300+
case LuaSyntaxNodeKind::BinaryExpression: {
301+
for (auto childNode: expr.GetChildren(t)) {
302+
if (childNode.IsNode(t) && IsExprShouldIndent(childNode, t)) {
303+
return true;
304+
} else if (childNode.GetStartLine(t) != symbolLine) {
305+
return true;
306+
}
307+
}
308+
break;
309+
}
310+
default: {
311+
break;
312+
}
313+
}
314+
return false;
315+
}

CodeService/src/TypeFormat/LuaTypeFormat.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ void LuaTypeFormat::FormatLine(std::size_t line, std::size_t character, const Lu
165165
auto &file = t.GetFile();
166166
auto offset = file.GetOffset(line, character);
167167
auto prevToken = t.GetTokenBeforeOffset(offset);
168+
auto tempStyle = style;
168169
switch (prevToken.GetTokenKind(t)) {
169170
case TK_END: {
170171
auto parent = prevToken.GetParent(t);
@@ -177,15 +178,14 @@ void LuaTypeFormat::FormatLine(std::size_t line, std::size_t character, const Lu
177178
formatRange.EndLine = line - 1;
178179

179180
if (_typeOptions.auto_complete_table_sep) {
180-
LuaStyle tempStyle = style;
181181
tempStyle.trailing_table_separator = TrailingTableSeparator::Smart;
182-
return FormatByRange(formatRange, t, tempStyle);
183182
}
184183

185184
break;
186185
}
187186
}
188-
return FormatByRange(formatRange, t, style);
187+
tempStyle.call_arg_parentheses = CallArgParentheses::Keep;
188+
return FormatByRange(formatRange, t, tempStyle);
189189
}
190190

191191
void LuaTypeFormat::FixIndent(std::size_t line, std::size_t character, const LuaSyntaxTree &t, LuaStyle &style) {

Test/src/FormatResult_unitest.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,106 @@ t[-1] = -1
640640
p(-1)
641641
t[-1] = -1
642642
)"));
643+
}
644+
645+
TEST(Format, codestyle_89) {
646+
EXPECT_TRUE(TestHelper::TestFormatted(
647+
R"(
648+
local t = {
649+
a = cond
650+
and "is true"
651+
or "is not true"
652+
}
653+
654+
655+
local s = ("%s")
656+
:format("text")
657+
658+
659+
print(("%s")
660+
:format("text"))
661+
662+
663+
print("s1"
664+
.. "s2")
665+
666+
667+
local f1 = function()
668+
print("test")
669+
end
670+
671+
672+
local f2 = cond or function()
673+
print("test")
674+
end
675+
)",
676+
R"(
677+
local t = {
678+
a = cond
679+
and "is true"
680+
or "is not true"
681+
}
682+
683+
684+
local s = ("%s")
685+
:format("text")
686+
687+
688+
print(("%s")
689+
:format("text"))
690+
691+
692+
print("s1"
693+
.. "s2")
694+
695+
696+
local f1 = function()
697+
print("test")
698+
end
699+
700+
701+
local f2 = cond or function()
702+
print("test")
703+
end
704+
)"));
705+
}
706+
707+
TEST(Format, codestyle_90) {
708+
LuaStyle style;
709+
style.call_arg_parentheses = CallArgParentheses::Remove;
710+
EXPECT_TRUE(TestHelper::TestFormatted(
711+
R"(
712+
null_ls.setup {
713+
sources = {
714+
null_ls.builtins.formatting.prettierd.with {
715+
disabled_filetypes = { 'html' },
716+
},
717+
null_ls.builtins.formatting.autopep8.with {
718+
extra_args = {
719+
'--indent-size=2'
720+
}
721+
},
722+
null_ls.builtins.formatting.xmlformat.with {
723+
724+
},
725+
}
726+
}
727+
)",
728+
R"(
729+
null_ls.setup {
730+
sources = {
731+
null_ls.builtins.formatting.prettierd.with {
732+
disabled_filetypes = { 'html' },
733+
},
734+
null_ls.builtins.formatting.autopep8.with {
735+
extra_args = {
736+
'--indent-size=2'
737+
}
738+
},
739+
null_ls.builtins.formatting.xmlformat.with {
740+
741+
},
742+
}
743+
}
744+
)", style));
643745
}

include/CodeService/Config/LuaStyle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,8 @@ class LuaStyle {
145145

146146
// not implement now
147147
bool leading_comma_style = false;
148+
149+
bool table_use_continuation_indent = false;
150+
151+
bool function_call_use_continuation_indent = false;
148152
};

include/CodeService/Format/Analyzer/IndentationAnalyzer.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ class IndentationAnalyzer : public FormatAnalyzer {
2121

2222
void Analyze(FormatState &f, const LuaSyntaxTree &t) override;
2323

24-
void Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve& resolve) override;
24+
void Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) override;
2525

26-
void Indenter(LuaSyntaxNode &n, const LuaSyntaxTree &t, IndentData indentData=IndentData());
26+
void Indenter(LuaSyntaxNode &n, const LuaSyntaxTree &t, IndentData indentData = IndentData());
2727

2828
// 在格式化过程中标记Token缩进
29-
void MarkIndent(LuaSyntaxNode& n, const LuaSyntaxTree& t);
29+
void MarkIndent(LuaSyntaxNode &n, const LuaSyntaxTree &t);
30+
3031
private:
31-
void AnalyzeExprList(FormatState &f, LuaSyntaxNode& exprList, const LuaSyntaxTree &t);
32+
void AnalyzeExprList(FormatState &f, LuaSyntaxNode &exprList, const LuaSyntaxTree &t);
33+
34+
void AnalyzeCallExprList(FormatState &f, LuaSyntaxNode &exprList, const LuaSyntaxTree &t);
35+
36+
bool IsExprShouldIndent(LuaSyntaxNode &expr, const LuaSyntaxTree &t);
3237

3338
std::unordered_map<std::size_t, IndentData> _indent;
3439
std::unordered_set<std::size_t> _indentMark;

0 commit comments

Comments
 (0)