Skip to content

Commit 0da44ca

Browse files
committed
Reuse color definitions between SourceReferenceFormatter and CommonSyntaxTest
1 parent d4f5503 commit 0da44ca

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

liblangutil/SourceReferenceFormatter.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error
5555
);
5656
}
5757

58+
char const* SourceReferenceFormatter::errorTextColor(Error::Severity _severity)
59+
{
60+
switch (_severity)
61+
{
62+
case Error::Severity::Error: return RED;
63+
case Error::Severity::Warning: return YELLOW;
64+
case Error::Severity::Info: return WHITE;
65+
}
66+
util::unreachable();
67+
}
68+
69+
char const* SourceReferenceFormatter::errorHighlightColor(Error::Severity _severity)
70+
{
71+
switch (_severity)
72+
{
73+
case Error::Severity::Error: return RED_BACKGROUND;
74+
case Error::Severity::Warning: return ORANGE_BACKGROUND_256;
75+
case Error::Severity::Info: return GRAY_BACKGROUND;
76+
}
77+
util::unreachable();
78+
}
79+
5880
AnsiColorized SourceReferenceFormatter::normalColored() const
5981
{
6082
return AnsiColorized(m_stream, m_colored, {WHITE});
@@ -67,18 +89,7 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
6789

6890
AnsiColorized SourceReferenceFormatter::errorColored(std::ostream& _stream, bool _colored, Error::Severity _severity)
6991
{
70-
// We used to color messages of any severity as errors so this seems like a good default
71-
// for cases where severity cannot be determined.
72-
char const* textColor = RED;
73-
74-
switch (_severity)
75-
{
76-
case Error::Severity::Error: textColor = RED; break;
77-
case Error::Severity::Warning: textColor = YELLOW; break;
78-
case Error::Severity::Info: textColor = WHITE; break;
79-
}
80-
81-
return AnsiColorized(_stream, _colored, {BOLD, textColor});
92+
return AnsiColorized(_stream, _colored, {BOLD, errorTextColor(_severity)});
8293
}
8394

8495
AnsiColorized SourceReferenceFormatter::messageColored(std::ostream& _stream, bool _colored)

liblangutil/SourceReferenceFormatter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ class SourceReferenceFormatter
127127
bool _withErrorIds = false
128128
);
129129

130+
/// The default text color for printing error messages of a given severity in the terminal.
131+
/// Assumes a dark background color.
132+
static char const* errorTextColor(Error::Severity _severity);
133+
134+
/// The default background color for highlighting source fragments corresponding to an error
135+
/// of a given severity in the terminal. Assumes a light text color.
136+
/// @note This is *not* meant to be used for the same text in combination with @a errorTextColor().
137+
/// It's an alternative way to highlight it, while preserving the original text color.
138+
static char const* errorHighlightColor(Error::Severity _severity);
139+
130140
private:
131141
util::AnsiColorized normalColored() const;
132142
util::AnsiColorized frameColored() const;

test/CommonSyntaxTest.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
#include <test/CommonSyntaxTest.h>
2020
#include <test/Common.h>
2121
#include <test/TestCase.h>
22+
23+
#include <liblangutil/SourceReferenceFormatter.h>
24+
2225
#include <libsolutil/CommonIO.h>
2326
#include <libsolutil/StringUtils.h>
27+
2428
#include <boost/algorithm/string.hpp>
2529
#include <boost/algorithm/string/predicate.hpp>
2630
#include <boost/test/unit_test.hpp>
2731
#include <boost/throw_exception.hpp>
32+
2833
#include <fstream>
2934
#include <memory>
3035
#include <stdexcept>
@@ -116,21 +121,17 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix,
116121
assert(static_cast<size_t>(error.locationStart) <= source.length());
117122
assert(static_cast<size_t>(error.locationEnd) <= source.length());
118123
for (int i = error.locationStart; i < error.locationEnd; i++)
119-
if (error.type == Error::Type::Info)
120-
{
121-
if (sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET)
122-
sourceFormatting[static_cast<size_t>(i)] = util::formatting::GRAY_BACKGROUND;
123-
}
124-
else if (error.type == Error::Type::Warning)
125-
{
126-
if (
127-
sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET ||
128-
sourceFormatting[static_cast<size_t>(i)] == util::formatting::GRAY_BACKGROUND
129-
)
130-
sourceFormatting[static_cast<size_t>(i)] = util::formatting::ORANGE_BACKGROUND_256;
131-
}
132-
else
133-
sourceFormatting[static_cast<size_t>(i)] = util::formatting::RED_BACKGROUND;
124+
{
125+
char const*& cellFormat = sourceFormatting[static_cast<size_t>(i)];
126+
char const* infoBgColor = SourceReferenceFormatter::errorHighlightColor(Error::Severity::Info);
127+
128+
if (
129+
(error.type != Error::Type::Warning && error.type != Error::Type::Info) ||
130+
(error.type == Error::Type::Warning && (cellFormat == RESET || cellFormat == infoBgColor)) ||
131+
(error.type == Error::Type::Info && cellFormat == RESET)
132+
)
133+
cellFormat = SourceReferenceFormatter::errorHighlightColor(Error::errorSeverity(error.type));
134+
}
134135
}
135136

136137
_stream << _linePrefix << sourceFormatting.front() << source.front();
@@ -200,7 +201,7 @@ void CommonSyntaxTest::printErrorList(
200201
util::AnsiColorized scope(
201202
_stream,
202203
_formatted,
203-
{BOLD, error.type == Error::Type::Info ? WHITE : (error.type == Error::Type::Warning ? YELLOW : RED)}
204+
{BOLD, SourceReferenceFormatter::errorTextColor(Error::errorSeverity(error.type))}
204205
);
205206
_stream << _linePrefix << Error::formatErrorType(error.type);
206207
if (error.errorId.has_value())

0 commit comments

Comments
 (0)