Skip to content

Commit 7182130

Browse files
author
Case Nelson
committed
adding cycle-privacy and cycle-thread
1 parent c41c173 commit 7182130

File tree

7 files changed

+88
-54
lines changed

7 files changed

+88
-54
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ Using Vundle, add this to your vundle .config/nvim/init.vim section:
4949
- [ ] [create-fn-from-example](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/create-fn-from-example.gif)
5050
- [x] `crcc` [cycle-coll](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-coll.gif)
5151
- [x] `crci` [cycle-if](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-if.gif)
52-
- [ ] [cycle-privacy](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-privacy.gif)
53-
- [ ] [cycle-thread](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-thread.gif)
52+
- [x] `crcp` [cycle-privacy](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-privacy.gif)
53+
- [x] `crct` [cycle-thread](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-thread.gif)
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

plugin/refactor.vim

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
noremap crad :CAddDeclaration
2-
noremap cril :CIntroduceLet
3-
noremap crel :CExpandLet<CR>
4-
noremap crml :CMoveToLet
2+
noremap cram :CAddMissingLibSpec<CR>
53
noremap crcc :CCycleColl<CR>
64
noremap crci :CCycleIf<CR>
7-
noremap cram :CAddMissingLibSpec<CR>
8-
noremap crth :CThread<CR>
9-
noremap crtt :CThreadLast<CR>
5+
noremap crcp :CCyclePrivacy<CR>
6+
noremap crct :CCycleThread<CR>
7+
noremap crel :CExpandLet<CR>
8+
noremap cril :CIntroduceLet
9+
noremap crml :CMoveToLet
1010
noremap crtf :CThreadFirstAll<CR>
11+
noremap crth :CThread<CR>
1112
noremap crtl :CThreadLastAll<CR>
12-
noremap cruw :CUnwindThread<CR>
13+
noremap crtt :CThreadLast<CR>
1314
noremap crua :CUnwindAll<CR>
15+
noremap cruw :CUnwindThread<CR>
1416
1517
noremap crcn :CCleanNS<CR>
1618
noremap crrf :CRenameFile

rplugin/node/clj-refactor.js

Lines changed: 24 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/clj_refactor/edit.cljs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
(defn top? [loc]
1111
(= nf/FormsNode (type (z/node loc))))
1212

13-
(defn zdbg [loc]
14-
(doto (z/sexpr loc) prn)
13+
(defn zdbg [loc msg]
14+
(doto (z/sexpr loc) (prn msg))
1515
loc)
1616

1717
(defn exec-to [loc f p?]
@@ -29,6 +29,15 @@
2929
(z/down zloc)
3030
(z/leftmost zloc)))
3131

32+
(defn find-ops-up
33+
[zloc & op-syms]
34+
(let [oploc (find-op zloc)]
35+
(if (contains? (set op-syms) (z/sexpr oploc))
36+
oploc
37+
(let [next-op (z/leftmost (z/up oploc))]
38+
(when-not (= next-op zloc)
39+
(apply find-ops-up next-op op-syms))))))
40+
3241
(defn single-child?
3342
[zloc]
3443
(let [child (z/down zloc)]

src/clj_refactor/main.cljs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,20 @@
7171
(try
7272
(when (exists? js/plugin)
7373
(jdbg "hello refactor")
74-
(.command js/plugin "CIntroduceLet" #js {:eval "getpos('.')" :nargs 1}
75-
(partial run-transform transform/introduce-let))
76-
(.command js/plugin "CExpandLet" #js {:eval "getpos('.')" :nargs "*"}
77-
(partial run-transform transform/expand-let))
78-
(.command js/plugin "CMoveToLet" #js {:eval "getpos('.')" :nargs 1}
79-
(partial run-transform transform/move-to-let))
80-
(.command js/plugin "CAddDeclaration" #js {:eval "getpos('.')" :nargs 0}
81-
(partial run-transform transform/add-declaration))
82-
(.command js/plugin "CCycleColl" #js {:eval "getpos('.')" :nargs 0}
83-
(partial run-transform transform/cycle-coll))
84-
(.command js/plugin "CCycleIf" #js {:eval "getpos('.')" :nargs 0}
85-
(partial run-transform transform/cycle-if))
86-
(.command js/plugin "CThread" #js {:eval "getpos('.')" :nargs 0}
87-
(partial run-transform transform/thread))
88-
(.command js/plugin "CThreadLast" #js {:eval "getpos('.')" :nargs 0}
89-
(partial run-transform transform/thread-last))
90-
(.command js/plugin "CThreadFirstAll" #js {:eval "getpos('.')" :nargs 0}
91-
(partial run-transform transform/thread-first-all))
92-
(.command js/plugin "CThreadLastAll" #js {:eval "getpos('.')" :nargs 0}
93-
(partial run-transform transform/thread-last-all))
94-
(.command js/plugin "CUnwindThread" #js {:eval "getpos('.')" :nargs 0}
95-
(partial run-transform transform/unwind-thread))
96-
(.command js/plugin "CUnwindAll" #js {:eval "getpos('.')" :nargs 0}
97-
(partial run-transform transform/unwind-all))
74+
(.command js/plugin "CAddDeclaration" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/add-declaration))
75+
(.command js/plugin "CCycleColl" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/cycle-coll))
76+
(.command js/plugin "CCycleIf" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/cycle-if))
77+
(.command js/plugin "CCyclePrivacy" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/cycle-privacy))
78+
(.command js/plugin "CCycleThread" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/cycle-thread))
79+
(.command js/plugin "CExpandLet" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/expand-let))
80+
(.command js/plugin "CIntroduceLet" #js {:eval "getpos('.')" :nargs 1} (partial run-transform transform/introduce-let))
81+
(.command js/plugin "CMoveToLet" #js {:eval "getpos('.')" :nargs 1} (partial run-transform transform/move-to-let))
82+
(.command js/plugin "CThread" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/thread))
83+
(.command js/plugin "CThreadFirstAll" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/thread-first-all))
84+
(.command js/plugin "CThreadLast" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/thread-last))
85+
(.command js/plugin "CThreadLastAll" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/thread-last-all))
86+
(.command js/plugin "CUnwindAll" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/unwind-all))
87+
(.command js/plugin "CUnwindThread" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/unwind-thread))
9888

9989
;; REPL only commands
10090
(.command js/plugin "CAddMissingLibSpec" #js {:eval "expand('<cword>')" :nargs 0}

src/clj_refactor/transform.cljs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,20 @@
233233
(z/insert-right new-ns)
234234
(z/remove)))
235235

236+
(defn cycle-op
237+
[zloc a-op b-op]
238+
(if-let [oploc (edit/find-ops-up zloc a-op b-op)]
239+
(let [thread-type (z/sexpr oploc)]
240+
(cond
241+
(= a-op thread-type) (z/replace oploc b-op)
242+
(= b-op thread-type) (z/replace oploc a-op)
243+
:else zloc))
244+
zloc))
245+
246+
(defn cycle-thread
247+
[zloc _]
248+
(cycle-op zloc '-> '->>))
249+
250+
(defn cycle-privacy
251+
[zloc _]
252+
(cycle-op zloc 'defn 'defn-))

test/clj_refactor/transform_test.cljs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@
7878
'(e (d (c (b a)) d')) (apply-zip '(-> a b c (d d') e) 'e t/unwind-all)
7979
'(e (d d' (c (b a)))) (apply-zip '(->> a b c (d d') e) 'e t/unwind-all)))
8080

81+
(deftest testing-cycle-thread
82+
(are [i j] (= i j)
83+
'(->> a (b c)) (apply-zip '(-> a (b c)) 'b t/cycle-thread)
84+
'(-> a (b c)) (apply-zip '(->> a (b c)) 'b t/cycle-thread)
85+
'(a (b c)) (apply-zip '(a (b c)) 'b t/cycle-thread)))
86+
87+
(deftest testing-cycle-privacy
88+
(are [i j] (= i j)
89+
'(defn a [b] (b c)) (apply-zip '(defn- a [b] (b c)) 'c t/cycle-privacy)
90+
'(defn- a [b] (b c)) (apply-zip '(defn a [b] (b c)) 'c t/cycle-privacy)
91+
'(a (b c)) (apply-zip '(a (b c)) 'b t/cycle-privacy)))

0 commit comments

Comments
 (0)