Skip to content

[Fix #429] Last occurrence sometimes not replaced for move-to-let #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bugs fixed

* [#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`.
* [#423](https://github.com/clojure-emacs/clojure-mode/issues/423): Make `clojure-match-next-def` more robust against zero-arity def-like forms.

### New features
Expand Down
18 changes: 10 additions & 8 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2213,24 +2213,26 @@ Assume that point is in the binding form of a let."
"[[:space:]\n\r]+")
"\\([^[:word:]^-]\\)"))

(defun clojure--replace-sexp-with-binding (bound-name init-expr end)
(defun clojure--replace-sexp-with-binding (bound-name init-expr)
(save-excursion
(while (re-search-forward (clojure--sexp-regexp init-expr) end t)
(while (re-search-forward
(clojure--sexp-regexp init-expr)
(clojure--point-after 'clojure--goto-let 'forward-sexp)
t)
(replace-match (concat "\\1" bound-name "\\2")))))

(defun clojure--replace-sexps-with-bindings (bindings end)
(defun clojure--replace-sexps-with-bindings (bindings)
"Replace bindings with their respective bound names in the let form.
BINDINGS is the list of bound names and init expressions, END denotes the end of the let expression."
BINDINGS is the list of bound names and init expressions."
(let ((bound-name (pop bindings))
(init-expr (pop bindings)))
(when bound-name
(clojure--replace-sexp-with-binding bound-name init-expr end)
(clojure--replace-sexps-with-bindings bindings end))))
(clojure--replace-sexp-with-binding bound-name init-expr)
(clojure--replace-sexps-with-bindings bindings))))

(defun clojure--replace-sexps-with-bindings-and-indent ()
(clojure--replace-sexps-with-bindings
(clojure--read-let-bindings)
(clojure--point-after 'clojure--goto-let 'forward-sexp))
(clojure--read-let-bindings))
(clojure-indent-region
(clojure--point-after 'clojure--goto-let)
(clojure--point-after 'clojure--goto-let 'forward-sexp)))
Expand Down
15 changes: 15 additions & 0 deletions test/clojure-mode-refactor-let-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@
(search-backward "(or ")
(clojure--move-to-let-internal "status"))

(def-refactor-test test-move-to-let-name-longer-than-expression
"(defn handle-request
(let []
(println \"body: \" body \", params: \" \", status: \" 5)
{:body body
:status 5}))"
"(defn handle-request
(let [status 5]
(println \"body: \" body \", params: \" \", status: \" status)
{:body body
:status status}))"
(search-backward "5")
(search-backward "5")
(clojure--move-to-let-internal "status"))

;; clojure-emacs/clj-refactor.el#41
(def-refactor-test test-move-to-let-nested-scope
"(defn foo []
Expand Down