Fix and improve C comment indent heuristic

This commit is contained in:
Jonathan Müller 2018-01-15 09:08:48 +01:00
commit 1e5271a4b7
2 changed files with 16 additions and 6 deletions

View file

@ -192,7 +192,7 @@ namespace
{ {
public: public:
position(ts::object_ref<std::string> result, const char* ptr) noexcept position(ts::object_ref<std::string> result, const char* ptr) noexcept
: result_(result), cur_line_(1u), ptr_(ptr), write_(true) : result_(result), cur_line_(1u), cur_column_(0u), ptr_(ptr), write_(true)
{ {
} }
@ -436,13 +436,20 @@ namespace
while (!result.comment.empty() && result.comment.back() == ' ') while (!result.comment.empty() && result.comment.back() == ' ')
result.comment.pop_back(); result.comment.pop_back();
// skip newline // skip newline(s)
p.skip_with_linecount(); while (starts_with(p, "\n"))
result.comment += '\n'; {
p.skip_with_linecount();
result.comment += '\n';
}
// skip indentation // skip indentation
auto actual_indent = 0u;
for (auto i = 0u; i < indent && starts_with(p, " "); ++i) for (auto i = 0u; i < indent && starts_with(p, " "); ++i)
{
++actual_indent;
p.skip(); p.skip();
}
auto extra_indent = 0u; auto extra_indent = 0u;
while (starts_with(p, " ")) while (starts_with(p, " "))
@ -463,6 +470,8 @@ namespace
{ {
// insert extra indent again // insert extra indent again
result.comment += std::string(extra_indent, ' '); result.comment += std::string(extra_indent, ' ');
// use minimum indent in the future
indent = std::min(actual_indent, indent);
} }
} }
else else

View file

@ -202,7 +202,8 @@ TEST_CASE("comment content")
/** Multiline /** Multiline
C C
comment */ comment
with indent */
/** Multiline /** Multiline
C C
@ -227,7 +228,7 @@ comment */
REQUIRE(comments[2u].content == "multi\nline\ncomment"); REQUIRE(comments[2u].content == "multi\nline\ncomment");
REQUIRE(comments[3u].content == "C comment"); REQUIRE(comments[3u].content == "C comment");
REQUIRE(comments[4u].content == "C comment no space"); REQUIRE(comments[4u].content == "C comment no space");
REQUIRE(comments[5u].content == "Multiline\nC\ncomment"); REQUIRE(comments[5u].content == "Multiline\nC\n comment\nwith indent");
REQUIRE(comments[6u].content == "Multiline\nC\n comment\n with\n indent"); REQUIRE(comments[6u].content == "Multiline\nC\n comment\n with\n indent");
REQUIRE(comments[7u].content == "Multiline\nC\ncomment\nwith\nindent\nstar"); REQUIRE(comments[7u].content == "Multiline\nC\ncomment\nwith\nindent\nstar");
} }