Skip to content

Commit

Permalink
Accept an argument instead
Browse files Browse the repository at this point in the history
  • Loading branch information
vemv committed Aug 22, 2023
1 parent fd6c420 commit 4bf10fc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Changes

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

## 5.16.1 (2023-06-26)

Expand Down
60 changes: 33 additions & 27 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2127,39 +2127,45 @@ the cached value will be updated automatically."
(defun clojure--find-ns-in-direction (direction)
"Return the nearest namespace in a specific DIRECTION.
DIRECTION is `forward' or `backward'."
(ignore-errors
(let ((candidate)
(fn (if (eq direction 'forward)
#'search-forward-regexp
#'search-backward-regexp)))
(while (and (not candidate)
(funcall fn clojure-namespace-regexp nil t))
(let ((end (match-end 0)))
(save-excursion
(save-match-data
(goto-char end)
(clojure-forward-logical-sexp)
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
candidate)))

(defun clojure-find-ns ()
"Return the namespace of the current Clojure buffer.
(let ((candidate)
(fn (if (eq direction 'forward)
#'search-forward-regexp
#'search-backward-regexp)))
(while (and (not candidate)
(funcall fn clojure-namespace-regexp nil t))
(let ((end (match-end 0)))
(save-excursion
(save-match-data
(goto-char end)
(clojure-forward-logical-sexp)
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
candidate))

(defun clojure-find-ns (&optional favor-nil)
"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
19 changes: 14 additions & 5 deletions test/clojure-mode-sexp-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,20 @@
(goto-char (point-max))
(expect (clojure-find-ns) :to-equal expected))))))

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

0 comments on commit 4bf10fc

Please sign in to comment.