[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/lua-mode 56af490 446/468: lua-find-regexp: escape string/c
From: |
Philip Kaludercic |
Subject: |
[nongnu] elpa/lua-mode 56af490 446/468: lua-find-regexp: escape string/comment after a match is ignored |
Date: |
Thu, 5 Aug 2021 04:59:26 -0400 (EDT) |
branch: elpa/lua-mode
commit 56af49090bcf28cc097a147693a2af2c8da600f6
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>
lua-find-regexp: escape string/comment after a match is ignored
Previously, one string literal/comment block might produce multiple matches.
---
lua-mode.el | 68 +++++++++++++++++++++++++++++++--------
test/test-strings-and-comments.el | 31 ++++++++++++++++++
2 files changed, 86 insertions(+), 13 deletions(-)
diff --git a/lua-mode.el b/lua-mode.el
index 1cd96c2..9bdf532 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -957,23 +957,65 @@ Return the amount the indentation changed by."
0
lua-indent-level))))))
-(defun lua-find-regexp (direction regexp &optional limit ignore-p)
+
+(defun lua--ensure-point-within-limit (limit backward)
+ "Return non-nil if point is within LIMIT going forward.
+
+With BACKWARD non-nil, return non-nil if point is within LIMIT
+going backward.
+
+If point is beyond limit, move it onto limit."
+ (if (= (cl-signum (- (point) limit))
+ (if backward 1 -1))
+ t
+ (goto-char limit)
+ nil))
+
+
+(defun lua--escape-from-string (&optional backward)
+ "Move point outside of string if it is inside one.
+
+By default, point is placed after the string, with BACKWARD it is
+placed before the string."
+ (interactive)
+ (let ((parse-state (syntax-ppss)))
+ (when (nth 3 parse-state)
+ (if backward
+ (goto-char (nth 8 parse-state))
+ (parse-partial-sexp (point) (line-end-position) nil nil (syntax-ppss)
'syntax-table))
+ t)))
+
+
+(defun lua-find-regexp (direction regexp &optional limit)
"Searches for a regular expression in the direction specified.
+
Direction is one of 'forward and 'backward.
-By default, matches in comments and strings are ignored, but what to ignore is
-configurable by specifying ignore-p. If the regexp is found, returns point
-position, nil otherwise.
-ignore-p returns true if the match at the current point position should be
-ignored, nil otherwise."
- (let ((ignore-func (or ignore-p 'lua-comment-or-string-p))
- (search-func (if (eq direction 'forward)
+
+Matches in comments and strings are ignored. If the regexp is
+found, returns point position, nil otherwise."
+ (let ((search-func (if (eq direction 'forward)
're-search-forward 're-search-backward))
(case-fold-search nil))
- (catch 'found
- (while (funcall search-func regexp limit t)
- (if (and (not (funcall ignore-func (match-beginning 0)))
- (not (funcall ignore-func (match-end 0))))
- (throw 'found (point)))))))
+ (cl-loop
+ always (or (null limit)
+ (lua--ensure-point-within-limit limit (not (eq direction
'forward))))
+ always (funcall search-func regexp limit 'noerror)
+ for match-beg = (match-beginning 0)
+ for match-end = (match-end 0)
+ while (or (lua-comment-or-string-p match-beg)
+ (lua-comment-or-string-p match-end))
+ do (let ((parse-state (syntax-ppss)))
+ (cond
+ ;; Inside a string
+ ((nth 3 parse-state)
+ (lua--escape-from-string (not (eq direction 'forward))))
+ ;; Inside a comment
+ ((nth 4 parse-state)
+ (goto-char (nth 8 parse-state))
+ (when (eq direction 'forward)
+ (forward-comment 1)))))
+ finally return (point))))
+
(defconst lua-block-regexp
(eval-when-compile
diff --git a/test/test-strings-and-comments.el
b/test/test-strings-and-comments.el
index da20308..4476172 100644
--- a/test/test-strings-and-comments.el
+++ b/test/test-strings-and-comments.el
@@ -471,3 +471,34 @@
:with-point-at "<1>"
:after-executing (lua-skip-ws-and-comments-forward limit)
:to-end-up-at "<2>"))))
+
+(describe "lua-find-regexp"
+ (it "does not match open-bracket that is part of multiline string opener:
forward"
+ (with-lua-buffer
+ (lua-insert-goto-<> '("<>foo = [[ bar ]]"))
+ (expect (lua-find-regexp 'forward "\\[") :not :to-be-truthy)))
+
+ (it "does not match open-bracket that is part of multiline string opener:
backward"
+ (with-lua-buffer
+ (lua-insert-goto-<> '("foo = [[ bar ]]<>"))
+ (expect (lua-find-regexp 'backward "\\[") :not :to-be-truthy)))
+
+ (it "does not match close-bracket that is part of multiline string closer:
forward"
+ (with-lua-buffer
+ (lua-insert-goto-<> '("<>foo = [[ bar ]]"))
+ (expect (lua-find-regexp 'forward "]") :not :to-be-truthy)))
+
+ (it "does not match close-bracket that is part of multiline string closer:
backward"
+ (with-lua-buffer
+ (lua-insert-goto-<> '("<>foo = [[ bar ]]"))
+ (expect (lua-find-regexp 'backward "]") :not :to-be-truthy)))
+
+ (it "does not match minus that is part of comment starter: forward"
+ (with-lua-buffer
+ (lua-insert-goto-<> '("<>foo = [[ bar ]] -- baz"))
+ (expect (lua-find-regexp 'forward "-") :not :to-be-truthy)))
+
+ (it "does not match minus that is part of comment starter: backward"
+ (with-lua-buffer
+ (lua-insert-goto-<> '("<>foo = [[ bar ]] -- baz"))
+ (expect (lua-find-regexp 'backward "-") :not :to-be-truthy))))
- [nongnu] elpa/lua-mode dccda19 336/468: Wrap lua--fill-paragraph in save-excursion, (continued)
- [nongnu] elpa/lua-mode dccda19 336/468: Wrap lua--fill-paragraph in save-excursion, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 7a97e1b 341/468: correct spelling mistake, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 0715340 337/468: Merge pull request #131 from jorams/fill-paragraph-save-excursion, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode d759699 335/468: Merge pull request #129 from immerrr/fix-global-variable-in-lua-interpreter, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 459e3b7 427/468: Fix FIXME: `lua-prefix-key' is always `boundp', Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode b793852 434/468: Add regression test, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 6a30c39 447/468: lua-is-continuing-statement-p: short-circuit to false for "breaker" kws, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode b043ca4 451/468: lua-is-continuing-statement-p-1: fix missing save-excursion on branch, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 6a837f7 458/468: with-lua-buffer: ensure font-lock is activated, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 08d83fc 443/468: Merge pull request #180 from nbfalcon/bugfix/lua-start-process-hangs, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 56af490 446/468: lua-find-regexp: escape string/comment after a match is ignored,
Philip Kaludercic <=
- [nongnu] elpa/lua-mode 93da584 449/468: Bump indentation tests, add missing function-call-arguments suite, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 0018e95 452/468: make-indentation-pair: don't calculate full indentation on every closer, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode d2ff304 456/468: Copy cl-signum into lua--signum for backward compat, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 3688bf7 347/468: Merge pull request #136 from EdwardBetts/spelling, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 6c69183 351/468: Merge pull request #139 from rrthomas/master, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode eacf137 349/468: Spell “Lua” consistently and its “process” (not “subprocess”), Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode acd0c55 354/468: Revert to using group-n in lua--left-shifter-regexp, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 3ba5ecc 360/468: Minor simplification of lua-get-create-process, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode c6e053e 361/468: Spell “Lua” consistently and its “process” (not “subprocess”), Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 7909513 363/468: Merge pull request #141 from rrthomas/master, Philip Kaludercic, 2021/08/05