Skip to content

Commit b14d5af

Browse files
author
Case Nelson
committed
add extract-function
1 parent 838f3ee commit b14d5af

File tree

7 files changed

+65
-15
lines changed

7 files changed

+65
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Using Vundle, add this to your vundle .config/nvim/init.vim section:
5454
- [ ] [describe-refactor](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/describe-refactor.gif)
5555
- [ ] [destructure-keys](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/destructure-keys.gif)
5656
- [x] `crel` [expand-let](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/expand-let.gif) * Doesn't yet replace other usages of bindings
57-
- [ ] [extract-fn](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/extract-fn.gif)
57+
- [x] `cref` [extract-fn](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/extract-fn.gif)
5858
- [ ] [find-usages](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/find-usages.gif)
5959
- [ ] [hotload-dependency](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/hotload-dependency.gif)
6060
- [ ] [inline-symbol](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/inline-symbol.gif)

plugin/refactor.vim

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ noremap crci :CCycleIf<CR>
55
noremap crcp :CCyclePrivacy<CR>
66
noremap crct :CCycleThread<CR>
77
noremap crel :CExpandLet<CR>
8+
noremap cref :CExtractFunction
89
noremap crfe :CFunctionFromExample<CR>
910
noremap cril :CIntroduceLet
1011
noremap crml :CMoveToLet

rplugin/node/clj-refactor.js

+19-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/clj_refactor/main.cljs

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@
9898
repl/rename-dir)
9999
(.command js/plugin "CRenameSymbol"
100100
#js {:eval "[getcwd(), expand('%:p'), fireplace#ns(), expand('<cword>'), fireplace#info(expand('<cword>')), getpos('.')]" :nargs 1}
101-
(partial repl/extract-definition repl/rename-symbol)))
101+
(partial repl/extract-definition repl/rename-symbol))
102+
(.command js/plugin "CExtractFunction"
103+
#js {:eval "[expand('%:p'), getpos('.')]" :nargs 1}
104+
(partial repl/find-used-locals run-transform transform/extract-function)))
102105

103106
(catch js/Error e
104107
(jdbg "main exception" e))))

src/clj_refactor/repl.cljs

+16
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
(defn rename-symbol
121121
[nvim sym-ns sym-name defs new-symbol]
122122
(go
123+
;; TODO look at clj-refactor.el for safer impl
123124
(let [wait-ch (chan)]
124125
(let [{:keys [line-beg col-beg file name]} (:definition defs)]
125126
(.command nvim (str "e " file " | "
@@ -135,3 +136,18 @@
135136
(fn [err]
136137
(close! wait-ch)))
137138
(<! wait-ch)))))
139+
140+
(defn find-used-locals
141+
[run-transform transform-fn nvim args [file [_ row col _]]]
142+
(fireplace-message
143+
nvim
144+
{:op "find-used-locals"
145+
:file file
146+
:line row
147+
:column col}
148+
(fn [err results]
149+
(js/debug "find-used-locals" err results)
150+
(if-let [error (aget (first results) "error")]
151+
(.command nvim (str "echo \"" error "\""))
152+
(let [used-locals (seq (js->clj (aget (first results) "used-locals")))]
153+
(run-transform transform-fn nvim (conj (js->clj args) used-locals) [0 row col 0]))))))

src/clj_refactor/transform.cljs

+15
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,18 @@
266266
(z/insert-left `(~'defn ~fn-name [~@args])) ; add declare
267267
(z/insert-left (n/newline-node "\n\n"))))) ; add new line after location
268268

269+
(defn extract-function
270+
[zloc [fn-name used-locals]]
271+
(let [expr-loc (z/up (edit/find-op zloc))
272+
expr (z/sexpr expr-loc)
273+
fn-sym (symbol fn-name)
274+
used-syms (map symbol used-locals)]
275+
(-> expr-loc
276+
(z/replace `(~fn-sym ~@used-syms))
277+
(edit/exec-to z/up #(not (edit/top? %))) ; Go to top level form
278+
(z/insert-left `(~'defn ~fn-sym [~@used-syms] ~expr))
279+
(z/insert-left (n/newline-node "\n\n"))
280+
(z/left)
281+
(z/up))))
282+
283+

test/clj_refactor/transform_test.cljs

+9
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@
9797

9898
'(do (defn b [arg1 z]) (defn a [z] (b (x y) z)))
9999
(apply-zip-root '(defn a [z] (b (x y) z)) 'b t/function-from-example)))
100+
101+
(deftest testing-extract-function
102+
(are [i j] (= i j)
103+
'(do (defn my-fn [c] (a (b c)))
104+
(defn old-fn [] (let [c 1] (my-fn c))))
105+
(apply-zip-root '(defn old-fn [] (let [c 1] (a (b c))))
106+
'a
107+
#(t/extract-function % ["my-fn" ["c"]]))))
108+

0 commit comments

Comments
 (0)