aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2024-03-22 16:46:28 -0400
committerStefan Monnier2024-03-22 16:46:28 -0400
commit7269a2f1586733bd03b569608bd77112b2e6487f (patch)
tree9acfc5c656863bf6ce482f32e1ca273929e83a59
parentaccd79c93935b50dddfcd6fe7fb6912c80bcddb1 (diff)
downloademacs-7269a2f1586733bd03b569608bd77112b2e6487f.tar.gz
emacs-7269a2f1586733bd03b569608bd77112b2e6487f.zip
(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.
-rw-r--r--lisp/emacs-lisp/pp.el14
-rw-r--r--test/lisp/emacs-lisp/pp-tests.el30
2 files changed, 39 insertions, 5 deletions
diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el
index de7468b3e38..b48f44545bf 100644
--- a/lisp/emacs-lisp/pp.el
+++ b/lisp/emacs-lisp/pp.el
@@ -193,11 +193,15 @@ it inserts and pretty-prints that arg at point."
193 (and 193 (and
194 (save-excursion 194 (save-excursion
195 (goto-char beg) 195 (goto-char beg)
196 (if (save-excursion (skip-chars-backward " \t({[',") 196 ;; We skip backward over open parens because cutting
197 (bolp)) 197 ;; the line right after an open paren does not help
198 ;; The sexp was already on its own line. 198 ;; reduce the indentation depth.
199 nil 199 ;; Similarly, we prefer to cut before a "." than after
200 (skip-chars-backward " \t") 200 ;; it because it reduces the indentation depth.
201 (skip-chars-backward " \t({[',.")
202 (if (bolp)
203 ;; The sexp already starts on its own line.
204 (progn (goto-char beg) nil)
201 (setq beg (copy-marker beg t)) 205 (setq beg (copy-marker beg t))
202 (if paired (setq paired (copy-marker paired t))) 206 (if paired (setq paired (copy-marker paired t)))
203 ;; We could try to undo this insertion if it 207 ;; We could try to undo this insertion if it
diff --git a/test/lisp/emacs-lisp/pp-tests.el b/test/lisp/emacs-lisp/pp-tests.el
index b663fb365a8..7f7c798cde8 100644
--- a/test/lisp/emacs-lisp/pp-tests.el
+++ b/test/lisp/emacs-lisp/pp-tests.el
@@ -36,4 +36,34 @@
36(ert-deftest test-indentation () 36(ert-deftest test-indentation ()
37 (ert-test-erts-file (ert-resource-file "code-formats.erts"))) 37 (ert-test-erts-file (ert-resource-file "code-formats.erts")))
38 38
39(defun pp-tests--dimensions ()
40 (save-excursion
41 (let ((width 0)
42 (height 0))
43 (goto-char (point-min))
44 (while (not (eobp))
45 (end-of-line)
46 (setq height (1+ height))
47 (setq width (max width (current-column)))
48 (forward-char 1))
49 (cons width height))))
50
51(ert-deftest pp-tests--cut-before ()
52 (with-temp-buffer
53 (lisp-data-mode)
54 (pp '(1 (quite-a-long-package-name
55 . [(0 10 0) ((avy (0 5 0))) "Quickly switch windows." tar
56 ((:url . "https://github.com/abo-abo/ace-window")
57 (:maintainer "Oleh Krehel" . "ohwoeowho@gmail.com")
58 (:authors ("Oleh Krehel" . "ohwoeowho@gmail.com"))
59 (:keywords "window" "location"))]))
60 (current-buffer))
61 ;; (message "Filled:\n%s" (buffer-string))
62 (let ((dimensions (pp-tests--dimensions)))
63 (should (< (car dimensions) 80))
64 (should (< (cdr dimensions) 8)))
65 (goto-char (point-min))
66 (while (search-forward "." nil t)
67 (should (not (eolp))))))
68
39;;; pp-tests.el ends here. 69;;; pp-tests.el ends here.