Added 'row' and 'column' layout hints

This commit is contained in:
Bartek Kryza
2023-02-04 21:26:57 +01:00
parent edba233030
commit fbc4fadbb7
9 changed files with 210 additions and 28 deletions

View File

@@ -149,6 +149,12 @@ protected:
template <typename E> inja::json element_context(const E &e) const;
private:
void generate_row_column_hints(std::ostream &ostr,
const std::string &entity_name, const config::layout_hint &hint) const;
void generate_position_hints(std::ostream &ostr,
const std::string &entity_name, const config::layout_hint &hint) const;
void init_context();
void init_env();
@@ -218,43 +224,100 @@ void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
{
using namespace clanguml::util;
const auto &uns = m_config.using_namespace();
// Generate layout hints
for (const auto &[entity_name, hints] : m_config.layout()) {
for (const auto &hint : hints) {
std::stringstream hint_str;
try {
if (hint.hint == config::hint_t::together) {
// 'together' layout hint is handled separately
if (hint.hint == config::hint_t::together)
continue;
}
else if (hint.hint == config::hint_t::row ||
hint.hint == config::hint_t::column) {
generate_row_column_hints(ostr, entity_name, hint);
}
else {
generate_position_hints(ostr, entity_name, hint);
}
}
catch (clanguml::error::uml_alias_missing &e) {
LOG_DBG("=== Skipping layout hint '{}' from {} due "
"to: {}",
to_string(hint.hint), entity_name, e.what());
}
}
}
}
template <typename C, typename D>
void generator<C, D>::generate_row_column_hints(std::ostream &ostr,
const std::string &entity_name, const config::layout_hint &hint) const
{
const auto &uns = m_config.using_namespace();
std::vector<std::string> group_elements;
std::vector<std::pair<std::string, std::string>> element_aliases_pairs;
group_elements.push_back(entity_name);
const auto &group_tail = std::get<std::vector<std::string>>(hint.entity);
std::copy(group_tail.begin(), group_tail.end(),
std::back_inserter(group_elements));
auto element_opt = this->m_model.get(entity_name);
if (!element_opt)
element_opt = this->m_model.get((uns | entity_name).to_string());
for (auto it = cbegin(group_elements);
it != cend(group_elements) && std::next(it) != cend(group_elements);
++it) {
const auto &first = *it;
const auto &second = *std::next(it);
auto first_opt = this->m_model.get(first);
if (!first_opt)
first_opt = this->m_model.get((uns | first).to_string());
auto second_opt = this->m_model.get(second);
if (!second_opt)
second_opt = this->m_model.get((uns | second).to_string());
element_aliases_pairs.emplace_back(
first_opt.value().alias(), second_opt.value().alias());
}
std::string hint_direction =
hint.hint == clanguml::config::hint_t::row ? "right" : "down";
for (const auto &[f, s] : element_aliases_pairs) {
ostr << f << " -[hidden]" << hint_direction << "- " << s << '\n';
}
}
template <typename C, typename D>
void generator<C, D>::generate_position_hints(std::ostream &ostr,
const std::string &entity_name, const config::layout_hint &hint) const
{
const auto &uns = m_config.using_namespace();
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);
if (!hint_element_opt)
hint_element_opt =
m_model.get((uns | hint_entity).to_string());
hint_element_opt = m_model.get((uns | hint_entity).to_string());
if (!element_opt || !hint_element_opt)
continue;
return;
std::stringstream hint_str;
hint_str << element_opt.value().alias() << " -[hidden]"
<< clanguml::config::to_string(hint.hint) << "- "
<< hint_element_opt.value().alias() << '\n';
ostr << hint_str.str();
}
catch (clanguml::error::uml_alias_missing &e) {
LOG_DBG("=== Skipping layout hint from {} to {} due "
"to: {}",
entity_name, hint_entity, e.what());
}
}
}
}
template <typename C, typename D>

View File

@@ -34,6 +34,12 @@ std::string to_string(const hint_t t)
return "left";
case hint_t::right:
return "right";
case hint_t::together:
return "together";
case hint_t::row:
return "row";
case hint_t::column:
return "column";
default:
assert(false);
return "";

View File

@@ -80,7 +80,7 @@ struct filter {
std::vector<std::filesystem::path> paths;
};
enum class hint_t { up, down, left, right, together };
enum class hint_t { up, down, left, right, together, row, column };
std::string to_string(hint_t t);

View File

@@ -472,6 +472,14 @@ template <> struct convert<layout_hint> {
rhs.hint = hint_t::together;
rhs.entity = node["together"].as<std::vector<std::string>>();
}
else if (node["row"]) {
rhs.hint = hint_t::row;
rhs.entity = node["row"].as<std::vector<std::string>>();
}
else if (node["column"]) {
rhs.hint = hint_t::column;
rhs.entity = node["column"].as<std::vector<std::string>>();
}
else
return false;

17
tests/t00055/.clang-uml Normal file
View File

@@ -0,0 +1,17 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00055_class:
type: class
glob:
- ../../tests/t00055/t00055.cc
include:
namespaces:
- clanguml::t00055
using_namespace:
- clanguml::t00055
layout:
A:
- row: [C, E, G, I]
B:
- column: [D, F, H, J]

24
tests/t00055/t00055.cc Normal file
View File

@@ -0,0 +1,24 @@
namespace clanguml {
namespace t00055 {
struct A {
};
struct B {
};
struct C {
};
struct D {
};
struct E {
};
struct F {
};
struct G {
};
struct H {
};
struct I {
};
struct J {
};
}
}

60
tests/t00055/test_case.h Normal file
View File

@@ -0,0 +1,60 @@
/**
* tests/t00055/test_case.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
TEST_CASE("t00055", "[test-case][class]")
{
auto [config, db] = load_config("t00055");
auto diagram = config.diagrams["t00055_class"];
REQUIRE(diagram->name == "t00055_class");
auto model = generate_class_diagram(*db, diagram);
REQUIRE(model->name() == "t00055_class");
auto puml = generate_class_puml(diagram, *model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
// Check if all classes exist
REQUIRE_THAT(puml, IsClass(_A("A")));
REQUIRE_THAT(puml, IsClass(_A("B")));
REQUIRE_THAT(puml, IsClass(_A("C")));
REQUIRE_THAT(puml, IsClass(_A("D")));
REQUIRE_THAT(puml, IsClass(_A("E")));
REQUIRE_THAT(puml, IsClass(_A("F")));
REQUIRE_THAT(puml, IsClass(_A("G")));
REQUIRE_THAT(puml, IsClass(_A("H")));
REQUIRE_THAT(puml, IsClass(_A("I")));
REQUIRE_THAT(puml, IsClass(_A("J")));
REQUIRE_THAT(puml, IsLayoutHint(_A("A"), "right", _A("C")));
REQUIRE_THAT(puml, IsLayoutHint(_A("C"), "right", _A("E")));
REQUIRE_THAT(puml, IsLayoutHint(_A("E"), "right", _A("G")));
REQUIRE_THAT(puml, IsLayoutHint(_A("G"), "right", _A("I")));
REQUIRE_THAT(puml, IsLayoutHint(_A("B"), "down", _A("D")));
REQUIRE_THAT(puml, IsLayoutHint(_A("D"), "down", _A("F")));
REQUIRE_THAT(puml, IsLayoutHint(_A("F"), "down", _A("H")));
REQUIRE_THAT(puml, IsLayoutHint(_A("H"), "down", _A("J")));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
}

View File

@@ -248,6 +248,7 @@ using namespace clanguml::test::matchers;
#include "t00052/test_case.h"
#include "t00053/test_case.h"
#include "t00054/test_case.h"
#include "t00055/test_case.h"
///
/// Sequence diagram tests

View File

@@ -159,6 +159,9 @@ test_cases:
- name: t00054
title: Test case for `together` layout hint in class diagram with rendered namespaces
description:
- name: t00055
title: Test case for `row` and `column` layout hints
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram test case