Skip to content

Commit 1cf7788

Browse files
committed
Better docstring detection when folding
1 parent fc54812 commit 1cf7788

10 files changed

+67
-45
lines changed

elpy.el

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,15 +3260,6 @@ display the current class and method instead."
32603260
"^\\s-*[uU]?[rR]?\"\"\"\n?\\s-*"
32613261
"Version of `hs-block-start-regexp' for docstrings.")
32623262

3263-
;; Herlpers
3264-
(defun elpy-info-docstring-p (&optional syntax-ppss)
3265-
"Return non-nil if point is in a docstring."
3266-
(save-excursion
3267-
(and (progn (python-nav-beginning-of-statement)
3268-
(looking-at "\\(\"\\|'\\)"))
3269-
(progn (forward-line -1)
3270-
(beginning-of-line)
3271-
(python-info-looking-at-beginning-of-defun)))))
32723263
;; Indicators
32733264
(defun elpy-folding--display-code-line-counts (ov)
32743265
"Display a folded region indicator with the number of folded lines.
@@ -3415,11 +3406,11 @@ docstring body."
34153406
"Hide the docstring at point."
34163407
(hs-life-goes-on
34173408
(let ((hs-block-start-regexp elpy-docstring-block-start-regexp))
3418-
(when (and (elpy-info-docstring-p) (not (hs-already-hidden-p)))
3409+
(when (and (python-info-docstring-p) (not (hs-already-hidden-p)))
34193410
(let (beg end line-beg line-end)
34203411
;; Get first doc line
34213412
(if (not (save-excursion (forward-line -1)
3422-
(elpy-info-docstring-p)))
3413+
(python-info-docstring-p)))
34233414
(setq beg (line-beginning-position))
34243415
(forward-line -1)
34253416
(end-of-line)
@@ -3432,7 +3423,7 @@ docstring body."
34323423
(setq line-beg (line-number-at-pos))
34333424
;; Get last line
34343425
(if (not (save-excursion (forward-line 1)
3435-
(elpy-info-docstring-p)))
3426+
(python-info-docstring-p)))
34363427
(progn
34373428
(setq end (line-end-position))
34383429
(setq line-end (line-number-at-pos)))
@@ -3447,7 +3438,7 @@ docstring body."
34473438
"Show docstring at point."
34483439
(hs-life-goes-on
34493440
(let ((hs-block-start-regexp elpy-docstring-block-start-regexp))
3450-
(when (elpy-info-docstring-p)
3441+
(when (python-info-docstring-p)
34513442
(hs-show-block)))))
34523443

34533444
(defvar-local elpy-folding-docstrings-hidden nil
@@ -3464,7 +3455,7 @@ docstring body."
34643455
(while (python-nav-forward-defun)
34653456
(search-forward-regexp ")\\s-*:" nil t)
34663457
(forward-line)
3467-
(when (and (elpy-info-docstring-p)
3458+
(when (and (python-info-docstring-p)
34683459
(progn
34693460
(beginning-of-line)
34703461
(search-forward-regexp elpy-folding-docstring-regex
@@ -3577,18 +3568,19 @@ If a region is selected, fold that region."
35773568
(elpy-folding--hide-region (region-beginning) (region-end))
35783569
;; Adapt starting regexp if on a docstring
35793570
(let ((hs-block-start-regexp
3580-
(if (elpy-info-docstring-p)
3571+
(if (python-info-docstring-p)
35813572
elpy-docstring-block-start-regexp
35823573
hs-block-start-regexp)))
35833574
;; Hide or fold
35843575
(cond
35853576
((hs-already-hidden-p)
35863577
(hs-show-block))
3587-
((elpy-info-docstring-p)
3578+
((python-info-docstring-p)
35883579
(elpy-folding--hide-docstring-at-point))
35893580
(t
35903581
(hs-hide-block))))))))
35913582

3583+
35923584
;;;;;;;;;;;;;;;;;;;
35933585
;;; Module: Flymake
35943586

test/elpy-folding-fold-all-comments-test.el

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@
2828
overlay)
2929
(should (= 6 (length overlays)))
3030
;; first two lines comment
31-
(setq overlay (nth 5 overlays))
31+
(setq overlay (elpy-get-overlay-at 49 'comment))
32+
(should overlay)
3233
(should (eq (overlay-get overlay 'hs) 'comment))
3334
(should (= (overlay-start overlay) 49))
3435
(should (= (overlay-end overlay) 83))
3536
;; second three lines comment
36-
(setq overlay (nth 2 overlays))
37+
(setq overlay (elpy-get-overlay-at 184 'comment))
38+
(should overlay)
3739
(should (eq (overlay-get overlay 'hs) 'comment))
3840
(should (= (overlay-start overlay) 184))
3941
(should (= (overlay-end overlay) 229))
4042
;; third two lines comment
41-
(setq overlay (nth 0 overlays))
43+
(setq overlay (elpy-get-overlay-at 340 'comment))
44+
(should overlay)
4245
(should (eq (overlay-get overlay 'hs) 'comment))
4346
(should (= (overlay-start overlay) 340))
4447
(should (= (overlay-end overlay) 354)))

test/elpy-folding-fold-all-docstrings-test.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
overlay)
3737
(should (= 6 (length overlays)))
3838
;; First docstring
39-
(setq overlay (nth 3 overlays))
39+
(setq overlay (elpy-get-overlay-at 97 'docstring))
40+
(should overlay)
4041
(should (eq (overlay-get overlay 'hs) 'docstring))
4142
(should (= (overlay-start overlay) 97))
4243
(should (= (overlay-end overlay) 124))
4344
;; Second docstring
44-
(setq overlay (nth 1 overlays))
45+
(setq overlay (elpy-get-overlay-at 206 'docstring))
46+
(should overlay)
4547
(should (eq (overlay-get overlay 'hs) 'docstring))
4648
(should (= (overlay-start overlay) 206))
4749
(should (= (overlay-end overlay) 280)))

test/elpy-folding-fold-blocks-test.el

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
(let* ((overlays (apply 'nconc (overlay-lists)))
1313
overlay)
1414
(should (= 2 (length overlays)))
15-
(setq overlay (nth 0 overlays))
15+
(setq overlay (elpy-get-overlay-at 25 'code))
16+
(should overlay)
1617
(should (eq (overlay-get overlay 'hs) 'code))
1718
(should (= (overlay-start overlay) 25))
1819
(should (or (= (overlay-end overlay) 37)
@@ -40,7 +41,8 @@
4041
(let* ((overlays (apply 'nconc (overlay-lists)))
4142
overlay)
4243
(should (= 2 (length overlays)))
43-
(setq overlay (nth 0 overlays))
44+
(setq overlay (elpy-get-overlay-at 25 'code))
45+
(should overlay)
4446
(should (eq (overlay-get overlay 'hs) 'code))
4547
(should (= (overlay-start overlay) 25))
4648
(should (or (= (overlay-end overlay) 37)
@@ -90,7 +92,8 @@
9092
(let* ((overlays (apply 'nconc (overlay-lists)))
9193
overlay)
9294
(should (= 3 (length overlays)))
93-
(setq overlay (nth 0 overlays))
95+
(setq overlay (elpy-get-overlay-at 54 'code))
96+
(should overlay)
9497
(should (eq (overlay-get overlay 'hs) 'code))
9598
(should (= (overlay-start overlay) 54))
9699
(should (or (= (overlay-end overlay) 100)
@@ -124,7 +127,8 @@
124127
(let* ((overlays (apply 'nconc (overlay-lists)))
125128
overlay)
126129
(should (= 4 (length overlays)))
127-
(setq overlay (nth 0 overlays))
130+
(setq overlay (elpy-get-overlay-at 104 'code))
131+
(should overlay)
128132
(should (eq (overlay-get overlay 'hs) 'code))
129133
(should (= (overlay-start overlay) 104))
130134
(should (or (= (overlay-end overlay) 150)
@@ -158,7 +162,8 @@
158162
(let* ((overlays (apply 'nconc (overlay-lists)))
159163
overlay)
160164
(should (= 4 (length overlays)))
161-
(setq overlay (nth 0 overlays))
165+
(setq overlay (elpy-get-overlay-at 29 'code))
166+
(should overlay)
162167
(should (eq (overlay-get overlay 'hs) 'code))
163168
(should (= (overlay-start overlay) 29))
164169
(should (or (= (overlay-end overlay) 150)

test/elpy-folding-fold-comments-test.el

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
(let* ((overlays (overlays-in (point-min) (point-max)))
2222
overlay)
2323
(should (= 4 (length overlays)))
24-
(setq overlay (nth 1 overlays))
24+
(setq overlay (elpy-get-overlay-at 111 'comment))
25+
(should overlay)
2526
(should (eq (overlay-get overlay 'hs) 'comment))
2627
(should (= (overlay-start overlay) 111))
2728
(should (= (overlay-end overlay) 156)))

test/elpy-folding-fold-docstrings-test.el

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
;; (buffer-substring (overlay-start overlay)
3131
;; (overlay-end overlay))))
3232
(should (= 4 (length overlays)))
33-
(setq overlay (nth 0 overlays))
33+
(setq overlay (elpy-get-overlay-at 138 'docstring))
34+
(should overlay)
3435
(should (eq (overlay-get overlay 'hs) 'docstring))
3536
(should (= (overlay-start overlay) 138))
3637
(should (= (overlay-end overlay) 212)))
@@ -70,7 +71,8 @@
7071
(let* ((overlays (overlays-in (point-min) (point-max)))
7172
overlay)
7273
(should (= 4 (length overlays)))
73-
(setq overlay (nth 0 overlays))
74+
(setq overlay (elpy-get-overlay-at 142 'docstring))
75+
(should overlay)
7476
(should (eq (overlay-get overlay 'hs) 'docstring))
7577
(should (= (overlay-start overlay) 142))
7678
(should (= (overlay-end overlay) 216)))
@@ -114,8 +116,8 @@
114116
" self.a = a"
115117
" self.b = b"
116118
" def bar(mess):"
117-
" \" This is just _|_a string\""
118119
" mess *= 2"
120+
" \" This is just _|_a string\""
119121
" print(mess)"
120122
" return mess"
121123
"var2 = foo(var1, 4)")
@@ -143,12 +145,13 @@
143145
"var2 = foo(var1, 4)")
144146
(python-mode)
145147
(elpy-mode)
146-
(elpy-folding-toggle-at-point)
147-
(let* ((overlays (overlays-in (point-min) (point-max)))
148-
overlay)
149-
(should (= 4 (length overlays)))
150-
(dolist (overlay overlays)
151-
(should-not (eq (overlay-get overlay 'hs) 'docstring))))))
148+
(let ((nmb-overlays (length (overlays-in (point-min) (point-max)))))
149+
(elpy-folding-toggle-at-point)
150+
(let* ((overlays (overlays-in (point-min) (point-max)))
151+
overlay)
152+
(should (= nmb-overlays (length overlays)))
153+
(dolist (overlay overlays)
154+
(should-not (eq (overlay-get overlay 'hs) 'docstring)))))))
152155

153156
(ert-deftest elpy-fold-at-point-should-NOT-fold-strings-3 ()
154157
(elpy-testcase ()
@@ -171,7 +174,8 @@
171174
(let* ((overlays (overlays-in (point-min) (point-max)))
172175
overlay)
173176
(should (= 4 (length overlays)))
174-
(setq overlay (nth 0 overlays))
177+
(setq overlay (elpy-get-overlay-at 104 'code))
178+
(should overlay)
175179
(should (eq (overlay-get overlay 'hs) 'code))
176180
(should (= (overlay-start overlay) 104))
177181
(should (or (= (overlay-end overlay) 190)
@@ -203,7 +207,8 @@
203207
(let* ((overlays (overlays-in (point-min) (point-max)))
204208
overlay)
205209
(should (= 4 (length overlays)))
206-
(setq overlay (nth 0 overlays))
210+
(setq overlay (elpy-get-overlay-at 104 'code))
211+
(should overlay)
207212
(should (eq (overlay-get overlay 'hs) 'code))
208213
(should (= (overlay-start overlay) 104))
209214
(should (or (= (overlay-end overlay) 229)

test/elpy-folding-fold-leafs-test.el

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
(let* ((overlays (overlays-in (point-min) (point-max)))
2121
overlay)
2222
(should (= 6 (length overlays)))
23-
(setq overlay (nth 3 overlays))
23+
24+
(setq overlay (elpy-get-overlay-at 57 'code))
25+
(should overlay)
2426
(should (eq (overlay-get overlay 'hs) 'code))
2527
(should (= (overlay-start overlay) 57))
2628
(should (or (= (overlay-end overlay) 87)
2729
(= (overlay-end overlay) 88)))
28-
(setq overlay (nth 0 overlays))
30+
(setq overlay (elpy-get-overlay-at 138 'code))
31+
(should overlay)
2932
(should (eq (overlay-get overlay 'hs) 'code))
3033
(should (= (overlay-start overlay) 138))
3134
(should (or (= (overlay-end overlay) 156)

test/elpy-folding-fold-on-click-test.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
(let* ((overlays (apply 'nconc (overlay-lists)))
1414
overlay)
1515
(should (= 2 (length overlays)))
16-
(setq overlay (nth 0 overlays))
16+
(setq overlay (elpy-get-overlay-at 25 'code))
17+
(should overlay)
1718
(should (eq (overlay-get overlay 'hs) 'code))
1819
(should (= (overlay-start overlay) 25))
1920
(should (or (= (overlay-end overlay) 37)
@@ -43,7 +44,8 @@
4344
(let* ((overlays (apply 'nconc (overlay-lists)))
4445
overlay)
4546
(should (= 2 (length overlays)))
46-
(setq overlay (nth 0 overlays))
47+
(setq overlay (elpy-get-overlay-at 25 'code))
48+
(should overlay)
4749
(should (eq (overlay-get overlay 'hs) 'code))
4850
(should (= (overlay-start overlay) 25))
4951
(should (or (= (overlay-end overlay) 37)

test/elpy-folding-should-mark-foldable-lines-test.el

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@
2424
overlay)
2525
(should (= 3 (length overlays)))
2626
;; Second mark
27-
(setq overlay (nth 2 overlays))
27+
(setq overlay (elpy-get-overlay-at 11 nil))
28+
(should overlay)
2829
(should (eq (overlay-get overlay 'hs) nil))
2930
(should (= (overlay-start overlay) 11))
3031
(should (= (overlay-end overlay) 29))
3132
;; Second mark
32-
(setq overlay (nth 1 overlays))
33+
(setq overlay (elpy-get-overlay-at 30 nil))
34+
(should overlay)
3335
(should (eq (overlay-get overlay 'hs) nil))
3436
(should (= (overlay-start overlay) 30))
3537
(should (= (overlay-end overlay) 57))
3638
;; Third mark
37-
(setq overlay (nth 0 overlays))
39+
(setq overlay (elpy-get-overlay-at 88 nil))
40+
(should overlay)
3841
(should (eq (overlay-get overlay 'hs) nil))
3942
(should (= (overlay-start overlay) 88))
4043
(should (= (overlay-end overlay) 104)))))

test/test-helper.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,11 @@ for that file."
223223
,(buffer-string-with-point)))
224224
(put 'buffer-be 'ert-explainer 'buffer-be-explainer)
225225

226+
(defun elpy-get-overlay-at (start kind)
227+
(dolist (tmp-overlay overlays overlay)
228+
(when (and (= (overlay-start tmp-overlay) start)
229+
(eq (overlay-get tmp-overlay 'hs) kind))
230+
(setq overlay tmp-overlay))))
231+
226232
(setq yas-verbosity 0)
227233
(setq yas-snippet-dirs ())

0 commit comments

Comments
 (0)