Fork me on GitHub
#cider
<
2023-11-05
>
pesterhazy13:11:20

Hi! I'm a simple-minded user of CIDER: I use it only to connect to the running project and eval the current buffer As a result, I'd like a command that will • connect to the first currently running backend it finds, • eval the current buffer, and • do so without asking my any questions (asking me to choose the port from a list etc) Essentially, I want to remove all friction from the (only) cider command I use. I wrote a function that worked for a while, but it's now often not detecting the right nREPL server. So I'm struggling to get it to work. Any help appreciated

pesterhazy13:11:04

Here's what I used, but it's not working

(defun cider-force-connect ()
  (interactive)
  (let ((repl (cider-current-repl nil nil)))
    (when repl
      (cider-quit repl))
    (cider-connect (plist-put '(:host "localhost" :cljs-repl-type shadow)
                              :port (cl-second (cl-first (cider-locate-running-nrepl-ports)))))))

(defun cider-force-eval-buffer ()
  (interactive)
  (let ((repl (cider-current-repl nil nil)))
    (when (not repl)
      (cider-force-connect))
    (cider-eval-buffer)))

vemv13:11:37

First and second bullet point should be orthogonal. A connection is supposed to be reasonably long-lived You most likely don't want to setup/teardown a bunch of stuff every time you want to eval a buffer I do respect the intent to keep things minimalistic, but I'd heartily recommend to understand how this stuff is supposed to work, and make some minimal adaptations to your intended workflow, accordingly

pesterhazy13:11:33

I think that's I tried to do. My cider-force-eval-buffer has

(when (not repl)
      (cider-force-connect))

pesterhazy13:11:43

I tried to understand how port detection is supposed to work, but it's fairly complex. It seems to be using "ps u | grep java | grep -v leiningen | grep nrepl.cmdline" as a heuristic, which often fails I couldn't figure out how or when .nrepl-port is used (is this documented somewhere?)

vemv13:11:44

But it also resets the conn, for some reason

(when repl
      (cider-quit repl))
but we can leave that aside, ok :)

pesterhazy13:11:24

These are two different functions. cider-force-connect is only called when no pre-existing repl exists

👍 1
vemv13:11:57

The current internal incantation is:

(format "ps u | grep -E 'java|%s' | grep -E 'nrepl.cmdline|%s' | grep -v -E 'leiningen|grep'"
                                   bb-indicator
                                   bb-indicator)
So it understands a bunch of stuff: Lein, pure Java nrepls, bb servers I'd recommend upgrading, so that you're peeking at the latest and greatest primitives

pesterhazy13:11:16

Good idea. I'll try upgrading to latest CIDER

pesterhazy13:11:30

Having said that, the newer incantation also won't work, I think, because I'm not using nrepl.cmdline (I'm calling nrepl.server/start-server from the code). Perhaps .nrepl-port is an easier technique for my use case? It has the advantage of being project-relative (I see the code is using clojure-project-dir, which sounds right)

pesterhazy13:11:25

I'd like to figure out how .nrepl-port is written typcally – is that a lein thing, or does nrepl.cmdline do that?

vemv13:11:22

Yes, we have helpers that read .nrepl-port There's defun nrepl-extract-port (dir) available > I'd like to figure out how .nrepl-port is written typcally – is that a lein thing, or does nrepl.cmdline do that? It's standard from https://github.com/nrepl/nrepl so most servers will just write it

pesterhazy13:11:54

Alright, will try that next

pesterhazy14:11:49

OK that worked. Here's what I came up with

(defun cider-force-connect ()
  (interactive)
  (let ((port (nth 1 (car (cider-locate-running-nrepl-ports (clojure-project-root-path))))))
    (if port
        (cider-nrepl-connect
         (thread-first
           nil
           (cider--update-project-dir)
           (plist-put :host "localhost")
           (plist-put :port port)
           (cider--check-existing-session)
           (plist-put :repl-init-function nil)
           (plist-put :session-name nil)
           (plist-put :repl-type 'clj)))
      (cider-connect))))

🙌 1
Ed14:11:47

no idea if you're interested, but there's a (cider-connected-p) that should tell you if cider is connected or not - which might be better than port hunting? Or did that not work?

pesterhazy14:11:51

@U0P0TMEFJ I'm port-hunting to find the right process to connect to, not to see if CIDER is connected

Ed14:11:15

facepalm ... so you are - I missed that - apologies