Skip to content

Commit

Permalink
New connection API and jack-in rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Jun 11, 2018
1 parent d71342f commit e5f4e2c
Show file tree
Hide file tree
Showing 16 changed files with 872 additions and 1,360 deletions.
806 changes: 59 additions & 747 deletions cider-client.el

Large diffs are not rendered by default.

402 changes: 402 additions & 0 deletions cider-connection.el

Large diffs are not rendered by default.

174 changes: 13 additions & 161 deletions cider-interaction.el

Large diffs are not rendered by default.

28 changes: 13 additions & 15 deletions cider-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@

(defun cider--modeline-info ()
"Return info for the cider mode modeline.
Info contains the connection type, project name and host:port endpoint."
(if-let* ((current-connection (ignore-errors (cider-current-connection))))
(with-current-buffer current-connection
Expand Down Expand Up @@ -178,7 +177,7 @@ See also the related commands `cider-repl-clear-buffer' and
`cider-repl-clear-output'."
(interactive "P")
(let ((origin-buffer (current-buffer)))
(switch-to-buffer (cider-current-repl-buffer))
(switch-to-buffer (cider-current-connection))
(if clear-repl
(cider-repl-clear-buffer)
(cider-repl-clear-output))
Expand All @@ -194,20 +193,20 @@ See also the related commands `cider-repl-clear-buffer' and
:help "Connects to a REPL that's already running."]
["Replicate connection" cider-replicate-connection
:help "Opens another connection based on a existing one. The new connection uses the same host and port as the base connection."]
["Quit" cider-quit :active cider-connections]
["Restart" cider-restart :active cider-connections]
["Quit" cider-quit :active (cider-connections)]
["Restart" cider-restart :active (cider-connections)]
("ClojureScript"
["Start a Clojure REPL, and a ClojureScript REPL" cider-jack-in-clojurescript
:help "Starts an nREPL server, connects a Clojure REPL to it, and then a ClojureScript REPL.
Configure `cider-cljs-repl-types' to change the ClojureScript REPL to use for your build tool."]
["Connect to a ClojureScript REPL" cider-connect-clojurescript
:help "Connects to a ClojureScript REPL that's already running."]
["Create a ClojureScript REPL from a Clojure REPL" cider-create-sibling-cljs-repl])
["Create a ClojureScript REPL from a Clojure REPL" cider-jack-in-sibling-clojurescript])
"--"
["Connection info" cider-display-connection-info
:active cider-connections]
["Connection info" cider-describe-current-connection
:active (cider-connections)]
["Rotate default connection" cider-rotate-default-connection
:active (cdr cider-connections)]
:active (cdr (cider-connections))]
["Select any CIDER buffer" cider-selector]
"--"
["Configure CIDER" (customize-group 'cider)]
Expand All @@ -220,14 +219,13 @@ Configure `cider-cljs-repl-types' to change the ClojureScript REPL to use for yo
"--"
["Close ancillary buffers" cider-close-ancillary-buffers
:active (seq-remove #'null cider-ancillary-buffers)]
("nREPL" :active cider-connections
["Describe session" cider-describe-nrepl-session]
["Close session" cider-close-nrepl-session]
("nREPL" :active (cider-connections)
["Describe nrepl session" cider-describe-nrepl-session]
["Toggle message logging" nrepl-toggle-message-logging]))
"Menu for CIDER mode.")

(defconst cider-mode-eval-menu
'("CIDER Eval" :visible cider-connections
'("CIDER Eval" :visible (cider-connections)
["Eval top-level sexp" cider-eval-defun-at-point]
["Eval top-level sexp to point" cider-eval-defun-to-point]
["Eval current sexp" cider-eval-sexp-at-point]
Expand Down Expand Up @@ -262,7 +260,7 @@ Configure `cider-cljs-repl-types' to change the ClojureScript REPL to use for yo
"Menu for CIDER mode eval commands.")

(defconst cider-mode-interactions-menu
`("CIDER Interactions" :visible cider-connections
`("CIDER Interactions" :visible (cider-connections)
["Complete symbol" complete-symbol]
"--"
("REPL"
Expand Down Expand Up @@ -355,7 +353,7 @@ Configure `cider-cljs-repl-types' to change the ClojureScript REPL to use for yo
(define-key map (kbd "C-c C-t") 'cider-test-commands-map)
(define-key map (kbd "C-c M-s") #'cider-selector)
(define-key map (kbd "C-c M-r") #'cider-rotate-default-connection)
(define-key map (kbd "C-c M-d") #'cider-display-connection-info)
(define-key map (kbd "C-c M-d") #'cider-describe-current-connection)
(define-key map (kbd "C-c C-=") #'cider-profile-map)
(define-key map (kbd "C-c C-x") #'cider-refresh)
(define-key map (kbd "C-c C-q") #'cider-quit)
Expand Down Expand Up @@ -509,7 +507,7 @@ Search is done with the given LIMIT."
(defun cider--anchored-search-suppressed-forms-internal (limit)
"Helper function for `cider--anchored-search-suppressed-forms`.
LIMIT is the same as the LIMIT in `cider--anchored-search-suppressed-forms`"
(let ((types (cider-project-connections-types)))
(let ((types (seq-uniq (seq-map #'cider--connection-type (cider-connections)))))
(when (= (length types) 1)
(let ((type (car types))
(expr (read (current-buffer)))
Expand Down
94 changes: 51 additions & 43 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
:prefix "cider-repl-"
:group 'cider)

(defvar-local cider-repl-type nil
"The type of this REPL buffer, usually either \"clj\" or \"cljs\".")

(defface cider-repl-prompt-face
'((t (:inherit font-lock-keyword-face)))
"Face for the prompt in the REPL buffer."
Expand Down Expand Up @@ -222,8 +225,7 @@ Currently its only purpose is to facilitate `cider-repl-clear-buffer'.")

(defvar-local cider-repl-ns-cache nil
"A dict holding information about all currently loaded namespaces.
This cache is stored in the connection buffer. Other buffer's access it
via `cider-current-connection'.")
This cache is stored in the connection buffer.")

(defvar cider-mode)
(declare-function cider-refresh-dynamic-font-lock "cider-mode")
Expand All @@ -250,28 +252,31 @@ via `cider-current-connection'.")
(cider-refresh-dynamic-font-lock ns-dict))))))))))

(declare-function cider-default-err-handler "cider-interaction")

(defun cider-repl-create (endpoint)
"Create a REPL buffer and install `cider-repl-mode'.
ENDPOINT is a plist as returned by `nrepl-connect'."
;; Connection might not have been set as yet. Please don't send requests here.
(let* ((reuse-buff (not (eq 'new nrepl-use-this-as-repl-buffer)))
(buff-name (nrepl-make-buffer-name nrepl-repl-buffer-name-template nil
(plist-get endpoint :host)
(plist-get endpoint :port)
reuse-buff)))
;; when reusing, rename the buffer accordingly
(when (and reuse-buff
(not (equal buff-name nrepl-use-this-as-repl-buffer)))
;; uniquify as it might be Nth connection to the same endpoint
(setq buff-name (generate-new-buffer-name buff-name))
(with-current-buffer nrepl-use-this-as-repl-buffer
(rename-buffer buff-name)))
(with-current-buffer (get-buffer-create buff-name)
(defvar-local cider-repl-init-function nil)
(defun cider-repl-create (params)
"Create new repl buffer.
PARAMS is a plist which contains :repl-type, :host, :port, :project-dir,
:repl-init-function and :session-name. When non-nil, :repl-init-function must be a
function with no arguments which is called after repl creation function
with the repl buffer set as current."
;; Connection might not have been set as yet. Please don't send requests in
;; this function, but use cider--connected-handler instead.
(let ((buffer (or (plist-get params :repl-buffer)
(get-buffer-create (generate-new-buffer-name "*cider-uninitialized-repl*")))))
(with-current-buffer buffer
;; register with sesman session first
(let ((ses-name (or (plist-get params :session-name)
(cider-new-session-name params))))
(sesman-add-object 'CIDER ses-name buffer t))
(unless (derived-mode-p 'cider-repl-mode)
(cider-repl-mode)
(cider-repl-set-type "clj"))
(setq nrepl-err-handler #'cider-default-err-handler)
(cider-repl-mode))
(setq nrepl-err-handler #'cider-default-err-handler
;; used as a new-repl marker in cider-repl-set-type
mode-name nil
;; REPLs start with clj and then "upgrade" to a different type
cider-repl-type "clj"
;; ran at the end of cider--connected-handler
cider-repl-init-function (plist-get params :repl-init-function))
(cider-repl-reset-markers)
(add-hook 'nrepl-response-handler-functions #'cider-repl--state-handler nil 'local)
(add-hook 'nrepl-connected-hook 'cider--connected-handler nil 'local)
Expand Down Expand Up @@ -695,7 +700,7 @@ If BOL is non-nil insert at the beginning of line. Run

(defun cider-repl--emit-interactive-output (string face)
"Emit STRING as interactive output using FACE."
(with-current-buffer (cider-current-repl-buffer)
(with-current-buffer (cider-current-connection)
(let ((pos (cider-repl--end-of-line-before-input-start))
(string (replace-regexp-in-string "\n\\'" "" string)))
(cider-repl--emit-output-at-pos (current-buffer) string face pos t))))
Expand Down Expand Up @@ -1160,12 +1165,9 @@ With a prefix argument CLEAR-REPL it will clear the entire REPL buffer instead."

(defun cider-repl-set-ns (ns)
"Switch the namespace of the REPL buffer to NS.
If called from a cljc buffer act on both the Clojure and
ClojureScript REPL if there are more than one REPL present.
If invoked in a REPL buffer the command will prompt for the name of the
namespace to switch to."
If called from a cljc buffer act on both the Clojure and ClojureScript REPL
if there are more than one REPL present. If invoked in a REPL buffer the
command will prompt for the name of the namespace to switch to."
(interactive (list (if (or (derived-mode-p 'cider-repl-mode)
(null (cider-ns-form)))
(completing-read "Switch to namespace: "
Expand All @@ -1180,16 +1182,17 @@ namespace to switch to."
:both))

(defun cider-repl-set-type (&optional type)
"Set REPL TYPE to \"clj\" or \"cljs\"."
(interactive)
"Set REPL TYPE to \"clj\" or \"cljs\".
Assume that the current buffer is a REPL."
(let ((type (or type (completing-read
(format "Set REPL type (currently `%s') to: "
cider-repl-type)
'("clj" "cljs")))))
(setq cider-repl-type type)
(if (equal type "cljs")
(setq mode-name "REPL[cljs]")
(setq mode-name "REPL[clj]"))))
(when (or (not (equal cider-repl-type type))
(null mode-name))
(setq cider-repl-type type)
(setq mode-name (format "REPL[%s]" type))
(rename-buffer (nrepl-repl-buffer-name)))))


;;; Location References
Expand Down Expand Up @@ -1527,8 +1530,6 @@ constructs."
"Add a REPL shortcut command, defined by NAME and HANDLER."
(puthash name handler cider-repl-shortcuts))

(declare-function cider-restart "cider-interaction")
(declare-function cider-quit "cider-interaction")
(declare-function cider-toggle-trace-ns "cider-interaction")
(declare-function cider-undef "cider-interaction")
(declare-function cider-browse-ns "cider-browse-ns")
Expand Down Expand Up @@ -1560,7 +1561,7 @@ constructs."
(cider-repl-add-shortcut "test-project-with-filters" (lambda () (interactive) (cider-test-run-project-tests 'prompt-for-filters)))
(cider-repl-add-shortcut "test-report" #'cider-test-show-report)
(cider-repl-add-shortcut "run" #'cider-run)
(cider-repl-add-shortcut "conn-info" #'cider-display-connection-info)
(cider-repl-add-shortcut "conn-info" #'cider-describe-current-connection)
(cider-repl-add-shortcut "conn-rotate" #'cider-rotate-default-connection)
(cider-repl-add-shortcut "hasta la vista" #'cider-quit)
(cider-repl-add-shortcut "adios" #'cider-quit)
Expand Down Expand Up @@ -1618,7 +1619,6 @@ constructs."
(declare-function cider-toggle-trace-ns "cider-interaction")
(declare-function cider-toggle-trace-var "cider-interaction")
(declare-function cider-find-resource "cider-interaction")
(declare-function cider-restart "cider-interaction")
(declare-function cider-find-ns "cider-interaction")
(declare-function cider-find-keyword "cider-interaction")
(declare-function cider-switch-to-last-clojure-buffer "cider-mode")
Expand Down Expand Up @@ -1657,10 +1657,11 @@ constructs."
(define-key map (kbd "C-c C-c") #'cider-interrupt)
(define-key map (kbd "C-c C-m") #'cider-macroexpand-1)
(define-key map (kbd "C-c M-m") #'cider-macroexpand-all)
(define-key map (kbd "C-c C-s") #'sesman-map)
(define-key map (kbd "C-c C-z") #'cider-switch-to-last-clojure-buffer)
(define-key map (kbd "C-c M-o") #'cider-repl-switch-to-other)
(define-key map (kbd "C-c M-s") #'cider-selector)
(define-key map (kbd "C-c M-d") #'cider-display-connection-info)
(define-key map (kbd "C-c M-d") #'cider-describe-current-connection)
(define-key map (kbd "C-c C-q") #'cider-quit)
(define-key map (kbd "C-c M-i") #'cider-inspect)
(define-key map (kbd "C-c M-p") #'cider-repl-history)
Expand All @@ -1670,6 +1671,13 @@ constructs."
(define-key map (kbd "C-x C-e") #'cider-eval-last-sexp)
(define-key map (kbd "C-c C-r") 'clojure-refactor-map)
(define-key map (kbd "C-c C-v") 'cider-eval-commands-map)
(define-key map (kbd "C-c M-j") #'cider-jack-in-clojure)
(define-key map (kbd "C-c M-J") #'cider-jack-in-clojurescript)
(define-key map (kbd "C-c M-c") #'cider-connect-clojure)
(define-key map (kbd "C-c M-C") #'cider-connect-clojurescript)
(define-key map (kbd "C-c M-s") #'cider-connect-sibling-clojure)
(define-key map (kbd "C-c M-S") #'cider-connect-sibling-clojurescript)

(define-key map (string cider-repl-shortcut-dispatch-char) #'cider-repl-handle-shortcut)
(easy-menu-define cider-repl-mode-menu map
"Menu for CIDER's REPL mode"
Expand Down Expand Up @@ -1701,7 +1709,6 @@ constructs."
["Refresh loaded code" cider-refresh]
"--"
["Set REPL ns" cider-repl-set-ns]
["Set REPL type" cider-repl-set-type]
["Toggle pretty printing" cider-repl-toggle-pretty-printing]
["Require REPL utils" cider-repl-require-repl-utils]
"--"
Expand All @@ -1722,7 +1729,7 @@ constructs."
"--"
["Interrupt evaluation" cider-interrupt]
"--"
["Connection info" cider-display-connection-info]
["Connection info" cider-describe-current-connection]
"--"
["Close ancillary buffers" cider-close-ancillary-buffers]
["Quit" cider-quit]
Expand Down Expand Up @@ -1757,6 +1764,7 @@ constructs."
(clojure-mode-variables)
(clojure-font-lock-setup)
(font-lock-add-keywords nil cider--static-font-lock-keywords)
(setq-local sesman-system 'CIDER)
(setq-local font-lock-fontify-region-function
(cider-repl-wrap-fontify-function font-lock-fontify-region-function))
(setq-local font-lock-unfontify-region-function
Expand Down
4 changes: 2 additions & 2 deletions cider-resolve.el
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@

(defun cider-resolve--get-in (&rest keys)
"Return (nrepl-dict-get-in cider-repl-ns-cache KEYS)."
(when cider-connections
(with-current-buffer (cider-current-connection)
(when-let* ((conn (cider-current-connection)))
(with-current-buffer conn
(nrepl-dict-get-in cider-repl-ns-cache keys))))

(defun cider-resolve-alias (ns alias)
Expand Down
6 changes: 1 addition & 5 deletions cider-scratch.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
'("Clojure Interaction"
(["Eval and print last sexp" #'cider-eval-print-last-sexp]
"--"
["Reset" #'cider-scratch-reset]
"--"
["Set buffer connection" #'cider-assoc-buffer-with-connection]
["Toggle buffer connection" #'cider-toggle-buffer-connection]
["Reset buffer connection" #'cider-clear-buffer-local-connection])))
["Reset" #'cider-scratch-reset])))
map))

(defconst cider-scratch-buffer-name "*cider-scratch*")
Expand Down
2 changes: 1 addition & 1 deletion cider-selector.el
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ is chosen. The returned buffer is selected with

(def-cider-selector-method ?r
"Current REPL buffer."
(cider-current-repl-buffer))
(cider-current-connection))

(def-cider-selector-method ?n
"Connections browser buffer."
Expand Down
2 changes: 1 addition & 1 deletion cider-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ through a stack of help buffers. Variables `help-back-label' and
"Press <C-u C-u \\[cider-inspect]> to read Clojure code from the minibuffer and inspect its result."
"Press <\\[cider-refresh]> to reload modified and unloaded namespaces."
"You can define Clojure functions to be called before and after `cider-refresh' (see `cider-refresh-before-fn' and `cider-refresh-after-fn'."
"Press <\\[cider-display-connection-info]> to view information about the connection."
"Press <\\[cider-describe-current-connection]> to view information about the connection."
"Press <\\[cider-undef]> to undefine a symbol in the current namespace."
"Press <\\[cider-interrupt]> to interrupt an ongoing evaluation."
"Use <M-x customize-group RET cider RET> to see every possible setting you can customize."
Expand Down
Loading

0 comments on commit e5f4e2c

Please sign in to comment.