@dpsutton is there a way to inject the cider-nrepl middleware after doing M-x cider-connect
if I want to connect to a specific repl?
don't believe so
that stuff needs to be registered at the beginning as far as i know
could you give us some ideas for the last thread currently in #lsp @dpsutton? My intent is having the server that's opened by clojure-lsp
to include the cider-nrepl
middleware
honestly I don't even understand how we can use the same server for both lsp and nrepl, but let's maybe unify the discussion theer
do you understand how cider normally starts a project up?
nrepl is just a port listening for instructions and executing in your program. it can exist in any arbitrary project, which is how nrepl works in all clojure projects
it might be a bit difficult though. nrepl expects to be the "main". if lsp also expects to have its own main to do its own startup, it might be easier to just add a call to startup an nrepl server in lsp so there's no conflict on what the entry point is
here's my understanding: when I issue cider-jack-in
, cider opens an nrepl server with all the middleware I'm interested in, and an nrepl client which I activate from the emacs side
yes. and if you do that and look at the top of the repl buffer you'll see the exact command that is run to accomplish this
right, and I can see that when I start a project that uses clojure-cli. It's something like:
[nREPL] Starting server via /run/current-system/sw/bin/clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.8.3"} cider/piggieback {:mvn/version "0.5.2"} refactor-nrepl {:mvn/version "2.5.1"} cider/cider-nrepl {:mvn/version "0.25.9"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware,cider.piggieback/wrap-cljs-repl]"]}}}' -M:cider/nrepl
now though, @ericdallo suggests some steps to hack on clojure-lsp that involve issuing cider-connect
on the port in which clojure-lsp
has exposed an nrepl server used for debugging
the point is that that doesn't inject the cider-nrepl
stuff, so it seems that my option would be convincing clojure-lsp
to create a server with that dependency in the first place
I think that should involve adding the dependency here https://github.com/clojure-lsp/clojure-lsp/blob/master/project.clj#L27
so that that line looks more like:
:debug ^:leaky {:dependencies [[nrepl/nrepl "0.8.3"]
[cider/cider-nrepl "0.25.9"]]}
but this seems not to be enough to start the nrepl server with the extension (I'm issuing lein with-profile +debug bin
)
i would add the deps from the -Sdeps
from that startup command to your .clojure/deps.edn
and then set your clojure lsp startup command to clj -M:cider/nrepl:debug
{:aliases
{:cider/nrepl
{:deps {nrepl/nrepl {:mvn/version "0.8.3"}
refactor-nrepl {:mvn/version "2.5.1"}
cider/cider-nrepl {:mvn/version "0.25.9"}}
:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware"}}}
add this alias to your ~/.clojure/deps.edn file and then clj -M:debug:cider/nrepl
and you should have what you need. again, with the caveat that you might need to manually "start" the lsp stuff since the nrepl will be the main instead of whatever clojure-lsp expects to be the maindoes this work even if clojure-lsp
is based on lein
?
no. this is just if you're using deps.edn which i think will be the easier way to do this. but clojure-lsp has a deps.edn file and now that the java is compiled i thought you said it works
no, @ericdallo explained that there's a WIP branch for clojure-lsp
which will switch to clojure-cli, but currently clojure-lsp
is a lein project
~/p/c/clojure-lsp ❯❯❯ clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.8.3"} cider/cider-nrepl {:mvn/version "0.25.9"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}}}' -M:cider/nrepl
nREPL server started on port 59664 on host localhost - <nrepl://localhost:59664>
works for me
i just copied the normal cider startup command and ran it in clojure-lsp
connects, etc
debugging:
I see I see, let me try 🤯
Btw, you can actually inject cider-nrepl into a running nREPL instance, but CIDER can't do this just yet.
(you can inject any middleware in a running nREPL server)
i thought i remembered some functionality like that but i figured it would complicate things at this stage
The functionality exists, but I never got to implementing the support for it in CIDER - I do know that some people played with it and it worked for them (on other editors).
i had a patch to create the startup command and copy it for exactly these situations but i don't know where its gone. some gist, branch, or stash somewhere
as of now have to crank up some test project, copy from the repl buffer, and then kill it, then start up a project from the command line and connect to it
@dpsutton what happens when I start the command you pasted in the clojure-lsp
directory is that a server starts, then I open the clojure-lsp codebase in my editor, cider-connect
there,
and when I try to mark something for debug I get:
did you require that namespace first?
i don't know what's going on at the moment. which function are you trying to instrument?
ah no, ok, if I evaluate all the buffer first, I can correctly instrument it (I never understood when/if I have to load namespaces myself, and when it's done automatically)
still though
I'm instrumenting the incoming
function at clojure-lsp/src/clojure_lsp/feature/call_hierarchy.clj
, and then I call that feature from an emacs command, and it doesn't stop at my breakpoint
I invoke the command M-x lsp-treemacs-call-hierarchy
which is supposed to use incoming
at some point
I suspect that happens because the running instance of the lsp
server knows nothing about the server I created with the last command
i suspect that you're running the clojure-lsp server in the terminal and your emacs also started its own clojure-lsp server for editing
did you update the command that emacs uses ot create an lsp server?
but remember your goal is for emacs, when it starts up a clojure-lsp server, to start one that has an nrepl server you can connect to
I'm using:
(after! clojure-mode (setq lsp-clojure-custom-server-command '("bash" "-c" "/home/carlo/code/clojure/clojure-lsp/target/clojure-lsp")))
for that now, which probably isn't the one I want. Maybe this is the place for the command that we used before, not my shellyes. that's starting a clojure-lsp as normal, without any cider stuff involved
right, so I modified it to:
(after! clojure-mode (setq lsp-clojure-custom-server-command '("bash" "-c" "cd /home/carlo/code/clojure/clojure-lsp && clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version \"0.8.3\"} cider/cider-nrepl {:mvn/version \"0.25.9\"}} :aliases {:cider/nrepl {:main-opts [\"-m\" \"nrepl.cmdline\" \"--middleware\" \"[cider.nrepl/cider-middleware]\"]}}}' -M:cider/nrepl")))
but I can't start lsp-mode
nowright. because you've swapped out the mains. go see what clojure-lsp is doing at the beginning, cider connect, and call that
could you explain what swapped out the mains
means? What's a main in this context?
this is where it might be easier to just add an nrepl server with the right middleware to the main of clojure-lsp. it needs to start up its stuff
main-opts [\"-m\" \"nrepl.cmdline\" \"--middleware\" \"[cider.nrepl/cider-middleware]\"]
this is what is running at startup. its starting an nrepl server. it's NOT starting an lsp server.
https://github.com/clojure-lsp/clojure-lsp/blob/master/deps.edn#L21 is what you need to call to start an lsp server
right
so, is just swapping nrepl.cmdline
-> clojure-lsp.main
enough?
well then you lose your nrepl server you want to connect to
you need to "start" two things. and you only have a way to automatically do one. so its probably easier to just add cider and stuff to clojure-lsp and have the main start up an nrepl server with the correct middleware
ok, and I can accomplish this by modifying my https://github.com/clojure-lsp/clojure-lsp/blob/master/deps.edn
the main-opts
in run
is where I will have to insert the nrepl-call, and the dependencies can go in debug
?
probably not. you want to add an (nrepl/start-server ...)
code in the main run in clojure-lsp
that is here https://github.com/clojure-lsp/clojure-lsp/blob/master/src/clojure_lsp/main.clj#L412-L416
I don't know how to accomplish this, it's possible that the task is beyond my current understanding 😂
what should I read to bridge the gap?
my suggestion would be start a small simple project and figure out how to start an nrepl server in code and then connect to it. once you've got that, figure out how to get cider's middleware into it and connect to it. once that is finished figure out how to do that in clojure-lsp and then you're done
Not sure if this is specific to cider, or maybe just Emacs.
Is there a way to make sure that the ;
in code does not get format to the right of the code in Clojure e.g.
If the comment string is ;;
then the code in Emacs indent with the code before/after it but not with the single ;
string
Thanks @bozhidar will take a peek at the code
The reason is when I have to contribute to some project that they don't use Cider/Emacs the code will get reformatted on save which I don't want to introduce as part of my contribution. Thanks again for your pointer.
You're welcome!
Awww man, I've had a similar frustration with this as well, trying to add and delete ;;
comments.