Skip to content

Commit a5f6e9e

Browse files
rfkmcap10morgan
authored andcommitted
Suppress eldoc when the current sexp seems to be too large
1 parent 2ba7a0d commit a5f6e9e

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

CHANGELOG.md

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

55
### Changes
66

7+
* Suppress eldoc when the current sexp seems to be too large.
78
* [#1500](https://github.com/clojure-emacs/cider/pull/1500): Improve the performance of REPL buffers by using text properties instead of overlays for ANSI coloring.
89

910
### Bugs fixed

cider-eldoc.el

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
(defvar cider-extra-eldoc-commands '("yas-expand")
4343
"Extra commands to be added to eldoc's safe commands list.")
4444

45+
(defvar cider-eldoc-max-num-sexps-to-skip 30
46+
"The maximum number of sexps to skip while searching the beginning of current sexp.")
47+
4548
(defvar-local cider-eldoc-last-symbol nil
4649
"The eldoc information for the last symbol we checked.")
4750

@@ -89,7 +92,8 @@ POS is the index of current argument."
8992
(defun cider-eldoc-beginning-of-sexp ()
9093
"Move to the beginning of current sexp.
9194
92-
Return the number of nested sexp the point was over or after."
95+
Return the number of nested sexp the point was over or after. Return nil
96+
if the maximum number of sexps to skip is exceeded."
9397
(let ((parse-sexp-ignore-comments t)
9498
(num-skipped-sexps 0))
9599
(condition-case _
@@ -107,14 +111,25 @@ Return the number of nested sexp the point was over or after."
107111
(let ((p (point)))
108112
(forward-sexp -1)
109113
(when (< (point) p)
110-
(setq num-skipped-sexps (1+ num-skipped-sexps))))))
114+
(setq num-skipped-sexps
115+
(unless (and cider-eldoc-max-num-sexps-to-skip
116+
(>= num-skipped-sexps
117+
cider-eldoc-max-num-sexps-to-skip))
118+
;; Without the above guard,
119+
;; `cider-eldoc-beginning-of-sexp' could traverse the
120+
;; whole buffer when the point is not within a
121+
;; list. This behavior is problematic especially with
122+
;; a buffer containing a large number of
123+
;; non-expressions like a REPL buffer.
124+
(1+ num-skipped-sexps)))))))
111125
(error))
112126
num-skipped-sexps))
113127

114128
(defun cider-eldoc-info-in-current-sexp ()
115129
"Return a list of the current sexp and the current argument index."
116130
(save-excursion
117-
(let ((argument-index (1- (cider-eldoc-beginning-of-sexp))))
131+
(when-let ((beginning-of-sexp (cider-eldoc-beginning-of-sexp))
132+
(argument-index (1- beginning-of-sexp)))
118133
;; If we are at the beginning of function name, this will be -1.
119134
(when (< argument-index 0)
120135
(setq argument-index 0))

test/cider-tests.el

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,33 @@
538538
(should (equal "/space test" (cider--url-to-file "file:/space%20test")))
539539
(should (equal "C:/space test" (cider--url-to-file "file:/C:/space%20test"))))
540540

541+
(ert-deftest test-cider-eldoc-beginning-of-sexp ()
542+
(with-temp-buffer
543+
(save-excursion
544+
(insert "(a (b b) (c c) d)"))
545+
(search-forward "d")
546+
547+
(let ((cider-eldoc-max-num-sexps-to-skip nil))
548+
(save-excursion
549+
(should (eq (cider-eldoc-beginning-of-sexp) 4))
550+
(should (eq (point) 2))))
551+
552+
(let ((cider-eldoc-max-num-sexps-to-skip 4))
553+
(save-excursion
554+
(should (eq (cider-eldoc-beginning-of-sexp) 4))
555+
(should (eq (point) 2))))
556+
557+
(let ((cider-eldoc-max-num-sexps-to-skip 3))
558+
(save-excursion
559+
(should (eq (cider-eldoc-beginning-of-sexp) nil))
560+
(should (eq (point) 2))))
561+
562+
(let ((cider-eldoc-max-num-sexps-to-skip 2))
563+
(save-excursion
564+
(should (eq (cider-eldoc-beginning-of-sexp) nil))
565+
(should (eq (point) 4))))))
566+
567+
541568

542569
;;; Cygwin tests
543570

0 commit comments

Comments
 (0)