Skip to content

Commit

Permalink
Fix infinite loop when reverse searching for next definition (#624)
Browse files Browse the repository at this point in the history
Fixes #595 
Fixes #612
  • Loading branch information
OknoLombarda authored Jul 19, 2022
1 parent fee38d7 commit 54a62cc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* [#622](https://github.com/clojure-emacs/clojure-mode/issues/622): Add font locking for missing clojure.core macros
* [#615](https://github.com/clojure-emacs/clojure-mode/issues/615): Support clojure-dart files.

### Bugs fixed

* [#595](https://github.com/clojure-emacs/clojure-mode/issues/595), [#612](https://github.com/clojure-emacs/clojure-mode/issues/612): Fix buffer freezing when typing metadata for a definition

## 5.14.0 (2022-03-07)

### New features
Expand Down
7 changes: 5 additions & 2 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,11 @@ Called by `imenu--generic-function'."
(when (char-equal ?\) (char-after (point)))
(backward-sexp)))
(cl-destructuring-bind (def-beg . def-end) (bounds-of-thing-at-point 'sexp)
(if (char-equal ?^ (char-after def-beg))
(progn (forward-sexp) (backward-sexp))
(when (char-equal ?^ (char-after def-beg))
;; move to the beginning of next sexp
(progn (forward-sexp) (backward-sexp)))
(when (or (not (char-equal ?^ (char-after (point))))
(and (char-equal ?^ (char-after (point))) (= def-beg (point))))
(setq found? t)
(when (string= deftype "defmethod")
(setq def-end (progn (goto-char def-end)
Expand Down
4 changes: 4 additions & 0 deletions test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@
([x y & more]
(reduce1 max (max x y) more)))


;; definitions with metadata only don't cause freezing
(def ^String)

(defn ^String reverse
"Returns s with its characters reversed."
{:added "1.2"}
Expand Down
31 changes: 31 additions & 0 deletions test/clojure-mode-syntax-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@
(expect (non-func "^hint " form) :to-be nil)
(expect (non-func "#macro " form) :to-be nil))))

(describe "clojure-match-next-def"
(let ((some-sexp "\n(list [1 2 3])"))
(it "handles vars with metadata"
(dolist (form '("(def ^Integer a 1)"
"(def ^:a a 1)"
"(def ^::a a 1)"
"(def ^::a/b a 1)"
"(def ^{:macro true} a 1)"))
(with-clojure-buffer (concat form some-sexp)
(end-of-buffer)
(clojure-match-next-def)
(expect (looking-at "(def")))))

(it "handles vars without metadata"
(with-clojure-buffer (concat "(def a 1)" some-sexp)
(end-of-buffer)
(clojure-match-next-def)
(expect (looking-at "(def"))))

(it "handles invalid def forms"
(dolist (form '("(def ^Integer)"
"(def)"
"(def ^{:macro})"
"(def ^{:macro true})"
"(def ^{:macro true} foo)"
"(def ^{:macro} foo)"))
(with-clojure-buffer (concat form some-sexp)
(end-of-buffer)
(clojure-match-next-def)
(expect (looking-at "(def")))))))

(describe "clojure syntax"
(it "handles prefixed symbols"
(dolist (form '(("#?@aaa" . "aaa")
Expand Down

0 comments on commit 54a62cc

Please sign in to comment.