diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index 76cb1df61..0e84242a7 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -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 diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index 91f1d7ddd..46cd97490 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -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; diff --git a/test/CommonSyntaxTest.cpp b/test/CommonSyntaxTest.cpp index d26f60ccc..209a041ff 100644 --- a/test/CommonSyntaxTest.cpp +++ b/test/CommonSyntaxTest.cpp @@ -19,11 +19,16 @@ #include #include #include + +#include + #include + #include #include #include #include + #include #include #include @@ -115,21 +120,17 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix, assert(static_cast(error.locationStart) <= source.length()); assert(static_cast(error.locationEnd) <= source.length()); for (int i = error.locationStart; i < error.locationEnd; i++) - if (error.type == Error::Type::Info) - { - if (sourceFormatting[static_cast(i)] == util::formatting::RESET) - sourceFormatting[static_cast(i)] = util::formatting::GRAY_BACKGROUND; - } - else if (error.type == Error::Type::Warning) - { - if ( - sourceFormatting[static_cast(i)] == util::formatting::RESET || - sourceFormatting[static_cast(i)] == util::formatting::GRAY_BACKGROUND - ) - sourceFormatting[static_cast(i)] = util::formatting::ORANGE_BACKGROUND_256; - } - else - sourceFormatting[static_cast(i)] = util::formatting::RED_BACKGROUND; + { + char const*& cellFormat = sourceFormatting[static_cast(i)]; + char const* infoBgColor = SourceReferenceFormatter::errorHighlightColor(Error::Severity::Info); + + if ( + (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) + ) + cellFormat = SourceReferenceFormatter::errorHighlightColor(Error::errorSeverity(error.type)); + } } _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())