Skip to content

Commit e91956c

Browse files
committed
support align_continuous_similar_call_args
1 parent ccfd166 commit e91956c

7 files changed

Lines changed: 133 additions & 4 deletions

File tree

CodeService/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
207207
}
208208
}
209209

210+
BOOL_OPTION(align_continuous_similar_call_args)
211+
210212
BOOL_OPTION(align_continuous_inline_comment)
211213

212214
BOOL_OPTION(never_indent_before_if_condition)

CodeService/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,96 @@ void AlignAnalyzer::PushNormalAlignGroup(std::size_t alignPos, std::vector<std::
554554
}
555555

556556
void AlignAnalyzer::AnalyzeContinuousSimilarCallArgs(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
557+
auto exprStmts = syntaxNode.GetChildSyntaxNodes(LuaSyntaxNodeKind::ExpressionStatement, t);
558+
std::size_t lastLine = 0;
559+
std::size_t prefixLen = 0;
560+
std::vector<LuaSyntaxNode> group;
561+
562+
for (auto stmt: exprStmts) {
563+
auto suffix = stmt.GetChildSyntaxNode(LuaSyntaxNodeKind::SuffixedExpression, t);
564+
auto callexpr = suffix.GetLastChildSyntaxNode(LuaSyntaxNodeKind::CallExpression, t);
565+
auto isSimpleStmt = stmt.IsSingleLineNode(t);
566+
567+
if (group.empty() && isSimpleStmt) {
568+
group.push_back(callexpr);
569+
prefixLen = callexpr.GetStartCol(t) - stmt.GetStartCol(t);
570+
lastLine = callexpr.GetEndLine(t);
571+
continue;
572+
}
573+
574+
if (isSimpleStmt) {
575+
auto line = callexpr.GetStartLine(t);
576+
auto stmtPrefixLen = callexpr.GetStartCol(t) - stmt.GetStartCol(t);
577+
if (line - lastLine <= f.GetStyle().align_continuous_line_space && stmtPrefixLen == prefixLen) {
578+
group.push_back(callexpr);
579+
} else {
580+
if (group.size() > 1) {
581+
AnalyzeSimilarCallAlign(f, group, prefixLen, t);
582+
}
583+
group.clear();
584+
group.push_back(callexpr);
585+
}
586+
prefixLen = stmtPrefixLen;
587+
lastLine = callexpr.GetEndLine(t);
588+
} else if (group.size() > 1) {
589+
AnalyzeSimilarCallAlign(f, group, prefixLen, t);
590+
group.clear();
591+
} else {
592+
group.clear();
593+
}
594+
}
595+
596+
if (group.size() > 1) {
597+
AnalyzeSimilarCallAlign(f, group, prefixLen, t);
598+
}
599+
}
600+
601+
void AlignAnalyzer::AnalyzeSimilarCallAlign(FormatState &f, std::vector<LuaSyntaxNode> &callExprs, std::size_t prefixLen, const LuaSyntaxTree &t) {
602+
std::vector<std::vector<LuaSyntaxNode>> argsVec;
603+
std::size_t maxAlign = 0;
604+
for (auto &callExpr: callExprs) {
605+
auto argExprList = callExpr.GetChildSyntaxNode(LuaSyntaxNodeKind::ExpressionList, t);
606+
auto args = argExprList.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
607+
if (args.size() > maxAlign) {
608+
maxAlign = args.size();
609+
}
610+
argsVec.push_back(std::move(args));
611+
}
612+
613+
std::vector<std::size_t> group;
614+
std::size_t alignPos = prefixLen + 1;
615+
if (f.GetStyle().space_inside_function_call_parentheses) {
616+
alignPos++;
617+
}
618+
619+
std::size_t elementLength = 0;
620+
for (std::size_t i = 0; i < maxAlign; i++) {
621+
for (auto &args: argsVec) {
622+
if (i < args.size()) {
623+
auto text = args[i].GetText(t);
624+
if (elementLength < text.size()) {
625+
elementLength = text.size();
626+
}
627+
group.push_back(args[i].GetFirstToken(t).GetIndex());
628+
}
629+
}
630+
PushNormalAlignGroup(alignPos, group);
631+
alignPos += elementLength;
632+
if (i + 1 == maxAlign) {
633+
if (f.GetStyle().space_inside_function_call_parentheses) {
634+
alignPos++;
635+
}
636+
} else {
637+
if (f.GetStyle().space_after_comma) {
638+
alignPos += 2;
639+
} else {
640+
alignPos++;
641+
}
642+
}
643+
644+
elementLength = 0;
645+
group.clear();
646+
}
557647
}
558648

559649
void AlignAnalyzer::AnalyzeInlineComment(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {

Test/src/FormatStyle_unitest.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,4 +1325,37 @@ local t = aaaaaaaaaaaa.wfgoiwjofjw()
13251325
aaaaaaaaaaaa.wfgoiwjofjw()
13261326
.afjoajofjw()
13271327
)", style));
1328+
}
1329+
1330+
TEST(FormatByStyleOption, align_continuous_similar_call_args){
1331+
LuaStyle style;
1332+
1333+
style.align_continuous_similar_call_args = false;
1334+
EXPECT_TRUE(TestHelper::TestFormatted(
1335+
R"(
1336+
map("n", "<C-j>", quickfix_step("below"), { buffer = true })
1337+
map("n", "<C-k>", quickfix_step("above"), { buffer = true })
1338+
map("n", "<Space>", "<CR><C-w>p", { buffer = true })
1339+
map({ "n", "x" }, "<CR>", "<CR>", { buffer = true })
1340+
)",
1341+
R"(
1342+
map("n", "<C-j>", quickfix_step("below"), { buffer = true })
1343+
map("n", "<C-k>", quickfix_step("above"), { buffer = true })
1344+
map("n", "<Space>", "<CR><C-w>p", { buffer = true })
1345+
map({ "n", "x" }, "<CR>", "<CR>", { buffer = true })
1346+
)", style));
1347+
style.align_continuous_similar_call_args = true;
1348+
EXPECT_TRUE(TestHelper::TestFormatted(
1349+
R"(
1350+
map("n", "<C-j>", quickfix_step("below"), { buffer = true })
1351+
map("n", "<C-k>", quickfix_step("above"), { buffer = true })
1352+
map("n", "<Space>", "<CR><C-w>p", { buffer = true })
1353+
map({ "n", "x" }, "<CR>", "<CR>", { buffer = true })
1354+
)",
1355+
R"(
1356+
map("n", "<C-j>", quickfix_step("below"), { buffer = true })
1357+
map("n", "<C-k>", quickfix_step("above"), { buffer = true })
1358+
map("n", "<Space>", "<CR><C-w>p", { buffer = true })
1359+
map({ "n", "x" }, "<CR>", "<CR>", { buffer = true })
1360+
)", style));
13281361
}

Test2/src/FormatTest2.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
int main() {
1010
std::string buffer = R"(
11-
local t = { --131
12-
}
11+
map(1231, 3131, 12331113131,1 )
12+
map(213112, 1, 12313131,1313113131)
1313
)";
1414

1515
auto file = std::make_shared<LuaFile>(std::move(buffer));
@@ -24,7 +24,7 @@ local t = { --131
2424
std::cout << t.GetDebugView() << std::endl;
2525

2626
LuaStyle s;
27-
s.break_all_list_when_line_exceed = true;
27+
s.align_continuous_similar_call_args = true;
2828
FormatBuilder b(s);
2929
auto text = b.GetFormatResult(t);
3030
std::cout<< text << std::endl;

include/CodeService/Config/LuaStyle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class LuaStyle {
9999
std::size_t align_continuous_line_space = 2;
100100

101101
bool align_if_branch = false;
102-
// not implement now
102+
103103
bool align_continuous_similar_call_args = false;
104104

105105
bool align_continuous_inline_comment = true;

include/CodeService/Format/Analyzer/AlignAnalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class AlignAnalyzer : public FormatAnalyzer {
3939

4040
void AnalyzeContinuousSimilarCallArgs(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
4141

42+
void AnalyzeSimilarCallAlign(FormatState &f, std::vector<LuaSyntaxNode> &callExprs, std::size_t prefixLen, const LuaSyntaxTree &t);
43+
4244
void AnalyzeInlineComment(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
4345

4446
std::vector<AlignGroup> _alignGroup;

lua.template.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ align_if_branch = false
8989
# option none / always / contain_curly/
9090
align_array_table = true
9191

92+
align_continuous_similar_call_args = false
93+
9294
align_continuous_inline_comment = true
9395
# option none / always / only_call_stmt
9496
align_chain_expr = none

0 commit comments

Comments
 (0)