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

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

[nongnu] elpa/lua-mode fbdbf05 439/468: Merge pull request #181 from imm


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode fbdbf05 439/468: Merge pull request #181 from immerrr/treat-comment-delimiters-as-comments-too
Date: Thu, 5 Aug 2021 04:59:25 -0400 (EDT)

branch: elpa/lua-mode
commit fbdbf05ad058a8859a577d70e9a62413adb30160
Merge: 6638de0 a4f49d3
Author: immerrr again <immerrr@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #181 from 
immerrr/treat-comment-delimiters-as-comments-too
    
    Consider point as inside comment if it is inside "--" opener
---
 lua-mode.el                       |  49 ++++----
 test/test-strings-and-comments.el | 230 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 258 insertions(+), 21 deletions(-)

diff --git a/lua-mode.el b/lua-mode.el
index a5f5d6a..5f828fc 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -805,22 +805,41 @@ This function replaces previous prefix-key binding with a 
new one."
   "Returns true if the point is in a string."
   (save-excursion (elt (syntax-ppss pos) 3)))
 
-(defun lua-comment-start-pos (parsing-state)
+(defun lua--containing-double-hyphen-start-pos ()
+  "Return position of the beginning comment delimiter (--).
+
+Emacs syntax framework does not consider comment delimiters as
+part of the comment itself, but for this package it is useful to
+consider point as inside comment when it is between the two hyphens"
+  (and (eql (char-before) ?-)
+       (eql (char-after) ?-)
+       (1- (point))))
+
+(defun lua-comment-start-pos (&optional parsing-state)
   "Return position of comment containing current point.
 
 If point is not inside a comment, return nil."
-  (and parsing-state (nth 4 parsing-state) (nth 8 parsing-state)))
+  (unless parsing-state (setq parsing-state (syntax-ppss)))
+  (and
+   ;; Not a string
+   (not (nth 3 parsing-state))
+   ;; Syntax-based comment
+   (or (and (nth 4 parsing-state) (nth 8 parsing-state))
+       (lua--containing-double-hyphen-start-pos))))
 
 (defun lua-comment-or-string-p (&optional pos)
   "Returns true if the point is in a comment or string."
   (save-excursion (let ((parse-result (syntax-ppss pos)))
-                    (or (elt parse-result 3) (elt parse-result 4)))))
+                    (or (elt parse-result 3) (lua-comment-start-pos 
parse-result)))))
 
 (defun lua-comment-or-string-start-pos (&optional pos)
   "Returns start position of string or comment which contains point.
 
 If point is not inside string or comment, return nil."
-  (save-excursion (elt (syntax-ppss pos) 8)))
+  (save-excursion
+    (when pos (goto-char pos))
+    (or (elt (syntax-ppss pos) 8)
+        (lua--containing-double-hyphen-start-pos))))
 
 ;; They're propertized as follows:
 ;; 1. generic-comment
@@ -864,23 +883,15 @@ If none can be found before reaching LIMIT, return nil."
         ;; inside strings or comments ending either at EOL or at valid token.
         (and (setq last-search-matched
                    (re-search-forward lua-ml-begin-regexp limit 'noerror))
-
-             ;; Handle triple-hyphen '---[[' situation in which the multiline
-             ;; opener should be skipped.
+             ;; Ensure --[[ is not inside a comment or string.
              ;;
-             ;; In HYPHEN1-HYPHEN2-BRACKET1-BRACKET2 situation (match-beginning
-             ;; 0) points to HYPHEN1, but if there's another hyphen before
-             ;; HYPHEN1, standard syntax table will only detect comment-start
-             ;; at HYPHEN2.
+             ;; This includes "---[[" sequence, in which "--" at the beginning
+             ;; creates a single-line comment, and thus "-[[" is no longer a
+             ;; multi-line opener.
              ;;
-             ;; We could check for comment-start at HYPHEN2, but then we'd have
-             ;; to flush syntax-ppss cache to remove the result saying that at
-             ;; HYPHEN2 there's no comment or string, because under some
-             ;; circumstances that would hide the fact that we put a
-             ;; comment-start property at HYPHEN1.
-             (or (lua-comment-or-string-start-pos (match-beginning 0))
-                 (and (eq ?- (char-after (match-beginning 0)))
-                      (eq ?- (char-before (match-beginning 0)))))))
+             ;; XXX: need to ensure syntax-ppss beyond (match-beginning 0) is
+             ;; not calculated, or otherwise we'll need to flush the cache.
+             (lua-comment-or-string-start-pos (match-beginning 0))))
 
     last-search-matched))
 
diff --git a/test/test-strings-and-comments.el 
b/test/test-strings-and-comments.el
index 5ce9111..bc68cca 100644
--- a/test/test-strings-and-comments.el
+++ b/test/test-strings-and-comments.el
@@ -4,8 +4,6 @@
                                        default-directory))
               "utils.el") nil 'nomessage 'nosuffix)
 
-(defmacro should= (lhs rhs)
-  `(should (equal ,lhs ,rhs)))
 
 (describe "Test indent-new-comment-line"
   (it "works with -- ..."
@@ -97,3 +95,231 @@
             :to-equal
             '("--"
               "--"))))
+
+(describe "lua-comment-start-pos"
+  ;; Single-line comments
+  (it "returns beginning of single-line comment if inside"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--   <>"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of single-line comment if between delimiters"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("-<>-   "))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns nil if before delimiters"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("<>--   "))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if before single-line comment"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("<>"
+                           "--   "))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if after single-line comment"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--"
+                           "<>"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  ;; Single-line comments + strings
+  (it "returns nil if inside single-line string"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("'--<>'"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if inside multi-line string"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("[[--<>]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  ;; Multi-line comments
+  (it "returns beginning of multi-line comment if inside 1"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[[<>   ]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of multi-line comment if inside 2"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[[<>"
+                           "]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of multi-line comment if inside 3"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[["
+                           "<>]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of multi-line comment if between delimiters 1"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("-<>-[[   ]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of single-line comment if between delimiters 2"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--<>[[   ]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of multi-line comment if between delimiters 3"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[<>[  ]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of multi-line comment if between delimiters 4"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[=<>[  ]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns beginning of multi-line comment if between delimiters 5"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[[  ]<>]"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  (it "returns nil if before multi-line opener"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("<>--[[   ]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if after multi-line closer"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[[   ]]<>"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns beginning of multi-line comment if after multi-line closer with 
different opener"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("--[==[   ]]<>"))
+     (expect (lua-comment-start-pos)
+             :to-equal 1)))
+
+  ;; Multi-line comments with strings
+  (it "returns nil if multi-line opener is inside string 1"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("'--[['   <>]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if multi-line opener is inside string 2"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("'--[['   ]]<>"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if multi-line opener is inside multi-line string 1"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("[[--[[]]   <>]]"))
+     (expect (lua-comment-start-pos)
+             :to-equal nil)))
+
+  (it "returns nil if multi-line opener is inside multi-line string 2"
+    (with-lua-buffer
+     (lua-insert-goto-<> '("[[--[[]]   ]]<>"))
+     (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)
+   (;; single-line strings: single-quote
+    (("<>'foo'") nil)
+    (("'<>foo'") 1)
+    (("'foo<>'") 1)
+    (("'foo'<>") nil)
+
+    ;; single-line strings: double-quote
+    (("<>\"foo\"") nil)
+    (("\"<>foo\"") 1)
+    (("\"foo<>\"") 1)
+    (("\"foo\"<>") nil)
+
+    ;; multi-line strings
+    (("<>[[foo]]") nil)
+    (("[[<>foo]]") 1)
+    (("[<>[foo]]") 1)
+    (("[=<>[foo]=]") 1)
+    (("[<>=[foo]=]") 1)
+    (("[[foo<>]]") 1)
+    (("[[foo]<>]") 1)
+    (("[[foo]<>=]") 1)
+    (("[[foo]=<>]") 1)
+    (("[[foo]]<>") nil)
+
+    ;; single-line comments
+    (("foo <>-- bar") nil)
+    (("foo -<>- bar") 5)
+    (("foo --<> bar") 5)
+    (("foo -- <>bar") 5)
+    (("foo -- bar<>") 5)
+
+    ;; multi-line comments
+    (("foo <>--[[ bar ]]") nil)
+    (("foo -<>-[[ bar ]]") 5)
+    (("foo --<>[[ bar ]]") 5)
+    (("foo --[<>[ bar ]]") 5)
+    (("foo --[[<> bar ]]") 5)
+    (("foo --[[ bar <>]]") 5)
+    (("foo --[[ bar ]<>]") 5)
+    (("foo --[[ bar ]]<>") nil)
+    (("foo --[==[ bar ]]<>") 5)
+
+    ;; single-line comment containing multi-line comment
+    (("foo <>---[[ bar ]]") nil)
+    (("foo --<>-[[ bar ]]") 5)
+    (("foo ---<>[[ bar ]]") 5)
+    (("foo ---[<>[ bar ]]") 5)
+    (("foo ---[[<> bar ]]") 5)
+    (("foo ---[[ bar ]]<>") 5)
+
+    ;; multi-line comment containing single-line comment
+    (("foo --[[ -- bar ]]<>") nil)
+
+    ;; string containing multi-line comment opener
+    (("foo '--[[' <> bar ]]") nil)
+    (("foo [[--[[]] <> bar ]]") nil)
+    (("foo [[--[==[]] <> bar ]==]") nil)
+
+    ;; 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)
+   (with-lua-buffer
+    (lua-insert-goto-<> strings)
+    (expect (lua-comment-or-string-start-pos)
+            :to-equal expected-result)
+    (if expected-result
+        (expect (lua-comment-or-string-p) :to-be-truthy)
+      (expect (lua-comment-or-string-p) :not :to-be-truthy)))))



reply via email to

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