13

When using the leiningen REPL, is there a way to make a file or ns automatically reload in the repl on file save. Currently I reload the ns by typing the following in the repl - (use 'sample.ns :reload-all).

However can I have it reload automatically on file save ?

noahlz
  • 10,202
  • 7
  • 56
  • 75
murtaza52
  • 46,887
  • 28
  • 84
  • 120
  • 3
    Are you using emacs? Because if so, this is a supported feature of slime/swank: http://stackoverflow.com/questions/2596222/how-to-reload-files-upon-save-when-using-swankleiningenemacs – noahlz Sep 09 '12 at 18:02

3 Answers3

2

You can easily reuse the code from duct framework.

You will need only hawk files watcher.

Here is how it can be looks like:

(defn- clojure-file? [_ {:keys [file]}]
  (re-matches #"[^.].*(\.clj|\.edn)$" (.getName file)))

(defn- auto-reset-handler [ctx event]
  (binding [*ns* *ns*]
    (clojure.tools.namespace.repl/refresh)
    ctx))

(defn auto-reset
  "Automatically reset the system when a Clojure or edn file is changed in
  `src` or `resources`."
  []
  (hawk.core/watch! [{:paths ["src/" "resources/" "dev/src/" "dev/resources/"]
                 :filter clojure-file?
                 :handler auto-reset-handler}]))
Aleksei Sotnikov
  • 633
  • 4
  • 15
1

Clojure-Watch library does what you need. It observes a file and performs some action. In your case, an action would be to reload a namespace from that file. Also, it requires to write some initial code to launch the observer.

This way seems a bit complicated to me. Plain REPL launched directly from Lein is not effective way to develop. You better to use some Clojure-friendly editor like Emacs or Lightable.

Ivan Grishaev
  • 1,583
  • 10
  • 15
1

Most major editors support custom hotkey bindings and have a Clojure plugin that allows you to connect to the active REPL over the network (via "nREPL"). Personally, I use vim and therefore use vim-fireplace for this purpose.

This means you can have a custom hotkey for reloading whatever file you're editing as you edit it. From there, it's typically trivial to add a custom on-save hook that does the reloading.

Venantius
  • 2,471
  • 2
  • 28
  • 36