diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el index f0bea28ceac..7f47f7588ff 100644 --- a/lisp/progmodes/hideshow.el +++ b/lisp/progmodes/hideshow.el @@ -1425,7 +1425,7 @@ only blocks which are that many levels below the level of point." (message "Hide %d level" level)) (t (let* (hs-allow-nesting - (block (hs-block-positions nil :ad-end)) + (block (hs-block-positions :ad-beg :ad-end)) (ov (seq-find (lambda (o) (and (eq (overlay-get o 'invisible) 'hs))) @@ -1436,7 +1436,8 @@ only blocks which are that many levels below the level of point." (hs-hide-block) (message "Hide block and nested blocks")) ;; Hide the children blocks if the parent block is hidden - ((= (overlay-end ov) (cadr block)) + ((and (= (overlay-start ov) (car block)) + (= (overlay-end ov) (cadr block))) (apply #'hs-hide-level-recursive 1 block) (message "Hide first nested blocks")) ;; Otherwise show all in the parent block, we cannot use diff --git a/test/lisp/progmodes/hideshow-tests.el b/test/lisp/progmodes/hideshow-tests.el index 39161f2455c..49f661a2390 100644 --- a/test/lisp/progmodes/hideshow-tests.el +++ b/test/lisp/progmodes/hideshow-tests.el @@ -342,6 +342,90 @@ main() (funcall call-at "}") (should (string= (hideshow-tests-visible-string) contents))))) +(ert-deftest hideshow-cycle-with-delimiters () + "Should cycle the visibility of a block with delimiters." + (let ((contents " +int +main () +{ + { + { + } + } +} +")) + (hideshow-tests-with-temp-buffer + c-mode + contents + (hideshow-tests-look-at "{") + (hs-cycle 1) + (should (string= + (hideshow-tests-visible-string) + " +int +main () +{} +")) + (hs-cycle 1) + (should (string= + (hideshow-tests-visible-string) + " +int +main () +{ + {} +} +")) + (hs-cycle 1) + (should (string= + (hideshow-tests-visible-string) + contents))))) + +(ert-deftest hideshow-cycle-without-delimiters () + "Should cycle the visibility of a block without delimiters." + (let ((contents " +def test1 (): + def test2 (): + def test3(): +")) + (hideshow-tests-with-temp-buffer + python-mode + contents + (hideshow-tests-look-at "test1") + (hs-cycle 1) + (should (string= + (hideshow-tests-visible-string) + " +def test1 (): +")) + (hs-cycle 1) + (should (string= + (hideshow-tests-visible-string) + " +def test1 (): + def test2 (): +")) + (hs-cycle 1) + (should (string= + (hideshow-tests-visible-string) + contents))))) + +(ert-deftest hideshow-check-unbalanced-parens () + (let ((contents " +(defun test1 ()) + +(defun test2 +")) + (hideshow-tests-with-temp-buffer + c-mode + contents + (hideshow-tests-look-at "test1") + (beginning-of-line) + (should (hs-block-positions)) + (hideshow-tests-look-at "test2") + (beginning-of-line) + (should-not (hs-block-positions))))) + (provide 'hideshow-tests) ;;; hideshow-tests.el ends here