Improved sequence diagram message comment formatting

This commit is contained in:
Bartek Kryza
2023-10-16 21:47:46 +02:00
parent 03e8c867f4
commit 1616f79ceb
5 changed files with 90 additions and 12 deletions

View File

@@ -287,19 +287,10 @@ void generator::generate_message_comment(
if (!from)
return;
if (m.type() == message_t::kCall) {
const auto &to = model().get_participant<model::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';
}

View File

@@ -199,6 +199,28 @@ std::vector<std::string> split(
return result;
}
std::vector<std::string> split_isspace(std::string str)
{
std::vector<std::string> result;
while (static_cast<unsigned int>(!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<std::string> &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

View File

@@ -167,6 +167,8 @@ std::string get_os_name();
std::vector<std::string> split(
std::string str, std::string_view delimiter, bool skip_empty = true);
std::vector<std::string> 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