From 1e5271a4b7a3e0ed601ec8dbc9ad1fa7720f6cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Mon, 15 Jan 2018 09:08:48 +0100 Subject: [PATCH] Fix and improve C comment indent heuristic --- src/libclang/preprocessor.cpp | 17 +++++++++++++---- test/cpp_preprocessor.cpp | 5 +++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/libclang/preprocessor.cpp b/src/libclang/preprocessor.cpp index 15c911c..e3036f3 100644 --- a/src/libclang/preprocessor.cpp +++ b/src/libclang/preprocessor.cpp @@ -192,7 +192,7 @@ namespace { public: position(ts::object_ref 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() == ' ') result.comment.pop_back(); - // skip newline - p.skip_with_linecount(); - result.comment += '\n'; + // skip newline(s) + while (starts_with(p, "\n")) + { + p.skip_with_linecount(); + result.comment += '\n'; + } // skip indentation + auto actual_indent = 0u; for (auto i = 0u; i < indent && starts_with(p, " "); ++i) + { + ++actual_indent; p.skip(); + } auto extra_indent = 0u; while (starts_with(p, " ")) @@ -463,6 +470,8 @@ namespace { // insert extra indent again result.comment += std::string(extra_indent, ' '); + // use minimum indent in the future + indent = std::min(actual_indent, indent); } } else diff --git a/test/cpp_preprocessor.cpp b/test/cpp_preprocessor.cpp index e46c7eb..52a8906 100644 --- a/test/cpp_preprocessor.cpp +++ b/test/cpp_preprocessor.cpp @@ -202,7 +202,8 @@ TEST_CASE("comment content") /** Multiline C -comment */ + comment +with indent */ /** Multiline C @@ -227,7 +228,7 @@ comment */ REQUIRE(comments[2u].content == "multi\nline\ncomment"); REQUIRE(comments[3u].content == "C comment"); 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[7u].content == "Multiline\nC\ncomment\nwith\nindent\nstar"); }