greglook / whidbey

nREPL middleware to pretty-print colored values
The Unlicense
158 stars 10 forks source link

Standalone nREPL server initialization with whidbey middleware #23

Closed venantius closed 8 years ago

venantius commented 9 years ago

Yo! It's me again. Not an Ultra question this time around, though.

I'd like to be able to have Whidbey's nREPL middleware work when I embed an nREPL server in my application. Thus far, I've been doing the following:

(ns titan.server.nrepl
  "Logic for starting an nREPL server if we're in development mode."
  (:require [clojure.tools.logging :as log]
            [clojure.tools.nrepl.middleware :as middleware]
            [clojure.tools.nrepl.middleware.render-values :refer [render-values]]
            [clojure.tools.nrepl.server :as nrepl]
            [whidbey.repl]))

(def highlight
  render-values)

(middleware/set-descriptor!
  #'highlight
  {:requires #{}
   :expects #{"eval"}
   :handles {}})

(def handler
  (nrepl/default-handler #'highlight))

(defn start-server
  ([]
   (start-server {}))
  ([{:keys [port]
     :or {port 7002}
     :as opts}]
   (log/infof "Starting nREPL server on port %s..." port)
   (nrepl/start-server :port port :handler handler)))

I've had quite a bit of trouble getting this to play nicely on the client-side, unfortunately. In one of my projects, if I also add whidbey to my project's plugin map, then when I lein repl :connect I do get syntax-highlighting at the REPL, but surrounded by quotes.

If I try to do this in a bare-bones project that consists only of the relevant dependencies, running lein repl :connect ends up rendering and highlighting every value as a string (see screenshot, below).

screen shot 2015-11-08 at 1 54 37 pm

At least in the latter case, I don't have any other plugins in the project or in my profiles.clj, so there shouldn't be any interference.

Assuming this is just a matter of configuration, it would be great to update the README accordingly. If it's more complicated than that an requires code changes, let me know where to focus my attention and I can try to help.

Cheers!

greglook commented 9 years ago

I definitely ran into a problem like this when I was first working on Whidbey. The root cause is that your values are getting pretty-printed twice, so the second pass is rendering the string value output by the first pass. Same reason I had to forcibly change the default pr-values middleware into a no-op.

I'm not sure off the top of my head what the best workaround is.

venantius commented 9 years ago

Is it possible that it's an nREPL middleware ordering issue?

venantius commented 8 years ago

I had to think a bit about your comment but I figured it out. Just had to change the following piece of code and it started working:

(def handler
  (do (whidbey/init! {})
      (nrepl/default-handler #'highlight)))
greglook commented 8 years ago

Cool, glad you got it working.