diff --git a/elpy.el b/elpy.el index 2ca69e123..8e39ad25c 100644 --- a/elpy.el +++ b/elpy.el @@ -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. @@ -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) @@ -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))) @@ -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 @@ -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 @@ -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)))))))) @@ -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 diff --git a/test/elpy-folding-fold-all-comments-test.el b/test/elpy-folding-fold-all-comments-test.el index 517525bc5..dadc605c5 100644 --- a/test/elpy-folding-fold-all-comments-test.el +++ b/test/elpy-folding-fold-all-comments-test.el @@ -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))) diff --git a/test/elpy-folding-fold-all-docstrings-test.el b/test/elpy-folding-fold-all-docstrings-test.el index 5db8589e2..932437791 100644 --- a/test/elpy-folding-fold-all-docstrings-test.el +++ b/test/elpy-folding-fold-all-docstrings-test.el @@ -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))) diff --git a/test/elpy-folding-fold-blocks-test.el b/test/elpy-folding-fold-blocks-test.el index 18c99fa0f..ee391d818 100644 --- a/test/elpy-folding-fold-blocks-test.el +++ b/test/elpy-folding-fold-blocks-test.el @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/test/elpy-folding-fold-comments-test.el b/test/elpy-folding-fold-comments-test.el index b96c0fcda..89d5b29a9 100644 --- a/test/elpy-folding-fold-comments-test.el +++ b/test/elpy-folding-fold-comments-test.el @@ -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))) diff --git a/test/elpy-folding-fold-docstrings-test.el b/test/elpy-folding-fold-docstrings-test.el index 3235b03ee..d11a57eb7 100644 --- a/test/elpy-folding-fold-docstrings-test.el +++ b/test/elpy-folding-fold-docstrings-test.el @@ -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))) @@ -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))) @@ -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)") @@ -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 () @@ -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) @@ -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) diff --git a/test/elpy-folding-fold-leafs-test.el b/test/elpy-folding-fold-leafs-test.el index 6c6957cc3..9acb7a715 100644 --- a/test/elpy-folding-fold-leafs-test.el +++ b/test/elpy-folding-fold-leafs-test.el @@ -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) diff --git a/test/elpy-folding-fold-on-click-test.el b/test/elpy-folding-fold-on-click-test.el index cd3ef8769..6ac7ff28d 100644 --- a/test/elpy-folding-fold-on-click-test.el +++ b/test/elpy-folding-fold-on-click-test.el @@ -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) @@ -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) diff --git a/test/elpy-folding-should-mark-foldable-lines-test.el b/test/elpy-folding-should-mark-foldable-lines-test.el index cc13116c1..6d1691095 100644 --- a/test/elpy-folding-should-mark-foldable-lines-test.el +++ b/test/elpy-folding-should-mark-foldable-lines-test.el @@ -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))))) diff --git a/test/test-helper.el b/test/test-helper.el index e9f681017..82aa1622e 100644 --- a/test/test-helper.el +++ b/test/test-helper.el @@ -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 ())