Skip to content

Commit aa49bbd

Browse files
committed
Preview cljr-inline-symbol too
Route cljr-inline-symbol through cljr--run-previewable-refactoring, like rename and change-signature: gather the edits, show a diff, and write only on confirm. Its two save-buffer calls (after the occurrence loop and in cljr--delete-definition) become cljr--save-buffer so they defer during a preview session, and the success message is guarded on the edits actually being applied. Validated live against a real refactor-nrepl connection: inlining a def (both usages replaced, definition removed) previewed and applied on confirm, left untouched on decline.
1 parent d402285 commit aa49bbd

2 files changed

Lines changed: 29 additions & 27 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
- Preview project-wide refactorings before they touch disk. `cljr-rename-symbol` and `cljr-change-function-signature` now gather all their edits first and show them as a diff, and nothing is written until you confirm. Controlled by the new `cljr-preview-refactorings` (default on); set it to nil for the old apply-immediately behavior.
5+
- Preview project-wide refactorings before they touch disk. `cljr-rename-symbol`, `cljr-change-function-signature` and `cljr-inline-symbol` now gather all their edits first and show them as a diff, and nothing is written until you confirm. Controlled by the new `cljr-preview-refactorings` (default on); set it to nil for the old apply-immediately behavior.
66
- New `cljr-undo-last-refactoring` reverts every file changed by the last applied rename or change-signature in one step (bound to `ur`, and in `clj-refactor-menu`).
77

88
- `cljr-change-function-signature` can now add and remove parameters, not just reorder and rename them. In the edit buffer, `a` adds a parameter (you're prompted for its name and a placeholder to insert at call sites) and `k`/`d` marks one for removal. Added parameters get their placeholder inserted at every call site; removals are never auto-deleted (an argument might have side effects) - the affected call sites are routed to the manual-intervention buffer for review.

‎clj-refactor.el‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,7 +3679,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-add-stubs"
36793679
(cljr--indent-defun))
36803680
(when (looking-at-p "\s*\n")
36813681
(cljr--just-one-blank-line))
3682-
(save-buffer))))
3682+
(cljr--save-buffer))))
36833683

36843684
(defun cljr--sort-occurrences (occurrences)
36853685
"Sort the OCCURRENCES so the last ones in the file comes first."
@@ -3718,29 +3718,31 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-add-stubs"
37183718
(insert def)))
37193719

37203720
(defun cljr--inline-symbol (definition occurrences)
3721-
(cljr--with-opened-buffers
3722-
(let* ((def (gethash :definition definition))
3723-
(inline-fn-p (string-prefix-p "(fn" def)))
3724-
(dolist (symbol-meta (cljr--sort-occurrences occurrences))
3725-
(let* ((file (cljr--get-valid-filename symbol-meta))
3726-
(line-beg (gethash :line-beg symbol-meta))
3727-
(col-beg (gethash :col-beg symbol-meta)))
3728-
(with-current-buffer (cljr--find-file-noselect file)
3729-
(goto-char (point-min))
3730-
(forward-line (1- line-beg))
3731-
(forward-char (1- col-beg))
3732-
(let* ((call-site-p (and inline-fn-p
3733-
(looking-back "(\s*" (line-beginning-position))))
3734-
(sexp (if call-site-p
3735-
(prog1 (cljr--extract-sexp-as-list)
3736-
(paredit-backward-up)
3737-
(clojure-delete-and-extract-sexp))
3738-
(clojure-delete-and-extract-sexp))))
3739-
(if call-site-p
3740-
(cljr--inline-fn-at-call-site def sexp)
3741-
(insert def)))))))
3742-
(save-buffer)
3743-
(cljr--delete-definition definition)))
3721+
(cljr--run-previewable-refactoring
3722+
"Inline symbol"
3723+
(lambda ()
3724+
(let* ((def (gethash :definition definition))
3725+
(inline-fn-p (string-prefix-p "(fn" def)))
3726+
(dolist (symbol-meta (cljr--sort-occurrences occurrences))
3727+
(let* ((file (cljr--get-valid-filename symbol-meta))
3728+
(line-beg (gethash :line-beg symbol-meta))
3729+
(col-beg (gethash :col-beg symbol-meta)))
3730+
(with-current-buffer (cljr--find-file-noselect file)
3731+
(goto-char (point-min))
3732+
(forward-line (1- line-beg))
3733+
(forward-char (1- col-beg))
3734+
(let* ((call-site-p (and inline-fn-p
3735+
(looking-back "(\s*" (line-beginning-position))))
3736+
(sexp (if call-site-p
3737+
(prog1 (cljr--extract-sexp-as-list)
3738+
(paredit-backward-up)
3739+
(clojure-delete-and-extract-sexp))
3740+
(clojure-delete-and-extract-sexp))))
3741+
(if call-site-p
3742+
(cljr--inline-fn-at-call-site def sexp)
3743+
(insert def))))))
3744+
(cljr--save-buffer)
3745+
(cljr--delete-definition definition)))))
37443746

37453747
(defun cljr--var-info (&optional symbol all)
37463748
"Like `cider-var-info' but also handles locally bound vars.
@@ -3794,8 +3796,8 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-inline-symbol"
37943796
extract-definition-request "definition")))
37953797
(definition (gethash :definition response))
37963798
(occurrences (gethash :occurrences response)))
3797-
(cljr--inline-symbol definition occurrences)
3798-
(unless *cljr--noninteractive* ; don't spam when called from `cljr-remove-let'
3799+
(when (and (cljr--inline-symbol definition occurrences)
3800+
(not *cljr--noninteractive*))
37993801
(if occurrences
38003802
(cljr--post-command-message "Inlined %s occurrence(s) of '%s'" (length occurrences) symbol)
38013803
(cljr--post-command-message "No occurrences of '%s' found. Deleted the definition." symbol)))))

0 commit comments

Comments
 (0)