mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
refactor: use source_location, not region_base*
This commit is contained in:
101
toml/region.hpp
101
toml/region.hpp
@@ -412,107 +412,6 @@ struct region final : public region_base
|
||||
const_iterator first_, last_;
|
||||
};
|
||||
|
||||
// to show a better error message.
|
||||
inline std::string format_underline(const std::string& message,
|
||||
const std::vector<std::pair<region_base const*, std::string>>& reg_com,
|
||||
const std::vector<std::string>& helps = {},
|
||||
const bool colorize = TOML11_ERROR_MESSAGE_COLORIZED)
|
||||
{
|
||||
assert(!reg_com.empty());
|
||||
|
||||
const auto line_num_width = static_cast<int>(std::max_element(
|
||||
reg_com.begin(), reg_com.end(),
|
||||
[](std::pair<region_base const*, std::string> const& lhs,
|
||||
std::pair<region_base const*, std::string> const& rhs)
|
||||
{
|
||||
return lhs.first->line_num().size() < rhs.first->line_num().size();
|
||||
}
|
||||
)->first->line_num().size());
|
||||
|
||||
std::ostringstream retval;
|
||||
|
||||
if(colorize)
|
||||
{
|
||||
retval << color::colorize; // turn on ANSI color
|
||||
}
|
||||
|
||||
// XXX
|
||||
// Here, before `colorize` support, it does not output `[error]` prefix
|
||||
// automatically. So some user may output it manually and this change may
|
||||
// duplicate the prefix. To avoid it, check the first 7 characters and
|
||||
// if it is "[error]", it removes that part from the message shown.
|
||||
if(message.size() > 7 && message.substr(0, 7) == "[error]")
|
||||
{
|
||||
retval << color::bold << color::red << "[error]" << color::reset
|
||||
<< color::bold << message.substr(7) << color::reset << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
retval << color::bold << color::red << "[error] " << color::reset
|
||||
<< color::bold << message << color::reset << '\n';
|
||||
}
|
||||
|
||||
for(auto iter = reg_com.begin(); iter != reg_com.end(); ++iter)
|
||||
{
|
||||
// if the filenames are the same, print "..."
|
||||
if(iter != reg_com.begin() &&
|
||||
std::prev(iter)->first->name() == iter->first->name())
|
||||
{
|
||||
retval << color::bold << color::blue << "\n ...\n" << color::reset;
|
||||
}
|
||||
else // if filename differs, print " --> filename.toml"
|
||||
{
|
||||
if(iter != reg_com.begin()) {retval << '\n';}
|
||||
retval << color::bold << color::blue << " --> " << color::reset
|
||||
<< iter->first->name() << '\n';
|
||||
// add one almost-empty line for readability
|
||||
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ')
|
||||
<< color::bold << color::blue << " | " << color::reset << '\n';
|
||||
}
|
||||
const region_base* const reg = iter->first;
|
||||
const std::string& comment = iter->second;
|
||||
|
||||
retval << ' ' << color::bold << color::blue << std::setw(line_num_width)
|
||||
<< std::right << reg->line_num() << " | " << color::reset
|
||||
<< reg->line() << '\n';
|
||||
|
||||
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ')
|
||||
<< color::bold << color::blue << " | " << color::reset
|
||||
<< make_string(reg->before(), ' ');
|
||||
|
||||
if(reg->size() == 1)
|
||||
{
|
||||
// invalid
|
||||
// ^------
|
||||
retval << color::bold << color::red
|
||||
<< '^' << make_string(reg->after(), '-') << color::reset;
|
||||
}
|
||||
else
|
||||
{
|
||||
// invalid
|
||||
// ~~~~~~~
|
||||
const auto underline_len = (std::min)(reg->size(), reg->line().size());
|
||||
retval << color::bold << color::red
|
||||
<< make_string(underline_len, '~') << color::reset;
|
||||
}
|
||||
retval << ' ';
|
||||
retval << comment;
|
||||
}
|
||||
|
||||
if(!helps.empty())
|
||||
{
|
||||
retval << '\n';
|
||||
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ');
|
||||
retval << color::bold << color::blue << " | " << color::reset;
|
||||
for(const auto& help : helps)
|
||||
{
|
||||
retval << color::bold << "\nHint: " << color::reset;
|
||||
retval << help;
|
||||
}
|
||||
}
|
||||
return retval.str();
|
||||
}
|
||||
|
||||
} // detail
|
||||
} // toml
|
||||
#endif// TOML11_REGION_H
|
||||
|
Reference in New Issue
Block a user