nrepl/piggieback

Middleware on cljs-repl launch

divs1210 opened this issue · 1 comments

I basically started out trying to find a fix to this issue.

Could we modify cljs-repl in the following manner to allow loading middleware once the REPL has launched and entered the cljs.user namespace?

(defmacro functionize [macro]
  `(fn [& args#] (eval (cons '~macro args#))))

(defmacro apply-macro [macro args]
   `(apply (functionize ~macro) ~args))

(defn cljs-repl
  "Starts a ClojureScript REPL on top of an nREPL session.  Accepts
   all options usually accepted by e.g. cljs.repl/repl."
  [repl-env & {:as options}]
  ; TODO I think we need a var to set! the compiler environment from the REPL
  ; environment after each eval
  (try
    (let [repl-env (delegating-repl-env repl-env nil)
          on-start (apply list   ;; <= HERE!
                          '(ns cljs.user
                             (:require [cljs.repl :refer-macros [source doc find-doc
                                                                 apropos dir pst]]))
                          (:on-start-middleware options))] ;; <= HERE!
      (set! ana/*cljs-ns* 'cljs.user)
      ; this will implicitly set! *cljs-compiler-env*
      (run-cljs-repl (assoc ieval/*msg* ::first-cljs-repl true)
        (apply-macro nrepl/code on-start)
        repl-env nil options)
      ; (clojure.pprint/pprint (:options @*cljs-compiler-env*))
      (set! *cljs-repl-env* repl-env)
      (set! *cljs-repl-options* options)
      ; interruptible-eval is in charge of emitting the final :ns response in this context
      (set! *original-clj-ns* *ns*)
      (set! *ns* (find-ns ana/*cljs-ns*))
      (println "To quit, type:" :cljs/quit))
    (catch Exception e
      (set! *cljs-repl-env* nil)
      (throw e))))

Then we could pass middleware like this:

(cljs-repl env {:on-start-middleware '((defn debug-reader [form]
                                         (comment Handler code))
                                       (cljs.reader/register-tag-parser! 'dbg debug-reader)
                                       (println "middleware launched"))})

I don't even know if I'm talking sense over here.

Yeah, this seems sorta confused. First, the functionalize thing is a little eye-popping; but beyond that, you're talking about middleware, which is a Clojure nREPL thing, completely related to ClojureScript.