Fixed relative package diagram generation based on using_namespace

This commit is contained in:
Bartek Kryza 2022-01-22 23:35:46 +01:00
parent 2279192f4f
commit b931845ad0
5 changed files with 33 additions and 17 deletions

View File

@ -25,7 +25,6 @@ diagrams:
## Source code
File t30001.cc
```cpp
namespace clanguml {
namespace t30001 {
namespace A {
@ -48,8 +47,8 @@ namespace BBB {
namespace BB {
} // namespace BB
} // namespace B
}
}
} // namespace t30001
} // namespace clanguml
```
## Generated UML diagrams

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

View File

@ -1,5 +1,5 @@
/**
* src/class_diagram/generators/plantuml/class_diagram_generator.cc
* src/package_diagram/generators/plantuml/package_diagram_generator.cc
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
@ -79,7 +79,8 @@ std::string generator::name(relationship_t r) const
}
}
void generator::generate(const package &p, std::ostream &ostr) const
void generator::generate(
const package &p, std::ostream &ostr) const
{
const auto uns = m_config.using_namespace;
@ -143,12 +144,8 @@ void generator::generate(std::ostream &ostr) const
std::string note{b};
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(note, alias_match)) {
auto full_name =
fmt::format("{}::{}", fmt::join(m_config.using_namespace, "::"),
ns_relative(
m_config.using_namespace, std::get<0>(alias_match)));
auto alias = m_model.to_alias(full_name);
auto alias = m_model.to_alias(ns_relative(
m_config.using_namespace, std::get<0>(alias_match)));
note.replace(
std::get<1>(alias_match), std::get<2>(alias_match), alias);
}

View File

@ -90,12 +90,23 @@ void translation_unit_visitor::operator()(const cppast::cpp_entity &file)
if (!ns_declaration.is_anonymous() &&
!ns_declaration.is_inline()) {
auto p = std::make_unique<package>(
ctx.config().using_namespace);
p->set_name(e.name());
p->set_namespace(ctx.get_namespace());
ctx.diagram().add_package(
ctx.get_namespace(), std::move(p));
auto package_path = ctx.get_namespace();
package_path.push_back(e.name());
auto usn =
util::split(ctx.config().using_namespace[0], "::");
if (!starts_with(usn, package_path)) {
auto p = std::make_unique<package>(
ctx.config().using_namespace);
remove_prefix(package_path, usn);
package_path.pop_back();
p->set_name(e.name());
p->set_namespace(package_path);
ctx.diagram().add_package(
package_path, std::move(p));
}
ctx.push_namespace(e.name());
}

View File

@ -145,4 +145,13 @@ bool starts_with(const std::vector<T> &col, const std::vector<T> &prefix)
return std::vector<std::string>(prefix.begin(), prefix.end()) ==
std::vector<std::string>(col.begin(), col.begin() + prefix.size());
}
template <typename T>
void remove_prefix(std::vector<T> &col, const std::vector<T> &prefix)
{
if(!starts_with(col, prefix))
return;
col = std::vector<T>(col.begin()+prefix.size(), col.end());
}
}