]> git.eshelyaron.com Git - emacs.git/commitdiff
(pp-fill): Cut before parens and dots
authorStefan Monnier <monnier@iro.umontreal.ca>
Fri, 22 Mar 2024 20:46:28 +0000 (16:46 -0400)
committerEshel Yaron <me@eshelyaron.com>
Sun, 24 Mar 2024 14:20:32 +0000 (15:20 +0100)
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.

(cherry picked from commit 7269a2f1586733bd03b569608bd77112b2e6487f)

lisp/emacs-lisp/pp.el
test/lisp/emacs-lisp/pp-tests.el

index de7468b3e38a74da5be05edb051ce23d7fdf8421..b48f44545bfec1ae39975109c11bee836237e7a0 100644 (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
index b663fb365a892b7db7ed851b7085c38ef3ea4333..7f7c798cde886635c8c8eff1f3212132d58920b9 100644 (file)
 (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.