Skip to content

Commit 838f3ee

Browse files
author
Case Nelson
committed
add function-from-example
1 parent 7182130 commit 838f3ee

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ A neovim port of [clj-refactor.el](https://github.com/clojure-emacs/clj-refactor
22

33
# Usage
44

5-
All commands are mapped under the `cr` prefix and use a two letter mnemonic shortcut. E.g. `crrs` for `:CRRenameSymbol`. The full list is below.
5+
All commands are mapped under the `cr` prefix and use a two letter mnemonic shortcut. E.g. `crrs` for `Clojure Refactor Rename Symbol`.The full list is below.
66

77
# Installation
88

@@ -46,7 +46,7 @@ Using Vundle, add this to your vundle .config/nvim/init.vim section:
4646
- [ ] [add-project-dependency](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/add-project-dependency.gif)
4747
- [ ] [add-stubs](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/add-stubs.gif)
4848
- [x] `crcn` [clean-ns](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/clean-ns.gif)
49-
- [ ] [create-fn-from-example](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/create-fn-from-example.gif)
49+
- [x] `crfe` [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)
5252
- [x] `crcp` [cycle-privacy](https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/cycle-privacy.gif)

plugin/refactor.vim

Lines changed: 1 addition & 0 deletions
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 crfe :CFunctionFromExample<CR>
89
noremap cril :CIntroduceLet
910
noremap crml :CMoveToLet
1011
noremap crtf :CThreadFirstAll<CR>

rplugin/node/clj-refactor.js

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

src/clj_refactor/main.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
(.command js/plugin "CCyclePrivacy" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/cycle-privacy))
7878
(.command js/plugin "CCycleThread" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/cycle-thread))
7979
(.command js/plugin "CExpandLet" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/expand-let))
80+
(.command js/plugin "CFunctionFromExample" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/function-from-example))
8081
(.command js/plugin "CIntroduceLet" #js {:eval "getpos('.')" :nargs 1} (partial run-transform transform/introduce-let))
8182
(.command js/plugin "CMoveToLet" #js {:eval "getpos('.')" :nargs 1} (partial run-transform transform/move-to-let))
8283
(.command js/plugin "CThread" #js {:eval "getpos('.')" :nargs 0} (partial run-transform transform/thread))

src/clj_refactor/transform.cljs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,19 @@
250250
(defn cycle-privacy
251251
[zloc _]
252252
(cycle-op zloc 'defn 'defn-))
253+
254+
(defn function-from-example
255+
[zloc _]
256+
(let [op-loc (edit/find-op zloc)
257+
example-loc (z/up (edit/find-op zloc))
258+
child-sexprs (n/child-sexprs (z/node example-loc))
259+
fn-name (first child-sexprs)
260+
args (for [[i arg] (map-indexed vector (rest child-sexprs))]
261+
(if (symbol? arg)
262+
arg
263+
(symbol (str "arg" (inc i)))))]
264+
(-> example-loc
265+
(edit/exec-to z/up #(not (edit/top? %))) ; Go to top level form
266+
(z/insert-left `(~'defn ~fn-name [~@args])) ; add declare
267+
(z/insert-left (n/newline-node "\n\n"))))) ; add new line after location
268+

test/clj_refactor/test_helper.cljs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@
1111
(z/find z/next #(= (z/sexpr %) goto))
1212
(f)))
1313

14+
(defn apply-goto
15+
[form goto f]
16+
(zip-to form goto f))
17+
1418
(defn apply-zip-to
1519
[form goto f]
16-
(-> form
17-
(zip-to goto f)
18-
(z/sexpr)))
20+
(z/sexpr (apply-goto form goto f)))
1921

2022
(defn apply-zip
2123
[form goto f]
22-
(-> form
23-
(zip-to goto f)
24+
(-> (apply-goto form goto f)
2425
(z/root-string)
2526
(r/read-string)))
2627

28+
(defn apply-zip-root
29+
[form goto f]
30+
(r/read-string
31+
(str "(do "
32+
(z/root-string (apply-goto form goto f))
33+
")")))

test/clj_refactor/transform_test.cljs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[clojure.string :as str]
55
[clj-refactor.main :as m]
66
[clj-refactor.transform :as t]
7-
[clj-refactor.test-helper :refer [apply-zip apply-zip-to]]))
7+
[clj-refactor.test-helper :refer [apply-zip apply-zip-to apply-zip-root]]))
88

99
(deftest testing-introduce-let
1010
(are [i j] (= i j)
@@ -89,3 +89,11 @@
8989
'(defn a [b] (b c)) (apply-zip '(defn- a [b] (b c)) 'c t/cycle-privacy)
9090
'(defn- a [b] (b c)) (apply-zip '(defn a [b] (b c)) 'c t/cycle-privacy)
9191
'(a (b c)) (apply-zip '(a (b c)) 'b t/cycle-privacy)))
92+
93+
(deftest testing-function-from-example
94+
(are [i j] (= i j)
95+
'(do (defn b [z]) (defn a [z] (b z)))
96+
(apply-zip-root '(defn a [z] (b z)) 'b t/function-from-example)
97+
98+
'(do (defn b [arg1 z]) (defn a [z] (b (x y) z)))
99+
(apply-zip-root '(defn a [z] (b (x y) z)) 'b t/function-from-example)))

0 commit comments

Comments
 (0)