Skip to content

Commit 9f46258

Browse files
authored
Merge pull request #222 from wjiec/feat/disable-next-line
Add the disable-next-line instruction to disable formatting for the next line
2 parents 4e3325e + b8737e3 commit 9f46258

File tree

3 files changed

+109
-9
lines changed

3 files changed

+109
-9
lines changed

CodeFormatCore/src/Format/Analyzer/FormatDocAnalyze.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const L
105105
AddIgnoreRange(IndexRange(n.GetIndex(), nextNode.GetIndex()), t);
106106
}
107107

108+
break;
109+
} else if (action == "disable-next-line") {
110+
const std::size_t ignoreLine = n.GetStartLine(t) + 1;
111+
auto isIgnore = [&] (const LuaSyntaxNode node) -> bool {
112+
return !node.IsNull(t) && node.GetStartLine(t) == ignoreLine;
113+
};
114+
115+
auto endNode = n.GetNextSibling(t);
116+
while (isIgnore(endNode)) {
117+
if (endNode.GetEndLine(t) > ignoreLine) {
118+
endNode = endNode.GetFirstChild(t);
119+
} else if (const auto next = endNode.GetNextSibling(t); isIgnore(next)) {
120+
endNode.ToNext(t);
121+
} else {
122+
break;
123+
}
124+
}
125+
126+
if (!endNode.IsNull(t)) {
127+
AddIgnoreRange(IndexRange(n.GetIndex(), endNode.GetIndex()), t);
128+
}
108129
break;
109130
} else if (action == "on") {
110131
state = ParseState::List;

CodeFormatCore/src/Format/FormatState.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,20 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
156156
resolve.Reset();
157157
if (traverse.Event == TraverseEvent::Enter) {
158158
traverseStack.back().Event = TraverseEvent::Exit;
159-
if (_ignoreRange.EndIndex != 0) {
160-
auto index = traverse.Node.GetIndex();
161-
if (index >= _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex) {
162-
continue;
163-
}
164-
}
165-
for (auto &analyzer: _analyzers) {
166-
analyzer->Query(*this, traverse.Node, t, resolve);
167-
}
159+
160+
// We need to first add all child nodes to the traversal list.
168161
auto children = traverse.Node.GetChildren(t);
169162
// 不采用 <range>
170163
for (auto rIt = children.rbegin(); rIt != children.rend(); rIt++) {
171164
traverseStack.emplace_back(*rIt, TraverseEvent::Enter);
172165
}
173166

167+
// For the code that has already been ignored, we also need to
168+
// analyze it to determine how to indent next.
169+
for (auto &analyzer: _analyzers) {
170+
analyzer->Query(*this, traverse.Node, t, resolve);
171+
}
172+
174173
if (resolve.GetIndentStrategy() != IndentStrategy::None) {
175174
auto indent = resolve.GetIndent();
176175
if (indent == 0 && resolve.GetIndentStrategy() != IndentStrategy::Absolute) {
@@ -204,6 +203,16 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
204203
}
205204
}
206205

206+
// Skip the nodes that need to be ignored. Since the range of ignoring starts
207+
// from the @format node, we need to execute enterHandle once when entering
208+
// the ignore range to output all the ignored content.
209+
if (_ignoreRange.EndIndex != 0) {
210+
auto index = traverse.Node.GetIndex();
211+
if (index > _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex) {
212+
continue;
213+
}
214+
}
215+
207216
enterHandle(traverse.Node, t, resolve);
208217
if (!_foreachContinue) {
209218
return;

Test/src/FormatResult_unitest.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,76 @@ local t = {
891891
lcc = 123,
892892
gjopepgo = 123,
893893
}
894+
)"));
895+
896+
EXPECT_TRUE(TestHelper::TestFormatted(
897+
R"(
898+
local t = {
899+
---@format disable-next-line
900+
foo = 123, bar = 234,
901+
baz = 345,
902+
}
903+
)",
904+
R"(
905+
local t = {
906+
---@format disable-next-line
907+
foo = 123, bar = 234,
908+
baz = 345,
909+
}
910+
)"));
911+
912+
EXPECT_TRUE(TestHelper::TestFormatted(
913+
R"(
914+
local t = {
915+
---@format disable-next-line
916+
foo = function( a, b )
917+
return {
918+
foo = a,
919+
bar = b,
920+
}
921+
end,
922+
bar = 1,
923+
}
924+
)",
925+
R"(
926+
local t = {
927+
---@format disable-next-line
928+
foo = function( a, b )
929+
return {
930+
foo = a,
931+
bar = b,
932+
}
933+
end,
934+
bar = 1,
935+
}
936+
)"));
937+
938+
EXPECT_TRUE(TestHelper::TestFormatted(
939+
R"(
940+
local t = {
941+
---@format disable-next-line
942+
foo = function( a, b )
943+
return {
944+
foo = a,
945+
bar = b,
946+
}
947+
end,
948+
bar = 1,
949+
}
950+
---@format disable-next-line
951+
)",
952+
R"(
953+
local t = {
954+
---@format disable-next-line
955+
foo = function( a, b )
956+
return {
957+
foo = a,
958+
bar = b,
959+
}
960+
end,
961+
bar = 1,
962+
}
963+
---@format disable-next-line
894964
)"));
895965
}
896966

0 commit comments

Comments
 (0)