Skip to content

Commit 0a25f2e

Browse files
[Fix #368] Fix inline symbol fails on inlining fn in def
It is not enough to check if occurrence looks like a call site the fact that the definition is some kind of fn needs to be checked too. Some minor refactorings: - add higher level `let` to extract def from defintion - rename some internal preds to comform with lispy naming conventions
1 parent 0fb72ef commit 0a25f2e

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

CHANGELOG.md

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

2323
### Bugs fixed
2424

25+
- [#368](https://github.com/clojure-emacs/clj-refactor.el/issues/368) Fix inline symbol fails on inlining composed function in a `def`
2526
- [#299](https://github.com/clojure-emacs/clj-refactor.el/issues/299) `ml` moves cursor
2627
- [#309](https://github.com/clojure-emacs/clj-refactor.el/issues/309) `am` creates alias for fully-qualified symbols.
2728
- [#313](https://github.com/clojure-emacs/clj-refactor.el/issues/313) teach `pf` about function literals using `%&`.

clj-refactor.el

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,24 +2989,26 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-add-stubs"
29892989
(insert def)))
29902990

29912991
(defun cljr--inline-symbol (definition occurrences)
2992-
(dolist (symbol-meta (cljr--sort-occurrences occurrences))
2993-
(let* ((file (cljr--get-valid-filename symbol-meta))
2994-
(line-beg (gethash :line-beg symbol-meta))
2995-
(col-beg (gethash :col-beg symbol-meta))
2996-
(def (gethash :definition definition)))
2997-
(with-current-buffer (find-file-noselect file)
2998-
(goto-char (point-min))
2999-
(forward-line (1- line-beg))
3000-
(forward-char (1- col-beg))
3001-
(let* ((call-site? (looking-back "(\s*" (point-at-bol)))
3002-
(sexp (if call-site?
3003-
(prog1 (cljr--extract-sexp-as-list)
3004-
(paredit-backward-up)
3005-
(clojure-delete-and-extract-sexp))
3006-
(clojure-delete-and-extract-sexp))))
3007-
(if call-site?
3008-
(cljr--inline-fn-at-call-site def sexp)
3009-
(insert def))))))
2992+
(let* ((def (gethash :definition definition))
2993+
(inline-fn-p (s-starts-with-p "(fn" def)))
2994+
(dolist (symbol-meta (cljr--sort-occurrences occurrences))
2995+
(let* ((file (cljr--get-valid-filename symbol-meta))
2996+
(line-beg (gethash :line-beg symbol-meta))
2997+
(col-beg (gethash :col-beg symbol-meta)))
2998+
(with-current-buffer (find-file-noselect file)
2999+
(goto-char (point-min))
3000+
(forward-line (1- line-beg))
3001+
(forward-char (1- col-beg))
3002+
(let* ((call-site-p (and inline-fn-p
3003+
(looking-back "(\s*" (point-at-bol))))
3004+
(sexp (if call-site-p
3005+
(prog1 (cljr--extract-sexp-as-list)
3006+
(paredit-backward-up)
3007+
(clojure-delete-and-extract-sexp))
3008+
(clojure-delete-and-extract-sexp))))
3009+
(if call-site-p
3010+
(cljr--inline-fn-at-call-site def sexp)
3011+
(insert def)))))))
30103012
(save-buffer)
30113013
(cljr--delete-definition definition))
30123014

features/cljr-inline-symbol.feature

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,17 @@ Feature: Inlining of symbols
8080
8181
(map (fn [n]
8282
(+ 1 n)) (range 10))
83-
"""
83+
"""
84+
85+
Scenario: Inline composed functions as first level def
86+
When I insert:
87+
"""
88+
(def trim-lower (comp str/lower-case str/trim))
89+
90+
(trim-lower a-string)
91+
"""
92+
And I call the cljr--inline-symbol function directly with mockdata to inline trim-lower
93+
Then I should see:
94+
"""
95+
((comp str/lower-case str/trim) a-string)
96+
"""

features/step-definitions/clj-refactor-steps.el

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,27 @@
248248
:name \"refactor-nrepl.foo/my-inc\"
249249
:file \"core.clj\"
250250
:match \"(map my-inc (range 10))\"})}")))
251+
(cljr--inline-symbol (gethash :definition response)
252+
(gethash :occurrences response)))))
253+
254+
(Given "I call the cljr--inline-symbol function directly with mockdata to inline trim-lower"
255+
(lambda ()
256+
(let ((response (edn-read "{:definition {:definition \"(comp str/lower-case str/trim)\"
257+
:line-beg 1
258+
:line-end 1
259+
:col-beg 2
260+
:col-end 52
261+
:name \"refactor-nrepl.foo/trim-lower\"
262+
:file \"core.clj\"
263+
:match \"(def trim-lower (comp str/lower-case str/trim))\"}
264+
:occurrences ({:line-beg 3
265+
:line-end 3
266+
:col-beg 2
267+
:col-end 26
268+
:name \"refactor-nrepl.foo/trim-lower\"
269+
:file \"core.clj\"
270+
:match \"(trim-lower a-string)\"})}")))
271+
251272
(cljr--inline-symbol (gethash :definition response)
252273
(gethash :occurrences response)))))
253274

0 commit comments

Comments
 (0)