Skip to content

Commit 92d2c3e

Browse files
cichlibbatsov
authored andcommitted
Fix file/directory handling in cider-expected-ns
* Use `file-truename` to relax the need for the given path to be absolute * Only consider actual directories on the classpath * Use `file-in-directory-p` and `file-relative-name` rather than `string-prefix-p` and `substring`
1 parent 97b56cc commit 92d2c3e

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Fix values for `cider-preferred-build-tool` variable.
1111
* Fix value and safe property for `cider-allow-jack-in-without-project` variable.
1212
* `cider-ns-save-files-on-refresh` will now save any modified buffers visiting files on the classpath, rather than just in the current project.
13+
* `cider-expected-ns` no longer requires an absolute path as its argument, and now internally handles paths canonically and consistently.
1314

1415
## 0.20.0 (2019-01-14)
1516

@@ -41,7 +42,6 @@
4142

4243
### Bug fixes
4344

44-
* `cider-expected-ns` no longer requires an absolute path as its argument, and now internally handles paths canonically and consistently.
4545
* [#2474](https://github.com/clojure-emacs/cider/issues/2474): Fix incorrect detection of output and out-of-order printing.
4646
* [#2514](https://github.com/clojure-emacs/cider/issues/2514): Don't auto-jump to warnings when `cider-auto-jump-to-error` is set to 'errors-only.
4747
* [#2453](https://github.com/clojure-emacs/cider/issues/2453): Make it possible to debug deftype methods by direct insertion of #dbg and #break readers into the deftype methods.

cider-client.el

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,22 @@ will return nil instead of \"user\"."
114114

115115
(defun cider-expected-ns (&optional path)
116116
"Return the namespace string matching PATH, or nil if not found.
117-
PATH is expected to be an absolute file path. If PATH is nil, use the path
118-
to the file backing the current buffer. The command falls back to
119-
`clojure-expected-ns' in the absence of an active nREPL connection."
117+
If PATH is nil, use the path to the file backing the current buffer. The
118+
command falls back to `clojure-expected-ns' in the absence of an active
119+
nREPL connection."
120120
(if (cider-connected-p)
121-
(let* ((path (or path (file-truename (buffer-file-name))))
121+
(let* ((path (file-truename (or path buffer-file-name)))
122122
(relpath (thread-last (cider-sync-request:classpath)
123-
(seq-map
124-
(lambda (cp)
125-
(when (string-prefix-p cp path)
126-
(substring path (length cp)))))
123+
(seq-filter #'file-directory-p)
124+
(seq-map (lambda (dir)
125+
(when (file-in-directory-p path dir)
126+
(file-relative-name path dir))))
127127
(seq-filter #'identity)
128128
(seq-sort (lambda (a b)
129129
(< (length a) (length b))))
130130
(car))))
131131
(if relpath
132-
(thread-last (substring relpath 1) ; remove leading /
132+
(thread-last relpath
133133
(file-name-sans-extension)
134134
(replace-regexp-in-string "/" ".")
135135
(replace-regexp-in-string "_" "-"))

test/cider-client-tests.el

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,15 @@
109109
:to-throw 'user-error)))
110110

111111
(describe "cider-expected-ns"
112-
(before-all
112+
(before-each
113113
(spy-on 'cider-connected-p :and-return-value t)
114114
(spy-on 'cider-sync-request:classpath :and-return-value
115-
'("/a" "/b" "/c" "/c/inner" "/base/clj" "/base/clj-dev")))
115+
'("/a" "/b" "/c" "/c/inner" "/base/clj" "/base/clj-dev"))
116+
(spy-on 'file-directory-p :and-return-value t)
117+
(spy-on 'file-in-directory-p :and-call-fake (lambda (file dir)
118+
(string-prefix-p dir file)))
119+
(spy-on 'file-relative-name :and-call-fake (lambda (file dir)
120+
(substring file (+ 1 (length dir))))))
116121

117122
(it "returns the namespace matching the given string path"
118123
(expect (cider-expected-ns "/a/foo/bar/baz_utils.clj") :to-equal

0 commit comments

Comments
 (0)