Skip to content

Commit

Permalink
nixd-lint: prettier note
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 19, 2023
1 parent a5b0bd7 commit 6486399
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions nixd/tools/nixd-lint/nixd-lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ opt<std::string> Filename(Positional, desc("<input file>"), 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<nix::AbstractPos> BeginPos,
std::shared_ptr<nix::AbstractPos> 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(" ");
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -97,8 +98,8 @@ int main(int argc, char *argv[]) {
nixd::syntax::parse(Buffer, &Data);

for (const auto &Diag : Data.Diags) {
std::shared_ptr<nix::AbstractPos> BeginAPos = (*PTable)[Diag.Range.Begin];
std::shared_ptr<nix::AbstractPos> 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;
Expand All @@ -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<nix::AbstractPos>(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<nix::AbstractPos>(NotePos)->getCodeLines()) {
nix::printCodeLines(std::cout, "", *BeginAPos, *Lines);
std::shared_ptr<nix::AbstractPos>(NoteBegin)->getCodeLines()) {
std::cout << "\n";
printCodeLines(std::cout, "", NoteBegin, NoteEnd, *Lines);
std::cout << "\n";
}
}
}
Expand Down

0 comments on commit 6486399

Please sign in to comment.