From 648639928c194a7287dc227609c8a469c35041a6 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Tue, 19 Sep 2023 22:29:19 +0800 Subject: [PATCH] nixd-lint: prettier note --- nixd/tools/nixd-lint/nixd-lint.cpp | 52 +++++++++++++++++------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/nixd/tools/nixd-lint/nixd-lint.cpp b/nixd/tools/nixd-lint/nixd-lint.cpp index d3d91ca7a..392c2b598 100644 --- a/nixd/tools/nixd-lint/nixd-lint.cpp +++ b/nixd/tools/nixd-lint/nixd-lint.cpp @@ -24,24 +24,25 @@ opt Filename(Positional, desc(""), init("-"), const OptionCategory *Cat[] = {&Misc}; static void printCodeLines(std::ostream &Out, const std::string &Prefix, - const nix::AbstractPos &BeginPos, - const nix::AbstractPos &EndPos, + std::shared_ptr BeginPos, + std::shared_ptr EndPos, const nix::LinesOfCode &LOC) { using namespace nix; // previous line of code. if (LOC.prevLineOfCode.has_value()) { Out << std::endl - << fmt("%1% %|2$5d|| %3%", Prefix, (BeginPos.line - 1), + << fmt("%1% %|2$5d|| %3%", Prefix, (BeginPos->line - 1), *LOC.prevLineOfCode); } if (LOC.errLineOfCode.has_value()) { // line of code containing the error. Out << std::endl - << fmt("%1% %|2$5d|| %3%", Prefix, (BeginPos.line), *LOC.errLineOfCode); + << fmt("%1% %|2$5d|| %3%", Prefix, (BeginPos->line), + *LOC.errLineOfCode); // error arrows for the column range. - if (BeginPos.column > 0) { - auto Start = BeginPos.column; + if (BeginPos->column > 0) { + auto Start = BeginPos->column; std::string Spaces; for (auto I = 0; I < Start; ++I) { Spaces.append(" "); @@ -53,10 +54,10 @@ static void printCodeLines(std::ostream &Out, const std::string &Prefix, << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL, Prefix, Spaces, arrows); - if (BeginPos.line == EndPos.line) { + if (BeginPos->line == EndPos->line) { Out << ANSI_RED; - for (auto I = BeginPos.column + 1; I < EndPos.column; I++) { - Out << (I == EndPos.column - 1 ? "^" : "~"); + for (auto I = BeginPos->column + 1; I < EndPos->column; I++) { + Out << (I == EndPos->column - 1 ? "^" : "~"); } Out << ANSI_NORMAL; } @@ -66,7 +67,7 @@ static void printCodeLines(std::ostream &Out, const std::string &Prefix, // next line of code. if (LOC.nextLineOfCode.has_value()) { Out << std::endl - << fmt("%1% %|2$5d|| %3%", Prefix, (BeginPos.line + 1), + << fmt("%1% %|2$5d|| %3%", Prefix, (BeginPos->line + 1), *LOC.nextLineOfCode); } } @@ -97,8 +98,8 @@ int main(int argc, char *argv[]) { nixd::syntax::parse(Buffer, &Data); for (const auto &Diag : Data.Diags) { - std::shared_ptr BeginAPos = (*PTable)[Diag.Range.Begin]; - std::shared_ptr EndAPos = (*PTable)[Diag.Range.End]; + auto BeginPos = (*PTable)[Diag.Range.Begin]; + auto EndPos = (*PTable)[Diag.Range.End]; switch (Diag.Kind) { case nixd::syntax::Diagnostic::Warning: std::cout << ANSI_WARNING "warning: " ANSI_NORMAL; @@ -108,27 +109,34 @@ int main(int argc, char *argv[]) { break; } std::cout << Diag.Msg << "\n"; - if (BeginAPos) { + if (BeginPos) { std::cout << "\n" - << ANSI_BLUE << "at " ANSI_WARNING - << (*PTable)[Diag.Range.Begin] << ANSI_NORMAL << ":"; - if (auto Lines = BeginAPos->getCodeLines()) { + << ANSI_BLUE << "at " ANSI_WARNING << BeginPos << ANSI_NORMAL + << ":"; + if (auto Lines = + std::shared_ptr(BeginPos)->getCodeLines()) { std::cout << "\n"; - printCodeLines(std::cout, "", *BeginAPos, *EndAPos, *Lines); + printCodeLines(std::cout, "", BeginPos, EndPos, *Lines); std::cout << "\n"; } } for (const auto &Note : Diag.Notes) { - auto NotePos = (*PTable)[Note.Range.Begin]; + auto NoteBegin = (*PTable)[Note.Range.Begin]; + auto NoteEnd = (*PTable)[Note.Range.End]; + std::cout << "\n"; std::cout << ANSI_CYAN "note: " ANSI_NORMAL; - if (NotePos) { + std::cout << Note.Msg << "\n"; + + if (NoteBegin) { std::cout << "\n" - << ANSI_BLUE << "at " ANSI_WARNING << NotePos << ANSI_NORMAL + << ANSI_BLUE << "at " ANSI_WARNING << NoteBegin << ANSI_NORMAL << ":"; if (auto Lines = - std::shared_ptr(NotePos)->getCodeLines()) { - nix::printCodeLines(std::cout, "", *BeginAPos, *Lines); + std::shared_ptr(NoteBegin)->getCodeLines()) { + std::cout << "\n"; + printCodeLines(std::cout, "", NoteBegin, NoteEnd, *Lines); + std::cout << "\n"; } } }