Skip to content

Commit fd6c420

Browse files
committed
clojure-find-ns: never raise errors
1 parent 0e62583 commit fd6c420

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
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`: never raise errors, returning nil instead on unparseable ns forms.
8+
59
## 5.16.1 (2023-06-26)
610

711
### Changes

clojure-mode.el

+15-14
Original file line numberDiff line numberDiff line change
@@ -2127,20 +2127,21 @@ 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-
(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))
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)))
21442145

21452146
(defun clojure-find-ns ()
21462147
"Return the namespace of the current Clojure buffer.

test/clojure-mode-sexp-test.el

+7-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@
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+
(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)))))))
171177

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

0 commit comments

Comments
 (0)