Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better docstring detection when folding #1776

Merged
merged 3 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions elpy.el
Original file line number Diff line number Diff line change
Expand Up @@ -3260,15 +3260,6 @@ display the current class and method instead."
"^\\s-*[uU]?[rR]?\"\"\"\n?\\s-*"
"Version of `hs-block-start-regexp' for docstrings.")

;; Herlpers
(defun elpy-info-docstring-p (&optional syntax-ppss)
"Return non-nil if point is in a docstring."
(save-excursion
(and (progn (python-nav-beginning-of-statement)
(looking-at "\\(\"\\|'\\)"))
(progn (forward-line -1)
(beginning-of-line)
(python-info-looking-at-beginning-of-defun)))))
;; Indicators
(defun elpy-folding--display-code-line-counts (ov)
"Display a folded region indicator with the number of folded lines.
Expand Down Expand Up @@ -3415,11 +3406,11 @@ docstring body."
"Hide the docstring at point."
(hs-life-goes-on
(let ((hs-block-start-regexp elpy-docstring-block-start-regexp))
(when (and (elpy-info-docstring-p) (not (hs-already-hidden-p)))
(when (and (python-info-docstring-p) (not (hs-already-hidden-p)))
(let (beg end line-beg line-end)
;; Get first doc line
(if (not (save-excursion (forward-line -1)
(elpy-info-docstring-p)))
(python-info-docstring-p)))
(setq beg (line-beginning-position))
(forward-line -1)
(end-of-line)
Expand All @@ -3432,7 +3423,7 @@ docstring body."
(setq line-beg (line-number-at-pos))
;; Get last line
(if (not (save-excursion (forward-line 1)
(elpy-info-docstring-p)))
(python-info-docstring-p)))
(progn
(setq end (line-end-position))
(setq line-end (line-number-at-pos)))
Expand All @@ -3447,7 +3438,7 @@ docstring body."
"Show docstring at point."
(hs-life-goes-on
(let ((hs-block-start-regexp elpy-docstring-block-start-regexp))
(when (elpy-info-docstring-p)
(when (python-info-docstring-p)
(hs-show-block)))))

(defvar-local elpy-folding-docstrings-hidden nil
Expand All @@ -3464,7 +3455,7 @@ docstring body."
(while (python-nav-forward-defun)
(search-forward-regexp ")\\s-*:" nil t)
(forward-line)
(when (and (elpy-info-docstring-p)
(when (and (python-info-docstring-p)
(progn
(beginning-of-line)
(search-forward-regexp elpy-folding-docstring-regex
Expand Down Expand Up @@ -3577,14 +3568,14 @@ If a region is selected, fold that region."
(elpy-folding--hide-region (region-beginning) (region-end))
;; Adapt starting regexp if on a docstring
(let ((hs-block-start-regexp
(if (elpy-info-docstring-p)
(if (python-info-docstring-p)
elpy-docstring-block-start-regexp
hs-block-start-regexp)))
;; Hide or fold
(cond
((hs-already-hidden-p)
(hs-show-block))
((elpy-info-docstring-p)
((python-info-docstring-p)
(elpy-folding--hide-docstring-at-point))
(t
(hs-hide-block))))))))
Expand Down Expand Up @@ -4029,5 +4020,15 @@ which we're looking."
(rx eos))
output)))

(unless (fboundp 'python-info-docstring-p)
(defun python-info-docstring-p (&optional syntax-ppss)
"Return non-nil if point is in a docstring."
(save-excursion
(and (progn (python-nav-beginning-of-statement)
(looking-at "\\(\"\\|'\\)"))
(progn (forward-line -1)
(beginning-of-line)
(python-info-looking-at-beginning-of-defun))))))

(provide 'elpy)
;;; elpy.el ends here
13 changes: 6 additions & 7 deletions test/elpy-folding-fold-all-comments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@
(elpy-folding-toggle-comments)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 6 (length overlays)))
;; first two lines comment
(setq overlay (nth 5 overlays))
(setq overlay (elpy-get-overlay-at 49 'comment))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'comment))
(should (= (overlay-start overlay) 49))
(should (= (overlay-end overlay) 83))
;; second three lines comment
(setq overlay (nth 2 overlays))
(setq overlay (elpy-get-overlay-at 184 'comment))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'comment))
(should (= (overlay-start overlay) 184))
(should (= (overlay-end overlay) 229))
;; third two lines comment
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 340 'comment))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'comment))
(should (= (overlay-start overlay) 340))
(should (= (overlay-end overlay) 354)))
;; point shouldn't move
(should (= (point) 177))
;; Unfold all comments
(elpy-folding-toggle-comments)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 3 (length overlays))))
;; Position
(should (= (point) 177))))
6 changes: 4 additions & 2 deletions test/elpy-folding-fold-all-docstrings-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
overlay)
(should (= 6 (length overlays)))
;; First docstring
(setq overlay (nth 3 overlays))
(setq overlay (elpy-get-overlay-at 97 'docstring))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'docstring))
(should (= (overlay-start overlay) 97))
(should (= (overlay-end overlay) 124))
;; Second docstring
(setq overlay (nth 1 overlays))
(setq overlay (elpy-get-overlay-at 206 'docstring))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'docstring))
(should (= (overlay-start overlay) 206))
(should (= (overlay-end overlay) 280)))
Expand Down
37 changes: 10 additions & 27 deletions test/elpy-folding-fold-blocks-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 2 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 25 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 25))
(should (or (= (overlay-end overlay) 37)
(= (overlay-end overlay) 38))))
(should (= (point) 14))
;; Unfold
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 1 (length overlays))))
;; Position
(should (= (point) 14))))

Expand All @@ -39,18 +36,15 @@
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 2 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 25 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 25))
(should (or (= (overlay-end overlay) 37)
(= (overlay-end overlay) 38))))
(should (= (point) 14))
;; Unfold
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 1 (length overlays))))
;; Position
(should (= (point) 14))))

Expand All @@ -67,8 +61,6 @@
(python-mode)
(elpy-mode)
(elpy-folding-toggle-at-point)
(let ((overlays (apply 'nconc (overlay-lists))))
(should (= 1 (length overlays))))
;; Position
(should (= (point) 49))))

Expand All @@ -89,18 +81,15 @@
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 3 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 54 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 54))
(should (or (= (overlay-end overlay) 100)
(= (overlay-end overlay) 101))))
(should (= (point) 43))
;; Unfold
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 2 (length overlays))))
;; Position
(should (= (point) 43))))

Expand All @@ -123,18 +112,15 @@
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 4 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 104 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 104))
(should (or (= (overlay-end overlay) 150)
(= (overlay-end overlay) 151))))
(should (= (point) 93))
;; Unfold
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 3 (length overlays))))
;; Position
(should (= (point) 93))))

Expand All @@ -157,17 +143,14 @@
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 4 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 29 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 29))
(should (or (= (overlay-end overlay) 150)
(= (overlay-end overlay) 151))))
(should (= (point) 16))
;; Unfold
(elpy-folding-toggle-at-point)
(let* ((overlays (apply 'nconc (overlay-lists)))
overlay)
(should (= 3 (length overlays))))
;; Position
(should (= (point) 16))))
10 changes: 2 additions & 8 deletions test/elpy-folding-fold-comments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
(elpy-folding-toggle-at-point)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 4 (length overlays)))
(setq overlay (nth 1 overlays))
(setq overlay (elpy-get-overlay-at 111 'comment))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'comment))
(should (= (overlay-start overlay) 111))
(should (= (overlay-end overlay) 156)))
(should (= (point) 92))
;; Unfold
(elpy-folding-toggle-at-point)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 3 (length overlays))))
;; Position
(should (= (point) 92))))

Expand All @@ -52,7 +49,4 @@
(python-mode)
(elpy-mode)
(elpy-folding-toggle-at-point)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 3 (length overlays))))
(should (= (point) 112))))
Loading