Skip to content

Commit

Permalink
Add ways to hook into and override the Clojure auto / passive connect #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Olical committed Jan 7, 2023
1 parent a74547e commit 70b4f17
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 250 deletions.
32 changes: 32 additions & 0 deletions doc/conjure-client-clojure-nrepl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CONTENTS *conjure-client-clojure-nrepl-contents*
1. Introduction ........ |conjure-client-clojure-nrepl-introduction|
2. Mappings ................ |conjure-client-clojure-nrepl-mappings|
3. Configuration ...... |conjure-client-clojure-nrepl-configuration|
4. Hooks . ..... |conjure-client-clojure-nrepl-hooks|

==============================================================================
INTRODUCTION *conjure-client-clojure-nrepl-introduction*
Expand Down Expand Up @@ -452,4 +453,35 @@ All configuration can be set as described in |conjure-configuration|.
machines in large buffers with lots of top level forms.
Default: `false`

==============================================================================
HOOKS *conjure-client-clojure-nrepl-hooks*

See |conjure-hooks| for more general details on hooks and how to use them.
Below are the hooks that this client defines and their behaviour.

`(client-clojure-nrepl-passive-connect cb)`
Called when we don't currently have a connection but the user tried to do
something that required one. This will call
`conjure.client.clojure.nrepl.action/connect-port-file` providing the
callback (`cb`) as an argument to it.

If you're overriding this then you need to make sure `cb` is called if
it's not `nil`.

The original implementation returns the result of calling
`connect-port-file` which you could inspect to work out if a connection
attempt was made or not.

If you wish to check if the connection actually succeeded and didn't just
get attempted you'll need to call `connect-port-file` yourself and provide
some overrides for `nrepl.connect` under the `connect-opts` key in it's
`opts` argument. This allows you to set your own `on-failure` function
that replaces the default one from Conjure. In there you can perform
different actions such as starting your own REPL or cleaning up port
files.

I suggest you read the original source code for this to get a full
understanding, but this should be enough for you to get pretty creative
with it.

vim:tw=78:sw=2:ts=2:ft=help:norl:et:listchars=
15 changes: 11 additions & 4 deletions fnl/conjure/client/clojure/nrepl/action.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ll conjure.linked-list
log conjure.log
fs conjure.fs
hook conjure.hook
client conjure.client
eval conjure.aniseed.eval
str conjure.aniseed.string
Expand Down Expand Up @@ -48,16 +49,22 @@
(let [cb (a.get opts :cb)]
(when cb
(cb)))
(passive-ns-require))})
(passive-ns-require))
:connect-opts (a.get opts :connect-opts)})
(when (not (a.get opts :silent?))
(log.append ["; No nREPL port file found"] {:break? true})
(auto-repl.upsert-auto-repl-proc)))))

(defn- try-ensure-conn [cb]
(if (not (server.connected?))
(hook.define
:client-clojure-nrepl-passive-connect
(fn [cb]
(connect-port-file
{:silent? true
:cb cb})
:cb cb})))

(defn- try-ensure-conn [cb]
(if (not (server.connected?))
(hook.exec :client-clojure-nrepl-passive-connect cb)
(when cb
(cb))))

Expand Down
83 changes: 43 additions & 40 deletions fnl/conjure/client/clojure/nrepl/server.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -252,54 +252,57 @@
"\n")
:session msg.session}))

(defn connect [{: host : port : cb : port_file_path}]
(defn connect [{: host : port : cb : port_file_path : connect-opts}]
(when (state.get :conn)
(disconnect))

(a.assoc
(state.get) :conn
(a.merge!
(nrepl.connect
{:host host
:port port

:on-failure
(fn [err]
(display-conn-status err)
(disconnect))

:on-success
(fn []
(display-conn-status :connected)
(capture-describe)
(assume-or-create-session)
(eval-preamble cb))

:on-error
(fn [err]
(if err
(a.merge
{:host host
:port port

:on-failure
(fn [err]
(display-conn-status err)
(disconnect)))

:on-message
(fn [msg]
(when msg.status.unknown-session
(log.append ["; Unknown session, correcting"])
(assume-or-create-session))
(when msg.status.namespace-not-found
(log.append [(str.join ["; Namespace not found: " msg.ns])])))

:side-effect-callback
(fn [msg]
(when msg.status.need-input
(client.schedule handle-input-request msg))

(when msg.status.need-debug-input
(client.schedule debugger.handle-input-request msg)))

:default-callback
(fn [msg]
(ui.display-result msg))})
(disconnect))

:on-success
(fn []
(display-conn-status :connected)
(capture-describe)
(assume-or-create-session)
(eval-preamble cb))

:on-error
(fn [err]
(if err
(display-conn-status err)
(disconnect)))

:on-message
(fn [msg]
(when msg.status.unknown-session
(log.append ["; Unknown session, correcting"])
(assume-or-create-session))
(when msg.status.namespace-not-found
(log.append [(str.join ["; Namespace not found: " msg.ns])])))

:side-effect-callback
(fn [msg]
(when msg.status.need-input
(client.schedule handle-input-request msg))

(when msg.status.need-debug-input
(client.schedule debugger.handle-input-request msg)))

:default-callback
(fn [msg]
(ui.display-result msg))}

connect-opts))

{:seen-ns {}
:port_file_path port_file_path})))
Loading

0 comments on commit 70b4f17

Please sign in to comment.