From 36d1bbfd034d1f72a3dc54e78f4dbd3c0ad7a96e Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Wed, 10 Apr 2024 00:40:07 +0800 Subject: [PATCH] nixd: redirect doc highlight error from UI into log --- nixd/lib/Controller/DocumentHighlight.cpp | 10 ++- .../nixd/test/document-highlight-fail.md | 66 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 nixd/tools/nixd/test/document-highlight-fail.md diff --git a/nixd/lib/Controller/DocumentHighlight.cpp b/nixd/lib/Controller/DocumentHighlight.cpp index e143efb2a..1dd5658e3 100644 --- a/nixd/lib/Controller/DocumentHighlight.cpp +++ b/nixd/lib/Controller/DocumentHighlight.cpp @@ -9,6 +9,7 @@ #include "nixd/Controller/Controller.h" #include +#include #include #include #include @@ -58,7 +59,14 @@ void Controller::onDocumentHighlight( std::vector Highlights; if (auto Err = highlight(*Desc, *TU->parentMap(), *TU->variableLookup(), URI, Highlights)) { - Reply(std::move(Err)); + // FIXME: Empty response if there are no def-use chain found. + // For document highlights, the specification explicitly specified LSP + // should do "fuzzy" things. + + // Empty response on error, don't reply all errors because this method + // is very frequently called. + Reply(std::vector{}); + lspserver::elog("textDocument/documentHighlight failed: {0}", Err); return; } Reply(std::move(Highlights)); diff --git a/nixd/tools/nixd/test/document-highlight-fail.md b/nixd/tools/nixd/test/document-highlight-fail.md new file mode 100644 index 000000000..f8d243777 --- /dev/null +++ b/nixd/tools/nixd/test/document-highlight-fail.md @@ -0,0 +1,66 @@ +# RUN: nixd --lit-test < %s | FileCheck %s + +<-- initialize(0) + +```json +{ + "jsonrpc":"2.0", + "id":0, + "method":"initialize", + "params":{ + "processId":123, + "rootPath":"", + "capabilities":{ + }, + "trace":"off" + } +} +``` + + +<-- textDocument/didOpen + +```json +{ + "jsonrpc":"2.0", + "method":"textDocument/didOpen", + "params":{ + "textDocument":{ + "uri":"file:///basic.nix", + "languageId":"nix", + "version":1, + "text":"let x = 1; y = 2; in x + y" + } + } +} +``` + +<-- textDocument/documentHighlight(2) + + +```json +{ + "jsonrpc":"2.0", + "id":2, + "method":"textDocument/documentHighlight", + "params":{ + "textDocument":{ + "uri":"file:///basic.nix" + }, + "position":{ + "line": 0, + "character":1 + } + } +} +``` + +``` + CHECK: "id": 2, +CHECK-NEXT: "jsonrpc": "2.0", +CHECK-NEXT: "result": [] +``` + +```json +{"jsonrpc":"2.0","method":"exit"} +```