Skip to content

Commit 51ee2c2

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 21160d3 commit 51ee2c2

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bugs fixed
66

7+
* [#429](https://github.com/clojure-emacs/clojure-mode/issues/429): Fix a bug causing last occurrence of expression sometimes is not replaced when using `move-to-let`.
78
* [#423](https://github.com/clojure-emacs/clojure-mode/issues/423): Make `clojure-match-next-def` more robust against zero-arity def-like forms.
89

910
### New features

clojure-mode.el

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

2216-
(defun clojure--replace-sexp-with-binding (bound-name init-expr end)
2216+
(defun clojure--replace-sexp-with-binding (bound-name init-expr)
22172217
(save-excursion
2218-
(while (re-search-forward (clojure--sexp-regexp init-expr) end t)
2218+
(while (re-search-forward
2219+
(clojure--sexp-regexp init-expr)
2220+
(clojure--point-after 'clojure--goto-let 'forward-sexp)
2221+
t)
22192222
(replace-match (concat "\\1" bound-name "\\2")))))
22202223

2221-
(defun clojure--replace-sexps-with-bindings (bindings end)
2224+
(defun clojure--replace-sexps-with-bindings (bindings)
22222225
"Replace bindings with their respective bound names in the let form.
2223-
BINDINGS is the list of bound names and init expressions, END denotes the end of the let expression."
2226+
BINDINGS is the list of bound names and init expressions."
22242227
(let ((bound-name (pop bindings))
22252228
(init-expr (pop bindings)))
22262229
(when bound-name
2227-
(clojure--replace-sexp-with-binding bound-name init-expr end)
2228-
(clojure--replace-sexps-with-bindings bindings end))))
2230+
(clojure--replace-sexp-with-binding bound-name init-expr)
2231+
(clojure--replace-sexps-with-bindings bindings))))
22292232

22302233
(defun clojure--replace-sexps-with-bindings-and-indent ()
22312234
(clojure--replace-sexps-with-bindings
2232-
(clojure--read-let-bindings)
2233-
(clojure--point-after 'clojure--goto-let 'forward-sexp))
2235+
(clojure--read-let-bindings))
22342236
(clojure-indent-region
22352237
(clojure--point-after 'clojure--goto-let)
22362238
(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)