Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clojure-find-ns: never raise errors #654

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 favor-nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think suppress-errors is a better name for this.

"Return the namespace of the current Clojure buffer, honor `FAVOR-NIL'.
Return the namespace closest to point and above it. If there are
no namespaces above point, return the first one in the buffer.

If `FAVOR-NIL' 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 favor-nil
(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 "`favor-nil' 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