branch: elpa/cider
commit 05c8203b252fcb69addd058fc05878b1b31ba837
Author: vemv <v...@users.noreply.github.com>
Commit: GitHub <nore...@github.com>

    Always match friendly sessions for special buffers (#3432)
---
 CHANGELOG.md                   |   1 +
 cider-repl.el                  | 116 ++++++++++++++++++++---------------------
 test/cider-connection-tests.el |   6 +++
 3 files changed, 65 insertions(+), 58 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4ee96b9eb..d85eb6e34d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,6 +44,7 @@
 - Preserve the `:cljs-repl-type` more reliably.
 - Improve the presentation of `xref` data.
 - [#3419](https://github.com/clojure-emacs/cider/issues/3419): Also match 
friendly sessions based on the buffer's ns form.
+- Always match friendly sessions for `cider-ancillary-buffers` (like 
`*cider-error*`, `*cider-result*`, etc).
 - `cider-test`: only show diffs for collections.
 - [#3375](https://github.com/clojure-emacs/cider/pull/3375): `cider-test`: 
don't render a newline between expected and actual, most times.
 - Ensure there's a leading `:` when using `cider-clojure-cli-aliases`.
diff --git a/cider-repl.el b/cider-repl.el
index bdbef1684d..dd28e54ee9 100644
--- a/cider-repl.el
+++ b/cider-repl.el
@@ -1750,75 +1750,75 @@ constructs."
                      (mapconcat #'identity (cider-repl--available-shortcuts) 
", "))))
         (error "No command selected")))))
 
-
 (defun cider--sesman-friendly-session-p (session &optional debug)
   "Check if SESSION is a friendly session, DEBUG optionally.
 
 The checking is done as follows:
 
+* Consider if the buffer belongs to `cider-ancillary-buffers`
 * Consider the buffer's filename, strip any Docker/TRAMP details from it
 * Check if that filename belongs to the classpath,
   or to the classpath roots (e.g. the project root dir)
 * As a fallback, check if the buffer's ns form
   matches any of the loaded namespaces."
   (setcdr session (seq-filter #'buffer-live-p (cdr session)))
-  (when-let* ((repl (cadr session))
-              (proc (get-buffer-process repl))
-              (file (file-truename (or (buffer-file-name) default-directory))))
-    ;; With avfs paths look like 
/path/to/.avfs/path/to/some.jar#uzip/path/to/file.clj
-    (when (string-match-p "#uzip" file)
-      (let ((avfs-path (directory-file-name (expand-file-name (or (getenv 
"AVFSBASE")  "~/.avfs/")))))
-        (setq file (replace-regexp-in-string avfs-path "" file t t))))
-    (when-let ((tp (cider-tramp-prefix (current-buffer))))
-      (setq file (string-remove-prefix tp file)))
-    (when (process-live-p proc)
-      (let* ((classpath (or (process-get proc :cached-classpath)
-                            (let ((cp (with-current-buffer repl
-                                        (cider-classpath-entries))))
-                              (process-put proc :cached-classpath cp)
-                              cp)))
-             (ns-list (or (process-get proc :all-namespaces)
-                          (let ((ns-list (with-current-buffer repl
-                                           (cider-sync-request:ns-list))))
-                            (process-put proc :all-namespaces ns-list)
-                            ns-list)))
-             (classpath-roots (or (process-get proc :cached-classpath-roots)
-                                  (let ((cp (thread-last
-                                              classpath
-                                              (seq-filter (lambda (path) (not 
(string-match-p "\\.jar$" path))))
-                                              (mapcar #'file-name-directory)
-                                              (seq-remove  #'null)
-                                              (seq-uniq))))
-                                    (process-put proc :cached-classpath-roots 
cp)
-                                    cp))))
-        (or (seq-find (lambda (path) (string-prefix-p path file))
-                      classpath)
-            (seq-find (lambda (path) (string-prefix-p path file))
-                      classpath-roots)
-            (when-let* ((cider-path-translations 
(cider--all-path-translations))
-                        (translated (cider--translate-path file 'to-nrepl 
:return-all)))
-              (seq-find (lambda (translated-path)
-                          (or (seq-find (lambda (path)
-                                          (string-prefix-p path 
translated-path))
-                                        classpath)
-                              (seq-find (lambda (path)
-                                          (string-prefix-p path 
translated-path))
-                                        classpath-roots)))
-                        translated))
-            (when-let ((ns (condition-case nil
-                               (substring-no-properties (cider-current-ns 
:no-default
-                                                                          ;; 
important - don't query the repl,
-                                                                          ;; 
avoiding a recursive invocation of `cider--sesman-friendly-session-p`:
-                                                                          
:no-repl-check))
-                             (error nil))))
-              ;; if the ns form matches with a ns of all runtime namespaces, 
we can consider the buffer to match
-              ;; (this is a bit lax, but also quite useful)
-              (with-current-buffer repl
-                (or (when cider-repl-ns-cache ;; may be nil on repl startup
-                      (member ns (nrepl-dict-keys cider-repl-ns-cache)))
-                    (member ns ns-list))))
-            (when debug
-              (list file "was not determined to belong to classpath:" 
classpath "or classpath-roots:" classpath-roots)))))))
+  (or (member (buffer-name) cider-ancillary-buffers)
+      (when-let* ((repl (cadr session))
+                  (proc (get-buffer-process repl))
+                  (file (file-truename (or (buffer-file-name) 
default-directory))))
+        ;; With avfs paths look like 
/path/to/.avfs/path/to/some.jar#uzip/path/to/file.clj
+        (when (string-match-p "#uzip" file)
+          (let ((avfs-path (directory-file-name (expand-file-name (or (getenv 
"AVFSBASE")  "~/.avfs/")))))
+            (setq file (replace-regexp-in-string avfs-path "" file t t))))
+        (when-let ((tp (cider-tramp-prefix (current-buffer))))
+          (setq file (string-remove-prefix tp file)))
+        (when (process-live-p proc)
+          (let* ((classpath (or (process-get proc :cached-classpath)
+                                (let ((cp (with-current-buffer repl
+                                            (cider-classpath-entries))))
+                                  (process-put proc :cached-classpath cp)
+                                  cp)))
+                 (ns-list (or (process-get proc :all-namespaces)
+                              (let ((ns-list (with-current-buffer repl
+                                               (cider-sync-request:ns-list))))
+                                (process-put proc :all-namespaces ns-list)
+                                ns-list)))
+                 (classpath-roots (or (process-get proc 
:cached-classpath-roots)
+                                      (let ((cp (thread-last classpath
+                                                             (seq-filter 
(lambda (path) (not (string-match-p "\\.jar$" path))))
+                                                             (mapcar 
#'file-name-directory)
+                                                             (seq-remove  
#'null)
+                                                             (seq-uniq))))
+                                        (process-put proc 
:cached-classpath-roots cp)
+                                        cp))))
+            (or (seq-find (lambda (path) (string-prefix-p path file))
+                          classpath)
+                (seq-find (lambda (path) (string-prefix-p path file))
+                          classpath-roots)
+                (when-let* ((cider-path-translations 
(cider--all-path-translations))
+                            (translated (cider--translate-path file 'to-nrepl 
:return-all)))
+                  (seq-find (lambda (translated-path)
+                              (or (seq-find (lambda (path)
+                                              (string-prefix-p path 
translated-path))
+                                            classpath)
+                                  (seq-find (lambda (path)
+                                              (string-prefix-p path 
translated-path))
+                                            classpath-roots)))
+                            translated))
+                (when-let ((ns (condition-case nil
+                                   (substring-no-properties (cider-current-ns 
:no-default
+                                                                              
;; important - don't query the repl,
+                                                                              
;; avoiding a recursive invocation of `cider--sesman-friendly-session-p`:
+                                                                              
:no-repl-check))
+                                 (error nil))))
+                  ;; if the ns form matches with a ns of all runtime 
namespaces, we can consider the buffer to match
+                  ;; (this is a bit lax, but also quite useful)
+                  (with-current-buffer repl
+                    (or (when cider-repl-ns-cache ;; may be nil on repl startup
+                          (member ns (nrepl-dict-keys cider-repl-ns-cache)))
+                        (member ns ns-list))))
+                (when debug
+                  (list file "was not determined to belong to classpath:" 
classpath "or classpath-roots:" classpath-roots))))))))
 
 (defun cider-debug-sesman-friendly-session-p ()
   "`message's debugging information relative to friendly sessions.
diff --git a/test/cider-connection-tests.el b/test/cider-connection-tests.el
index def21d5ebc..0ee9d73c9c 100644
--- a/test/cider-connection-tests.el
+++ b/test/cider-connection-tests.el
@@ -60,6 +60,12 @@
   (before-each
     (setq sesman-sessions-hashmap (make-hash-table :test #'equal)
           sesman-links-alist nil
+          cider-ancillary-buffers (seq-filter (lambda (s)
+                                                ;; sometimes "*temp*" buffers 
can sneak into cider-ancillary-buffers.
+                                                ;; Those are the artifact of 
some other test, and can break these tests
+                                                ;; by affecting the logic in 
cider--sesman-friendly-session-p.
+                                                (string-prefix-p "*cider" s))
+                                              cider-ancillary-buffers)
           ses-name "a-session"
           ses-name2 "b-session"))
 

Reply via email to