Prevent coloring irrelevant whitespaces

This commit is contained in:
a3d4 2020-04-24 00:51:15 +02:00
parent 8717c073a6
commit 523460da07

View File

@ -21,7 +21,6 @@
#include <liblangutil/SourceReferenceFormatterHuman.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/Exceptions.h>
#include <cmath>
#include <iomanip>
using namespace std;
@ -72,8 +71,8 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
if (_ref.position.line < 0)
{
frameColored() << "--> ";
m_stream << _ref.sourceName << "\n";
frameColored() << "-->";
m_stream << ' ' << _ref.sourceName << '\n';
return; // No line available, nothing else to print
}
@ -81,48 +80,55 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
string leftpad = string(line.size(), ' ');
// line 0: source name
frameColored() << leftpad << "--> ";
m_stream << _ref.sourceName << ":" << line << ":" << (_ref.position.column + 1) << ":" << '\n';
m_stream << leftpad;
frameColored() << "-->";
m_stream << ' ' << _ref.sourceName << ':' << line << ':' << (_ref.position.column + 1) << ":\n";
if (!_ref.multiline)
{
int const locationLength = _ref.endColumn - _ref.startColumn;
// line 1:
m_stream << leftpad;
frameColored() << " |" << '\n';
m_stream << leftpad << ' ';
frameColored() << '|';
m_stream << '\n';
// line 2:
frameColored() << line << " | ";
m_stream << _ref.text.substr(0, _ref.startColumn);
frameColored() << line << " |";
m_stream << ' ' << _ref.text.substr(0, _ref.startColumn);
highlightColored() << _ref.text.substr(_ref.startColumn, locationLength);
m_stream << _ref.text.substr(_ref.endColumn) << '\n';
// line 3:
m_stream << leftpad;
frameColored() << " | ";
m_stream << leftpad << ' ';
frameColored() << '|';
m_stream << ' ';
for_each(
_ref.text.cbegin(),
_ref.text.cbegin() + _ref.startColumn,
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
);
diagColored() << string(locationLength, '^') << '\n';
diagColored() << string(locationLength, '^');
m_stream << '\n';
}
else
{
// line 1:
m_stream << leftpad;
frameColored() << " |" << '\n';
m_stream << leftpad << ' ';
frameColored() << '|';
m_stream << '\n';
// line 2:
frameColored() << line << " | ";
m_stream << _ref.text.substr(0, _ref.startColumn);
frameColored() << line << " |";
m_stream << ' ' << _ref.text.substr(0, _ref.startColumn);
highlightColored() << _ref.text.substr(_ref.startColumn) << '\n';
// line 3:
frameColored() << leftpad << " | ";
m_stream << string(_ref.startColumn, ' ');
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).\n";
m_stream << leftpad << ' ';
frameColored() << '|';
m_stream << ' ' << string(_ref.startColumn, ' ');
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).";
m_stream << '\n';
}
}