From 1616f79cebbf07318195d1d8fad89032cd58cd2e Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Mon, 16 Oct 2023 21:47:46 +0200 Subject: [PATCH] Improved sequence diagram message comment formatting --- .../plantuml/sequence_diagram_generator.cc | 13 +---- src/util/util.cc | 58 +++++++++++++++++++ src/util/util.h | 4 ++ tests/t20029/t20029.cc | 6 +- tests/test_util.cc | 21 +++++++ 5 files changed, 90 insertions(+), 12 deletions(-) diff --git a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc index ef3ca714..d33d8b24 100644 --- a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc +++ b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc @@ -287,19 +287,10 @@ void generator::generate_message_comment( if (!from) return; - if (m.type() == message_t::kCall) { - const auto &to = model().get_participant(m.to()); - if (!to) - return; + ostr << "note over " << generate_alias(from.value()) << '\n'; - ostr << "note over " << generate_alias(from.value()) << ", " - << generate_alias(to.value()) << '\n'; - } - else { - ostr << "note over " << generate_alias(from.value()) << '\n'; - } + ostr << util::format_message_comment(m.comment().value()) << '\n'; - ostr << m.comment().value() << '\n'; ostr << "end note" << '\n'; } diff --git a/src/util/util.cc b/src/util/util.cc index 2f42ddb2..d7e88ba8 100644 --- a/src/util/util.cc +++ b/src/util/util.cc @@ -199,6 +199,28 @@ std::vector split( return result; } +std::vector split_isspace(std::string str) +{ + std::vector result; + + while (static_cast(!str.empty()) != 0U) { + auto index = std::find_if( + str.begin(), str.end(), [](auto c) { return std::isspace(c); }); + if (index != str.end()) { + auto tok = str.substr(0, std::distance(str.begin(), index)); + if (!tok.empty()) + result.push_back(std::move(tok)); + str = str.substr(std::distance(str.begin(), index) + 1); + } + else { + if (!str.empty()) + result.push_back(str); + str = ""; + } + } + return result; +} + std::string join( const std::vector &toks, std::string_view delimiter) { @@ -344,4 +366,40 @@ std::filesystem::path ensure_path_is_absolute( return result; } +std::string format_message_comment(const std::string &comment, unsigned width) +{ + if (width == 0) + return comment; + + std::string result; + + if (comment.empty()) + return result; + + auto tokens = split_isspace(comment); + + if (tokens.empty()) + return result; + + unsigned current_line_length{0}; + for (auto it = tokens.begin(); it != tokens.end(); it++) { + if (current_line_length < width) { + result += *it; + result += ' '; + } + else { + result.back() = '\n'; + current_line_length = 0; + result += *it; + result += ' '; + } + + current_line_length += it->size() + 1; + } + + result.pop_back(); + + return result; +} + } // namespace clanguml::util diff --git a/src/util/util.h b/src/util/util.h index bff44542..27fdd40f 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -167,6 +167,8 @@ std::string get_os_name(); std::vector split( std::string str, std::string_view delimiter, bool skip_empty = true); +std::vector split_isspace(std::string str); + /** * @brief Remove and erase elements from a vector * @@ -424,4 +426,6 @@ std::string path_to_url(const std::filesystem::path &p); std::filesystem::path ensure_path_is_absolute(const std::filesystem::path &p, const std::filesystem::path &root = std::filesystem::current_path()); +std::string format_message_comment(const std::string &c, unsigned width = 25); + } // namespace clanguml::util \ No newline at end of file diff --git a/tests/t20029/t20029.cc b/tests/t20029/t20029.cc index 4d1345f4..9d9c79b0 100644 --- a/tests/t20029/t20029.cc +++ b/tests/t20029/t20029.cc @@ -12,7 +12,10 @@ template class Encoder : public T { public: bool send(std::string &&msg) { - return T::send(std::move(encode(std::move(msg)))); + return T::send(std::move( + // Encode the message using Base64 encoding and pass it to the next + // layer + encode(std::move(msg)))); } protected: @@ -27,6 +30,7 @@ public: int retryCount = 5; + // Repeat until send() succeeds or retry count is exceeded while (retryCount--) { if (T::send(buffer)) return true; diff --git a/tests/test_util.cc b/tests/test_util.cc index 5c069c7c..862de173 100644 --- a/tests/test_util.cc +++ b/tests/test_util.cc @@ -396,3 +396,24 @@ TEST_CASE("Test tokenize_unexposed_template_parameter", "[unit-test]") CHECK(r[i++] == ")"); } } + +TEST_CASE("Test format_message_comment", "[unit-test]") +{ + using namespace clanguml::util; + + CHECK(format_message_comment("\n\n ABCD \n", 0) == "\n\n ABCD \n"); + CHECK(format_message_comment("\n\n ABCD \n") == "ABCD"); + + CHECK(format_message_comment( + "Repeat until send() succeeds or retry count is exceeded") == + "Repeat until send() succeeds\nor retry count is exceeded"); + + CHECK(format_message_comment("12345678", 5) == "12345678"); + + CHECK(format_message_comment("") == ""); + + CHECK(format_message_comment(" ") == ""); + + CHECK(format_message_comment("This is a url: http://example.com/test/12345", + 15) == "This is a url:\nhttp://example.com/test/12345"); +}