From 6f9935a7817c0ebfac56048e46a265393ded8366 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Sun, 11 Feb 2024 23:45:08 -0600 Subject: [PATCH] Fix renames that cover method calls That was a tough one to debug! I almost thought it was a bug in fennel, but it, of course, turned out to be a fennel-ls problem. When you have a symbol like (foo:bar), fennel compiles it by "macroexpanding" it to (: foo :bar). When fennel macroexpands a macro, it tries to clean up the output by giving it a best-guess bytestart/byteend. Fennel-ls was picking up *both* the accurate `foo:bar` sym and the best-guess virtual `foo` sym as separate references to `foo`. When computing the renames, the best-guess values were getting used, which made the replacement range inaccurate. --- src/fennel-ls/compiler.fnl | 3 ++- src/fennel-ls/handlers.fnl | 3 ++- test/rename-test.fnl | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/fennel-ls/compiler.fnl b/src/fennel-ls/compiler.fnl index 652cb4e..0bf2550 100644 --- a/src/fennel-ls/compiler.fnl +++ b/src/fennel-ls/compiler.fnl @@ -317,8 +317,9 @@ are declared / referenced in which places." ast (icollect [ok ast parser &until (not ok)] ast)] (λ collect-everything [ast result] + (when (or (table? ast) (list? ast) (sym? ast)) + (tset result ast true)) (when (or (table? ast) (list? ast)) - (tset result ast true) (each [k v (iter ast)] (collect-everything k result) (collect-everything v result)))) diff --git a/src/fennel-ls/handlers.fnl b/src/fennel-ls/handlers.fnl index 46cdf3f..fe2ce49 100644 --- a/src/fennel-ls/handlers.fnl +++ b/src/fennel-ls/handlers.fnl @@ -207,7 +207,8 @@ 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}]] - (if (not (rawequal symbol definition.binding)) + (if (and (. file.lexical symbol) + (not (rawequal symbol definition.binding))) {:newText new-name :range (message.multisym->range self def-file symbol 1)}))] diff --git a/test/rename-test.fnl b/test/rename-test.fnl index fbae206..045d4d7 100644 --- a/test/rename-test.fnl +++ b/test/rename-test.fnl @@ -31,7 +31,9 @@ (check-rename "(let [old-name {:field 10}] old-name.field)" 0 9 :new-name "(let [new-name {:field 10}] new-name.field)") (check-rename "(let [old-name {:field 10}] old-name.field)" 0 30 :new-name - "(let [new-name {:field 10}] new-name.field)")) + "(let [new-name {:field 10}] new-name.field)") + (check-rename "(let [[old-name] [{:field 10}]] (old-name:field 10))" 0 7 :new-name + "(let [[new-name] [{:field 10}]] (new-name:field 10))")) (it "renames from destructure/args" (check-rename "(fn [{: x}] x)" 0 8 :foo "(fn [{: foo}] foo)")