This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-05
Channels
- # adventofcode (22)
- # ai (1)
- # announcements (1)
- # babashka (3)
- # beginners (8)
- # calva (8)
- # cider (17)
- # clojure (3)
- # clojure-europe (4)
- # clojure-norway (13)
- # clojurescript (20)
- # core-logic (1)
- # data-science (3)
- # datahike (6)
- # deps-new (4)
- # events (6)
- # hoplon (4)
- # hyperfiddle (4)
- # lsp (34)
- # overtone (8)
- # podcasts-discuss (2)
- # releases (3)
- # ring (5)
- # vim (10)
- # xtdb (2)
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
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)))
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
I think that's I tried to do. My cider-force-eval-buffer
has
(when (not repl)
(cider-force-connect))
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?)
But it also resets the conn, for some reason
(when repl
(cider-quit repl))
but we can leave that aside, ok :)These are two different functions. cider-force-connect
is only called when no pre-existing repl exists
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 primitivesGood idea. I'll try upgrading to latest CIDER
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)
I'd like to figure out how .nrepl-port
is written typcally – is that a lein
thing, or does nrepl.cmdline
do that?
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
Alright, will try that next
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))))
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?
@U0P0TMEFJ I'm port-hunting to find the right process to connect to, not to see if CIDER is connected