@@ -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-
180157void
181158IndentationAnalyzer::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+ }
0 commit comments