summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp
diff options
context:
space:
mode:
authorNoam Postavsky <npostavs@gmail.com>2017-04-23 10:43:05 -0400
committerNoam Postavsky <npostavs@gmail.com>2017-05-09 20:50:19 -0400
commite7b6751c0a74f24c14cd207d57a4e1a95f409256 (patch)
tree14f60933c9d78aabf42ba79abd911f9ef3e3d373 /test/lisp/emacs-lisp
parent17e540aa428c5392f7a9b4c1f7495bac8a8fe5da (diff)
downloademacs-e7b6751c0a74f24c14cd207d57a4e1a95f409256.tar.gz
emacs-e7b6751c0a74f24c14cd207d57a4e1a95f409256.tar.bz2
emacs-e7b6751c0a74f24c14cd207d57a4e1a95f409256.zip
Fix lisp-indent-region and indent-sexp (Bug#26619)
The new lisp-indent-region introduced in 2017-04-22 "Add new `lisp-indent-region' that doesn't reparse the code." is broken because it doesn't save the calculated indent amounts for already seen sexp depths. Fix this by unifying the indent-sexp and lisp-indent-region code. Furthermore, only preserve position 2 of the running parse when the depth doesn't change. * lisp/emacs-lisp/lisp-mode.el (lisp-ppss): Use an OLDSTATE that corresponds with the start point when calling parse-partial-sexp. (lisp-indent-state): New struct. (lisp-indent-calc-next): New function, extracted from indent-sexp. (indent-sexp, lisp-indent-region): Use it. (lisp-indent-line): Take indentation, instead of parse state. * test/lisp/emacs-lisp/lisp-mode-tests.el (lisp-mode-tests--correctly-indented-sexp): New constant. (lisp-indent-region, lisp-indent-region-defun-with-docstring): (lisp-indent-region-open-paren, lisp-indent-region-in-sexp): New tests.
Diffstat (limited to 'test/lisp/emacs-lisp')
-rw-r--r--test/lisp/emacs-lisp/lisp-mode-tests.el85
1 files changed, 80 insertions, 5 deletions
diff --git a/test/lisp/emacs-lisp/lisp-mode-tests.el b/test/lisp/emacs-lisp/lisp-mode-tests.el
index 27f0bb5ec13..1f78eb30105 100644
--- a/test/lisp/emacs-lisp/lisp-mode-tests.el
+++ b/test/lisp/emacs-lisp/lisp-mode-tests.el
@@ -21,10 +21,7 @@
(require 'cl-lib)
(require 'lisp-mode)
-(ert-deftest indent-sexp ()
- "Test basics of \\[indent-sexp]."
- (with-temp-buffer
- (insert "\
+(defconst lisp-mode-tests--correctly-indented-sexp "\
\(a
(prog1
(prog1
@@ -42,9 +39,14 @@ noindent\" 3
2) ; comment
;; comment
b)")
+
+(ert-deftest indent-sexp ()
+ "Test basics of \\[indent-sexp]."
+ (with-temp-buffer
+ (insert lisp-mode-tests--correctly-indented-sexp)
(goto-char (point-min))
(let ((indent-tabs-mode nil)
- (correct (buffer-string)))
+ (correct lisp-mode-tests--correctly-indented-sexp))
(dolist (mode '(fundamental-mode emacs-lisp-mode))
(funcall mode)
(indent-sexp)
@@ -97,5 +99,78 @@ noindent\" 3
(indent-sexp)
(should (equal (buffer-string) correct)))))
+(ert-deftest lisp-indent-region ()
+ "Test basics of `lisp-indent-region'."
+ (with-temp-buffer
+ (insert lisp-mode-tests--correctly-indented-sexp)
+ (goto-char (point-min))
+ (let ((indent-tabs-mode nil)
+ (correct lisp-mode-tests--correctly-indented-sexp))
+ (emacs-lisp-mode)
+ (indent-region (point-min) (point-max))
+ ;; Don't mess up correctly indented code.
+ (should (string= (buffer-string) correct))
+ ;; Correctly add indentation.
+ (save-excursion
+ (while (not (eobp))
+ (delete-horizontal-space)
+ (forward-line)))
+ (indent-region (point-min) (point-max))
+ (should (equal (buffer-string) correct))
+ ;; Correctly remove indentation.
+ (save-excursion
+ (let ((n 0))
+ (while (not (eobp))
+ (unless (looking-at "noindent\\|^[[:blank:]]*$")
+ (insert (make-string n ?\s)))
+ (cl-incf n)
+ (forward-line))))
+ (indent-region (point-min) (point-max))
+ (should (equal (buffer-string) correct)))))
+
+
+(ert-deftest lisp-indent-region-defun-with-docstring ()
+ "Test Bug#26619."
+ (with-temp-buffer
+ (insert "\
+\(defun test ()
+ \"This is a test.
+Test indentation in emacs-lisp-mode\"
+ (message \"Hi!\"))")
+ (let ((indent-tabs-mode nil)
+ (correct (buffer-string)))
+ (emacs-lisp-mode)
+ (indent-region (point-min) (point-max))
+ (should (equal (buffer-string) correct)))))
+
+(ert-deftest lisp-indent-region-open-paren ()
+ (with-temp-buffer
+ (insert "\
+\(with-eval-after-load 'foo
+ (setq bar `(
+ baz)))")
+ (let ((indent-tabs-mode nil)
+ (correct (buffer-string)))
+ (emacs-lisp-mode)
+ (indent-region (point-min) (point-max))
+ (should (equal (buffer-string) correct)))))
+
+(ert-deftest lisp-indent-region-in-sexp ()
+ (with-temp-buffer
+ (insert "\
+\(when t
+ (when t
+ (list 1 2 3)
+ 'etc)
+ (quote etc)
+ (quote etc))")
+ (let ((indent-tabs-mode nil)
+ (correct (buffer-string)))
+ (emacs-lisp-mode)
+ (search-backward "1")
+ (indent-region (point) (point-max))
+ (should (equal (buffer-string) correct)))))
+
+
(provide 'lisp-mode-tests)
;;; lisp-mode-tests.el ends here