From 95245726e107362fa82542164616d1d420fc52d6 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Fri, 7 Jul 2023 13:40:12 -0500 Subject: [PATCH] switch more "match" to "case" --- src/fennel-ls/dispatch.fnl | 8 ++++---- src/fennel-ls/json-rpc.fnl | 5 ++--- src/fennel-ls/message.fnl | 6 +++--- src/fennel-ls/searcher.fnl | 4 ++-- src/fennel-ls/utils-utf16-surrogate-pairs.fnl | 12 +----------- src/fennel-ls/utils.fnl | 10 ++++------ test/string-processing-test.fnl | 7 ------- 7 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/fennel-ls/dispatch.fnl b/src/fennel-ls/dispatch.fnl index aeae09d..49d43c1 100644 --- a/src/fennel-ls/dispatch.fnl +++ b/src/fennel-ls/dispatch.fnl @@ -13,9 +13,9 @@ In general, this involves: (λ handle-request [self send id method ?params] ;; Call the appropriate request handler. ;; The return value of the request is sent back to the server. - (match (. handlers.requests method) + (case (. handlers.requests method) callback - (match (callback self send ?params) + (case (callback self send ?params) (nil err) (send (message.create-error :InternalError err id)) ?response (send (message.create-response id ?response))) nil @@ -35,7 +35,7 @@ In general, this involves: (λ handle-notification [self send method ?params] ;; Call the appropriate notification handler. - (match (. handlers.notifications method) + (case (. handlers.notifications method) callback (callback self send ?params))) ;; Silent error for unknown notifications @@ -48,7 +48,7 @@ Takes: * `self`, which is the state of the server, * `send`, which is a callback for sending responses, and * `msg`, which is the message to receive." - (match (values msg (type msg)) + (case (values msg (type msg)) {:jsonrpc "2.0" : id : method :params ?params} (handle-request self send id method ?params) {:jsonrpc "2.0" : method :params ?params} diff --git a/src/fennel-ls/json-rpc.fnl b/src/fennel-ls/json-rpc.fnl index 5652e5c..63ef5a8 100644 --- a/src/fennel-ls/json-rpc.fnl +++ b/src/fennel-ls/json-rpc.fnl @@ -16,7 +16,7 @@ Luckily, I'm testing with Neovim, so I can pretend these problems don't exist fo (λ read-header [in ?header] "Reads the header of a JSON-RPC message" (let [header (or ?header {})] - (match (in:read) + (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 @@ -33,7 +33,7 @@ If there aren't enough bytes, return nil" (local buffer (or ?buffer [])) (if (<= len 0) (table.concat buffer) - (match (in:read len) + (case (in:read len) content (read-n in (- len (length content)) @@ -52,7 +52,6 @@ Returns a table with the message if it succeeded, or a string with the parse err (pcall decode))] result)) - (λ write [out msg] "Serializes and writes a JSON-RPC message to the given output stream" (let [content (encode msg) diff --git a/src/fennel-ls/message.fnl b/src/fennel-ls/message.fnl index 2944d0c..309d9b1 100644 --- a/src/fennel-ls/message.fnl +++ b/src/fennel-ls/message.fnl @@ -57,8 +57,8 @@ to look to fix this in the future." :end {:line el :character ec}}) (λ ast->range [?ast file] - (match (values (utils.get-ast-info ?ast :bytestart) - (utils.get-ast-info ?ast :byteend)) + (case (values (utils.get-ast-info ?ast :bytestart) + (utils.get-ast-info ?ast :byteend)) (i j) (let [(start-line start-col) (utils.byte->pos file.text i) (end-line end-col) (utils.byte->pos file.text (+ j 1))] @@ -66,7 +66,7 @@ to look to fix this in the future." (λ range-and-uri [?ast {: uri &as file}] "if possible, returns the location of a symbol" - (match (ast->range ?ast file) + (case (ast->range ?ast file) range {: range : uri})) (λ log [msg] diff --git a/src/fennel-ls/searcher.fnl b/src/fennel-ls/searcher.fnl index f939962..8dd9992 100644 --- a/src/fennel-ls/searcher.fnl +++ b/src/fennel-ls/searcher.fnl @@ -36,8 +36,8 @@ I suspect this file may be gone after a bit of refactoring." (table.concat result ";"))) (λ lookup [{:configuration {: fennel-path} : root-uri} mod] - (match (or ;; TODO support lua ;; (fennel.searchModule mod (add-workspaces-to-path luapath [root-uri])) - (fennel.searchModule mod (add-workspaces-to-path fennel-path [root-uri]))) + (case (or ;; TODO support lua ;; (fennel.searchModule mod (add-workspaces-to-path luapath [root-uri])) + (fennel.searchModule mod (add-workspaces-to-path fennel-path [root-uri]))) modname (utils.path->uri modname) nil nil)) diff --git a/src/fennel-ls/utils-utf16-surrogate-pairs.fnl b/src/fennel-ls/utils-utf16-surrogate-pairs.fnl index 7f36ce4..c4f1022 100644 --- a/src/fennel-ls/utils-utf16-surrogate-pairs.fnl +++ b/src/fennel-ls/utils-utf16-surrogate-pairs.fnl @@ -22,19 +22,9 @@ (set o16 (+ o16 a16)))) (if (= o8 unit8) o16 - (error :utf8-error))) + (error :utf8-error)))) - (let [byte (or ?byte (length str)) - substr (str:sub 1 byte)] - (accumulate - [total (accumulate - [total byte - _ (substr:gmatch "[\192-\223]")] - (- total 1)) - _ (substr:gmatch "[\224-\247]")] - (- total 2)))) - (fn unit16->byte [str unit16] "convert from utf16 garbage to normal units" (var o8 0) diff --git a/src/fennel-ls/utils.fnl b/src/fennel-ls/utils.fnl index e9617cb..2b35700 100644 --- a/src/fennel-ls/utils.fnl +++ b/src/fennel-ls/utils.fnl @@ -20,7 +20,7 @@ These functions are all pure functions, which makes me happy." (λ next-line [str ?from] "Find the start of the next line from a given byte offset, or from the start of the string." (let [from (or ?from 1)] - (match (str:find "[\r\n]" from) + (case (str:find "[\r\n]" from) i (+ i (length (str:match "\r?\n?" i))) nil nil))) @@ -59,14 +59,12 @@ These functions are all pure functions, which makes me happy." (accumulate [contents initial-text _ change (ipairs contentChanges)] - (match change + (case change ;; Handle a change {:range {: start : end} : text} (replace contents - start.line - start.character - end.line - end.character + start.line start.character + end.line end.character text) ;; A replacment of the entire body {: text} diff --git a/test/string-processing-test.fnl b/test/string-processing-test.fnl index 6e1a30c..32be186 100644 --- a/test/string-processing-test.fnl +++ b/test/string-processing-test.fnl @@ -11,13 +11,6 @@ ;; test for multiline edits ;; test for unicode utf8 utf16 nightmare - ;; (it "can handle unicode" - ;; (utils.apply-changes "" - ;; [{:range (range 0 0 0 0) :text "どれみふぁそらてぃど") - ;; (document.replace my-document 0 1 0 3 "😀") - ;; (document.replace my-document 0 11 0 11 "end") - ;; (is-matching my-document {:text "ど😀ふぁそらてぃどend"}))) - (describe "apply-changes" (fn range [start-line start-col end-line end-col]