Added support for 'together' option in class diagrams without rendered namespaces

This commit is contained in:
Bartek Kryza
2023-02-04 00:23:51 +01:00
parent b910e9b02f
commit d7d80ab41e
14 changed files with 318 additions and 57 deletions

View File

@@ -479,14 +479,30 @@ void generator::generate(const package &p, std::ostream &ostr) const
}
else if (dynamic_cast<class_ *>(subpackage.get()) != nullptr) {
if (m_model.should_include(*subpackage)) {
generate_alias(dynamic_cast<class_ &>(*subpackage), ostr);
generate(dynamic_cast<class_ &>(*subpackage), ostr);
auto together_group =
m_config.get_together_group(subpackage->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(subpackage.get());
}
else {
generate_alias(dynamic_cast<class_ &>(*subpackage), ostr);
generate(dynamic_cast<class_ &>(*subpackage), ostr);
}
}
}
else if (dynamic_cast<enum_ *>(subpackage.get()) != nullptr) {
if (m_model.should_include(*subpackage)) {
generate_alias(dynamic_cast<enum_ &>(*subpackage), ostr);
generate(dynamic_cast<enum_ &>(*subpackage), ostr);
auto together_group =
m_config.get_together_group(subpackage->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(subpackage.get());
}
else {
generate_alias(dynamic_cast<enum_ &>(*subpackage), ostr);
generate(dynamic_cast<enum_ &>(*subpackage), ostr);
}
}
}
}
@@ -536,37 +552,71 @@ void generator::generate(std::ostream &ostr) const
generate_plantuml_directives(ostr, m_config.puml().before);
for (const auto &p : m_model) {
if (dynamic_cast<package *>(p.get()) != nullptr) {
const auto &sp = dynamic_cast<package &>(*p);
if (!sp.is_empty())
generate(sp, ostr);
if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
if (!pkg->is_empty())
generate(*pkg, ostr);
}
else if (dynamic_cast<class_ *>(p.get()) != nullptr) {
if (m_model.should_include(*p)) {
generate_alias(dynamic_cast<class_ &>(*p), ostr);
generate(dynamic_cast<class_ &>(*p), ostr);
else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {
if (m_model.should_include(*cls)) {
auto together_group =
m_config.get_together_group(cls->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(cls);
}
else {
generate_alias(*cls, ostr);
generate(*cls, ostr);
}
}
}
else if (dynamic_cast<enum_ *>(p.get()) != nullptr) {
if (m_model.should_include(*p)) {
generate_alias(dynamic_cast<enum_ &>(*p), ostr);
generate(dynamic_cast<enum_ &>(*p), ostr);
else if (auto *enm = dynamic_cast<enum_ *>(p.get()); enm) {
if (m_model.should_include(*enm)) {
auto together_group =
m_config.get_together_group(enm->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(enm);
}
else {
generate_alias(*enm, ostr);
generate(*enm, ostr);
}
}
}
}
for (const auto &p : m_model) {
if (dynamic_cast<package *>(p.get()) != nullptr) {
generate_relationships(dynamic_cast<package &>(*p), ostr);
}
else if (dynamic_cast<class_ *>(p.get()) != nullptr) {
if (m_model.should_include(*p)) {
generate_relationships(dynamic_cast<class_ &>(*p), ostr);
// Now generate any diagram elements which are in together groups
for (const auto &[group_name, group_elements] :
current_level_together_groups_) {
ostr << "' together group for " << group_name << "\n";
ostr << "together {\n";
for (auto *e : group_elements) {
if (auto *cls = dynamic_cast<class_ *>(e); cls) {
generate_alias(*cls, ostr);
generate(*cls, ostr);
}
if (auto *enm = dynamic_cast<enum_ *>(e); enm) {
generate_alias(*enm, ostr);
generate(*enm, ostr);
}
}
else if (dynamic_cast<enum_ *>(p.get()) != nullptr) {
if (m_model.should_include(*p)) {
generate_relationships(dynamic_cast<enum_ &>(*p), ostr);
ostr << "}\n";
}
for (const auto &p : m_model) {
if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
generate_relationships(*pkg, ostr);
}
else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {
if (m_model.should_include(*cls)) {
generate_relationships(*cls, ostr);
}
}
else if (auto *enm = dynamic_cast<enum_ *>(p.get()); enm) {
if (m_model.should_include(*enm)) {
generate_relationships(*enm, ostr);
}
}
}

View File

@@ -83,6 +83,9 @@ public:
private:
std::string render_name(std::string name) const;
mutable std::map<std::string, std::vector<common::model::element *>>
current_level_together_groups_;
};
} // namespace plantuml

View File

@@ -224,15 +224,22 @@ void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
for (const auto &[entity_name, hints] : m_config.layout()) {
for (const auto &hint : hints) {
std::stringstream hint_str;
// 'together' layout hint is handled separately
if (hint.hint == config::hint_t::together)
continue;
const auto &hint_entity = std::get<std::string>(hint.entity);
try {
auto element_opt = m_model.get(entity_name);
if (!element_opt)
element_opt = m_model.get((uns | entity_name).to_string());
auto hint_element_opt = m_model.get(hint.entity);
auto hint_element_opt = m_model.get(hint_entity);
if (!hint_element_opt)
hint_element_opt =
m_model.get((uns | hint.entity).to_string());
m_model.get((uns | hint_entity).to_string());
if (!element_opt || !hint_element_opt)
continue;
@@ -244,7 +251,7 @@ void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
catch (clanguml::error::uml_alias_missing &e) {
LOG_DBG("=== Skipping layout hint from {} to {} due "
"to: {}",
entity_name, hint.entity, e.what());
entity_name, hint_entity, e.what());
}
}
}

View File

@@ -142,6 +142,31 @@ std::vector<std::string> diagram::get_translation_units() const
return translation_units;
}
std::optional<std::string> class_diagram::get_together_group(
const std::string &full_name) const
{
const auto relative_name = using_namespace().relative(full_name);
for (const auto &[hint_target, hints] : layout()) {
for (const auto &hint : hints) {
if (hint.hint == hint_t::together) {
const auto &together_others =
std::get<std::vector<std::string>>(hint.entity);
if ((full_name == hint_target) ||
util::contains(together_others, full_name))
return hint_target;
if ((relative_name == hint_target) ||
util::contains(together_others, relative_name))
return hint_target;
}
}
}
return std::nullopt;
}
void diagram::initialize_type_aliases()
{
if (type_aliases().count("std::basic_string<char>") == 0U) {

View File

@@ -80,13 +80,13 @@ struct filter {
std::vector<std::filesystem::path> paths;
};
enum class hint_t { up, down, left, right };
enum class hint_t { up, down, left, right, together };
std::string to_string(hint_t t);
struct layout_hint {
hint_t hint{hint_t::up};
std::string entity;
std::variant<std::string, std::vector<std::string>> entity;
};
using layout_hints = std::map<std::string, std::vector<layout_hint>>;
@@ -184,6 +184,9 @@ struct class_diagram : public diagram {
option<layout_hints> layout{"layout"};
std::optional<std::string> get_together_group(
const std::string &full_name) const;
void initialize_relationship_hints();
};

View File

@@ -468,6 +468,10 @@ template <> struct convert<layout_hint> {
rhs.hint = hint_t::right;
rhs.entity = node["right"].as<std::string>();
}
else if (node["together"]) {
rhs.hint = hint_t::together;
rhs.entity = node["together"].as<std::vector<std::string>>();
}
else
return false;
@@ -645,7 +649,8 @@ config load(
doc["git"] = git_config;
}
return doc.as<config>();
auto d = doc.as<config>();
return d;
}
catch (YAML::BadFile &e) {
throw std::runtime_error(fmt::format(

View File

@@ -159,7 +159,13 @@ YAML::Emitter &operator<<(
YAML::Emitter &operator<<(YAML::Emitter &out, const layout_hint &c)
{
out << YAML::BeginMap;
out << YAML::Key << c.hint << YAML::Value << c.entity;
out << YAML::Key << c.hint << YAML::Value;
if (std::holds_alternative<std::string>(c.entity))
out << std::get<std::string>(c.entity);
else if (std::holds_alternative<std::vector<std::string>>(c.entity))
out << std::get<std::vector<std::string>>(c.entity);
out << YAML::EndMap;
return out;
}