Skip to content

Commit

Permalink
Better docstring detection when folding
Browse files Browse the repository at this point in the history
  • Loading branch information
galaunay committed Apr 18, 2020
1 parent fc54812 commit 1cf7788
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 45 deletions.
24 changes: 8 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,18 +3568,19 @@ 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))))))))


;;;;;;;;;;;;;;;;;;;
;;; Module: Flymake

Expand Down
9 changes: 6 additions & 3 deletions test/elpy-folding-fold-all-comments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@
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)))
Expand Down
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
15 changes: 10 additions & 5 deletions test/elpy-folding-fold-blocks-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
(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)
Expand Down Expand Up @@ -40,7 +41,8 @@
(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)
Expand Down Expand Up @@ -90,7 +92,8 @@
(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)
Expand Down Expand Up @@ -124,7 +127,8 @@
(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)
Expand Down Expand Up @@ -158,7 +162,8 @@
(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)
Expand Down
3 changes: 2 additions & 1 deletion test/elpy-folding-fold-comments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
(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)))
Expand Down
27 changes: 16 additions & 11 deletions test/elpy-folding-fold-docstrings-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
;; (buffer-substring (overlay-start overlay)
;; (overlay-end overlay))))
(should (= 4 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 138 'docstring))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'docstring))
(should (= (overlay-start overlay) 138))
(should (= (overlay-end overlay) 212)))
Expand Down Expand Up @@ -70,7 +71,8 @@
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 4 (length overlays)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 142 'docstring))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'docstring))
(should (= (overlay-start overlay) 142))
(should (= (overlay-end overlay) 216)))
Expand Down Expand Up @@ -114,8 +116,8 @@
" self.a = a"
" self.b = b"
" def bar(mess):"
" \" This is just _|_a string\""
" mess *= 2"
" \" This is just _|_a string\""
" print(mess)"
" return mess"
"var2 = foo(var1, 4)")
Expand Down Expand Up @@ -143,12 +145,13 @@
"var2 = foo(var1, 4)")
(python-mode)
(elpy-mode)
(elpy-folding-toggle-at-point)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 4 (length overlays)))
(dolist (overlay overlays)
(should-not (eq (overlay-get overlay 'hs) 'docstring))))))
(let ((nmb-overlays (length (overlays-in (point-min) (point-max)))))
(elpy-folding-toggle-at-point)
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= nmb-overlays (length overlays)))
(dolist (overlay overlays)
(should-not (eq (overlay-get overlay 'hs) 'docstring)))))))

(ert-deftest elpy-fold-at-point-should-NOT-fold-strings-3 ()
(elpy-testcase ()
Expand All @@ -171,7 +174,8 @@
(let* ((overlays (overlays-in (point-min) (point-max)))
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) 190)
Expand Down Expand Up @@ -203,7 +207,8 @@
(let* ((overlays (overlays-in (point-min) (point-max)))
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) 229)
Expand Down
7 changes: 5 additions & 2 deletions test/elpy-folding-fold-leafs-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
(let* ((overlays (overlays-in (point-min) (point-max)))
overlay)
(should (= 6 (length overlays)))
(setq overlay (nth 3 overlays))

(setq overlay (elpy-get-overlay-at 57 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 57))
(should (or (= (overlay-end overlay) 87)
(= (overlay-end overlay) 88)))
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 138 'code))
(should overlay)
(should (eq (overlay-get overlay 'hs) 'code))
(should (= (overlay-start overlay) 138))
(should (or (= (overlay-end overlay) 156)
Expand Down
6 changes: 4 additions & 2 deletions test/elpy-folding-fold-on-click-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
(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)
Expand Down Expand Up @@ -43,7 +44,8 @@
(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)
Expand Down
9 changes: 6 additions & 3 deletions test/elpy-folding-should-mark-foldable-lines-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
overlay)
(should (= 3 (length overlays)))
;; Second mark
(setq overlay (nth 2 overlays))
(setq overlay (elpy-get-overlay-at 11 nil))
(should overlay)
(should (eq (overlay-get overlay 'hs) nil))
(should (= (overlay-start overlay) 11))
(should (= (overlay-end overlay) 29))
;; Second mark
(setq overlay (nth 1 overlays))
(setq overlay (elpy-get-overlay-at 30 nil))
(should overlay)
(should (eq (overlay-get overlay 'hs) nil))
(should (= (overlay-start overlay) 30))
(should (= (overlay-end overlay) 57))
;; Third mark
(setq overlay (nth 0 overlays))
(setq overlay (elpy-get-overlay-at 88 nil))
(should overlay)
(should (eq (overlay-get overlay 'hs) nil))
(should (= (overlay-start overlay) 88))
(should (= (overlay-end overlay) 104)))))
6 changes: 6 additions & 0 deletions test/test-helper.el
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,11 @@ for that file."
,(buffer-string-with-point)))
(put 'buffer-be 'ert-explainer 'buffer-be-explainer)

(defun elpy-get-overlay-at (start kind)
(dolist (tmp-overlay overlays overlay)
(when (and (= (overlay-start tmp-overlay) start)
(eq (overlay-get tmp-overlay 'hs) kind))
(setq overlay tmp-overlay))))

(setq yas-verbosity 0)
(setq yas-snippet-dirs ())

0 comments on commit 1cf7788

Please sign in to comment.