Fixed link generation for template specializations

This commit is contained in:
Bartek Kryza
2023-12-10 11:54:52 +01:00
parent 85ffe2cdee
commit cea427e32d
12 changed files with 59 additions and 35 deletions

View File

@@ -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);

View File

@@ -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;
}
@@ -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;
}
@@ -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));
}

View File

@@ -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));
}

View File

@@ -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();

View File

@@ -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();
@@ -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));
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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(

View File

@@ -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"));
}
}
}

View File

@@ -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)

View File

@@ -10,6 +10,7 @@ glob:
include:
namespaces:
- clanguml
- YAML
exclude:
elements:
- r: "clanguml::config::option.*"

View File

@@ -8,6 +8,7 @@ glob:
include:
namespaces:
- clanguml
- YAML
exclude:
elements:
- r: "clanguml::config::option.*"