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).
This commit is contained in:
parent
61a5bbc683
commit
985e7148a7
3 changed files with 76 additions and 9 deletions
|
@ -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)))
|
||||
|
||||
|
|
40
test/lisp/progmodes/sh-script-resources/sh-indents.erts
Normal file
40
test/lisp/progmodes/sh-script-resources/sh-indents.erts
Normal file
|
@ -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
|
||||
=-=-=
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue