Skip to content

Commit 963eb15

Browse files
committed
SourceReferenceFormatter: Expose a function for formatting just a message, without other details
1 parent e98f174 commit 963eb15

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

liblangutil/SourceReferenceFormatter.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
6565
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE});
6666
}
6767

68-
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
68+
AnsiColorized SourceReferenceFormatter::errorColored(std::ostream& _stream, bool _colored, Error::Severity _severity)
6969
{
7070
// We used to color messages of any severity as errors so this seems like a good default
7171
// for cases where severity cannot be determined.
@@ -78,12 +78,12 @@ AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity)
7878
case Error::Severity::Info: textColor = WHITE; break;
7979
}
8080

81-
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
81+
return AnsiColorized(_stream, _colored, {BOLD, textColor});
8282
}
8383

84-
AnsiColorized SourceReferenceFormatter::messageColored() const
84+
AnsiColorized SourceReferenceFormatter::messageColored(std::ostream& _stream, bool _colored)
8585
{
86-
return AnsiColorized(m_stream, m_colored, {BOLD, WHITE});
86+
return AnsiColorized(_stream, _colored, {BOLD, WHITE});
8787
}
8888

8989
AnsiColorized SourceReferenceFormatter::secondaryColored() const
@@ -175,14 +175,26 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
175175
}
176176
}
177177

178-
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
178+
void SourceReferenceFormatter::printPrimaryMessage(
179+
std::ostream& _stream,
180+
std::string _message,
181+
std::variant<Error::Type, Error::Severity> _typeOrSeverity,
182+
std::optional<ErrorId> _errorId,
183+
bool _colored,
184+
bool _withErrorIds
185+
)
179186
{
180-
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << Error::formatTypeOrSeverity(_msg._typeOrSeverity);
187+
errorColored(_stream, _colored, Error::errorSeverityOrType(_typeOrSeverity)) << Error::formatTypeOrSeverity(_typeOrSeverity);
188+
189+
if (_withErrorIds && _errorId.has_value())
190+
errorColored(_stream, _colored, Error::errorSeverityOrType(_typeOrSeverity)) << " (" << _errorId.value().error << ")";
181191

182-
if (m_withErrorIds && _msg.errorId.has_value())
183-
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << " (" << _msg.errorId.value().error << ")";
192+
messageColored(_stream, _colored) << ": " << _message << '\n';
193+
}
184194

185-
messageColored() << ": " << _msg.primary.message << '\n';
195+
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
196+
{
197+
printPrimaryMessage(m_stream, _msg.primary.message, _msg._typeOrSeverity, _msg.errorId, m_colored, m_withErrorIds);
186198
printSourceLocation(_msg.primary);
187199

188200
for (auto const& secondary: _msg.secondary)

liblangutil/SourceReferenceFormatter.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,27 @@ class SourceReferenceFormatter
118118

119119
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
120120

121+
static void printPrimaryMessage(
122+
std::ostream& _stream,
123+
std::string _message,
124+
std::variant<Error::Type, Error::Severity> _typeOrSeverity,
125+
std::optional<ErrorId> _errorId = std::nullopt,
126+
bool _colored = false,
127+
bool _withErrorIds = false
128+
);
129+
121130
private:
122131
util::AnsiColorized normalColored() const;
123132
util::AnsiColorized frameColored() const;
124-
util::AnsiColorized errorColored(langutil::Error::Severity _severity) const;
125-
util::AnsiColorized messageColored() const;
133+
util::AnsiColorized errorColored(Error::Severity _severity) const { return errorColored(m_stream, m_colored, _severity); }
134+
util::AnsiColorized messageColored() const { return messageColored(m_stream, m_colored); }
126135
util::AnsiColorized secondaryColored() const;
127136
util::AnsiColorized highlightColored() const;
128137
util::AnsiColorized diagColored() const;
129138

139+
static util::AnsiColorized errorColored(std::ostream& _stream, bool _colored, langutil::Error::Severity _severity);
140+
static util::AnsiColorized messageColored(std::ostream& _stream, bool _colored);
141+
130142
private:
131143
std::ostream& m_stream;
132144
CharStreamProvider const& m_charStreamProvider;

0 commit comments

Comments
 (0)