From ea46f368bea4c749eb11399d8c6abf6305925bde Mon Sep 17 00:00:00 2001 From: XeroOl Date: Fri, 11 Oct 2024 22:01:12 -0500 Subject: [PATCH] json-rpc: handle when \r isn't present In windows, (in:read) will remove the \r, so the json rpc parsing code shouldn't assume that the carriage return is always present. --- src/fennel-ls/json-rpc.fnl | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/fennel-ls/json-rpc.fnl b/src/fennel-ls/json-rpc.fnl index 87bf42f..3536c27 100644 --- a/src/fennel-ls/json-rpc.fnl +++ b/src/fennel-ls/json-rpc.fnl @@ -14,15 +14,14 @@ Luckily, I'm testing with Neovim, so I can pretend these problems don't exist fo "Reads the header of a JSON-RPC message" (let [header (or ?header {})] (case (in:read) - "\r" header ;; hit an empty line, I'm done reading - nil nil ;; hit end of stream, return nil - ;; reading an actual line - header-line - (let [sep (string.find header-line ": ") - k (string.sub header-line 1 (- sep 1)) - v (string.sub header-line (+ sep 2) -2)] ;; trim off the \r - (tset header k v) - (read-header in header))))) + nil nil ;; I've hit end of stream, return nil instead of a header + line (case (line:match "^(.-)\r$") ;; strip trailing \r + "" header ;; base case. empty line marks end of header + line (let [(k v) (line:match "^(.-): (.-)$")] + (if (not (and k v)) + (error "fennel-ls encountered a malformed json-rpc header: \"" line "\"")) + (tset header k v) + (read-header in header)))))) (λ read-n [in len ?buffer] "read a string of exactly `len` characters from the `in` stream.