Skip to content

Commit 583e0e4

Browse files
[Fix #429] Last occurrence sometimes not replaced for move-to-let
In case * there are more than one occurrences of expression * and move to let is not initiated from the last occurrence * and actual bound name is longer than the expression being moved to let the last expression won't be replaced. Fix: end of `let` expression is not cached before calling `clojure--replace-sexps-with-binding`.
1 parent 4dcd7bc commit 583e0e4

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Bugs fixed
6+
7+
* [#429](https://github.com/clojure-emacs/clojure-mode/issues/429): Fix last occurrence of expression sometimes is not replaced when using `move-to-let`.
8+
59
### New features
610

711
* New interactive command `clojure-cycle-when`.

clojure-mode.el

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,24 +2216,26 @@ Assume that point is in the binding form of a let."
22162216
"[[:space:]\n\r]+")
22172217
"\\([^[:word:]^-]\\)"))
22182218

2219-
(defun clojure--replace-sexp-with-binding (bound-name init-expr end)
2219+
(defun clojure--replace-sexp-with-binding (bound-name init-expr)
22202220
(save-excursion
2221-
(while (re-search-forward (clojure--sexp-regexp init-expr) end t)
2221+
(while (re-search-forward
2222+
(clojure--sexp-regexp init-expr)
2223+
(clojure--point-after 'clojure--goto-let 'forward-sexp)
2224+
t)
22222225
(replace-match (concat "\\1" bound-name "\\2")))))
22232226

2224-
(defun clojure--replace-sexps-with-bindings (bindings end)
2227+
(defun clojure--replace-sexps-with-bindings (bindings)
22252228
"Replace bindings with their respective bound names in the let form.
2226-
BINDINGS is the list of bound names and init expressions, END denotes the end of the let expression."
2229+
BINDINGS is the list of bound names and init expressions."
22272230
(let ((bound-name (pop bindings))
22282231
(init-expr (pop bindings)))
22292232
(when bound-name
2230-
(clojure--replace-sexp-with-binding bound-name init-expr end)
2231-
(clojure--replace-sexps-with-bindings bindings end))))
2233+
(clojure--replace-sexp-with-binding bound-name init-expr)
2234+
(clojure--replace-sexps-with-bindings bindings))))
22322235

22332236
(defun clojure--replace-sexps-with-bindings-and-indent ()
22342237
(clojure--replace-sexps-with-bindings
2235-
(clojure--read-let-bindings)
2236-
(clojure--point-after 'clojure--goto-let 'forward-sexp))
2238+
(clojure--read-let-bindings))
22372239
(clojure-indent-region
22382240
(clojure--point-after 'clojure--goto-let)
22392241
(clojure--point-after 'clojure--goto-let 'forward-sexp)))

test/clojure-mode-refactor-let-test.el

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@
164164
(search-backward "(or ")
165165
(clojure--move-to-let-internal "status"))
166166

167+
(def-refactor-test test-move-to-let-name-longer-than-expression
168+
"(defn handle-request
169+
(let []
170+
(println \"body: \" body \", params: \" \", status: \" 5)
171+
{:body body
172+
:status 5}))"
173+
"(defn handle-request
174+
(let [status 5]
175+
(println \"body: \" body \", params: \" \", status: \" status)
176+
{:body body
177+
:status status}))"
178+
(search-backward "5")
179+
(search-backward "5")
180+
(clojure--move-to-let-internal "status"))
181+
167182
;; clojure-emacs/clj-refactor.el#41
168183
(def-refactor-test test-move-to-let-nested-scope
169184
"(defn foo []

0 commit comments

Comments
 (0)