Skip to content

Commit

Permalink
clojure-find-ns: add an option to suppress errors (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
vemv authored Aug 23, 2023
1 parent 0e62583 commit 27cd085
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

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

## 5.16.1 (2023-06-26)

### Changes
Expand Down
29 changes: 18 additions & 11 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2142,23 +2142,30 @@ DIRECTION is `forward' or `backward'."
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
candidate))

(defun clojure-find-ns ()
"Return the namespace of the current Clojure buffer.
(defun clojure-find-ns (&optional suppress-errors)
"Return the namespace of the current Clojure buffer, honor `SUPPRESS-ERRORS'.
Return the namespace closest to point and above it. If there are
no namespaces above point, return the first one in the buffer.
If `SUPPRESS-ERRORS' is t, errors during ns form parsing will be swallowed,
and nil will be returned instead of letting this function fail.
The results will be cached if `clojure-cache-ns' is set to t."
(if (and clojure-cache-ns clojure-cached-ns)
clojure-cached-ns
(let ((ns (save-excursion
(save-restriction
(widen)

;; Move to top-level to avoid searching from inside ns
(ignore-errors (while t (up-list nil t t)))

(or (clojure--find-ns-in-direction 'backward)
(clojure--find-ns-in-direction 'forward))))))
(let* ((f (lambda (direction)
(if suppress-errors
(ignore-errors (clojure--find-ns-in-direction direction))
(clojure--find-ns-in-direction direction))))
(ns (save-excursion
(save-restriction
(widen)

;; Move to top-level to avoid searching from inside ns
(ignore-errors (while t (up-list nil t t)))

(or (funcall f 'backward)
(funcall f 'forward))))))
(setq clojure-cached-ns ns)
ns)))

Expand Down
17 changes: 16 additions & 1 deletion test/clojure-mode-sexp-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,22 @@
(expect (clojure-find-ns) :to-equal expected)
;; After both namespaces
(goto-char (point-max))
(expect (clojure-find-ns) :to-equal expected)))))))
(expect (clojure-find-ns) :to-equal expected))))))

(describe "`suppress-errors' argument"
(let ((clojure-cache-ns nil))
(describe "given a faulty ns form"
(let ((ns-form "(ns )"))
(describe "when the argument is `t'"
(it "causes `clojure-find-ns' to return nil"
(with-clojure-buffer ns-form
(expect (equal nil (clojure-find-ns t))))))

(describe "when the argument is `nil'"
(it "causes `clojure-find-ns' to return raise an error"
(with-clojure-buffer ns-form
(expect (clojure-find-ns nil)
:to-throw 'error)))))))))

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

0 comments on commit 27cd085

Please sign in to comment.