Skip to content

Commit 27220d5

Browse files
committed
fmt: Properly handle transform functions
1 parent f910448 commit 27220d5

2 files changed

Lines changed: 39 additions & 18 deletions

File tree

generators/include/pl/formatters/formatter_json.hpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,27 @@ namespace pl::gen::fmt {
142142
}
143143
}
144144

145+
std::string formatLiteral(const core::Token::Literal &literal) {
146+
return std::visit(wolv::util::overloaded {
147+
[&](integral auto value) -> std::string { return ::fmt::format("{}", value); },
148+
[&](std::floating_point auto value) -> std::string { return ::fmt::format("{}", value); },
149+
[&](const std::string &value) -> std::string { return ::fmt::format("\"{}\"", value); },
150+
[&](bool value) -> std::string { return value ? "true" : "false"; },
151+
[&](char value) -> std::string { return ::fmt::format("\"{}\"", value); },
152+
[&](const std::shared_ptr<ptrn::Pattern> &value) -> std::string { return ::fmt::format("\"{}\"", value->toString()); },
153+
}, literal);
154+
}
155+
145156
void formatValue(pl::ptrn::Pattern *pattern) {
146157
if (pattern->getVisibility() == ptrn::Visibility::Hidden) return;
147158
if (pattern->getVisibility() == ptrn::Visibility::TreeHidden) return;
148159

149-
if (const auto &functionName = pattern->getReadFormatterFunction(); !functionName.empty())
160+
if (!pattern->getReadFormatterFunction().empty())
150161
formatString(pattern);
151-
else if (!pattern->isSealed()) {
162+
else if (pattern->isSealed()) {
152163
auto literal = pattern->getValue();
153164

154-
addLine(pattern->getVariableName(), std::visit(wolv::util::overloaded {
155-
[&](integral auto value) -> std::string { return ::fmt::format("{}", value); },
156-
[&](std::floating_point auto value) -> std::string { return ::fmt::format("{}", value); },
157-
[&](const std::string &value) -> std::string { return ::fmt::format("\"{}\"", value); },
158-
[&](bool value) -> std::string { return value ? "true" : "false"; },
159-
[&](char value) -> std::string { return ::fmt::format("\"{}\"", value); },
160-
[&](const std::shared_ptr<ptrn::Pattern> &value) -> std::string { return ::fmt::format("\"{}\"", value->toString()); },
161-
}, literal) + ",");
165+
addLine(pattern->getVariableName(), formatLiteral(literal) + ",");
162166
}
163167
}
164168

generators/include/pl/formatters/formatter_yaml.hpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace pl::gen::fmt {
8585
if (pattern->getVisibility() == ptrn::Visibility::TreeHidden) return;
8686

8787
if (pattern->isSealed()) {
88-
formatString(pattern);
88+
formatValue(pattern);
8989
} else {
9090
addLine(pattern->getVariableName());
9191
pushIndent();
@@ -110,19 +110,36 @@ namespace pl::gen::fmt {
110110
addLine(pattern->getVariableName(), ::fmt::format("\"{}\"", hlp::encodeByteString({ result.begin(), result.end() })));
111111
}
112112

113-
void formatValue(pl::ptrn::Pattern *pattern) {
114-
if (pattern->getVisibility() == ptrn::Visibility::Hidden) return;
115-
if (pattern->getVisibility() == ptrn::Visibility::TreeHidden) return;
116-
117-
auto result = pattern->toString();
113+
std::string formatLiteral(const core::Token::Literal &literal) {
114+
auto result = std::visit(wolv::util::overloaded {
115+
[&](integral auto value) -> std::string { return ::fmt::format("{}", value); },
116+
[&](std::floating_point auto value) -> std::string { return ::fmt::format("{}", value); },
117+
[&](const std::string &value) -> std::string { return ::fmt::format("\"{}\"", value); },
118+
[&](bool value) -> std::string { return value ? "true" : "false"; },
119+
[&](char value) -> std::string { return ::fmt::format("\"{}\"", value); },
120+
[&](const std::shared_ptr<ptrn::Pattern> &value) -> std::string { return ::fmt::format("\"{}\"", value->toString()); },
121+
}, literal);
118122

119123
const bool number = std::ranges::all_of(result, [](char c) { return std::isdigit(c) || c == '.' || c == '-' || c == '+'; });
120124
const bool needsEscape = std::ranges::any_of(result, [](char c) { return std::ispunct(c) || !std::isprint(c); });
121125

122126
if (!number && needsEscape)
123-
formatString(pattern);
127+
return result;
124128
else
125-
addLine(pattern->getVariableName(), wolv::util::replaceStrings(result, "\n", " "));
129+
return wolv::util::replaceStrings(result, "\n", " ");
130+
}
131+
132+
void formatValue(pl::ptrn::Pattern *pattern) {
133+
if (pattern->getVisibility() == ptrn::Visibility::Hidden) return;
134+
if (pattern->getVisibility() == ptrn::Visibility::TreeHidden) return;
135+
136+
if (!pattern->getReadFormatterFunction().empty())
137+
formatString(pattern);
138+
else {
139+
auto literal = pattern->getValue();
140+
141+
addLine(pattern->getVariableName(), wolv::util::replaceStrings(formatLiteral(literal), "\n", " "));
142+
}
126143
}
127144

128145
private:

0 commit comments

Comments
 (0)