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