Reuse color definitions between SourceReferenceFormatter and CommonSyntaxTest

This commit is contained in:
Kamil Śliwak 2023-09-29 22:43:49 +02:00
parent 7857562a3b
commit 6c1ed8c2d0
3 changed files with 49 additions and 28 deletions

View File

@ -55,6 +55,28 @@ std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error
);
}
char const* SourceReferenceFormatter::errorTextColor(Error::Severity _severity)
{
switch (_severity)
{
case Error::Severity::Error: return RED;
case Error::Severity::Warning: return YELLOW;
case Error::Severity::Info: return WHITE;
}
util::unreachable();
}
char const* SourceReferenceFormatter::errorHighlightColor(Error::Severity _severity)
{
switch (_severity)
{
case Error::Severity::Error: return RED_BACKGROUND;
case Error::Severity::Warning: return ORANGE_BACKGROUND_256;
case Error::Severity::Info: return GRAY_BACKGROUND;
}
util::unreachable();
}
AnsiColorized SourceReferenceFormatter::normalColored() const
{
return AnsiColorized(m_stream, m_colored, {WHITE});
@ -67,18 +89,7 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
{
// We used to color messages of any severity as errors so this seems like a good default
// for cases where severity cannot be determined.
char const* textColor = RED;
switch (_severity)
{
case Error::Severity::Error: textColor = RED; break;
case Error::Severity::Warning: textColor = YELLOW; break;
case Error::Severity::Info: textColor = WHITE; break;
}
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
return AnsiColorized(m_stream, m_colored, {BOLD, errorTextColor(_severity)});
}
AnsiColorized SourceReferenceFormatter::messageColored() const

View File

@ -118,6 +118,15 @@ public:
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
/// The default text color for printing error messages of a given severity in the terminal.
/// Only suitable for color schemes with a dark background.
static char const* errorTextColor(Error::Severity _severity);
/// The default background color for highlighting source fragments corresponding to an error
/// of a given severity in the terminal.
/// Only suitable for color schemes with a light text.
static char const* errorHighlightColor(Error::Severity _severity);
private:
util::AnsiColorized normalColored() const;
util::AnsiColorized frameColored() const;

View File

@ -19,11 +19,16 @@
#include <test/CommonSyntaxTest.h>
#include <test/Common.h>
#include <test/TestCase.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <libsolutil/CommonIO.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp>
#include <fstream>
#include <memory>
#include <stdexcept>
@ -115,21 +120,17 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix,
assert(static_cast<size_t>(error.locationStart) <= source.length());
assert(static_cast<size_t>(error.locationEnd) <= source.length());
for (int i = error.locationStart; i < error.locationEnd; i++)
if (error.type == Error::Type::Info)
{
if (sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET)
sourceFormatting[static_cast<size_t>(i)] = util::formatting::GRAY_BACKGROUND;
}
else if (error.type == Error::Type::Warning)
{
char const*& cellFormat = sourceFormatting[static_cast<size_t>(i)];
char const* infoBgColor = SourceReferenceFormatter::errorHighlightColor(Error::Severity::Info);
if (
sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET ||
sourceFormatting[static_cast<size_t>(i)] == util::formatting::GRAY_BACKGROUND
(error.type != Error::Type::Warning && error.type != Error::Type::Info) ||
(error.type == Error::Type::Warning && (cellFormat == RESET || cellFormat == infoBgColor)) ||
(error.type == Error::Type::Info && cellFormat == RESET)
)
sourceFormatting[static_cast<size_t>(i)] = util::formatting::ORANGE_BACKGROUND_256;
cellFormat = SourceReferenceFormatter::errorHighlightColor(Error::errorSeverity(error.type));
}
else
sourceFormatting[static_cast<size_t>(i)] = util::formatting::RED_BACKGROUND;
}
_stream << _linePrefix << sourceFormatting.front() << source.front();
@ -202,7 +203,7 @@ void CommonSyntaxTest::printErrorList(
util::AnsiColorized scope(
_stream,
_formatted,
{BOLD, error.type == Error::Type::Info ? WHITE : (error.type == Error::Type::Warning ? YELLOW : RED)}
{BOLD, SourceReferenceFormatter::errorTextColor(Error::errorSeverity(error.type))}
);
_stream << _linePrefix << Error::formatErrorType(error.type);
if (error.errorId.has_value())