clojure-emacs / clj-refactor.el

A CIDER extension that provides powerful commands for refactoring Clojure code.
GNU General Public License v3.0
773 stars 111 forks source link

Inlining #101

Closed expez closed 9 years ago

expez commented 9 years ago

I'd like to be able to inline

A function would turn into the fn form and then we can consider adding cljr-demote-function to turn an fn into a function literal.

benedekfazekas commented 9 years ago

can you add some examples pls?

expez commented 9 years ago

of how inlining should work?

benedekfazekas commented 9 years ago

basically what you want to see passing as tests

expez commented 9 years ago

Going out for a pint now; I'll add some examples when I get back. But the gist is to remove the var from the let and inline it if the var is let-bound. Remove the def and inline if the var is defined in that manner. If the var is a function we remove the top-level defn and replace it with an inline fn.

expez commented 9 years ago

Let | indicate the cursor. Given this buffer:

(ns example.com)

(def ^:private ^:const tolerance 0.01)

(defn- calculate-frobble
  [max-width]
  (* max-width toler|ance))

(defn frobnicate
  (let [max-width 5
        frobble (calculate-frobble max-width)]
    (map #(* frobble) (range 10))))

And I inline I get:

(ns example.com)

(defn- calculate-frobble
  [max-width]
  (* max-width 0.01))

(defn frobnicate
  (let [max-width 5
        frobble (calculate-frobble max-width)]
    (map #(* % frob|ble) (range 10))))

And I inline again:

(ns example.com)

(defn- calculate-frobble
  [max-width]
  (* max-width 0.01))

(defn frobnicate
  (let [max-width 5]
    (map #(* % (calculate-fr|obble max-width)) (range 10))))

And I inline again:

(ns example.com)

(defn frobnicate
  (let [max-width 5]
    (map #(* % (* max-width 0.01)) (range 10))))