Changed JSON generator paths to relative

This commit is contained in:
Bartek Kryza
2023-03-17 00:39:52 +01:00
parent f13ce56840
commit 4e404a362e
9 changed files with 172 additions and 260 deletions

View File

@@ -25,7 +25,7 @@ using nlohmann::json;
void to_json(nlohmann::json &j, const source_location &sl)
{
j = json{{"file", sl.file()}, {"line", sl.line()}};
j = json{{"file", sl.file_relative()}, {"line", sl.line()}};
}
void to_json(nlohmann::json &j, const element &c)
@@ -37,9 +37,10 @@ void to_json(nlohmann::json &j, const element &c)
if (const auto &comment = c.comment(); comment)
j["comment"] = comment.value();
if (!c.file().empty())
if (!c.file().empty()) {
j["source_location"] =
dynamic_cast<const common::model::source_location &>(c);
}
}
void to_json(nlohmann::json &j, const template_parameter &c)

View File

@@ -36,6 +36,10 @@ public:
void set_file(const std::string &file) { file_ = file; }
const std::string &file_relative() const { return file_relative_; }
void set_file_relative(const std::string &file) { file_relative_ = file; }
unsigned int line() const { return line_; }
void set_line(const unsigned line) { line_ = line; }
@@ -46,6 +50,7 @@ public:
private:
std::string file_;
std::string file_relative_;
unsigned int line_{0};
unsigned int hash_{0};
};

View File

@@ -26,6 +26,7 @@ namespace clanguml::common::visitor {
translation_unit_visitor::translation_unit_visitor(
clang::SourceManager &sm, const clanguml::config::diagram &config)
: source_manager_{sm}
, relative_to_path_{config.relative_to()}
{
if (config.comment_parser() == config::comment_parser_t::plain) {
comment_visitor_ =
@@ -86,6 +87,9 @@ void translation_unit_visitor::set_source_location(
{
if (location.isValid()) {
element.set_file(source_manager_.getFilename(location).str());
element.set_file_relative(util::path_to_url(
std::filesystem::relative(element.file(), relative_to_path_)
.string()));
element.set_line(source_manager_.getSpellingLineNumber(location));
element.set_location_id(location.getHashValue());
}

View File

@@ -106,5 +106,7 @@ private:
clang::SourceManager &source_manager_;
std::unique_ptr<comment::comment_visitor> comment_visitor_;
std::filesystem::path relative_to_path_;
};
} // namespace clanguml::common::visitor