Skip to content

Commit

Permalink
nixd: emove nix ANSI color but not add unit test (#14)
Browse files Browse the repository at this point in the history
`ASSERT_TRUE(SRO.contains(R"("whatEverUndefined)"));` can not pass not
because ansi but the redundant `"`, so I remove the comment.

the reason why I not add unit test is that I can not find ANSI code in
the output of `SRO.str()` and `Diagnostics[0].message`

Fixes: #10
  • Loading branch information
euphgh committed May 16, 2023
1 parent 3287576 commit 8dcde63
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 28 additions & 1 deletion lib/nixd/src/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

#include "lspserver/Protocol.h"

#include <nix/ansicolor.hh>

#define FOREACH_ANSI_COLOR(FUNC) \
FUNC(ANSI_NORMAL) \
FUNC(ANSI_BOLD) \
FUNC(ANSI_FAINT) \
FUNC(ANSI_ITALIC) \
FUNC(ANSI_RED) \
FUNC(ANSI_GREEN) \
FUNC(ANSI_WARNING) \
FUNC(ANSI_BLUE) \
FUNC(ANSI_MAGENTA) \
FUNC(ANSI_CYAN)

static std::string stripANSI(std::string Msg){
#define REMOVE_ANSI_STR_FUNC(ANSI_STR) \
do { \
auto Pos = Msg.find(ANSI_STR); \
if (Pos == std::string::npos) \
break; \
Msg.erase(Pos, std::strlen(ANSI_STR)); \
} while(true);
FOREACH_ANSI_COLOR(REMOVE_ANSI_STR_FUNC)
#undef REMOVE_ANSI_STR_FUNC
return Msg;
}

namespace nixd {

std::vector<lspserver::Diagnostic> mkDiagnostics(const nix::Error &PE) {
Expand All @@ -11,7 +38,7 @@ std::vector<lspserver::Diagnostic> mkDiagnostics(const nix::Error &PE) {
ErrPos ? translatePosition(*ErrPos) : lspserver::Position{};
Ret.push_back(lspserver::Diagnostic{.range = {.start = ErrLoc, .end = ErrLoc},
.severity = /* Error */ 1,
.message = PE.info().msg.str()});
.message = stripANSI(PE.info().msg.str())});
return Ret;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/nixd/test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ TEST(Server, DiagnosticSemaUndefineVariable) {
llvm::StringRef SRO = S.getBuffer();
std::cout << SRO.str() << "\n";
ASSERT_TRUE(SRO.contains("undefined variable"));
// TODO: Strip ANSI colors here, or report upstream
// ASSERT_TRUE(SRO.contains(R"("whatEverUndefined)"));
ASSERT_TRUE(SRO.contains("whatEverUndefined"));
ASSERT_TRUE(SRO.starts_with("Content-Length:"));
}

Expand Down

0 comments on commit 8dcde63

Please sign in to comment.