emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/lua-mode 07c6bad 440/468: Add lua-skip-ws-and-comments-for


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode 07c6bad 440/468: Add lua-skip-ws-and-comments-forward/-backward functions
Date: Thu, 5 Aug 2021 04:59:25 -0400 (EDT)

branch: elpa/lua-mode
commit 07c6bade04abecc3289aff46a7d10a80ded68548
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>

    Add lua-skip-ws-and-comments-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)))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]