[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)))))
- [nongnu] elpa/lua-mode bf80b7e 414/468: Enable continuation after comma outside of parens/braces/brackets, (continued)
- [nongnu] elpa/lua-mode bf80b7e 414/468: Enable continuation after comma outside of parens/braces/brackets, Philip Kaludercic, 2021/08/05
- [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 <=
- [nongnu] elpa/lua-mode d63f2af 441/468: Merge pull request #183 from immerrr/add-lua-skip-ws-comment-functions, Philip Kaludercic, 2021/08/05
- [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