Skip to content

Commit b89e378

Browse files
committed
Accept an argument instead
1 parent fd6c420 commit b89e378

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Changes
66

7-
* `clojure-find-ns`: never raise errors, returning nil instead on unparseable ns forms.
7+
* `clojure-find-ns`: add an option to never raise errors, returning nil instead on unparseable ns forms.
88

99
## 5.16.1 (2023-06-26)
1010

clojure-mode.el

+33-27
Original file line numberDiff line numberDiff line change
@@ -2127,39 +2127,45 @@ the cached value will be updated automatically."
21272127
(defun clojure--find-ns-in-direction (direction)
21282128
"Return the nearest namespace in a specific DIRECTION.
21292129
DIRECTION is `forward' or `backward'."
2130-
(ignore-errors
2131-
(let ((candidate)
2132-
(fn (if (eq direction 'forward)
2133-
#'search-forward-regexp
2134-
#'search-backward-regexp)))
2135-
(while (and (not candidate)
2136-
(funcall fn clojure-namespace-regexp nil t))
2137-
(let ((end (match-end 0)))
2138-
(save-excursion
2139-
(save-match-data
2140-
(goto-char end)
2141-
(clojure-forward-logical-sexp)
2142-
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
2143-
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
2144-
candidate)))
2145-
2146-
(defun clojure-find-ns ()
2147-
"Return the namespace of the current Clojure buffer.
2130+
(let ((candidate)
2131+
(fn (if (eq direction 'forward)
2132+
#'search-forward-regexp
2133+
#'search-backward-regexp)))
2134+
(while (and (not candidate)
2135+
(funcall fn clojure-namespace-regexp nil t))
2136+
(let ((end (match-end 0)))
2137+
(save-excursion
2138+
(save-match-data
2139+
(goto-char end)
2140+
(clojure-forward-logical-sexp)
2141+
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
2142+
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
2143+
candidate))
2144+
2145+
(defun clojure-find-ns (&optional favor-nil)
2146+
"Return the namespace of the current Clojure buffer, honor `FAVOR-NIL'.
21482147
Return the namespace closest to point and above it. If there are
21492148
no namespaces above point, return the first one in the buffer.
21502149
2150+
If `FAVOR-NIL' is t, errors during ns form parsing will be swallowed,
2151+
and nil will be returned instead of letting this function fail.
2152+
21512153
The results will be cached if `clojure-cache-ns' is set to t."
21522154
(if (and clojure-cache-ns clojure-cached-ns)
21532155
clojure-cached-ns
2154-
(let ((ns (save-excursion
2155-
(save-restriction
2156-
(widen)
2157-
2158-
;; Move to top-level to avoid searching from inside ns
2159-
(ignore-errors (while t (up-list nil t t)))
2160-
2161-
(or (clojure--find-ns-in-direction 'backward)
2162-
(clojure--find-ns-in-direction 'forward))))))
2156+
(let* ((f (lambda (direction)
2157+
(if favor-nil
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))))))
21632169
(setq clojure-cached-ns ns)
21642170
ns)))
21652171

test/clojure-mode-sexp-test.el

+14-5
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,20 @@
169169
(goto-char (point-max))
170170
(expect (clojure-find-ns) :to-equal expected))))))
171171

172-
(it "should return nil for an invalid ns form"
173-
;; we should not cache the results of `clojure-find-ns' here
174-
(let ((clojure-cache-ns nil))
175-
(with-clojure-buffer "(ns )"
176-
(expect (equal nil (clojure-find-ns)))))))
172+
(describe "`favor-nil' 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)))))))))
177186

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

0 commit comments

Comments
 (0)