Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CodeFormatCore/src/Format/Analyzer/FormatDocAnalyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const L
AddIgnoreRange(IndexRange(n.GetIndex(), nextNode.GetIndex()), t);
}

break;
} else if (action == "disable-next-line") {
const std::size_t ignoreLine = n.GetStartLine(t) + 1;
auto isIgnore = [&] (const LuaSyntaxNode node) -> bool {
return !node.IsNull(t) && node.GetStartLine(t) == ignoreLine;
};

auto endNode = n.GetNextSibling(t);
while (isIgnore(endNode)) {
if (endNode.GetEndLine(t) > ignoreLine) {
endNode = endNode.GetFirstChild(t);
} else if (const auto next = endNode.GetNextSibling(t); isIgnore(next)) {
endNode.ToNext(t);
} else {
break;
}
}

if (!endNode.IsNull(t)) {
AddIgnoreRange(IndexRange(n.GetIndex(), endNode.GetIndex()), t);
}
break;
} else if (action == "on") {
state = ParseState::List;
Expand Down
27 changes: 18 additions & 9 deletions CodeFormatCore/src/Format/FormatState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,20 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
resolve.Reset();
if (traverse.Event == TraverseEvent::Enter) {
traverseStack.back().Event = TraverseEvent::Exit;
if (_ignoreRange.EndIndex != 0) {
auto index = traverse.Node.GetIndex();
if (index >= _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex) {
continue;
}
}
for (auto &analyzer: _analyzers) {
analyzer->Query(*this, traverse.Node, t, resolve);
}

// We need to first add all child nodes to the traversal list.
auto children = traverse.Node.GetChildren(t);
// 不采用 <range>
for (auto rIt = children.rbegin(); rIt != children.rend(); rIt++) {
traverseStack.emplace_back(*rIt, TraverseEvent::Enter);
}

// For the code that has already been ignored, we also need to
// analyze it to determine how to indent next.
for (auto &analyzer: _analyzers) {
analyzer->Query(*this, traverse.Node, t, resolve);
}

if (resolve.GetIndentStrategy() != IndentStrategy::None) {
auto indent = resolve.GetIndent();
if (indent == 0 && resolve.GetIndentStrategy() != IndentStrategy::Absolute) {
Expand Down Expand Up @@ -204,6 +203,16 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
}
}

// Skip the nodes that need to be ignored. Since the range of ignoring starts
// from the @format node, we need to execute enterHandle once when entering
// the ignore range to output all the ignored content.
if (_ignoreRange.EndIndex != 0) {
auto index = traverse.Node.GetIndex();
if (index > _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex) {
continue;
}
}

enterHandle(traverse.Node, t, resolve);
if (!_foreachContinue) {
return;
Expand Down
70 changes: 70 additions & 0 deletions Test/src/FormatResult_unitest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,76 @@ local t = {
lcc = 123,
gjopepgo = 123,
}
)"));

EXPECT_TRUE(TestHelper::TestFormatted(
R"(
local t = {
---@format disable-next-line
foo = 123, bar = 234,
baz = 345,
}
)",
R"(
local t = {
---@format disable-next-line
foo = 123, bar = 234,
baz = 345,
}
)"));

EXPECT_TRUE(TestHelper::TestFormatted(
R"(
local t = {
---@format disable-next-line
foo = function( a, b )
return {
foo = a,
bar = b,
}
end,
bar = 1,
}
)",
R"(
local t = {
---@format disable-next-line
foo = function( a, b )
return {
foo = a,
bar = b,
}
end,
bar = 1,
}
)"));

EXPECT_TRUE(TestHelper::TestFormatted(
R"(
local t = {
---@format disable-next-line
foo = function( a, b )
return {
foo = a,
bar = b,
}
end,
bar = 1,
}
---@format disable-next-line
)",
R"(
local t = {
---@format disable-next-line
foo = function( a, b )
return {
foo = a,
bar = b,
}
end,
bar = 1,
}
---@format disable-next-line
)"));
}

Expand Down
Loading