From 223976194ecff538ccb2297fbf853007cf0b12ec Mon Sep 17 00:00:00 2001 From: XeroOl Date: Sat, 16 Sep 2023 13:02:14 -0500 Subject: [PATCH] textDocument/rename Filter out when symbol references itself When a symbol is recorded to be its own reference, fennel-ls accidentally counts the reference twice. That's fine, but we don't want to replace the same words multiple times --- src/fennel-ls/handlers.fnl | 11 +++++++---- src/fennel-ls/utils.fnl | 12 ++++++++++++ test/rename-test.fnl | 6 ++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/fennel-ls/handlers.fnl b/src/fennel-ls/handlers.fnl index 1e55e33..065f68a 100644 --- a/src/fennel-ls/handlers.fnl +++ b/src/fennel-ls/handlers.fnl @@ -191,11 +191,14 @@ Every time the client sends a message, it gets handled by a function in the corr (let [usages (icollect [_ symbol (ipairs definition.referenced-by) &into [{:range (message.multisym->range self def-file definition.binding 1) :newText new-name}]] - {:newText new-name - :range (message.multisym->range self def-file symbol 1)})] + (if (not (rawequal symbol definition.binding)) + {:newText new-name + :range (message.multisym->range self def-file symbol 1)}))] + ;; NOTE: I don't care about encoding here because we just need the relative positions - (table.sort usages #(> (utils.position->byte def-file.text $1.range.start :utf-8) - (utils.position->byte def-file.text $2.range.start :utf-8))) + (table.sort usages + #(> (utils.position->byte def-file.text $1.range.start :utf-8) + (utils.position->byte def-file.text $2.range.start :utf-8))) {:changes {def-file.uri usages}}) (catch _ nil)))) diff --git a/src/fennel-ls/utils.fnl b/src/fennel-ls/utils.fnl index 754512a..d7feee8 100644 --- a/src/fennel-ls/utils.fnl +++ b/src/fennel-ls/utils.fnl @@ -157,6 +157,17 @@ WARNING: this is only used in the test code, not in the real language server" (λ type= [val typ] (= (type val) typ)) +(λ uniq-by [list eq?] + "I know the big O of this is bad, but we'll come back to it if it's a performance problem" + (let [result []] + (each [_ new-item (ipairs list)] + (if (not (accumulate [any? nil + _ seen-item (ipairs result) + &until any?] + (eq? seen-item new-item))) + (table.insert result new-item))) + result)) + {: uri->path : path->uri : pos->position @@ -166,4 +177,5 @@ WARNING: this is only used in the test code, not in the real language server" : apply-edits : multi-sym-split : get-ast-info + : uniq-by : type=} diff --git a/test/rename-test.fnl b/test/rename-test.fnl index f7ae131..225fac6 100644 --- a/test/rename-test.fnl +++ b/test/rename-test.fnl @@ -35,6 +35,8 @@ (it "renames from destructure/args" (check-rename "(fn [{: x}] x)" 0 8 :foo "(fn [{: foo}] foo)") - (check-rename "(fn [{:x x}] x)" 0 9 :foo "(fn [{:x foo}] foo)"))) - + (check-rename "(fn [{:x x}] x)" 0 9 :foo "(fn [{:x foo}] foo)")) + (it "renames a sym inside of lambda" + (check-rename "(λ [foo] (print foo))" 0 6 :something + "(λ [something] (print something))")))