branch: elpa/lua-mode
commit 1ede9073fd8437bad52aab10e75e9e42c0e58100
Author: immerrr <[email protected]>
Commit: immerrr <[email protected]>
Fix several cases for `indent-new-comment-line' (M-j)
---
Makefile | 1 +
ert-tests/lua-font-lock-test-helpers.el | 23 ++++++++-
ert-tests/test-strings-and-comments.el | 87 +++++++++++++++++++++++++++++++++
lua-mode.el | 5 +-
4 files changed, 113 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
index 3496186..1a7407c 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,7 @@ TESTS += ert-tests/test-defun-font-lock.el
TESTS += ert-tests/test-builtin-font-lock.el
TESTS += ert-tests/test-electric-mode.el
TESTS += ert-tests/test-indentation.el
+TESTS += ert-tests/test-strings-and-comments.el
default:
@echo version is $(VERSION)
diff --git a/ert-tests/lua-font-lock-test-helpers.el
b/ert-tests/lua-font-lock-test-helpers.el
index e52fc0f..ce98b32 100644
--- a/ert-tests/lua-font-lock-test-helpers.el
+++ b/ert-tests/lua-font-lock-test-helpers.el
@@ -53,6 +53,9 @@ This is a mere typing/reading aid for lua-mode's font-lock
tests."
(setq font-lock-verbose nil)
+(defun lua-join-lines (strs)
+ (mapconcat (lambda (x) (concat x "\n")) strs ""))
+
(defmacro with-lua-buffer (&rest body)
`(with-temp-buffer
(switch-to-buffer (current-buffer))
@@ -64,13 +67,29 @@ This is a mere typing/reading aid for lua-mode's font-lock
tests."
(butlast
(split-string
(with-lua-buffer
- (insert (replace-regexp-in-string
- "^\\s *" "" (mapconcat (lambda (x) (concat x "\n")) strs "")))
+ (insert (replace-regexp-in-string "^\\s *" "" (lua-join-lines strs)))
(indent-region (point-min) (point-max))
(buffer-substring-no-properties
(point-min) (point-max)))
"\n" nil)))
+(defun lua-insert-goto-<> (strs)
+ "Insert sequence of strings and put point in place of \"<>\"."
+ (insert (lua-join-lines strs))
+ (goto-char (point-min))
+ (re-search-forward "<>")
+ (replace-match "")
+ ;; Inserted text may contain multiline constructs which will only be
+ ;; recognized after fontification.
+ (font-lock-fontify-buffer))
+
+(defmacro lua-buffer-strs (&rest body)
+ `(butlast
+ (split-string
+ (with-lua-buffer
+ (progn ,@body)
+ (buffer-substring-no-properties (point-min) (point-max)))
+ "\n" nil)))
(defmacro should-lua-indent (strs)
`(should
(equal ,strs (lua-get-indented-strs ,strs))))
diff --git a/ert-tests/test-strings-and-comments.el
b/ert-tests/test-strings-and-comments.el
new file mode 100644
index 0000000..904805a
--- /dev/null
+++ b/ert-tests/test-strings-and-comments.el
@@ -0,0 +1,87 @@
+(require 'ert)
+(require 'lua-font-lock-test-helpers
+ ;; let's try a bit to help Emacs find the helpers, just in case
+ (concat (file-name-directory (or load-file-name (buffer-file-name)
+ default-directory))
+ "lua-font-lock-test-helpers.el"))
+
+(defmacro should= (lhs rhs)
+ `(should (equal ,lhs ,rhs)))
+
+(ert-deftest lua-M-j-works-for-simple-comment ()
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("-- foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("-- foobar"
+ "-- "))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("xyzzy -- foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("xyzzy -- foobar"
+ "-- "))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("xyz<> xyzzy -- foobar"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("xyz"
+ "xyzzy -- foobar")))
+
+
+(ert-deftest lua-M-j-works-for-longer-comment ()
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("---- foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("---- foobar"
+ "---- "))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("xyzzy ---- foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("xyzzy ---- foobar"
+ "---- ")))
+
+(ert-deftest lua-M-j-handles-string-and-multiline-comments ()
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("\"-- \" .. foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("\"-- \" .. foobar"
+ ""))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("'-- ' .. foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("'-- ' .. foobar"
+ ""))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("[[-- ]] .. foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("[[-- ]] .. foobar"
+ ""))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("--[[-- ]] .. foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("--[[-- ]] .. foobar"
+ ""))
+
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("---[[-- ]] .. foobar <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("---[[-- ]] .. foobar"
+ "---")))
+
+(ert-deftest lua-M-j-works-if-comment-is-empty ()
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("-- <>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("--"
+ "--"))
+
+ ;; Let's make sure that whitespace is optional.
+ (should= (lua-buffer-strs
+ (lua-insert-goto-<> '("--<>"))
+ (execute-kbd-macro (kbd "M-j")))
+ '("--"
+ "--")))
diff --git a/lua-mode.el b/lua-mode.el
index 5942109..314a28d 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -106,6 +106,7 @@
(require 'cl))
(require 'comint)
+(require 'newcomment)
(eval-and-compile
;; Backward compatibility for Emacsen < 24.1
@@ -193,7 +194,7 @@ for Emacsen that doesn't contain one (pre-23.3)."
:type 'string
:group 'lua)
-(defcustom lua-comment-start-skip "-- "
+(defcustom lua-comment-start-skip "---*[ \t]*"
"Default value of `comment-start-skip'."
:type 'string
:group 'lua)
@@ -654,6 +655,8 @@ Groups 6-9 can be used in any of argument regexps."
(indent-line-function . lua-indent-line)
(comment-start . ,lua-comment-start)
(comment-start-skip . ,lua-comment-start-skip)
+ (comment-use-syntax . t)
+ (comment-use-global-state . t)
(imenu-generic-expression . ,lua-imenu-generic-expression)))
;; setup menu bar entry (XEmacs style)