Skip to content

Commit

Permalink
Fixed link generation for template specializations
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Dec 10, 2023
1 parent 85ffe2c commit cea427e
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ generator::generator(diagram_config &config, diagram_model &model)
void generator::generate_link(
std::ostream &ostr, const class_diagram::model::class_element &e) const
{
if (e.file().empty())
if (e.file_relative().empty())
return;

auto context = element_context(e);
Expand Down
10 changes: 4 additions & 6 deletions src/class_diagram/visitor/template_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ std::unique_ptr<class_> template_builder::build(const clang::NamedDecl *cls,
template_instantiation.set_id(
common::to_id(template_instantiation_ptr->full_name(false)));

visitor_.set_source_location(*template_decl, template_instantiation);

LOG_DBG("**** {} -> {}", template_instantiation.full_name(false),
template_instantiation.file());
visitor_.set_source_location(*cls, template_instantiation);

return template_instantiation_ptr;
}
Expand Down Expand Up @@ -361,6 +358,8 @@ template_builder::build_from_class_template_specialization(
template_instantiation_ptr->full_name(false), templated_decl_id);
}

visitor_.set_source_location(*template_decl, template_instantiation);

return template_instantiation_ptr;
}

Expand Down Expand Up @@ -1064,8 +1063,7 @@ template_builder::try_as_template_specialization_type(

if (diagram().should_include(
namespace_{nested_template_instantiation_full_name})) {
visitor_.set_source_location(
*template_decl, *nested_template_instantiation);
visitor_.set_source_location(*cls, *nested_template_instantiation);
visitor_.add_class(std::move(nested_template_instantiation));
}

Expand Down
2 changes: 2 additions & 0 deletions src/class_diagram/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ bool translation_unit_visitor::VisitTypeAliasTemplateDecl(

LOG_DBG("Adding class {} with id {}", name, id);

set_source_location(*cls, *template_specialization_ptr);

add_class(std::move(template_specialization_ptr));
}

Expand Down
19 changes: 12 additions & 7 deletions src/common/generators/mermaid/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,22 @@ inja::json generator<C, D>::element_context(const E &e) const

if (!e.file().empty()) {
std::filesystem::path file{e.file()};
std::string relative_path = file.string();
std::string git_relative_path = file.string();
if (!e.file_relative().empty()) {
#if _MSC_VER
if (file.is_absolute() && ctx.contains("git"))
if (file.is_absolute() && ctx.contains("git"))
#else
if (file.is_absolute() && ctx.template contains("git"))
if (file.is_absolute() && ctx.template contains("git"))
#endif
relative_path =
std::filesystem::relative(file, ctx["git"]["toplevel"])
.string();
git_relative_path =
std::filesystem::relative(file, ctx["git"]["toplevel"])
.string();
}
else {
git_relative_path = "";
}

ctx["element"]["source"]["path"] = util::path_to_url(relative_path);
ctx["element"]["source"]["path"] = util::path_to_url(git_relative_path);
ctx["element"]["source"]["full_path"] = file.string();
ctx["element"]["source"]["name"] = file.filename().string();
ctx["element"]["source"]["line"] = e.line();
Expand Down
22 changes: 13 additions & 9 deletions src/common/generators/plantuml/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,22 @@ inja::json generator<C, D>::element_context(const E &e) const

if (!e.file().empty()) {
std::filesystem::path file{e.file()};
std::string relative_path = file.string();
std::string git_relative_path = file.string();
if (!e.file_relative().empty()) {
#if _MSC_VER
if (file.is_absolute() && ctx.contains("git"))
if (file.is_absolute() && ctx.contains("git"))
#else
if (file.is_absolute() && ctx.template contains("git"))
if (file.is_absolute() && ctx.template contains("git"))
#endif
relative_path =
std::filesystem::relative(file, ctx["git"]["toplevel"])
.string();
git_relative_path =
std::filesystem::relative(file, ctx["git"]["toplevel"])
.string();
}
else {
git_relative_path = "";
}

ctx["element"]["source"]["path"] = util::path_to_url(relative_path);
ctx["element"]["source"]["path"] = util::path_to_url(git_relative_path);
ctx["element"]["source"]["full_path"] = file.string();
ctx["element"]["source"]["name"] = file.filename().string();
ctx["element"]["source"]["line"] = e.line();
Expand Down Expand Up @@ -477,13 +482,12 @@ void generator<C, D>::generate_link(std::ostream &ostr, const E &e) const
{
const auto &config = generators::generator<C, D>::config();

if (e.file().empty())
if (e.file_relative().empty())
return;

ostr << " [[";
try {
if (!config.generate_links().link.empty()) {

ostr << env().render(std::string_view{config.generate_links().link},
element_context(e));
}
Expand Down
9 changes: 5 additions & 4 deletions src/common/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@ void translation_unit_visitor::set_source_location(

file_path = fs::canonical(file_path);

if (!util::is_relative_to(file_path, relative_to_path_)) {
return;
}

file = file_path.string();

element.set_file(file);

if (util::is_relative_to(file_path, relative_to_path_)) {
element.set_file_relative(util::path_to_url(
fs::relative(element.file(), relative_to_path_).string()));
}
else {
element.set_file_relative("");
}

element.set_translation_unit(tu_path().string());
element.set_line(line);
element.set_column(column);
Expand Down
10 changes: 10 additions & 0 deletions src/include_diagram/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ translation_unit_visitor::include_visitor::process_source_file(
source_file.set_file(std::filesystem::absolute(file.string())
.lexically_normal()
.string());

if (util::is_relative_to(file_path, config().root_directory())) {
source_file.set_file_relative(util::path_to_url(
relative(source_file.file(), config().root_directory())
.string()));
}
else {
source_file.set_file_relative("");
}

source_file.set_line(0);

return source_file.id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,18 @@ void generator::generate_message_comment(
if (comment_generated_from_note_decorators)
return;

if (!config().generate_message_comments() || !m.comment())
if (!config().generate_message_comments())
return;

ostr << "note over " << generate_alias(from.value()) << '\n';
if (const auto &comment = m.comment(); comment) {
ostr << "note over " << generate_alias(from.value()) << '\n';

ostr << util::format_message_comment(
m.comment().value(), config().message_comment_width())
<< '\n';
ostr << util::format_message_comment(
comment.value(), config().message_comment_width())
<< '\n';

ostr << "end note" << '\n';
ostr << "end note" << '\n';
}
}

void generator::generate_participant(
Expand Down
2 changes: 1 addition & 1 deletion src/sequence_diagram/model/diagram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void diagram::print() const
from_participant.full_name(false), from_participant.id(),
to_participant.full_name(false), to_participant.id(),
message.message_name(), to_string(message.type()),
message.comment() ? message.comment().value() : "None");
message.comment().value_or("None"));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ bool is_relative_to(
if (child.has_root_directory() != parent.has_root_directory())
return false;

return starts_with(child, parent);
return starts_with(weakly_canonical(child), weakly_canonical(parent));
}

std::string format_message_comment(const std::string &comment, unsigned width)
Expand Down
1 change: 1 addition & 0 deletions uml/sequence/cli_handle_options_sequence.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ glob:
include:
namespaces:
- clanguml
- YAML
exclude:
elements:
- r: "clanguml::config::option.*"
Expand Down
1 change: 1 addition & 0 deletions uml/sequence/load_config_sequence.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ glob:
include:
namespaces:
- clanguml
- YAML
exclude:
elements:
- r: "clanguml::config::option.*"
Expand Down

0 comments on commit cea427e

Please sign in to comment.