Skip to content

Commit ca49356

Browse files
vspinubbatsov
authored andcommitted
Improve session and REPL buffer naming
- better abbreviation of directory names - re-create context buffer on every connect instead of `kill-all-local-variables` which could fail
1 parent ce05c1a commit ca49356

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

cider-connection.el

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,24 @@ The following formats can be used in TEMPLATE string:
451451
452452
In case some values are empty, extra separators (: and -) are automatically
453453
removed."
454-
(let* ((dir (or (plist-get params :project-dir)
455-
(clojure-project-dir (cider-current-dir))
456-
default-directory))
454+
(let* ((dir (directory-file-name
455+
(abbreviate-file-name
456+
(or (plist-get params :project-dir)
457+
(clojure-project-dir (cider-current-dir))
458+
default-directory))))
457459
(short-proj (file-name-nondirectory (directory-file-name dir)))
458460
(parent-dir (ignore-errors
459-
(thread-first dir directory-file-name file-name-directory
461+
(thread-first dir file-name-directory
460462
directory-file-name file-name-nondirectory
461463
file-name-as-directory)))
462464
(long-proj (format "%s%s" (or parent-dir "") short-proj))
465+
;; use `dir` if it is shorter than `long-proj` or `short-proj`
466+
(short-proj (if (>= (length short-proj) (length dir))
467+
dir
468+
short-proj))
469+
(long-proj (if (>= (length long-proj) (length dir))
470+
dir
471+
long-proj))
463472
(port (or (plist-get params :port) ""))
464473
(host (or (plist-get params :host) "localhost"))
465474
(remote-host (if (member host '("localhost" "127.0.0.1"))
@@ -562,6 +571,7 @@ function with the repl buffer set as current."
562571
(setq nrepl-err-handler #'cider-default-err-handler
563572
;; used as a new-repl marker in cider-set-repl-type
564573
mode-name nil
574+
nrepl-project-dir (plist-get params :project-dir)
565575
;; REPLs start with clj and then "upgrade" to a different type
566576
cider-repl-type "clj"
567577
;; ran at the end of cider--connected-handler

cider.el

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,19 +1073,23 @@ non-nil, don't start if ClojureScript requirements are not met."
10731073
;; If proj-dir is not a parent of default-directory, transfer all local
10741074
;; variables and hack dir-local variables into a temporary buffer and keep
10751075
;; that buffer within `params` for the later use by other --update-
1076-
;; functions.
1077-
(with-current-buffer (get-buffer-create " *cider-context-buffer*")
1078-
(kill-all-local-variables)
1079-
(dolist (pair (buffer-local-variables orig-buffer))
1080-
(pcase pair
1081-
(`(,name . ,value) ;ignore unbound variables
1082-
(ignore-errors (set (make-local-variable name) value))))
1083-
(setq-local buffer-file-name nil))
1084-
(let ((default-directory proj-dir))
1085-
(hack-dir-local-variables-non-file-buffer)
1086-
(thread-first params
1087-
(plist-put :project-dir proj-dir)
1088-
(plist-put :--context-buffer (current-buffer))))))))
1076+
;; functions. The context buffer should not be used outside of the param
1077+
;; initialization pipeline. Therefore, we don't bother with making it
1078+
;; unique or killing it anywhere.
1079+
(let ((context-buf-name " *cider-context-buffer*"))
1080+
(when (get-buffer context-buf-name)
1081+
(kill-buffer context-buf-name))
1082+
(with-current-buffer (get-buffer-create context-buf-name)
1083+
(dolist (pair (buffer-local-variables orig-buffer))
1084+
(pcase pair
1085+
(`(,name . ,value) ;ignore unbound variables
1086+
(ignore-errors (set (make-local-variable name) value))))
1087+
(setq-local buffer-file-name nil))
1088+
(let ((default-directory proj-dir))
1089+
(hack-dir-local-variables-non-file-buffer)
1090+
(thread-first params
1091+
(plist-put :project-dir proj-dir)
1092+
(plist-put :--context-buffer (current-buffer)))))))))
10891093

10901094
(defun cider--update-cljs-type (params)
10911095
"Update :cljs-repl-type in PARAMS."

test/cider-connection-tests.el

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,17 @@
307307
(expect (buffer-live-p b) :not :to-be-truthy)
308308
(expect (cider-repls) :to-equal (list a))
309309
(sesman-unregister 'CIDER session))))))
310+
311+
(describe "cider-format-connection-params"
312+
(describe "correctly abbreviates short directory names"
313+
(expect (cider-format-connection-params "%J" '(:project-dir "~"))
314+
:to-equal "~")
315+
(expect (cider-format-connection-params "%j" '(:project-dir "~"))
316+
:to-equal "~")
317+
(expect (cider-format-connection-params "%J" '(:project-dir "~/"))
318+
:to-equal "~")
319+
(expect (cider-format-connection-params "%J" '(:project-dir "/"))
320+
:to-equal "/")
321+
(expect (cider-format-connection-params "%J" '(:project-dir "/etc/"))
322+
:to-equal "/etc")))
323+

0 commit comments

Comments
 (0)