summaryrefslogtreecommitdiff
path: root/cider-common.el
diff options
context:
space:
mode:
authorBozhidar Batsov <bozhidar@batsov.com>2016-03-27 09:57:10 +0300
committerBozhidar Batsov <bozhidar@batsov.com>2016-03-27 09:57:10 +0300
commitb8ce6f736ea41bdf3f828a1e6138dfa2ebb5726b (patch)
treed88de769a5bb64c89ec8dfabfce112ee03cdb945 /cider-common.el
parentb7f363ce1a3c320a53f260048bd1547fe6aebd40 (diff)
Move cider-expected-ns to cider-common.el
Diffstat (limited to 'cider-common.el')
-rw-r--r--cider-common.el26
1 files changed, 26 insertions, 0 deletions
diff --git a/cider-common.el b/cider-common.el
index 7bd8de51..b03b4138 100644
--- a/cider-common.el
+++ b/cider-common.el
@@ -224,5 +224,31 @@ existing file ending with URL has been found."
(16 t) ; empty empty
(_ nil))))
+(defun cider-expected-ns (&optional path)
+ "Return the namespace string matching PATH, or nil if not found.
+
+PATH is expected to be an absolute file path.
+If PATH is nil, use the path to the file backing the current buffer.
+
+The command falls back to `clojure-expected-ns' in the absence of an
+active nREPL connection."
+ (if (cider-connected-p)
+ (let* ((path (or path (file-truename (buffer-file-name))))
+ (relpath (thread-last (cider-sync-request:classpath)
+ (seq-map
+ (lambda (cp)
+ (when (string-prefix-p cp path)
+ (substring path (length cp)))))
+ (seq-filter #'identity)
+ (seq-sort (lambda (a b)
+ (< (length a) (length b))))
+ (car))))
+ (when relpath
+ (thread-last (substring relpath 1) ; remove leading /
+ (file-name-sans-extension)
+ (replace-regexp-in-string "/" ".")
+ (replace-regexp-in-string "_" "-"))))
+ (clojure-expected-ns path)))
+
(provide 'cider-common)
;;; cider-common.el ends here