Skip to content

Commit

Permalink
nrepl: change behaviour of :match to be just a dry run of :join
Browse files Browse the repository at this point in the history
Also baked some other minor improvements into this commit.
  • Loading branch information
darwin committed Oct 12, 2016
1 parent 1e99f3d commit 90a4f69
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 70 deletions.
49 changes: 23 additions & 26 deletions src/nrepl/dirac/nrepl/controls.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,33 @@
; -- (dirac! :join) ---------------------------------------------------------------------------------------------------------

(defn announce-join! [& _]
(with-coallesced-output
(println (messages/make-after-join-msg))
(dirac! :match) ; this should give user immediate feedback about newly matched sessions
(println (messages/make-cljs-quit-msg)))) ; triggers Cursive switching to CLJS REPL mode
(println (messages/make-after-join-msg))
(dirac! :status) ; this should give user immediate feedback about newly joined session
(println (messages/make-cljs-quit-msg))) ; triggers Cursive switching to CLJS REPL mode

(defmethod dirac! :join [_ & [matcher]]
(defmethod dirac! :join [_ & [input]]
(with-coallesced-output
(let [session (sessions/get-current-session)]
(let [session (sessions/get-current-session)
matcher-description (sessions/make-matcher-description-pair input)]
(cond
(sessions/dirac-session? session) (error-println (messages/make-cannot-join-dirac-session-msg))
(nil? matcher) (announce-join! (sessions/join-session-with-most-recent-matcher! session))
(integer? matcher) (announce-join! (sessions/join-session-with-integer-matcher! session matcher))
(string? matcher) (announce-join! (sessions/join-session-with-substr-matcher! session matcher))
(instance? Pattern matcher) (announce-join! (sessions/join-session-with-regex-matcher! session matcher))
:else (error-println (messages/make-invalid-matcher-msg matcher)))))
(some? matcher-description) (announce-join! (apply sessions/join-session! session matcher-description))
:else (error-println (messages/make-invalid-session-matching-msg input)))))
::no-result)

; -- (dirac! :match) --------------------------------------------------------------------------------------------------------

(defmethod dirac! :match [_ & [input]]
(with-coallesced-output
(let [session (sessions/get-current-session)
matcher-description (sessions/make-matcher-description-pair input)]
(cond
(nil? matcher-description) (error-println (messages/make-invalid-session-matching-msg input))
:else (let [[matcher-fn description] matcher-description
tags (sessions/list-matching-sessions-tags matcher-fn)]
(if (empty? tags)
(println (messages/make-no-matching-dirac-sessions-msg description))
(println (messages/make-list-matching-dirac-sessions-msg description tags)))))))
::no-result)

; -- (dirac! :disjoin) ------------------------------------------------------------------------------------------------------
Expand All @@ -136,21 +148,6 @@
(println (messages/make-session-disjoined-msg))))))
::no-result)

; -- (dirac! :match) --------------------------------------------------------------------------------------------------------

(defmethod dirac! :match [_ & _]
(with-coallesced-output
(let [session (sessions/get-current-session)]
(cond
(sessions/dirac-session? session) (error-println (messages/make-cannot-join-dirac-session-msg))
(not (sessions/joined-session? session)) (error-println (messages/make-cannot-match-clojure-session-msg))
:else (let [description (sessions/get-target-session-info session)
tags (sessions/list-matching-sessions-tags session)]
(if (empty? tags)
(println (messages/make-no-matching-dirac-sessions-msg description))
(println (messages/make-list-matching-dirac-sessions-msg description tags)))))))
::no-result)

; -- (dirac! :switch) -------------------------------------------------------------------------------------------------------

(defn validate-selected-compiler [user-input]
Expand Down
16 changes: 6 additions & 10 deletions src/nrepl/dirac/nrepl/messages.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
(str "No such command '" command "'.\n"
"Execute `(dirac! :help)` for a list of available commands."))

(defn ^:dynamic make-invalid-matcher-msg [matcher]
(str "Invalid matching strategy provided. It must be either a number, a string, a regex or omitted.\n"
"Provided matching strategy '" matcher "' is of type " (type matcher)))
(defn ^:dynamic make-invalid-session-matching-msg [input]
(str "Invalid session matching strategy provided. It must be either an integer, a string, a regex or omitted.\n"
"Provided matching strategy '" input "' is of type " (type input) "."))

(defn ^:dynamic make-cannot-disjoin-dirac-session-msg []
(str "Your session is a Dirac session. Cannot disjoin this type of session."))
Expand All @@ -24,16 +24,13 @@
(defn ^:dynamic make-cannot-join-dirac-session-msg []
(str "Your session is a Dirac session. This type of session cannot join any other session."))

(defn ^:dynamic make-cannot-match-clojure-session-msg []
(str "Your session is not joined to Dirac. Use `(dirac! :join)` to join the Dirac first."))

(defn ^:dynamic make-no-matching-dirac-sessions-msg [info]
(str "No connected Dirac session is \"" info "\"."))
(str "Currently there are no Dirac sessions matching the strategy '" info "'."))

(defn ^:dynamic make-list-matching-dirac-sessions-msg [info tags]
(let [printer (fn [i tag]
(str (if (zero? i) " ~> " " ") tag))]
(str "Listing Dirac sessions which are '" info "':\n"
(str "Listing Dirac sessions which are matching the strategy '" info "':\n"
(string/join "\n" (map-indexed printer tags)))))

(defn ^:dynamic make-list-dirac-sessions-msg [tags current-tag marker]
Expand All @@ -50,8 +47,7 @@
"Use `(dirac! :help)` to list all available commands."))

(defn ^:dynamic make-after-join-msg []
(str "Your session joined Dirac (ClojureScript). "
"The specific target Dirac session will be determined dynamically according to current matching strategy."))
(str "Specific target Dirac session will be determined dynamically according to current matching strategy."))

(defn ^:dynamic make-list-compilers-msg [descriptor-ids selected-compiler-id marker]
(assert (and (string? marker) (= 2 (count marker))))
Expand Down
47 changes: 17 additions & 30 deletions src/nrepl/dirac/nrepl/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[dirac.nrepl.debug :as debug]
[clojure.tools.nrepl.middleware.interruptible-eval :as nrepl-ieval]
[dirac.backport.string :as backport-string]
[clojure.string :as string]))
[clojure.string :as string])
(:import (java.util.regex Pattern)))

; -- helpers ----------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -50,12 +51,12 @@

(defn remove-dirac-session-descriptor! [session]
(let [session-id (get-session-id session)]
(log/debug "remove-dirac-session-descriptor!" )
(log/trace (debug/pprint-session session))
(state/register-selected-compiler-for-dead-session! session-id (state/get-session-selected-compiler session))
(if-let [session-descriptor (find-dirac-session-descriptor session)]
(swap! state/session-descriptors #(remove #{session-descriptor} %))
(log/error "attempt to remove unknown session descriptor:\n" (debug/pprint-session session)))))
(log/debug "remove-dirac-session-descriptor!")
(log/trace (debug/pprint-session session))
(state/register-selected-compiler-for-dead-session! session-id (state/get-session-selected-compiler session))
(if-let [session-descriptor (find-dirac-session-descriptor session)]
(swap! state/session-descriptors #(remove #{session-descriptor} %))
(log/error "attempt to remove unknown session descriptor:\n" (debug/pprint-session session)))))

(defn find-matching-dirac-session-descriptors [matcher]
(let [descriptors @state/session-descriptors
Expand Down Expand Up @@ -129,10 +130,9 @@
(find-matching-dirac-session-descriptor matcher-fn)
(log/error "find-joined-session-descriptor called on a session without matcher-fn: " (debug/pprint-session session))))

(defn list-matching-sessions-tags [session]
(if-let [matcher-fn (get-joined-session-matcher session)]
(let [decriptors (find-matching-dirac-session-descriptors matcher-fn)]
(get-dirac-session-descriptors-tags decriptors))))
(defn list-matching-sessions-tags [matcher-fn]
(let [decriptors (find-matching-dirac-session-descriptors matcher-fn)]
(get-dirac-session-descriptors-tags decriptors)))

; -- session matchers -------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -166,25 +166,12 @@
(fn [_ index _]
(= index matching-index))))

(defn join-session-with-most-recent-matcher! [session]
(join-session! session
(make-most-recent-matcher)
(make-most-recent-matcher-description)))

(defn join-session-with-substr-matcher! [session substring]
(join-session! session
(make-substr-matcher substring)
(make-substr-matcher-description substring)))

(defn join-session-with-regex-matcher! [session re]
(join-session! session
(make-regex-matcher re)
(make-regex-matcher-description re)))

(defn join-session-with-integer-matcher! [session number]
(join-session! session
(make-number-matcher number)
(make-number-matcher-description number)))
(defn make-matcher-description-pair [matcher]
(cond
(nil? matcher) [(make-most-recent-matcher) (make-most-recent-matcher-description)]
(integer? matcher) [(make-number-matcher matcher) (make-number-matcher-description matcher)]
(string? matcher) [(make-substr-matcher matcher) (make-substr-matcher-description matcher)]
(instance? Pattern matcher) [(make-regex-matcher matcher) (make-regex-matcher-description matcher)]))

(defn get-target-session [session]
(if-let [target-session-descriptor (find-target-dirac-session-descriptor session)]
Expand Down
11 changes: 7 additions & 4 deletions src/nrepl/dirac/nrepl/usage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@
"Future eval requests will be executed in the context of your original Clojure session."])

(def ^:dynamic match-usage
["List matching Dirac sessions for this session (according to current matching strategy set by :join)."
["List Dirac sessions matching provided input."
""
" 1. `(dirac! :match)`"
" 1. `(dirac! :match <string>)`"
" 2. `(dirac! :match <integer>)`"
" 3. `(dirac! :match <regexp>)`"
" 4. `(dirac! :match)`"
""
"This command is available for testing purposes - for fine-tuning your matching substring or regexp."
"The first session(*) in the list would be used as the target Dirac session for incoming evaluation requests."])
"This command is available for testing purposes. It can be used for fine-tuning your session matching substring or regexp."
"The command has the same signature as `(dirac! :join ...)`. Please read `(dirac! :help :join)` for details about matching."])

(def ^:dynamic switch-usage
["Switch to another ClojureScript compiler matching provided input."
Expand Down

0 comments on commit 90a4f69

Please sign in to comment.