branch: elpa/lua-mode
commit 8b9d895b4a5672377d7cc3ff80f0a8555267c2b9
Author: immerrr <[email protected]>
Commit: immerrr <[email protected]>
Fix comments being spilled into the code (issue #25)
---
lua-mode.el | 23 +++++++++++++++++++++++
test/test-fill.el | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+)
diff --git a/lua-mode.el b/lua-mode.el
index 6684c12..2fd79e6 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -731,6 +731,7 @@ Groups 6-9 can be used in any of argument regexps."
(lua--setq-local comment-start lua-comment-start)
(lua--setq-local comment-start-skip lua-comment-start-skip)
(lua--setq-local comment-use-syntax t)
+ (lua--setq-local fill-paragraph-function #'lua--fill-paragraph)
(with-no-warnings
(lua--setq-local comment-use-global-state t))
(lua--setq-local imenu-generic-expression lua-imenu-generic-expression)
@@ -776,6 +777,28 @@ Groups 6-9 can be used in any of argument regexps."
;; private functions
+(defun lua--fill-paragraph (&optional justify region)
+ ;; Implementation of forward-paragraph for filling.
+ ;;
+ ;; This function works around a corner case in the following situations:
+ ;;
+ ;; <>
+ ;; -- some very long comment ....
+ ;; some_code_right_after_the_comment
+ ;;
+ ;; If point is at the beginning of the comment line, fill paragraph code
+ ;; would have gone for comment-based filling and done the right thing, but it
+ ;; does not find a comment at the beginning of the empty line before the
+ ;; comment and falls back to text-based filling ignoring comment-start and
+ ;; spilling the comment into the code.
+ (while (and (not (eobp))
+ (progn (move-to-left-margin)
+ (looking-at paragraph-separate)))
+ (forward-line 1))
+ (let ((fill-paragraph-handle-comment t))
+ (fill-paragraph justify region)))
+
+
(defun lua-prefix-key-update-bindings ()
(let (old-cons)
(if (eq lua-prefix-mode-map (keymap-parent lua-mode-map))
diff --git a/test/test-fill.el b/test/test-fill.el
new file mode 100644
index 0000000..8b93d10
--- /dev/null
+++ b/test/test-fill.el
@@ -0,0 +1,40 @@
+;; -*- flycheck-disabled-checkers: (emacs-lisp-checkdoc) -*-
+(load (concat (file-name-directory (or load-file-name (buffer-file-name)
+ default-directory))
+ "utils.el") nil 'nomessage 'nosuffix)
+
+
+
+(defun expect-filled-as (strs etalon)
+ (expect
+ (lua-buffer-strs
+ (let ((fill-column 10))
+ (lua-insert-goto-<>
+ strs)
+ (execute-kbd-macro (kbd "M-q"))))
+ :to-equal
+ etalon))
+
+
+
+(describe "Test fill-paragraph"
+ (it "filling single-line comment"
+ (expect-filled-as '("<>-- foo bar baz qux")
+ '("-- foo bar"
+ "-- baz qux")))
+ (it "filling comment after code"
+ (expect-filled-as '("<>foo -- bar baz")
+ '("foo -- bar"
+ " -- baz")))
+ (xit "filling multiline comment"
+ (expect-filled-as '("<>--[[ foo bar baz ]]")
+ '("--[[ foo bar"
+ " baz ]]")))
+ (it "does not spill comments into code (issue #25)"
+ (expect-filled-as '("<>"
+ "-- foo bar baz qux"
+ "foo_func()")
+ '(""
+ "-- foo bar"
+ "-- baz qux"
+ "foo_func()"))))