Skip to content

Commit 9d2af83

Browse files
dan suttonbbatsov
authored andcommitted
Remove comment aware toplevel defun code
This code has migrated to clojure-mode and is now installed such that `end-of-defun` and `beginning-of-defun` are aware of comment forms. As such CIDER no longer needs to special case them and can just navigate as usual.
1 parent 0a1f536 commit 9d2af83

File tree

3 files changed

+7
-133
lines changed

3 files changed

+7
-133
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### New features
66

7+
* [#2375](https://github.com/clojure-emacs/cider/issues/2375): Move `cider-eval-toplevel-inside-comment-form` into clojure-mode as `clojure-toplevel-inside-comment-form` so `beginning-of-defun` is aware of comment forms.
78
* Add new `cider-session-name-template` variable for flexible customization of cider session and REPL buffer names.
89
* Bind `C-c M-r` to `cider-restart`.
910
* Add new `cider-start-map` keymap (`C-c C-x`) for jack-in and connection commands.

cider-util.el

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -105,76 +105,16 @@ positions. Else returns the substring from START to END."
105105
(funcall (if bounds #'list #'buffer-substring-no-properties)
106106
start end))
107107

108-
(defun cider-top-level-comment-p ()
109-
"Return non-nil if point is in a comment form."
110-
(save-excursion
111-
(end-of-defun)
112-
(clojure-backward-logical-sexp 1)
113-
(forward-char 1)
114-
(clojure-forward-logical-sexp 1)
115-
(clojure-backward-logical-sexp 1)
116-
(looking-at-p "comment")))
117-
118-
(defcustom cider-eval-toplevel-inside-comment-form nil
119-
"Eval top level forms inside comment forms instead of the comment form itself.
120-
Experimental. Function `cider-defun-at-point' is used extensively so if we
121-
change this heuristic it needs to be bullet-proof and desired. While
122-
testing, give an easy way to turn this new behavior off."
123-
:group 'cider
124-
:type 'boolean
125-
:package-version '(cider . "0.18.0"))
126-
127-
(defun cider-sexp-starts-until-position (position)
128-
"Returns the starting points for forms before POSITION.
129-
Positions are in descending order to aide in finding the first starting
130-
position before the current position."
131-
(save-excursion
132-
(let (sexp-positions)
133-
(condition-case nil
134-
(while (< (point) position)
135-
(clojure-forward-logical-sexp 1)
136-
(clojure-backward-logical-sexp 1)
137-
(push (point) sexp-positions)
138-
(clojure-forward-logical-sexp 1))
139-
(scan-error nil))
140-
sexp-positions)))
141-
142-
(defun cider-defun-inside-comment-form (&optional bounds)
143-
"Return the toplevel form inside a comment containing point.
144-
Assumes point is inside a (comment ....) form and will return the text of
145-
that form or if BOUNDS, will return a list of the starting and ending
146-
position."
147-
(save-excursion
148-
(save-match-data
149-
(let ((original-position (point))
150-
cider-comment-start cider-comment-end)
151-
(end-of-defun)
152-
(setq cider-comment-end (point))
153-
(clojure-backward-logical-sexp 1) ;; beginning of comment form
154-
(setq cider-comment-start (point))
155-
(forward-char 1) ;; skip paren so we start at comment
156-
(clojure-forward-logical-sexp) ;; skip past the comment form itself
157-
(if-let* ((sexp-start (seq-find (lambda (beg-pos) (< beg-pos original-position))
158-
(cider-sexp-starts-until-position cider-comment-end))))
159-
(progn
160-
(goto-char sexp-start)
161-
(clojure-forward-logical-sexp 1)
162-
(cider--text-or-limits bounds sexp-start (point)))
163-
(cider--text-or-limits bounds cider-comment-start cider-comment-end))))))
164-
165108
(defun cider-defun-at-point (&optional bounds)
166109
"Return the text of the top level sexp at point.
167110
If BOUNDS is non-nil, return a list of its starting and ending position
168111
instead."
169-
(if (and cider-eval-toplevel-inside-comment-form
170-
(cider-top-level-comment-p))
171-
(cider-defun-inside-comment-form bounds)
172-
(save-excursion
173-
(save-match-data
174-
(end-of-defun)
175-
(let ((end (point)))
176-
(clojure-backward-logical-sexp 1)
177-
(cider--text-or-limits bounds (point) end))))))
112+
(save-excursion
113+
(save-match-data
114+
(end-of-defun)
115+
(let ((end (point)))
116+
(clojure-backward-logical-sexp 1)
117+
(cider--text-or-limits bounds (point) end)))))
178118

179119
(defun cider-ns-form ()
180120
"Retrieve the ns form."

test/cider-util-tests.el

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -125,73 +125,6 @@
125125
(insert "'")
126126
(expect (cider-sexp-at-point 'bounds) :to-equal '(5 15))))))
127127

128-
(defmacro cider-buffer-with-text
129-
(text &rest body)
130-
"Run body in a temporary clojure buffer with TEXT.
131-
TEXT is a string with a | indicating where point is. The | will be erased
132-
and point left there."
133-
(declare (indent 2))
134-
`(progn
135-
(with-temp-buffer
136-
(erase-buffer)
137-
(clojure-mode)
138-
(insert ,text)
139-
(goto-char (point-min))
140-
(re-search-forward "|")
141-
(delete-char -1)
142-
,@body)))
143-
144-
(describe "cider-defun-inside-comment-form"
145-
(describe "when param 'bounds is not given"
146-
(it "returns the form containing point"
147-
(cider-buffer-with-text
148-
"(comment
149-
(wrong)
150-
(wrong)
151-
(rig|ht)
152-
(wrong))"
153-
(expect (cider-defun-inside-comment-form) :to-equal "(right)")))
154-
(it "attaches to the previous top level comment"
155-
(cider-buffer-with-text
156-
"(comment
157-
(wrong)
158-
(wrong)
159-
(right) |
160-
(wrong))"
161-
(expect (cider-defun-inside-comment-form) :to-equal "(right)")))
162-
(it "attaches to the next top level comment even if its on the same line"
163-
(cider-buffer-with-text
164-
"(comment
165-
(wrong)
166-
(wrong)
167-
(right)
168-
| (wrong))"
169-
(expect (cider-defun-inside-comment-form) :to-equal "(right)")))
170-
(it "returns the top level even if heavily nested"
171-
(cider-buffer-with-text
172-
"(comment
173-
(wrong)
174-
(wrong)
175-
(((((r|ight)))))
176-
(wrong))"
177-
(expect (cider-defun-inside-comment-form) :to-equal "(((((right)))))")))
178-
(it "works even on the last form in a comment"
179-
(cider-buffer-with-text
180-
"(comment
181-
(wrong)
182-
(wrong)
183-
(right|))"
184-
(expect (cider-defun-inside-comment-form) :to-equal "(right)")))
185-
(it "works on the last form even after the comment form"
186-
(cider-buffer-with-text
187-
"(comment (wrong) (wrong) (right)) |"
188-
(expect (cider-defun-inside-comment-form) :to-equal "(right)"))))
189-
(describe "returns entire comment form when"
190-
(it "the comment form is empty"
191-
(cider-buffer-with-text
192-
"(comment|)"
193-
(expect (cider-defun-inside-comment-form) :to-equal "(comment)")))))
194-
195128
(describe "cider-defun-at-point"
196129
(describe "when the param 'bounds is not given"
197130
(it "returns the defun at point"

0 commit comments

Comments
 (0)