Fork me on GitHub
#cider
<
2021-03-21
>
william15:03:25

@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?

dpsutton15:03:06

don't believe so

dpsutton15:03:19

that stuff needs to be registered at the beginning as far as i know

william15:03:56

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

william15:03:32

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

dpsutton15:03:34

do you understand how cider normally starts a project up?

dpsutton15:03:04

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

dpsutton15:03:09

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

william15:03:12

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

dpsutton15:03:38

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

william15:03:16

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

william15:03:28

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

william15:03:23

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

william15:03:23

so that that line looks more like:

:debug ^:leaky {:dependencies [[nrepl/nrepl "0.8.3"]
                                            [cider/cider-nrepl "0.25.9"]]}

william15:03:39

but this seems not to be enough to start the nrepl server with the extension (I'm issuing lein with-profile +debug bin)

dpsutton16:03:33

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

dpsutton16:03:51

{: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 main

william16:03:00

does this work even if clojure-lsp is based on lein?

dpsutton16:03:04

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

william16:03:41

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

dpsutton16:03:32

~/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 - 

🙌 3
dpsutton16:03:38

works for me

dpsutton16:03:22

i just copied the normal cider startup command and ran it in clojure-lsp

dpsutton16:03:34

connects, etc

william16:03:15

I see I see, let me try 🤯

bozhidar16:03:44

Btw, you can actually inject cider-nrepl into a running nREPL instance, but CIDER can't do this just yet.

bozhidar16:03:04

(you can inject any middleware in a running nREPL server)

dpsutton16:03:06

i thought i remembered some functionality like that but i figured it would complicate things at this stage

bozhidar16:03:42

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).

dpsutton16:03:44

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

dpsutton16:03:24

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

william16:03:35

@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,

william16:03:47

and when I try to mark something for debug I get:

dpsutton16:03:16

did you require that namespace first?

dpsutton16:03:28

i don't know what's going on at the moment. which function are you trying to instrument?

william16:03:15

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)

william16:03:31

still though

william16:03:26

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

william16:03:15

I invoke the command M-x lsp-treemacs-call-hierarchy which is supposed to use incoming at some point

william16:03:01

I suspect that happens because the running instance of the lsp server knows nothing about the server I created with the last command

dpsutton16:03:16

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

dpsutton16:03:29

did you update the command that emacs uses ot create an lsp server?

dpsutton16:03:08

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

william16:03:10

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 shell

dpsutton16:03:27

yes. that's starting a clojure-lsp as normal, without any cider stuff involved

william16:03:56

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 now

dpsutton16:03:14

right. because you've swapped out the mains. go see what clojure-lsp is doing at the beginning, cider connect, and call that

william16:03:43

could you explain what swapped out the mains means? What's a main in this context?

dpsutton16:03:45

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

dpsutton16:03:21

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.

william16:03:17

so, is just swapping nrepl.cmdline -> clojure-lsp.main enough?

dpsutton16:03:38

well then you lose your nrepl server you want to connect to

dpsutton16:03:25

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

william16:03:19

the main-opts in run is where I will have to insert the nrepl-call, and the dependencies can go in debug?

dpsutton16:03:28

probably not. you want to add an (nrepl/start-server ...) code in the main run in clojure-lsp

william16:03:07

I don't know how to accomplish this, it's possible that the task is beyond my current understanding 😂

william16:03:50

what should I read to bridge the gap?

dpsutton17:03:32

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

☝️ 3
🙏 3
agilecreativity20:03:50

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

West23:03:52

Awww man, I've had a similar frustration with this as well, trying to add and delete ;; comments.

bozhidar06:03:45

The defaults follow the different semantics different comment levels have had historically. You can change this behaviour, though - https://github.com/clojure-emacs/clojure-mode#indentation-of-comments

👍 3
agilecreativity04:03:20

Thanks @U051BLM8F will take a peek at the code

agilecreativity04:03:05

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.

bozhidar07:03:07

You're welcome!