[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/lua-mode d63f2af 441/468: Merge pull request #183 from imm
From: |
Philip Kaludercic |
Subject: |
[nongnu] elpa/lua-mode d63f2af 441/468: Merge pull request #183 from immerrr/add-lua-skip-ws-comment-functions |
Date: |
Thu, 5 Aug 2021 04:59:25 -0400 (EDT) |
branch: elpa/lua-mode
commit d63f2afbb8ecfd01afda1bf0fc0e0b15178cc8f6
Merge: fbdbf05 07c6bad
Author: immerrr again <immerrr@gmail.com>
Commit: GitHub <noreply@github.com>
Merge pull request #183 from immerrr/add-lua-skip-ws-comment-functions
Add lua-skip-ws-comment-forward/-backward functions
---
lua-mode.el | 60 +++++++++++--
test/test-strings-and-comments.el | 178 ++++++++++++++++++++++++++++++++++----
test/utils.el | 12 +++
3 files changed, 227 insertions(+), 23 deletions(-)
diff --git a/lua-mode.el b/lua-mode.el
index 5f828fc..36e9a1f 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -1157,21 +1157,65 @@ is no block open/close open."
(error "Not on a block control keyword or brace")
position)))
+(defun lua-skip-ws-and-comments-backward (&optional limit)
+ "Move point back skipping all whitespace and comments.
+
+If LIMIT is given, stop at it or before.
+
+Return non-nil if moved point."
+ (interactive)
+ (unless (lua-string-p)
+ (let ((start-pos (point))
+ (comment-start-pos (lua-comment-start-pos)))
+ (setq limit (min (point) (or limit (point-min))))
+ (when comment-start-pos
+ (goto-char (max limit comment-start-pos)))
+ (when (< limit (point)) (forward-comment (- limit (point))))
+ (when (< (point) limit) (goto-char limit))
+ (when (/= start-pos (point))
+ (point)))))
+
+(defun lua-skip-ws-and-comments-forward (&optional limit)
+ "Move point forward skipping all whitespace and comments.
+
+If LIMIT is given, stop at it or before.
+
+Return non-nil if moved point."
+ (interactive)
+ (unless (lua-string-p)
+ (let ((start-pos (point))
+ (comment-start-pos (lua-comment-start-pos)))
+ (setq limit (max (point) (or limit (point-max))))
+ ;; Escape from current comment. It is necessary to use "while" because
+ ;; luadoc parameters have non-comment face, and parse-partial-sexp with
+ ;; 'syntax-table flag will stop on them.
+ (when comment-start-pos
+ (goto-char comment-start-pos)
+ (forward-comment 1))
+ (when (< (point) limit) (forward-comment (- limit (point))))
+ (when (< limit (point)) (goto-char limit))
+ (when (/= start-pos (point))
+ (point)))))
+
+
(defun lua-forward-line-skip-blanks (&optional back)
- "Move 1 line forward (back if BACK is non-nil) skipping blank lines.
+ "Move 1 line forward/backward and skip all insignificant ws/comment lines.
Moves point 1 line forward (or backward) skipping lines that contain
no Lua code besides comments. The point is put to the beginning of
the line.
Returns final value of point as integer or nil if operation failed."
- (catch 'found
- (while t
- (unless (eql (forward-line (if back -1 1)) 0) ;; 0 means success
- (throw 'found nil))
- (unless (or (looking-at "\\s *\\(--.*\\)?$")
- (lua-comment-or-string-p))
- (throw 'found (point))))))
+ (let ((start-pos (point)))
+ (if back
+ (progn
+ (beginning-of-line)
+ (lua-skip-ws-and-comments-backward))
+ (forward-line)
+ (lua-skip-ws-and-comments-forward))
+ (beginning-of-line)
+ (when (> (count-lines start-pos (point)) 0)
+ (point))))
(eval-when-compile
(defconst lua-operator-class
diff --git a/test/test-strings-and-comments.el
b/test/test-strings-and-comments.el
index bc68cca..da20308 100644
--- a/test/test-strings-and-comments.el
+++ b/test/test-strings-and-comments.el
@@ -237,20 +237,6 @@
(expect (lua-comment-start-pos)
:to-equal nil))))
-(defmacro lua--parametrize-tests (&rest args)
- (pcase args
- (`(,variables ,param-values :it ,description-form . ,body)
- `(progn
- ,@(cl-loop
- for params in param-values
- for let-bindings = (cl-loop for var in variables
- for param in params
- collect `(,var (quote ,param)))
- for description = (eval `(let ,let-bindings ,description-form))
- for test-body = `(let ,let-bindings ,@body)
- collect
- (macroexpand `(it ,description ,test-body)))))))
-
(describe "lua-comment-or-string-start-p/-pos"
(lua--parametrize-tests
(strings expected-result)
@@ -315,7 +301,8 @@
;; single dash: not a comment
(("foo = bar -<> baz") nil)
(("foo = bar <>- baz") nil))
- :it (format "returns %s for %S" (if expected-result (format "truthy/%S"
expected-result) "nil") strings)
+ :it (format "returns %s for %S"
+ (if expected-result (format "truthy/%S" expected-result) "nil")
strings)
(with-lua-buffer
(lua-insert-goto-<> strings)
(expect (lua-comment-or-string-start-pos)
@@ -323,3 +310,164 @@
(if expected-result
(expect (lua-comment-or-string-p) :to-be-truthy)
(expect (lua-comment-or-string-p) :not :to-be-truthy)))))
+
+
+(describe "lua-skip-ws-and-comments-backward"
+ (describe "doesn't move point"
+ (lua--parametrize-tests
+ (navigation-spec test-name)
+ (("<>" "empty buffer")
+ ("<> --[[]]foo" "at beginning of non-empty buffer")
+ (" f<>oo" "in the middle of variable")
+ (" foo<>" "at the end of variable")
+ (" foo<>--" "between variable and comment")
+ (" foo 'bar'<>" "at the end of single-quote string literal")
+ (" foo [[bar]]<>" "at the end of multi-line string literal")
+ (" foo '<>bar'" "inside string literal")
+ (" foo (<>bar)" "inside function call literal")
+ (" foo '-- <> bar'" "within whitespace inside single-line string
literal")
+ (" foo [[-- \n<> bar]]" "within whitespace inside multi-line string
literal")
+ )
+ :it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name
navigation-spec))
+ (expect navigation-spec
+ :with-point-at "<"
+ :after-executing (lua-skip-ws-and-comments-backward)
+ :to-end-up-at ">")))
+
+ (describe "moves point"
+ (lua--parametrize-tests
+ (navigation-spec test-name)
+ (("<2> <1>" "skip whitespace at the beginning of buffer")
+ ("foo<2> <1>" "skip ws after variable")
+ ("foo()<2> <1>" "skip ws after function call")
+ ("foo<2> \n\t\n<1>" "skip newlines/tabs/spaces after variable")
+ ;; single-line comments
+ ("foo<2> -- <1>" "escape single-line comment and skip ws")
+ ("foo<2> -<1>-" "escape single-line comment delimiter")
+ ("foo<2> -- '<1>'" "escape commented out string and skip ws")
+ ("foo<2> -- [[<1>]]" "escape commented out string and skip ws")
+ ("foo<2> -- \n<1>" "skip single-line comment and ws")
+ ("foo<2> -- \n--\n--\n<1>" "skip several single-line comments and ws")
+ ;; multi-line
+ ("foo<2> --[[ <1> ]]" "escape multi-line comment and skip ws")
+ ("foo<2> -<1>-[[ ]]" "escape multi-line comment delimiter and skip ws
1")
+ ("foo<2> --<1>[[ ]]" "escape multi-line comment delimiter and skip ws
2")
+ ("foo<2> --[<1>[ ]]" "escape multi-line comment delimiter and skip ws
3")
+ ("foo<2> --[[ ]<1>]" "escape multi-line comment delimiter and skip ws
4")
+ ("foo<2> --[[ \n\n ]]\n\n--[[ ]]<1>" "skip multi-line comments and ws")
+ ;; luadoc keywords
+ ("foo<2> --[[ @see foo <1>]]" "escape multi-line comment with luadoc
keyword 1")
+ ("foo<2> --[[ @s<1>ee foo ]]" "escape multi-line comment with luadoc
keyword 2")
+ ("foo<2> --[[ <1>@see foo ]]" "escape multi-line comment with luadoc
keyword 3")
+ ("foo<2> -- @see foo <1>" "escape single-line comment with luadoc
keyword 1")
+ ("foo<2> -- @s<1>ee foo " "escape single-line comment with luadoc
keyword 2")
+ ("foo<2> -- <1>@see foo " "escape single-line comment with luadoc
keyword 3")
+ )
+ :it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name
navigation-spec))
+ (expect navigation-spec
+ :with-point-at "<1>"
+ :after-executing (lua-skip-ws-and-comments-backward)
+ :to-end-up-at "<2>")))
+
+ (describe "respects limit"
+ (lua--parametrize-tests
+ (limit navigation-spec test-name)
+ ((3 " <2> <1>" "respect limit in whitespace")
+ (100 " <2><1> " "don't move if limit is beyond point")
+ (5 "-- <2> <1>" "respect limit when escaping single-line comment")
+ (5 "--[[<2> <1>]]" "respect limit when escaping multi-line comment")
+ (5 " <2>-- <1>" "respect limit when escaping multi-line comment")
+ (5 " <2>--[[ <1>]]" "respect limit when escaping multi-line
comment")
+
+ (5 "-- <2>@see x <1>" "respect limit when escaping single-line luadoc
comment")
+ (5 "--[[<2>@see x <1>]]" "respect limit when escaping multi-line
luadoc comment")
+ )
+ :it (replace-regexp-in-string "\n" "\\\\n" (format "%s: limit=%S %S"
test-name limit navigation-spec))
+ (expect navigation-spec
+ :with-point-at "<1>"
+ :after-executing (lua-skip-ws-and-comments-backward limit)
+ :to-end-up-at "<2>"))))
+
+
+(describe "lua-skip-ws-and-comments-forward"
+ (describe "doesn't move point"
+ (lua--parametrize-tests
+ (navigation-spec test-name)
+ (("<>" "empty buffer")
+ (" --[[]]<>" "at end of non-empty buffer")
+ (" f<>oo " "in the middle of variable")
+ (" <>foo " "at the beginning of variable")
+ (" --[[]]<>foo " "between variable and comment")
+ (" foo <>'bar'" "at the end of single-quote string literal")
+ (" foo <>[[bar]]" "at the end of multi-line string literal")
+ (" foo 'bar<>'" "inside string literal")
+ (" foo (bar<>)" "inside function call literal")
+ (" foo '-- <> bar'" "within whitespace inside single-line string
literal")
+ (" foo [[-- \n<>\n bar]]" "within whitespace inside multi-line
string literal")
+ )
+ :it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name
navigation-spec))
+ (expect navigation-spec
+ :with-point-at "<"
+ :after-executing (lua-skip-ws-and-comments-forward)
+ :to-end-up-at ">")))
+
+ (describe "moves point"
+ (lua--parametrize-tests
+ (navigation-spec test-name)
+ (("<1> <2>" "skip whitespace at the end of buffer")
+ ("<1> <2>bar" "skip ws before variable")
+ ("foo<1> <2>()" "skip ws before function call")
+ ("<1> \n\t\n<2>foo" "skip newlines/tabs/spaces before variable")
+
+ ;; single-line comments
+ ("foo -- <1>\n <2>bar" "escape single-line comment and skip ws")
+ ("foo -<1>- \n <2>bar" "escape single-line comment delimiter")
+ ("foo -- '<1>' \n <2>bar" "escape commented out string and skip ws")
+ ("foo -- [[<1>]] \n <2>bar" "escape commented out string and skip
ws")
+ ("foo <1>-- \n \n <2>bar" "skip single-line comment and ws")
+ ("foo <1>-- \n--\n--\n \n <2>bar" "skip several single-line comments
and ws")
+ ;; multi-line
+ ("foo --[[ <1> ]] <2>bar" "escape multi-line comment and skip ws")
+ ("foo -<1>-[[ ]] <2>bar" "escape multi-line comment delimiter and
skip ws 1")
+ ("foo --<1>[[ ]] <2>bar" "escape multi-line comment delimiter and
skip ws 2")
+ ("foo --[<1>[ ]] <2>bar" "escape multi-line comment delimiter and
skip ws 3")
+ ("foo --[[ ]<1>] <2>bar" "escape multi-line comment delimiter and
skip ws 4")
+ ("foo <1>--[[ \n\n ]]\n\n--[[ ]] <2>bar" "skip multi-line comments
and ws")
+ ;; luadoc keywords
+ ("foo --[[ @see foo <1>]] <2>bar" "escape multi-line comment with
luadoc keyword 1")
+ ("foo --[[ @s<1>ee foo ]] <2>bar" "escape multi-line comment with
luadoc keyword 2")
+ ("foo --[[ <1>@see foo ]] <2>bar" "escape multi-line comment with
luadoc keyword 3")
+ ("foo -- @see foo<1> \n <2>bar" "escape single-line comment with
luadoc keyword 1")
+ ("foo -- @s<1>ee foo \n <2>bar" "escape single-line comment with
luadoc keyword 2")
+ ("foo -- <1>@see foo \n <2>bar" "escape single-line comment with
luadoc keyword 3")
+ )
+ :it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name
navigation-spec))
+ (expect navigation-spec
+ :with-point-at "<1>"
+ :after-executing (lua-skip-ws-and-comments-forward)
+ :to-end-up-at "<2>")))
+
+ (describe "respects limit"
+ (lua--parametrize-tests
+ (limit navigation-spec test-name)
+ ((6 " <1> <2> " "respect limit in whitespace")
+ (1 " <2><1> " "don't move if limit is before point")
+ (8 "-- <1> <2> \n" "respect limit when escaping single-line comment
1")
+ (8 "-- <1> \n<2> " "respect limit when escaping single-line comment
2")
+ (8 "-- <1> <2>\n " "respect limit when escaping single-line comment
3")
+ (8 "--[[<1> <2> ]] \n" "respect limit when escaping multi-line comment
1")
+ (8 "--[[<1> ]<2>] \n" "respect limit when escaping multi-line comment
1")
+ (8 "--[[<1> <2> ]] \n" "respect limit when escaping multi-line comment
1")
+
+ (7 "-- <1>@s<2>ee x " "respect limit when escaping single-line luadoc
comment")
+ (8 "-- <1>@se<2>e x " "respect limit when escaping single-line luadoc
comment")
+ (9 "-- <1>@see<2> x " "respect limit when escaping single-line luadoc
comment")
+ (7 "--[[<1>@s<2>ee x]] " "respect limit when escaping single-line luadoc
comment")
+ (8 "--[[<1>@se<2>e x]] " "respect limit when escaping single-line luadoc
comment")
+ (9 "--[[<1>@see<2> x]] " "respect limit when escaping single-line luadoc
comment")
+ )
+ :it (replace-regexp-in-string "\n" "\\\\n" (format "%s: limit=%S %S"
test-name limit navigation-spec))
+ (expect navigation-spec
+ :with-point-at "<1>"
+ :after-executing (lua-skip-ws-and-comments-forward limit)
+ :to-end-up-at "<2>"))))
diff --git a/test/utils.el b/test/utils.el
index 6b05737..fb588ad 100644
--- a/test/utils.el
+++ b/test/utils.el
@@ -256,3 +256,15 @@ This is a mere typing/reading aid for lua-mode's font-lock
tests."
(lua--string-trim (mapconcat 'identity
indented-lines "\n")))
:expect-mismatch-phrase (format "Expected `%S' to not be reindented like
that"
lines))))
+
+(defmacro lua--parametrize-tests (variables param-values it description-form
&rest body)
+ `(progn
+ ,@(cl-loop
+ for params in param-values
+ for let-bindings = (cl-loop for var in variables
+ for param in params
+ collect `(,var (quote ,param)))
+ for description = (eval `(let ,let-bindings ,description-form))
+ for test-body = `(let ,let-bindings ,@body)
+ collect
+ (macroexpand `(it ,description ,test-body)))))
- [nongnu] elpa/lua-mode afd5aa3 415/468: test-indentation: remove unused subr-x dependency, (continued)
- [nongnu] elpa/lua-mode afd5aa3 415/468: test-indentation: remove unused subr-x dependency, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode b170500 416/468: Add backport of string-trim not available in Emacs 24, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 5bfdea5 420/468: Merge pull request #177 from immerrr/refactor-variable-list-fontification, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 0546418 423/468: Extend lua-default-application doc a bit, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 9d46ad3 424/468: Update readme, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 345ebfc 425/468: Merge pull request #174 from immerrr/assorted-fixes, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 85d01e9 429/468: `lua-funcname-at-point': fix regexes, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode b647537 428/468: `lua-start-proces': don't hang if already running, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 853b040 432/468: `lua-start-process': start a new process sometimes, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode fbdbf05 439/468: Merge pull request #181 from immerrr/treat-comment-delimiters-as-comments-too, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode d63f2af 441/468: Merge pull request #183 from immerrr/add-lua-skip-ws-comment-functions,
Philip Kaludercic <=
- [nongnu] elpa/lua-mode e81d0f2 442/468: Merge pull request #179 from nbfalcon/code-cleanup/fix-fixmes, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode d7ff9c0 444/468: Fix lua-goto-matching-block for when point is at the beginning of keyword, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 1f74cd5 453/468: Remove backward-up-list for "for" continuation lines, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 4e346d4 457/468: Add COPYING file, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode cf076a6 460/468: Replace funcall-interactively -> call-interactively (for Emacs-24.3), Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 9454aee 461/468: Merge pull request #187 from immerrr/re-enable-emacs-24, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 2d9a468 463/468: Merge pull request #189 from jcs-PR/badge, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode a57b1b1 462/468: Add badges to README, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode bc77051 464/468: Fix indentation, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 7ee3feb 468/468: Bump version tag for v20210802 release, Philip Kaludercic, 2021/08/05