Skip to content

Commit ebb17c1

Browse files
committed
implement align_continuous_inline_comment
1 parent 415c63d commit ebb17c1

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

CodeService/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,24 @@ void AlignAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
6363
break;
6464
}
6565
}
66+
} else {
67+
switch (syntaxNode.GetTokenKind(t)) {
68+
case TK_SHORT_COMMENT: {
69+
if (f.GetStyle().align_continuous_inline_comment) {
70+
AnalyzeInlineComment(f, syntaxNode, t);
71+
}
72+
break;
73+
}
74+
default: {
75+
break;
76+
}
77+
}
6678
}
6779
}
80+
81+
for (auto &group: _inlineCommentGroup) {
82+
PushAlignGroup(AlignStrategy::AlignComment, group);
83+
}
6884
}
6985

7086
void AlignAnalyzer::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
@@ -91,6 +107,10 @@ void AlignAnalyzer::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSy
91107
resolve.SetIndent(alignGroup.AlignPos, IndentStrategy::Absolute);
92108
break;
93109
}
110+
case AlignStrategy::AlignComment: {
111+
resolve.SetAlign(alignGroup.AlignPos);
112+
break;
113+
}
94114
default: {
95115
break;
96116
}
@@ -349,6 +369,23 @@ AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, AlignGr
349369
}
350370
break;
351371
}
372+
case AlignStrategy::AlignComment: {
373+
std::size_t maxDis = 0;
374+
auto &file = t.GetFile();
375+
for (auto i: group.SyntaxGroup) {
376+
auto comment = LuaSyntaxNode(i);
377+
if (comment.IsToken(t)) {
378+
auto prev = comment.GetPrevToken(t);
379+
auto newPos =
380+
file.GetColumn(prev.GetTextRange(t).EndOffset) + f.GetStyle().space_before_inline_comment + 1;
381+
if (newPos > maxDis) {
382+
maxDis = newPos;
383+
}
384+
_resolveGroupIndex[comment.GetIndex()] = groupIndex;
385+
}
386+
}
387+
group.AlignPos = maxDis;
388+
}
352389
default: {
353390
break;
354391
}
@@ -459,3 +496,31 @@ void
459496
AlignAnalyzer::AnalyzeContinuousSimilarCallArgs(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
460497

461498
}
499+
500+
void AlignAnalyzer::AnalyzeInlineComment(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
501+
auto prevToken = syntaxNode.GetPrevToken(t);
502+
if (prevToken.IsNull(t)) {
503+
return;
504+
}
505+
auto currentLine = syntaxNode.GetStartLine(t);
506+
if (prevToken.GetEndLine(t) != currentLine) {
507+
return;
508+
}
509+
// now it is inline comment
510+
if (_inlineCommentGroup.empty()) {
511+
_inlineCommentGroup.emplace_back();
512+
}
513+
auto &topGroup = _inlineCommentGroup.back();
514+
if (topGroup.empty()) {
515+
topGroup.push_back(syntaxNode.GetIndex());
516+
return;
517+
}
518+
519+
auto lastComment = LuaSyntaxNode(topGroup.back());
520+
if (currentLine - lastComment.GetEndLine(t) > 2) {
521+
auto &newTopGroup = _inlineCommentGroup.emplace_back();
522+
newTopGroup.push_back(syntaxNode.GetIndex());
523+
} else {
524+
topGroup.push_back(syntaxNode.GetIndex());
525+
}
526+
}

Test/src/FormatStyle_unitest.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,4 +1159,77 @@ p(1,2,3,)
11591159
R"(
11601160
p(1, 2, 3)
11611161
)", style));
1162+
}
1163+
1164+
TEST(FormatByStyleOption, align_continuous_inline_comment){
1165+
LuaStyle style;
1166+
1167+
style.align_continuous_inline_comment = false;
1168+
EXPECT_TRUE(TestHelper::TestFormatted(
1169+
R"(
1170+
function f() --hello world
1171+
local t = 132 --1313
1172+
end
1173+
1174+
local t = {
1175+
{ 11111, 2, 3333333333333 }, --hello
1176+
{ 123, 345, 46 }, --yes
1177+
{ 1, 2, 3 }, --hh
1178+
}
1179+
1180+
local t = {
1181+
aa = 1, -- e1
1182+
bbaa = 2, --bb
1183+
}
1184+
)",
1185+
R"(
1186+
function f() --hello world
1187+
local t = 132 --1313
1188+
end
1189+
1190+
local t = {
1191+
{ 11111, 2, 3333333333333 }, --hello
1192+
{ 123, 345, 46 }, --yes
1193+
{ 1, 2, 3 }, --hh
1194+
}
1195+
1196+
local t = {
1197+
aa = 1, -- e1
1198+
bbaa = 2, --bb
1199+
}
1200+
)", style));
1201+
style.align_continuous_inline_comment = true;
1202+
EXPECT_TRUE(TestHelper::TestFormatted(
1203+
R"(
1204+
function f() --hello world
1205+
local t = 132 --1313
1206+
end
1207+
1208+
local t = {
1209+
{ 11111, 2, 3333333333333 }, --hello
1210+
{ 123, 345, 46 }, --yes
1211+
{ 1, 2, 3 }, --hh
1212+
}
1213+
1214+
local t = {
1215+
aa = 1, -- e1
1216+
bbaa = 2, --bb
1217+
}
1218+
)",
1219+
R"(
1220+
function f() --hello world
1221+
local t = 132 --1313
1222+
end
1223+
1224+
local t = {
1225+
{ 11111, 2, 3333333333333 }, --hello
1226+
{ 123, 345, 46 }, --yes
1227+
{ 1, 2, 3 }, --hh
1228+
}
1229+
1230+
local t = {
1231+
aa = 1, -- e1
1232+
bbaa = 2, --bb
1233+
}
1234+
)", style));
11621235
}

include/CodeService/Config/LuaStyle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class LuaStyle {
100100
// not implement now
101101
bool align_continuous_similar_call_args = false;
102102

103+
bool align_continuous_inline_comment = false;
104+
103105
bool align_array_table = true;
104106

105107
// not implement now

include/CodeService/Format/Analyzer/AlignAnalyzer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ class AlignAnalyzer : public FormatAnalyzer {
3737

3838
void AnalyzeContinuousSimilarCallArgs(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
3939

40+
void AnalyzeInlineComment(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
41+
4042
std::vector<AlignGroup> _alignGroup;
4143
std::unordered_map<std::size_t, std::size_t> _startNodeToGroupIndex;
4244
std::unordered_map<std::size_t, std::size_t> _resolveGroupIndex;
45+
46+
std::vector<std::vector<std::size_t>> _inlineCommentGroup;
4347
};

include/CodeService/Format/Analyzer/FormatStrategy.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ struct LineBreakData {
105105
enum class AlignStrategy {
106106
Normal,
107107
AlignToEq,
108-
AlignToFirst
108+
AlignToFirst,
109+
AlignComment,
109110
};
110111

111112
struct AlignGroup {
112-
AlignGroup(AlignStrategy strategy, std::vector<std::size_t> &group)
113+
AlignGroup(AlignStrategy strategy, const std::vector<std::size_t> &group)
113114
: Strategy(strategy), SyntaxGroup(group), Resolve(false), AlignPos(0) {}
114115

115116
AlignStrategy Strategy;

0 commit comments

Comments
 (0)