Skip to content

Commit 7e8181d

Browse files
authored
Merge pull request #1776 from galaunay/better-docstring-folding
Better docstring detection when folding
2 parents 816061c + eb46e8e commit 7e8181d

10 files changed

+86
-114
lines changed

elpy.el

Lines changed: 17 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,14 +3568,14 @@ 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))))))))
@@ -4029,5 +4020,15 @@ which we're looking."
40294020
(rx eos))
40304021
output)))
40314022

4023+
(unless (fboundp 'python-info-docstring-p)
4024+
(defun python-info-docstring-p (&optional syntax-ppss)
4025+
"Return non-nil if point is in a docstring."
4026+
(save-excursion
4027+
(and (progn (python-nav-beginning-of-statement)
4028+
(looking-at "\\(\"\\|'\\)"))
4029+
(progn (forward-line -1)
4030+
(beginning-of-line)
4031+
(python-info-looking-at-beginning-of-defun))))))
4032+
40324033
(provide 'elpy)
40334034
;;; elpy.el ends here

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,27 @@
2626
(elpy-folding-toggle-comments)
2727
(let* ((overlays (overlays-in (point-min) (point-max)))
2828
overlay)
29-
(should (= 6 (length overlays)))
3029
;; first two lines comment
31-
(setq overlay (nth 5 overlays))
30+
(setq overlay (elpy-get-overlay-at 49 'comment))
31+
(should overlay)
3232
(should (eq (overlay-get overlay 'hs) 'comment))
3333
(should (= (overlay-start overlay) 49))
3434
(should (= (overlay-end overlay) 83))
3535
;; second three lines comment
36-
(setq overlay (nth 2 overlays))
36+
(setq overlay (elpy-get-overlay-at 184 'comment))
37+
(should overlay)
3738
(should (eq (overlay-get overlay 'hs) 'comment))
3839
(should (= (overlay-start overlay) 184))
3940
(should (= (overlay-end overlay) 229))
4041
;; third two lines comment
41-
(setq overlay (nth 0 overlays))
42+
(setq overlay (elpy-get-overlay-at 340 'comment))
43+
(should overlay)
4244
(should (eq (overlay-get overlay 'hs) 'comment))
4345
(should (= (overlay-start overlay) 340))
4446
(should (= (overlay-end overlay) 354)))
4547
;; point shouldn't move
4648
(should (= (point) 177))
4749
;; Unfold all comments
4850
(elpy-folding-toggle-comments)
49-
(let* ((overlays (overlays-in (point-min) (point-max)))
50-
overlay)
51-
(should (= 3 (length overlays))))
5251
;; Position
5352
(should (= (point) 177))))

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 & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111
(elpy-folding-toggle-at-point)
1212
(let* ((overlays (apply 'nconc (overlay-lists)))
1313
overlay)
14-
(should (= 2 (length overlays)))
15-
(setq overlay (nth 0 overlays))
14+
(setq overlay (elpy-get-overlay-at 25 'code))
15+
(should overlay)
1616
(should (eq (overlay-get overlay 'hs) 'code))
1717
(should (= (overlay-start overlay) 25))
1818
(should (or (= (overlay-end overlay) 37)
1919
(= (overlay-end overlay) 38))))
2020
(should (= (point) 14))
2121
;; Unfold
2222
(elpy-folding-toggle-at-point)
23-
(let* ((overlays (apply 'nconc (overlay-lists)))
24-
overlay)
25-
(should (= 1 (length overlays))))
2623
;; Position
2724
(should (= (point) 14))))
2825

@@ -39,18 +36,15 @@
3936
(elpy-folding-toggle-at-point)
4037
(let* ((overlays (apply 'nconc (overlay-lists)))
4138
overlay)
42-
(should (= 2 (length overlays)))
43-
(setq overlay (nth 0 overlays))
39+
(setq overlay (elpy-get-overlay-at 25 'code))
40+
(should overlay)
4441
(should (eq (overlay-get overlay 'hs) 'code))
4542
(should (= (overlay-start overlay) 25))
4643
(should (or (= (overlay-end overlay) 37)
4744
(= (overlay-end overlay) 38))))
4845
(should (= (point) 14))
4946
;; Unfold
5047
(elpy-folding-toggle-at-point)
51-
(let* ((overlays (apply 'nconc (overlay-lists)))
52-
overlay)
53-
(should (= 1 (length overlays))))
5448
;; Position
5549
(should (= (point) 14))))
5650

@@ -67,8 +61,6 @@
6761
(python-mode)
6862
(elpy-mode)
6963
(elpy-folding-toggle-at-point)
70-
(let ((overlays (apply 'nconc (overlay-lists))))
71-
(should (= 1 (length overlays))))
7264
;; Position
7365
(should (= (point) 49))))
7466

@@ -89,18 +81,15 @@
8981
(elpy-folding-toggle-at-point)
9082
(let* ((overlays (apply 'nconc (overlay-lists)))
9183
overlay)
92-
(should (= 3 (length overlays)))
93-
(setq overlay (nth 0 overlays))
84+
(setq overlay (elpy-get-overlay-at 54 'code))
85+
(should overlay)
9486
(should (eq (overlay-get overlay 'hs) 'code))
9587
(should (= (overlay-start overlay) 54))
9688
(should (or (= (overlay-end overlay) 100)
9789
(= (overlay-end overlay) 101))))
9890
(should (= (point) 43))
9991
;; Unfold
10092
(elpy-folding-toggle-at-point)
101-
(let* ((overlays (apply 'nconc (overlay-lists)))
102-
overlay)
103-
(should (= 2 (length overlays))))
10493
;; Position
10594
(should (= (point) 43))))
10695

@@ -123,18 +112,15 @@
123112
(elpy-folding-toggle-at-point)
124113
(let* ((overlays (apply 'nconc (overlay-lists)))
125114
overlay)
126-
(should (= 4 (length overlays)))
127-
(setq overlay (nth 0 overlays))
115+
(setq overlay (elpy-get-overlay-at 104 'code))
116+
(should overlay)
128117
(should (eq (overlay-get overlay 'hs) 'code))
129118
(should (= (overlay-start overlay) 104))
130119
(should (or (= (overlay-end overlay) 150)
131120
(= (overlay-end overlay) 151))))
132121
(should (= (point) 93))
133122
;; Unfold
134123
(elpy-folding-toggle-at-point)
135-
(let* ((overlays (apply 'nconc (overlay-lists)))
136-
overlay)
137-
(should (= 3 (length overlays))))
138124
;; Position
139125
(should (= (point) 93))))
140126

@@ -157,17 +143,14 @@
157143
(elpy-folding-toggle-at-point)
158144
(let* ((overlays (apply 'nconc (overlay-lists)))
159145
overlay)
160-
(should (= 4 (length overlays)))
161-
(setq overlay (nth 0 overlays))
146+
(setq overlay (elpy-get-overlay-at 29 'code))
147+
(should overlay)
162148
(should (eq (overlay-get overlay 'hs) 'code))
163149
(should (= (overlay-start overlay) 29))
164150
(should (or (= (overlay-end overlay) 150)
165151
(= (overlay-end overlay) 151))))
166152
(should (= (point) 16))
167153
;; Unfold
168154
(elpy-folding-toggle-at-point)
169-
(let* ((overlays (apply 'nconc (overlay-lists)))
170-
overlay)
171-
(should (= 3 (length overlays))))
172155
;; Position
173156
(should (= (point) 16))))

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
(elpy-folding-toggle-at-point)
2121
(let* ((overlays (overlays-in (point-min) (point-max)))
2222
overlay)
23-
(should (= 4 (length overlays)))
24-
(setq overlay (nth 1 overlays))
23+
(setq overlay (elpy-get-overlay-at 111 'comment))
24+
(should overlay)
2525
(should (eq (overlay-get overlay 'hs) 'comment))
2626
(should (= (overlay-start overlay) 111))
2727
(should (= (overlay-end overlay) 156)))
2828
(should (= (point) 92))
2929
;; Unfold
3030
(elpy-folding-toggle-at-point)
31-
(let* ((overlays (overlays-in (point-min) (point-max)))
32-
overlay)
33-
(should (= 3 (length overlays))))
3431
;; Position
3532
(should (= (point) 92))))
3633

@@ -52,7 +49,4 @@
5249
(python-mode)
5350
(elpy-mode)
5451
(elpy-folding-toggle-at-point)
55-
(let* ((overlays (overlays-in (point-min) (point-max)))
56-
overlay)
57-
(should (= 3 (length overlays))))
5852
(should (= (point) 112))))

0 commit comments

Comments
 (0)