emacs-diffs
[Top][All Lists]
Advanced

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

master 985e7148a7: Improve indentation of some shell script forms


From: Lars Ingebrigtsen
Subject: master 985e7148a7: Improve indentation of some shell script forms
Date: Mon, 7 Feb 2022 03:14:01 -0500 (EST)

branch: master
commit 985e7148a7576327e30fe9c48414a5c033ca42b2
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Improve indentation of some shell script forms
    
    * lisp/progmodes/sh-script.el (sh-smie--default-backward-token):
    Don't skip past things like "true;then" (bug#53817).
---
 lisp/progmodes/sh-script.el                        | 24 ++++++++-----
 .../progmodes/sh-script-resources/sh-indents.erts  | 40 ++++++++++++++++++++++
 test/lisp/progmodes/sh-script-tests.el             | 21 ++++++++++++
 3 files changed, 76 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index 0a2ec348c1..8dc5562143 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -1774,21 +1774,27 @@ Does not preserve point."
         (n (skip-syntax-backward ".")))
     (if (or (zerop n)
             (and (eq n -1)
+                 ;; Skip past quoted white space.
                  (let ((p (point)))
                    (if (eq -1 (% (skip-syntax-backward "\\") 2))
                        t
                      (goto-char p)
                      nil))))
         (while
-            (progn (skip-syntax-backward ".w_'")
-                   (or (not (zerop (skip-syntax-backward "\\")))
-                       (when (eq ?\\ (char-before (1- (point))))
-                         (let ((p (point)))
-                           (forward-char -1)
-                           (if (eq -1 (% (skip-syntax-backward "\\") 2))
-                               t
-                             (goto-char p)
-                             nil))))))
+            (progn
+              ;; Skip past words, but stop at semicolons.
+              (while (and (not (zerop (skip-syntax-backward "w_'")))
+                          (not (eq (char-before (point)) ?\;))
+                          (skip-syntax-backward ".")))
+              (or (not (zerop (skip-syntax-backward "\\")))
+                  ;; Skip past quoted white space.
+                  (when (eq ?\\ (char-before (1- (point))))
+                    (let ((p (point)))
+                      (forward-char -1)
+                      (if (eq -1 (% (skip-syntax-backward "\\") 2))
+                          t
+                        (goto-char p)
+                        nil))))))
       (goto-char (- (point) (% (skip-syntax-backward "\\") 2))))
     (buffer-substring-no-properties (point) pos)))
 
diff --git a/test/lisp/progmodes/sh-script-resources/sh-indents.erts 
b/test/lisp/progmodes/sh-script-resources/sh-indents.erts
new file mode 100644
index 0000000000..1f92610b3a
--- /dev/null
+++ b/test/lisp/progmodes/sh-script-resources/sh-indents.erts
@@ -0,0 +1,40 @@
+Code:
+  (lambda ()
+    (shell-script-mode)
+    (indent-region (point-min) (point-max)))
+
+Name: sh-indents1
+
+=-=
+if test;then
+    something
+fi
+other
+=-=-=
+
+Name: sh-indents2
+
+=-=
+if test; then
+    something
+fi
+other
+=-=-=
+
+Name: sh-indents3
+
+=-=
+if test ; then
+    something
+fi
+other
+=-=-=
+
+Name: sh-indents4
+
+=-=
+if test ;then
+    something
+fi
+other
+=-=-=
diff --git a/test/lisp/progmodes/sh-script-tests.el 
b/test/lisp/progmodes/sh-script-tests.el
index ebd26ab429..5d01cc1c22 100644
--- a/test/lisp/progmodes/sh-script-tests.el
+++ b/test/lisp/progmodes/sh-script-tests.el
@@ -23,6 +23,7 @@
 
 (require 'sh-script)
 (require 'ert)
+(require 'ert-x)
 
 (ert-deftest test-sh-script-indentation ()
   (with-temp-buffer
@@ -48,4 +49,24 @@
 }
 "))))
 
+(ert-deftest test-indentation ()
+  (ert-test-erts-file (ert-resource-file "sh-indents.erts")))
+
+(defun test-sh-back (string &optional pos)
+  (with-temp-buffer
+    (shell-script-mode)
+    (insert string)
+    (sh-smie--default-backward-token)
+    (= (point) (or pos 1))))
+
+(ert-deftest test-backward-token ()
+  (should (test-sh-back "foo"))
+  (should (test-sh-back "foo.bar"))
+  (should (test-sh-back "foo\\1bar"))
+  (should (test-sh-back "foo\\\nbar"))
+  (should (test-sh-back "foo\\\n\\\n\\\nbar"))
+  (should (test-sh-back "foo"))
+  (should-not (test-sh-back "foo;bar"))
+  (should (test-sh-back "foo#zot")))
+
 ;;; sh-script-tests.el ends here



reply via email to

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