(pp-fill): Cut before parens and dots

The `pp-fill` code sometimes end up generating things like:

    (foo .
         bar)

instead of

    (foo
     .  bar)

so make sure we cut before rather than after the dot (and open
parens while we're at it).

* lisp/emacs-lisp/pp.el (pp-fill): Cut before parens and dots.

* test/lisp/emacs-lisp/pp-tests.el (pp-tests--dimensions): New function.
(pp-tests--cut-before): New test.
This commit is contained in:
Stefan Monnier 2024-03-22 16:46:28 -04:00
parent accd79c939
commit 7269a2f158
2 changed files with 39 additions and 5 deletions

View file

@ -193,11 +193,15 @@ it inserts and pretty-prints that arg at point."
(and
(save-excursion
(goto-char beg)
(if (save-excursion (skip-chars-backward " \t({[',")
(bolp))
;; The sexp was already on its own line.
nil
(skip-chars-backward " \t")
;; We skip backward over open parens because cutting
;; the line right after an open paren does not help
;; reduce the indentation depth.
;; Similarly, we prefer to cut before a "." than after
;; it because it reduces the indentation depth.
(skip-chars-backward " \t({[',.")
(if (bolp)
;; The sexp already starts on its own line.
(progn (goto-char beg) nil)
(setq beg (copy-marker beg t))
(if paired (setq paired (copy-marker paired t)))
;; We could try to undo this insertion if it

View file

@ -36,4 +36,34 @@
(ert-deftest test-indentation ()
(ert-test-erts-file (ert-resource-file "code-formats.erts")))
(defun pp-tests--dimensions ()
(save-excursion
(let ((width 0)
(height 0))
(goto-char (point-min))
(while (not (eobp))
(end-of-line)
(setq height (1+ height))
(setq width (max width (current-column)))
(forward-char 1))
(cons width height))))
(ert-deftest pp-tests--cut-before ()
(with-temp-buffer
(lisp-data-mode)
(pp '(1 (quite-a-long-package-name
. [(0 10 0) ((avy (0 5 0))) "Quickly switch windows." tar
((:url . "https://github.com/abo-abo/ace-window")
(:maintainer "Oleh Krehel" . "ohwoeowho@gmail.com")
(:authors ("Oleh Krehel" . "ohwoeowho@gmail.com"))
(:keywords "window" "location"))]))
(current-buffer))
;; (message "Filled:\n%s" (buffer-string))
(let ((dimensions (pp-tests--dimensions)))
(should (< (car dimensions) 80))
(should (< (cdr dimensions) 8)))
(goto-char (point-min))
(while (search-forward "." nil t)
(should (not (eolp))))))
;;; pp-tests.el ends here.