Skip to content

Commit 27cd085

Browse files
authored
clojure-find-ns: add an option to suppress errors (#654)
1 parent 0e62583 commit 27cd085

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Changes
6+
7+
* `clojure-find-ns`: add an option to never raise errors, returning nil instead on unparseable ns forms.
8+
59
## 5.16.1 (2023-06-26)
610

711
### Changes

clojure-mode.el

+18-11
Original file line numberDiff line numberDiff line change
@@ -2142,23 +2142,30 @@ DIRECTION is `forward' or `backward'."
21422142
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
21432143
candidate))
21442144

2145-
(defun clojure-find-ns ()
2146-
"Return the namespace of the current Clojure buffer.
2145+
(defun clojure-find-ns (&optional suppress-errors)
2146+
"Return the namespace of the current Clojure buffer, honor `SUPPRESS-ERRORS'.
21472147
Return the namespace closest to point and above it. If there are
21482148
no namespaces above point, return the first one in the buffer.
21492149
2150+
If `SUPPRESS-ERRORS' is t, errors during ns form parsing will be swallowed,
2151+
and nil will be returned instead of letting this function fail.
2152+
21502153
The results will be cached if `clojure-cache-ns' is set to t."
21512154
(if (and clojure-cache-ns clojure-cached-ns)
21522155
clojure-cached-ns
2153-
(let ((ns (save-excursion
2154-
(save-restriction
2155-
(widen)
2156-
2157-
;; Move to top-level to avoid searching from inside ns
2158-
(ignore-errors (while t (up-list nil t t)))
2159-
2160-
(or (clojure--find-ns-in-direction 'backward)
2161-
(clojure--find-ns-in-direction 'forward))))))
2156+
(let* ((f (lambda (direction)
2157+
(if suppress-errors
2158+
(ignore-errors (clojure--find-ns-in-direction direction))
2159+
(clojure--find-ns-in-direction direction))))
2160+
(ns (save-excursion
2161+
(save-restriction
2162+
(widen)
2163+
2164+
;; Move to top-level to avoid searching from inside ns
2165+
(ignore-errors (while t (up-list nil t t)))
2166+
2167+
(or (funcall f 'backward)
2168+
(funcall f 'forward))))))
21622169
(setq clojure-cached-ns ns)
21632170
ns)))
21642171

test/clojure-mode-sexp-test.el

+16-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,22 @@
167167
(expect (clojure-find-ns) :to-equal expected)
168168
;; After both namespaces
169169
(goto-char (point-max))
170-
(expect (clojure-find-ns) :to-equal expected)))))))
170+
(expect (clojure-find-ns) :to-equal expected))))))
171+
172+
(describe "`suppress-errors' argument"
173+
(let ((clojure-cache-ns nil))
174+
(describe "given a faulty ns form"
175+
(let ((ns-form "(ns )"))
176+
(describe "when the argument is `t'"
177+
(it "causes `clojure-find-ns' to return nil"
178+
(with-clojure-buffer ns-form
179+
(expect (equal nil (clojure-find-ns t))))))
180+
181+
(describe "when the argument is `nil'"
182+
(it "causes `clojure-find-ns' to return raise an error"
183+
(with-clojure-buffer ns-form
184+
(expect (clojure-find-ns nil)
185+
:to-throw 'error)))))))))
171186

172187
(describe "clojure-sexp-starts-until-position"
173188
(it "should return starting points for forms after POINT until POSITION"

0 commit comments

Comments
 (0)