Skip to content

Commit

Permalink
New connection API and user level connection commands
Browse files Browse the repository at this point in the history
  New variables:

      cider-connection-alist, cider-connection-name, cider-connected-directories

  New connection API functions:

      cider-delete-connection-repl, cider-add-connection-repl,
      cider-get-connection, cider-project-connections-repls,
      cider-current-connection-repls, cider-current-connection-repl

  New commands for connection association:

      cider-assoc-buffer-with-connection
      cider-assoc-directory-with-connection
      cider-assoc-project-with-connection
      cider-assoc-with-connection

  New semantics:

      cider-assoc-project-with-connection, cider-connections,
      cider-current-connection, cider-project-connections

  Removed:

      cider-request-dispatch, cider-connections,
      cider-find-connection-buffer-for-project-directory,
      cider-toggle-buffer-connection, cider-clear-buffer-local-connection,
      cider-toggle-request-dispatch, cider-current-connection,
      cider--in-connection-buffer-p, cider-default-connection,
      cider-rotate-default-connection cider--guess-cljs-connection,
      cider--has-warned-about-bad-repl-type, cider-map-connections,
      cider-connections-make-default, cider--connections-make-default,
      nrepl-use-this-as-repl-buffer

  Work, but need to be re-factored/adjusted:

      cider-change-buffers-designation
      cider-connection-browser
  • Loading branch information
vspinu committed Aug 4, 2017
1 parent 71f737d commit a1184b2
Show file tree
Hide file tree
Showing 20 changed files with 805 additions and 946 deletions.
698 changes: 321 additions & 377 deletions cider-client.el

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cider-debug.el
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ In order to work properly, this mode must be activated by
;; cider-nrepl has a chance to send the next message, and so that the user
;; doesn't accidentally hit `n' between two messages (thus editing the code).
(when-let ((proc (unless nrepl-ongoing-sync-request
(get-buffer-process (cider-current-connection)))))
(get-buffer-process (cider-current-connection-repl)))))
(accept-process-output proc 1))
(unless cider--debug-mode
(setq buffer-read-only nil)
Expand Down
225 changes: 82 additions & 143 deletions cider-interaction.el

Large diffs are not rendered by default.

45 changes: 31 additions & 14 deletions cider-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"Return info for the `cider-mode' modeline.
Info contains project name and host:port endpoint."
(if-let ((current-connection (ignore-errors (cider-current-connection))))
(if-let ((current-connection (ignore-errors (cider-current-connection-repl))))
(with-current-buffer current-connection
(concat
cider-repl-type
Expand Down Expand Up @@ -137,8 +137,9 @@ the buffer should appear.
With a prefix arg SET-NAMESPACE sets the namespace in the REPL buffer to that
of the namespace in the Clojure source buffer."
(interactive "P")
(let* ((connections (cider-connections))
(buffer (seq-find (lambda (b) (member b connections))
(cider-ensure-connected)
(let* ((repls (cider-current-connection-repls))
(buffer (seq-find (lambda (b) (member b repls))
(buffer-list))))
(cider--switch-to-repl-buffer buffer set-namespace)))

Expand Down Expand Up @@ -172,7 +173,7 @@ With a prefix argument CLEAR-REPL the command clears the entire REPL buffer.
Returns to the buffer in which the command was invoked."
(interactive "P")
(let ((origin-buffer (current-buffer)))
(switch-to-buffer (cider-current-repl-buffer))
(switch-to-buffer (cider-current-connection-repls))
(if clear-repl
(cider-repl-clear-buffer)
(cider-repl-clear-output))
Expand All @@ -186,8 +187,8 @@ Returns to the buffer in which the command was invoked."
:help "Starts an nREPL server (with lein, boot, or maven) and connects a REPL to it."]
["Connect to a REPL" cider-connect
:help "Connects to a REPL that's already running."]
["Quit" cider-quit :active cider-connections]
["Restart" cider-restart :active cider-connections]
["Quit" cider-quit :active cider-connection-alist]
["Restart" cider-restart :active cider-connection-alist]
("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.
Expand All @@ -198,9 +199,11 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
["Form for launching a ClojureScript REPL via Gradle" (customize-variable 'cider-cljs-gradle-repl)])
"--"
["Connection info" cider-display-connection-info
:active cider-connections]
["Rotate default connection" cider-rotate-default-connection
:active (cdr cider-connections)]
:active cider-connection-alist]
("Connection assoc" :active cider-connection-alist
["Assoc buffer" cider-assoc-buffer-with-connection]
["Assoc directory" cider-assoc-directory-with-connection]
["Assoc project" cider-assoc-project-with-connection])
["Select any CIDER buffer" cider-selector]
"--"
["Configure CIDER" (customize-group 'cider)]
Expand All @@ -213,14 +216,14 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
"--"
["Close ancillary buffers" cider-close-ancillary-buffers
:active (seq-remove #'null cider-ancillary-buffers)]
("nREPL" :active cider-connections
("nREPL" :active cider-connection-alist
["Describe session" cider-describe-nrepl-session]
["Close session" cider-close-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-connection-alist
["Eval top-level sexp" cider-eval-defun-at-point]
["Eval current sexp" cider-eval-sexp-at-point]
["Eval last sexp" cider-eval-last-sexp]
Expand All @@ -247,7 +250,7 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
"Menu for CIDER mode eval commands.")

(defconst cider-mode-interactions-menu
`("CIDER Interactions" :visible cider-connections
`("CIDER Interactions" :visible cider-connection-alist
["Complete symbol" complete-symbol]
"--"
("REPL"
Expand Down Expand Up @@ -291,6 +294,19 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
["Flush completion cache" cider-completion-flush-caches]))
"Menu for CIDER interactions.")

(defconst cider-connection-map
(let ((map (define-prefix-command 'cider-connection-map)))
(define-key map (kbd "a") #'cider-assoc-with-connection)
(define-key map (kbd "C-a") #'cider-assoc-with-connection)
(define-key map (kbd "b") #'cider-assoc-buffer-with-connection)
(define-key map (kbd "C-b") #'cider-assoc-buffer-with-connection)
(define-key map (kbd "d") #'cider-assoc-directory-with-connection)
(define-key map (kbd "C-d") #'cider-assoc-directory-with-connection)
(define-key map (kbd "p") #'cider-assoc-project-with-connection)
(define-key map (kbd "C-p") #'cider-assoc-project-with-connection)
(define-key map (kbd "i") #'cider-display-connection-info)
(define-key map (kbd "C-i") #'cider-display-connection-info)))

(defconst cider-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-d") 'cider-doc-map)
Expand Down Expand Up @@ -326,8 +342,8 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
(define-key map (kbd "C-c C-b") #'cider-interrupt)
(define-key map (kbd "C-c ,") 'cider-test-commands-map)
(define-key map (kbd "C-c C-t") 'cider-test-commands-map)
(define-key map (kbd "C-c C-a") 'cider-connection-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 C-x") #'cider-refresh)
(define-key map (kbd "C-c C-q") #'cider-quit)
Expand Down Expand Up @@ -834,7 +850,8 @@ property."
(add-to-list 'completion-at-point-functions
#'cider-complete-at-point)
(font-lock-add-keywords nil cider--static-font-lock-keywords)
(cider-refresh-dynamic-font-lock)
(when (cider-current-connection)
(cider-refresh-dynamic-font-lock))
(font-lock-add-keywords nil cider--reader-conditionals-font-lock-keywords)
;; `font-lock-mode' might get enabled after `cider-mode'.
(add-hook 'font-lock-mode-hook #'cider-refresh-dynamic-font-lock nil 'local)
Expand Down
47 changes: 24 additions & 23 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,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'.")
via `cider-current-connection-repl'.")

(defvar cider-mode)
(declare-function cider-refresh-dynamic-font-lock "cider-mode")
Expand Down Expand Up @@ -228,31 +228,37 @@ via `cider-current-connection'.")

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

(defun cider-repl-create (endpoint)
(defun cider-repl-create (endpoint repl-buffer connection-name)
"Create a REPL buffer and install `cider-repl-mode'.
ENDPOINT is a plist as returned by `nrepl-connect'."
ENDPOINT is a plist as returned by `nrepl-connect'. REPL-BUFFER is a
placeholder buffer to be used as REPL. CONNECTION-NAME is the connection
of which this REPL is part of."
;; 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
(let* ((dup-ok (and repl-buffer
(not (eq 'new repl-buffer))))
(buff-name (nrepl-make-buffer-name nrepl-repl-buffer-name-template
connection-name
(plist-get endpoint :host)
(plist-get endpoint :port)
reuse-buff)))
dup-ok)))
;; when reusing, rename the buffer accordingly
(when (and reuse-buff
(not (equal buff-name nrepl-use-this-as-repl-buffer)))
(when (and dup-ok
(not (equal buff-name (buffer-name 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
(with-current-buffer repl-buffer
(rename-buffer buff-name)))
(with-current-buffer (get-buffer-create buff-name)
(unless (derived-mode-p 'cider-repl-mode)
(cider-repl-mode)
(setq cider-repl-type "clj"))
(setq nrepl-err-handler #'cider-default-err-handler)
(setq cider-connection-name connection-name
nrepl-err-handler #'cider-default-err-handler)
(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)
(add-hook 'nrepl-disconnected-hook 'cider--disconnected-handler nil 'local)
(cider-add-connection-repl connection-name (current-buffer))
(current-buffer))))

(defun cider-repl-require-repl-utils ()
Expand Down Expand Up @@ -631,7 +637,7 @@ If BOL is non-nil insert at the beginning of line."

(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-repl)
(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 @@ -871,7 +877,7 @@ text property `cider-old-input'."
(defun cider-repl-switch-to-other ()
"Switch between the Clojure and ClojureScript REPLs for the current project."
(interactive)
(if-let (other-connection (cider-other-connection))
(if-let (other-connection (cider-other-repl))
(switch-to-buffer other-connection)
(message "There's no other REPL for the current project")))

Expand Down Expand Up @@ -968,24 +974,20 @@ 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 or cljx 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."
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: "
(cider-sync-request:ns-list))
(cider-current-ns))))
(when (or (not ns) (equal ns ""))
(user-error "No namespace selected"))
(cider-map-connections
(lambda (connection)
(cider-nrepl-request:eval (format "(in-ns '%s)" ns)
(cider-repl-switch-ns-handler connection)))
:both))
(dolist (repl (cider-current-connection-repls))
(cider-nrepl-request:eval (format "(in-ns '%s)" ns)
(cider-repl-switch-ns-handler repl))))


;;; Location References
Expand Down Expand Up @@ -1341,7 +1343,6 @@ constructs."
(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-rotate" #'cider-rotate-default-connection)
(cider-repl-add-shortcut "hasta la vista" #'cider-quit)
(cider-repl-add-shortcut "adios" #'cider-quit)
(cider-repl-add-shortcut "sayonara" #'cider-quit)
Expand Down
6 changes: 3 additions & 3 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 ((buff (cider-current-connection-repl)))
(with-current-buffer buff
(nrepl-dict-get-in cider-repl-ns-cache keys))))

(defun cider-resolve-alias (ns alias)
Expand Down Expand Up @@ -105,7 +105,7 @@ Return nil only if VAR cannot be resolved."
"Return a dict of the core namespace for current connection.
This will be clojure.core or cljs.core depending on `cider-repl-type'."
(when (cider-connected-p)
(with-current-buffer (cider-current-connection)
(with-current-buffer (cider-current-connection-repl)
(cider-resolve--get-in (if (equal cider-repl-type "cljs")
"cljs.core"
"clojure.core")))))
Expand Down
4 changes: 1 addition & 3 deletions cider-scratch.el
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
"--"
["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])))
["Set buffer connection" #'cider-assoc-buffer-with-connection])))
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-repl))

(def-cider-selector-method ?n
"Connections browser buffer."
Expand Down
88 changes: 43 additions & 45 deletions cider-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -584,51 +584,49 @@ report is optionally displayed. When test failures/errors occur, their sources
are highlighted.
If SILENT is non-nil, suppress all messages other then test results."
(cider-test-clear-highlights)
(cider-map-connections
(lambda (conn)
(unless silent
(if (and tests (= (length tests) 1))
;; we generate a different message when running individual tests
(cider-test-echo-running ns (car tests))
(cider-test-echo-running ns)))
(cider-nrepl-send-request
`("op" ,(cond ((stringp ns) "test")
((eq :project ns) "test-all")
((eq :loaded ns) "test-all")
((eq :non-passing ns) "retest"))
"ns" ,(when (stringp ns) ns)
"tests" ,(when (stringp ns) tests)
"load?" ,(when (or (stringp ns)
(eq :project ns))
"true"))
(lambda (response)
(nrepl-dbind-response response (summary results status out err)
(cond ((member "namespace-not-found" status)
(unless silent
(message "No test namespace: %s" (cider-propertize ns 'ns))))
(out (cider-emit-interactive-eval-output out))
(err (cider-emit-interactive-eval-err-output err))
(results
(nrepl-dbind-response summary (error fail)
(setq cider-test-last-summary summary)
(setq cider-test-last-results results)
(cider-test-highlight-problems results)
(cider-test-echo-summary summary results)
(if (or (not (zerop (+ error fail)))
cider-test-show-report-on-success)
(cider-test-render-report
(cider-popup-buffer cider-test-report-buffer
cider-auto-select-test-report-buffer)
summary results)
(when (get-buffer cider-test-report-buffer)
(with-current-buffer cider-test-report-buffer
(let ((inhibit-read-only t))
(erase-buffer)))
(cider-test-render-report
cider-test-report-buffer
summary results))))))))
conn))
:clj))
(dolist (repl (cider-current-connection-repls "clj"))
(unless silent
(if (and tests (= (length tests) 1))
;; we generate a different message when running individual tests
(cider-test-echo-running ns (car tests))
(cider-test-echo-running ns)))
(cider-nrepl-send-request
`("op" ,(cond ((stringp ns) "test")
((eq :project ns) "test-all")
((eq :loaded ns) "test-all")
((eq :non-passing ns) "retest"))
"ns" ,(when (stringp ns) ns)
"tests" ,(when (stringp ns) tests)
"load?" ,(when (or (stringp ns)
(eq :project ns))
"true"))
(lambda (response)
(nrepl-dbind-response response (summary results status out err)
(cond ((member "namespace-not-found" status)
(unless silent
(message "No test namespace: %s" (cider-propertize ns 'ns))))
(out (cider-emit-interactive-eval-output out))
(err (cider-emit-interactive-eval-err-output err))
(results
(nrepl-dbind-response summary (error fail)
(setq cider-test-last-summary summary)
(setq cider-test-last-results results)
(cider-test-highlight-problems results)
(cider-test-echo-summary summary results)
(if (or (not (zerop (+ error fail)))
cider-test-show-report-on-success)
(cider-test-render-report
(cider-popup-buffer cider-test-report-buffer
cider-auto-select-test-report-buffer)
summary results)
(when (get-buffer cider-test-report-buffer)
(with-current-buffer cider-test-report-buffer
(let ((inhibit-read-only t))
(erase-buffer)))
(cider-test-render-report
cider-test-report-buffer
summary results))))))))
repl)))

(defun cider-test-rerun-failed-tests ()
"Rerun failed and erring tests from the last test run."
Expand Down
Loading

0 comments on commit a1184b2

Please sign in to comment.