emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/adjust-parens f788d92 2/8: * Makefile: New file to prov


From: Stefan Monnier
Subject: [elpa] externals/adjust-parens f788d92 2/8: * Makefile: New file to provide 'make check' tests
Date: Tue, 1 Dec 2020 15:11:59 -0500 (EST)

branch: externals/adjust-parens
commit f788d925cfe42eecfdfbf328249fbe0191c07262
Author: Barry O'Reilly <gundaetiapo@gmail.com>
Commit: Barry O'Reilly <gundaetiapo@gmail.com>

    * Makefile: New file to provide 'make check' tests
    * adjust-parens-tests.el: New tests
    (apt-near-bob-test): Test indenting near BOB
    (apt-indent-dedent-test): Test same case described in Commentary
    * adjust-parens.el: Fix bug when near BOB or EOB. Fix compiler
    warning due to using defvarred prefix-arg as lexical var.
---
 Makefile               | 16 ++++++++++
 adjust-parens-tests.el | 66 ++++++++++++++++++++++++++++++++++++++++
 adjust-parens.el       | 82 ++++++++++++++++++++++++++++++--------------------
 3 files changed, 131 insertions(+), 33 deletions(-)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..30b1e3f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,16 @@
+.PHONY: all clean
+
+ELCFILES = $(addsuffix .elc, $(basename $(wildcard *.el)))
+
+all: $(ELCFILES)
+
+%.elc : %.el
+       @echo Compiling $<
+       @emacs --batch -q --no-site-file -L . -f batch-byte-compile $<
+
+clean:
+       @rm -f *.elc
+
+check: $(ELCFILES)
+       @emacs --batch -q --no-site-file -L . -l adjust-parens-tests.el -f 
ert-run-tests-batch-and-exit
+
diff --git a/adjust-parens-tests.el b/adjust-parens-tests.el
new file mode 100644
index 0000000..5b249c5
--- /dev/null
+++ b/adjust-parens-tests.el
@@ -0,0 +1,66 @@
+;;; adjust-parens-tests.el --- Tests of adjust-parens package
+
+;; Copyright (C) 2013  Free Software Foundation, Inc.
+
+;; Author: Barry O'Reilly <gundaetiapo@gmail.com>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'ert)
+(require 'adjust-parens)
+
+(defun apt-check-buffer (text-before-point text-after-point)
+  (should (string= text-before-point
+                   (buffer-substring-no-properties (point-min)
+                                                   (point))))
+  (should (string= text-after-point
+                   (buffer-substring-no-properties (point)
+                                                   (point-max)))))
+
+(ert-deftest apt-near-bob-test ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "(foo)\n")
+    (lisp-indent-adjust-parens)
+    (apt-check-buffer "(foo\n " ")")))
+
+(ert-deftest apt-indent-dedent-test ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (setq indent-tabs-mode nil)
+    (insert ";;\n"
+            "(let ((x 10) (y (some-func 20))))\n"
+            "; Comment")
+    (beginning-of-line)
+    (lisp-indent-adjust-parens)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20)))\n"
+                              "  ")
+                      "); Comment")
+    (lisp-indent-adjust-parens 3)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20\n"
+                              "                           ")
+                      ")))); Comment")
+    (lisp-dedent-adjust-parens 2)
+    (apt-check-buffer (concat ";;\n"
+                              "(let ((x 10) (y (some-func 20))\n"
+                              "      ")
+                      ")); Comment")))
+
+;;; adjust-parens-tests.el ends here
diff --git a/adjust-parens.el b/adjust-parens.el
index 912f7ea..003302d 100644
--- a/adjust-parens.el
+++ b/adjust-parens.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2013  Free Software Foundation, Inc.
 
 ;; Author: Barry O'Reilly <gundaetiapo@gmail.com>
-;; Version: 1.0
+;; Version: 1.1
 
 ;; This program is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
@@ -100,7 +100,6 @@
 ;; Future work:
 ;;   - Consider taking a region as input in order to indent a sexp and
 ;;     its siblings in the region. Dedenting would not take a region.
-;;   - Write tests
 
 (require 'cl)
 
@@ -109,6 +108,8 @@
 return the position of the last sexp that had depth REL-DEPTH relative
 to FROM-POS. Returns nil if REL-DEPTH is not reached.
 
+May change point.
+
 Examples:
   Region:   a (b c (d)) e (f g (h i)) j
 
@@ -119,29 +120,41 @@ Examples:
   Returns:  position of (h i)
 
 This function assumes FROM-POS is not in a string or comment."
-  (save-excursion
-    (goto-char from-pos)
-    (let (the-last-pos
-          (parse-state '(0 nil nil nil nil nil nil nil nil)))
-      (while (< (point) to-pos)
-        (setq parse-state
-              (parse-partial-sexp (point)
-                                  to-pos
-                                  nil
-                                  t ; Stop before sexp
-                                  parse-state))
-        (and (not (eq (point) to-pos))
-             (eq (car parse-state) rel-depth)
-             (setq the-last-pos (point)))
-        ;; The previous parse may not advance. To advance and maintain
-        ;; correctness of depth, we parse over the next char.
+  (goto-char from-pos)
+  (let (the-last-pos
+        (parse-state '(0 nil nil nil nil nil nil nil nil)))
+    (while (< (point) to-pos)
+      (setq parse-state
+            (parse-partial-sexp (point)
+                                to-pos
+                                nil
+                                t ; Stop before sexp
+                                parse-state))
+      (and (not (eq (point) to-pos))
+           (eq (car parse-state) rel-depth)
+           (setq the-last-pos (point)))
+      ;; The previous parse may not advance. To advance and maintain
+      ;; correctness of depth, we parse over the next char.
+      (when (< (point) to-pos)
         (setq parse-state
               (parse-partial-sexp (point)
                                   (1+ (point))
                                   nil
                                   nil
-                                  parse-state)))
-      the-last-pos)))
+                                  parse-state))))
+    the-last-pos))
+
+
+(defun adjust-parens-check-prior-sexp ()
+  "Returns true if there is a full sexp before point, else false.
+
+May change point."
+  (let ((pos1 (progn (backward-sexp)
+                      (point)))
+        (pos2 (progn (forward-sexp)
+                     (backward-sexp)
+                     (point))))
+    (>= pos1 pos2)))
 
 (defun adjust-close-paren-for-indent ()
   "Adjust a close parentheses of a sexp so as
@@ -157,9 +170,11 @@ scan-error to propogate up."
     (let ((deleted-paren-pos
            (save-excursion
              (beginning-of-line)
-             (backward-sexp)
              ;; Account for edge case when point has no sexp before it
-             (if (bobp)
+             ;;
+             ;; This is primarily to avoid funny behavior when there
+             ;; is no sexp between bob and point.
+             (if (not (adjust-parens-check-prior-sexp))
                  nil
                ;; If the sexp at point is a list,
                ;; delete its closing paren
@@ -170,10 +185,11 @@ scan-error to propogate up."
                  (point))))))
       (when deleted-paren-pos
         (let ((sexp-to-close
-               (last-sexp-with-relative-depth (point)
-                                              (progn (end-of-line)
-                                                     (point))
-                                              0)))
+               (save-excursion
+                 (last-sexp-with-relative-depth (point)
+                                                (progn (end-of-line)
+                                                       (point))
+                                                0))))
           (when sexp-to-close
             (goto-char sexp-to-close)
             (forward-sexp))
@@ -227,11 +243,11 @@ scan-error to propogate up."
       (and (not (use-region-p))
            (<= orig-pos (point))))))
 
-(defun adjust-parens-and-indent (adjust-function prefix-arg)
+(defun adjust-parens-and-indent (adjust-function parg)
   "Adjust close parens and indent the region over which the parens
 moved."
   (let ((region-of-change (list (point) (point))))
-    (cl-loop for i from 1 to (or prefix-arg 1)
+    (cl-loop for i from 1 to (or parg 1)
              with finished = nil
              while (not finished)
              do
@@ -251,7 +267,7 @@ moved."
     (apply 'indent-region region-of-change))
   (back-to-indentation))
 
-(defun lisp-indent-adjust-parens (&optional prefix-arg)
+(defun lisp-indent-adjust-parens (&optional parg)
   "Indent Lisp code to the next level while adjusting sexp balanced
 expressions to be consistent.
 
@@ -260,10 +276,10 @@ potentially calls the latter."
   (interactive "P")
   (if (adjust-parens-p)
       (adjust-parens-and-indent 'adjust-close-paren-for-indent
-                                prefix-arg)
-    (indent-for-tab-command prefix-arg)))
+                                parg)
+    (indent-for-tab-command parg)))
 
-(defun lisp-dedent-adjust-parens (&optional prefix-arg)
+(defun lisp-dedent-adjust-parens (&optional parg)
   "Dedent Lisp code to the previous level while adjusting sexp
 balanced expressions to be consistent.
 
@@ -271,7 +287,7 @@ Binding to <backtab> (ie Shift-Tab) is a sensible choice."
   (interactive "P")
   (when (adjust-parens-p)
     (adjust-parens-and-indent 'adjust-close-paren-for-dedent
-                              prefix-arg)))
+                              parg)))
 
 (add-hook 'emacs-lisp-mode-hook
           (lambda ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]