Skip to content

Commit

Permalink
Remove session from nrepl interaction (#1925)
Browse files Browse the repository at this point in the history
Connection and session are inextricably linked. Rather than try to thread these
two in lockstep throughout the whole request life cycle, we let the last step
insert either the nrepl-session or the nrepl-tooling-session.
  • Loading branch information
dpsutton authored and bbatsov committed Jan 23, 2017
1 parent 04e428b commit efe1591
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 101 deletions.
33 changes: 7 additions & 26 deletions cider-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,6 @@ to be appended to the request message."
(cider-eval-spinner-handler connection callback)
callback)
connection
(cider-current-session)
ns line column additional-params)
(cider-spinner-start connection)))

Expand All @@ -690,7 +689,6 @@ If NS is non-nil, include it in the request."
(nrepl-sync-request:eval
input
(cider-current-connection)
(cider-current-session)
ns))

(defcustom cider-pprint-fn 'pprint
Expand Down Expand Up @@ -744,8 +742,8 @@ NS specifies the namespace in which to evaluate the request."
(nrepl-request:eval input
callback
(cider-current-connection)
(cider-current-tooling-session)
ns))
ns nil nil nil t ; tooling
))

(defalias 'cider-current-repl-buffer #'cider-current-connection
"The current REPL buffer.
Expand All @@ -761,8 +759,7 @@ Return the REPL buffer given by `cider-current-connection'.")
(nrepl-request:interrupt
request-id
(cider-interrupt-handler (current-buffer))
(cider-current-connection)
(cider-current-session))))))
(cider-current-connection))))))

(defun cider-current-session ()
"Return the eval nREPL session id of the current connection."
Expand Down Expand Up @@ -855,9 +852,6 @@ loaded.
If CONNECTION is nil, use `cider-current-connection'.
If CALLBACK is nil, use `cider-load-file-handler'."
(cider-nrepl-send-request (list "op" "load-file"
"session" (if connection
(cider-session-for-connection connection)
(cider-current-session))
"file" file-contents
"file-path" file-path
"file-name" file-name)
Expand Down Expand Up @@ -889,7 +883,6 @@ Optional arguments include SEARCH-NS, DOCS-P, PRIVATES-P, CASE-SENSITIVE-P."
(let* ((query (replace-regexp-in-string "[ \t]+" ".+" query))
(response (cider-nrepl-send-sync-request
`("op" "apropos"
"session" ,(cider-current-session)
"ns" ,(cider-current-ns)
"query" ,query
,@(when search-ns `("search-ns" ,search-ns))
Expand All @@ -904,16 +897,14 @@ Optional arguments include SEARCH-NS, DOCS-P, PRIVATES-P, CASE-SENSITIVE-P."
(defun cider-sync-request:classpath ()
"Return a list of classpath entries."
(cider-ensure-op-supported "classpath")
(thread-first (list "op" "classpath"
"session" (cider-current-session))
(thread-first (list "op" "classpath")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "classpath")))

(defun cider-sync-request:complete (str context)
"Return a list of completions for STR using nREPL's \"complete\" op.
CONTEXT represents a completion context for compliment."
(when-let ((dict (thread-first (list "op" "complete"
"session" (cider-current-session)
"ns" (cider-current-ns)
"symbol" str
"context" context)
Expand All @@ -923,7 +914,6 @@ CONTEXT represents a completion context for compliment."
(defun cider-sync-request:info (symbol &optional class member)
"Send \"info\" op with parameters SYMBOL or CLASS and MEMBER."
(let ((var-info (thread-first `("op" "info"
"session" ,(cider-current-session)
"ns" ,(cider-current-ns)
,@(when symbol (list "symbol" symbol))
,@(when class (list "class" class))
Expand All @@ -936,7 +926,6 @@ CONTEXT represents a completion context for compliment."
(defun cider-sync-request:eldoc (symbol &optional class member)
"Send \"eldoc\" op with parameters SYMBOL or CLASS and MEMBER."
(when-let ((eldoc (thread-first `("op" "eldoc"
"session" ,(cider-current-session)
"ns" ,(cider-current-ns)
,@(when symbol (list "symbol" symbol))
,@(when class (list "class" class))
Expand All @@ -949,61 +938,53 @@ CONTEXT represents a completion context for compliment."
(defun cider-sync-request:ns-list ()
"Get a list of the available namespaces."
(thread-first (list "op" "ns-list"
"filter-regexps" cider-filtered-namespaces-regexps
"session" (cider-current-session))
"filter-regexps" cider-filtered-namespaces-regexps)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "ns-list")))

(defun cider-sync-request:ns-vars (ns)
"Get a list of the vars in NS."
(thread-first (list "op" "ns-vars"
"session" (cider-current-session)
"ns" ns)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "ns-vars")))

(defun cider-sync-request:ns-vars-with-meta (ns)
"Get a map of the vars in NS to its metadata information."
(thread-first (list "op" "ns-vars-with-meta"
"session" (cider-current-session)
"ns" ns)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "ns-vars-with-meta")))

(defun cider-sync-request:ns-load-all ()
"Load all project namespaces."
(thread-first (list "op" "ns-load-all"
"session" (cider-current-session))
(thread-first (list "op" "ns-load-all")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "loaded-ns")))

(defun cider-sync-request:resource (name)
"Perform nREPL \"resource\" op with resource name NAME."
(thread-first (list "op" "resource"
"session" (cider-current-session)
"name" name)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "resource-path")))

(defun cider-sync-request:resources-list ()
"Return a list of all resources on the classpath."
(thread-first (list "op" "resources-list"
"session" (cider-current-session))
(thread-first (list "op" "resources-list")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "resources-list")))

(defun cider-sync-request:format-code (code)
"Perform nREPL \"format-code\" op with CODE."
(thread-first (list "op" "format-code"
"session" (cider-current-session)
"code" code)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "formatted-code")))

(defun cider-sync-request:format-edn (edn right-margin)
"Perform \"format-edn\" op with EDN and RIGHT-MARGIN."
(let* ((response (thread-first (list "op" "format-edn"
"session" (cider-current-session)
"edn" edn)
(append (cider--nrepl-pprint-request-plist right-margin))
(cider-nrepl-send-sync-request)))
Expand Down
20 changes: 7 additions & 13 deletions cider-inspector.el
Original file line number Diff line number Diff line change
Expand Up @@ -192,52 +192,46 @@ Current page will be reset to zero."
;; nREPL interactions
(defun cider-sync-request:inspect-pop ()
"Move one level up in the inspector stack."
(thread-first (list "op" "inspect-pop"
"session" (cider-current-session))
(thread-first (list "op" "inspect-pop")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-push (idx)
"Inspect the inside value specified by IDX."
(thread-first (list "op" "inspect-push"
"idx" idx
"session" (cider-current-session))
"idx" idx)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-refresh ()
"Re-render the currently inspected value."
(thread-first (list "op" "inspect-refresh"
"session" (cider-current-session))
(thread-first (list "op" "inspect-refresh")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-next-page ()
"Jump to the next page in paginated collection view."
(thread-first (list "op" "inspect-next-page"
"session" (cider-current-session))
(thread-first (list "op" "inspect-next-page")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-prev-page ()
"Jump to the previous page in paginated collection view."
(thread-first (list "op" "inspect-prev-page"
"session" (cider-current-session))
(thread-first (list "op" "inspect-prev-page")
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-set-page-size (page-size)
"Set the page size in paginated view to PAGE-SIZE."
(thread-first (list "op" "inspect-set-page-size"
"page-size" page-size
"session" (cider-current-session))
"page-size" page-size)
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-expr (expr ns page-size)
"Evaluate EXPR in context of NS and inspect its result.
Set the page size in paginated view to PAGE-SIZE."
(thread-first (append (nrepl--eval-request expr (cider-current-session) ns)
(thread-first (append (nrepl--eval-request expr ns)
(list "inspect" "true"
"page-size" page-size))
(cider-nrepl-send-sync-request)
Expand Down
26 changes: 8 additions & 18 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ into a new error buffer."
(let (causes)
(cider-nrepl-send-request
(append
(list "op" "stacktrace" "session" (cider-current-session))
(list "op" "stacktrace")
(when (cider--pprint-fn)
(list "pprint-fn" (cider--pprint-fn)))
(when cider-stacktrace-print-length
Expand Down Expand Up @@ -1007,8 +1007,7 @@ evaluation command. Honor `cider-auto-jump-to-error'."
(with-current-buffer buffer
(nrepl-request:stdin (concat (read-from-minibuffer "Stdin: ") "\n")
(cider-stdin-handler buffer)
(cider-current-connection)
(cider-current-session))))
(cider-current-connection))))

(defun cider-emit-into-color-buffer (buffer value)
"Emit into color BUFFER the provided VALUE."
Expand Down Expand Up @@ -1375,7 +1374,6 @@ See `cider-mode'."
(defun cider-sync-request:toggle-trace-var (symbol)
"Toggle var tracing for SYMBOL."
(thread-first (list "op" "toggle-trace-var"
"session" (cider-current-session)
"ns" (cider-current-ns)
"sym" symbol)
(cider-nrepl-send-sync-request)))
Expand Down Expand Up @@ -1405,7 +1403,6 @@ opposite of what that option dictates."
(defun cider-sync-request:toggle-trace-ns (ns)
"Toggle namespace tracing for NS."
(thread-first (list "op" "toggle-trace-ns"
"session" (cider-current-session)
"ns" ns)
(cider-nrepl-send-sync-request)))

Expand Down Expand Up @@ -1437,7 +1434,6 @@ Defaults to the current ns. With prefix arg QUERY, prompts for a ns."
(lambda (sym)
(cider-nrepl-send-request
(list "op" "undef"
"session" (cider-current-session)
"ns" (cider-current-ns)
"symbol" sym)
(cider-interactive-eval-handler (current-buffer))))))
Expand Down Expand Up @@ -1682,7 +1678,7 @@ START and END represent the region's boundaries."
(cider-ensure-connected)
(let ((selected-session (completing-read "Describe nREPL session: " (nrepl-sessions (cider-current-connection)))))
(when (and selected-session (not (equal selected-session "")))
(let* ((session-info (nrepl-sync-request:describe (cider-current-connection) selected-session))
(let* ((session-info (nrepl-sync-request:describe (cider-current-connection)))
(ops (nrepl-dict-keys (nrepl-dict-get session-info "ops")))
(session-id (nrepl-dict-get session-info "session"))
(session-type (cond
Expand All @@ -1701,10 +1697,8 @@ START and END represent the region's boundaries."
"Close an nREPL session for the current connection."
(interactive)
(cider-ensure-connected)
(let ((selected-session (completing-read "Close nREPL session: " (nrepl-sessions (cider-current-connection)))))
(when selected-session
(nrepl-sync-request:close (cider-current-connection) selected-session)
(message "Closed nREPL session %s" selected-session))))
(nrepl-sync-request:close (cider-current-connection))
(message "Closed nREPL session"))

;;; quiting
(defun cider--close-buffer (buffer)
Expand All @@ -1716,10 +1710,8 @@ START and END represent the region's boundaries."
(when (or (not nrepl-server-buffer)
;; Sync request will hang if the server is dead.
(process-live-p (get-buffer-process nrepl-server-buffer)))
(when nrepl-session
(nrepl-sync-request:close buffer nrepl-session))
(when nrepl-tooling-session
(nrepl-sync-request:close buffer nrepl-tooling-session)))
(when nrepl-session or nrepl-tooling-session
(nrepl-sync-request:close buffer)))
(when proc (delete-process proc)))))
(kill-buffer buffer)))

Expand Down Expand Up @@ -1816,9 +1808,7 @@ With a prefix argument, prompt for function to run instead of -main."
(cider-ensure-connected)
(let ((name (or function "-main")))
(when-let ((response (cider-nrepl-send-sync-request
(list "op" "ns-list-vars-by-name"
"session" (cider-current-session)
"name" name))))
(list "op" "ns-list-vars-by-name" "name" name))))
(if-let ((vars (split-string (substring (nrepl-dict-get response "var-list") 1 -1))))
(cider-interactive-eval
(if (= (length vars) 1)
Expand Down
5 changes: 2 additions & 3 deletions cider-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ prompt and whether to use a new window. Similar to `cider-find-var'."
(let (causes)
(cider-nrepl-send-request
(append
(list "op" "test-stacktrace" "session" (cider-current-session)
(list "op" "test-stacktrace"
"ns" ns "var" var "index" index)
(when (cider--pprint-fn)
(list "pprint-fn" (cider--pprint-fn)))
Expand Down Expand Up @@ -594,8 +594,7 @@ If SILENT is non-nil, suppress all messages other then test results."
"tests" (when (stringp ns) tests)
"load?" (when (or (stringp ns)
(eq :project ns))
"true")
"session" (cider-current-session))
"true"))
(lambda (response)
(nrepl-dbind-response response (summary results status out err)
(cond ((member "namespace-not-found" status)
Expand Down
1 change: 0 additions & 1 deletion cider.el
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ should be the regular Clojure REPL started by the server process filter."
(cider-nrepl-send-request
(list "op" "eval"
"ns" (cider-current-ns)
"session" nrepl-session
"code" cider-cljs-lein-repl)
(cider-repl-handler (current-buffer)))
(cider--offer-to-open-app-in-browser nrepl-server-buffer))))
Expand Down
Loading

0 comments on commit efe1591

Please sign in to comment.