From a460b3b28a839ccbd6d05a46675a6f206f7080f6 Mon Sep 17 00:00:00 2001 From: Sam Vervaeck Date: Wed, 31 May 2023 15:05:47 +0200 Subject: [PATCH] Fix some bugs in print logic of CST nodes --- include/bolt/CST.hpp | 3 ++- include/bolt/DiagnosticEngine.hpp | 2 +- src/CST.cc | 15 ++++++++++--- src/Diagnostics.cc | 35 +++++++++++++++++-------------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/include/bolt/CST.hpp b/include/bolt/CST.hpp index a41d0a5e9..1c70ce2f1 100644 --- a/include/bolt/CST.hpp +++ b/include/bolt/CST.hpp @@ -77,7 +77,8 @@ namespace bolt { size_t getLine(size_t Offset) const; size_t getColumn(size_t Offset) const; - size_t getStartOffset(size_t Line) const; + size_t getStartOffsetOfLine(size_t Line) const; + size_t getEndOffsetOfLine(size_t Line) const; size_t getLineCount() const; diff --git a/include/bolt/DiagnosticEngine.hpp b/include/bolt/DiagnosticEngine.hpp index 4c4e67834..449f1ca17 100644 --- a/include/bolt/DiagnosticEngine.hpp +++ b/include/bolt/DiagnosticEngine.hpp @@ -174,7 +174,7 @@ namespace bolt { void writeGutter( std::size_t GutterWidth, - std::size_t Line + std::string Text ); void writeHighlight( diff --git a/src/CST.cc b/src/CST.cc index 8779dbb69..87e8950a5 100644 --- a/src/CST.cc +++ b/src/CST.cc @@ -19,13 +19,22 @@ namespace bolt { } size_t TextFile::getLineCount() const { - return LineOffsets.size(); + return LineOffsets.size()-1; } - size_t TextFile::getStartOffset(size_t Line) const { + size_t TextFile::getStartOffsetOfLine(size_t Line) const { + ZEN_ASSERT(Line-1 < LineOffsets.size()); return LineOffsets[Line-1]; } + size_t TextFile::getEndOffsetOfLine(size_t Line) const { + ZEN_ASSERT(Line <= LineOffsets.size()); + if (Line == LineOffsets.size()) { + return Text.size(); + } + return LineOffsets[Line]; + } + size_t TextFile::getLine(size_t Offset) const { ZEN_ASSERT(Offset < Text.size()); for (size_t I = 0; I < LineOffsets.size(); ++I) { @@ -38,7 +47,7 @@ namespace bolt { size_t TextFile::getColumn(size_t Offset) const { auto Line = getLine(Offset); - auto StartOffset = getStartOffset(Line); + auto StartOffset = getStartOffsetOfLine(Line); return Offset - StartOffset + 1 ; } diff --git a/src/Diagnostics.cc b/src/Diagnostics.cc index 34548f749..ae83291da 100644 --- a/src/Diagnostics.cc +++ b/src/Diagnostics.cc @@ -387,17 +387,17 @@ namespace bolt { void ConsoleDiagnostics::writeGutter( std::size_t GutterWidth, - std::size_t Line + std::string Text ) { - auto LineNumberDigitCount = countDigits(Line); - auto LeadingSpaces = GutterWidth - LineNumberDigitCount; + ZEN_ASSERT(Text.size() <= GutterWidth); + auto LeadingSpaces = GutterWidth - Text.size(); Out << " "; setForegroundColor(Color::Black); setBackgroundColor(Color::White); for (std::size_t i = 0; i < LeadingSpaces; i++) { Out << ' '; } - Out << Line; + Out << Text; resetStyles(); Out << " "; } @@ -443,13 +443,14 @@ namespace bolt { Color HighlightColor ) { + auto LineCount = File.getLineCount(); auto Text = File.getText(); auto StartPos = ToPrint.Start; auto EndPos = ToPrint.End; - auto StartLine = StartPos.Line-1 > ExcerptLinesPre ? StartPos.Line - ExcerptLinesPost : 1; - auto StartOffset = File.getStartOffset(StartLine); - auto EndLine = std::min(File.getLineCount(), EndPos.Line + ExcerptLinesPost); - auto EndOffset = File.getStartOffset(EndLine+1); + auto StartLine = StartPos.Line-1 > ExcerptLinesPre ? StartPos.Line - ExcerptLinesPre : 1; + auto StartOffset = File.getStartOffsetOfLine(StartLine); + auto EndLine = std::min(LineCount, EndPos.Line + ExcerptLinesPost); + auto EndOffset = File.getEndOffsetOfLine(EndLine); auto GutterWidth = std::max(2, countDigits(EndLine+1)); auto HighlightStart = ToHighlight.Start; auto HighlightEnd = ToHighlight.End; @@ -457,19 +458,21 @@ namespace bolt { std::size_t CurrColumn = 1; std::size_t CurrLine = StartLine; - writeGutter(GutterWidth, CurrLine); - for (std::size_t i = StartOffset; i < EndOffset; i++) { - auto C = Text[i]; - Out << C; + bool AtBlankLine = true; + for (std::size_t I = StartOffset; I < EndOffset; I++) { + auto C = Text[I]; + if (AtBlankLine) { + writeGutter(GutterWidth, std::to_string(CurrLine)); + } if (C == '\n') { + Out << C; writeHighlight(GutterWidth, HighlightRange, HighlightColor, CurrLine, CurrColumn); - if (CurrLine == EndLine && C == '\n') { - break; - } CurrLine++; - writeGutter(GutterWidth, CurrLine); CurrColumn = 1; + AtBlankLine = true; } else { + AtBlankLine = false; + Out << C; CurrColumn++; } }