branch: elpa/lua-mode
commit d7ff9c09e903b0c9f18888b99b9f15e4bf5442ce
Author: immerrr <[email protected]>
Commit: immerrr <[email protected]>
Fix lua-goto-matching-block for when point is at the beginning of keyword
---
lua-mode.el | 5 +++--
test/test-generic.el | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
test/utils.el | 7 +++++++
3 files changed, 69 insertions(+), 2 deletions(-)
diff --git a/lua-mode.el b/lua-mode.el
index ccdb1be..42ccf31 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -1148,8 +1148,9 @@ If optional NOREPORT is non-nil, it won't flag an error
if there
is no block open/close open."
(interactive)
;; search backward to the beginning of the keyword if necessary
- (if (eq (char-syntax (following-char)) ?w)
- (re-search-backward "\\_<" nil t))
+ (when (and (eq (char-syntax (following-char)) ?w)
+ (not (looking-at "\\_<")))
+ (re-search-backward "\\_<" nil t))
(let ((position (lua-goto-matching-block-token)))
(if (and (not position)
(not noreport))
diff --git a/test/test-generic.el b/test/test-generic.el
index 1afcadc..796db3b 100644
--- a/test/test-generic.el
+++ b/test/test-generic.el
@@ -195,3 +195,62 @@ local foo = <2>{
:with-point-at "<1>"
:after-executing (lua-backward-up-list)
:to-end-up-at "<2>")))
+
+
+(describe "lua-goto-matching-block"
+ (it "works for do...end block"
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>do if true then print(123) end <1>end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>do if true then print(123) end e<1>nd")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>do if true then print(123) end en<1>d")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<1>do if true then print(123) end <2>end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "d<1>o if true then print(123) end <2>end"))
+
+ (it "works for repeat...until block"
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<1>repeat if true then print(123) end <2>until true")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>repeat if true then print(123) end <1>until true"))
+
+ (it "works for while...do...end block"
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<1>while foo() do if true then print(123) end <2>end")
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>while foo() do if true then print(123) end <1>end")
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "while foo() <1>do if true then print(123) end <2>end")
+ ;; The next line is a valid statement that ensures
+ ;; "lua-goto-matching-block" can distinguish between "while..do" and
+ ;; "do..end"
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<1>while false do print(123) <2>end do print(123) end")
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "while false do print(123) end <1>do print(123) <2>end"))
+
+ (it "works for if..elseif..else..end block"
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<1>if true then foo() elseif false then bar() else baz() <2>end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>if true then foo() elseif false then bar() else baz() <1>end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>if true then foo() elseif false then bar() <1>else baz() end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>if true then foo() elseif false <1>then bar() else baz() end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>if true then foo() <1>elseif false then bar() else baz() end")
+
+ (expect (lua-goto-matching-block) :to-move-point-from-1-to-2
+ "<2>if true <1>then foo() elseif false then bar() else baz()
end")))
diff --git a/test/utils.el b/test/utils.el
index fb588ad..6fc7912 100644
--- a/test/utils.el
+++ b/test/utils.el
@@ -231,6 +231,13 @@ This is a mere typing/reading aid for lua-mode's font-lock
tests."
(buttercup-define-matcher :with-point-at (&rest args)
(apply #'with-point-at-matcher `(:lua-code ,(car args) :with-point-at ,@(cdr
args))))
+;;; Shortcut for with-point-at with <1> and <2> placeholders
+(buttercup-define-matcher :to-move-point-from-1-to-2 (code-block lua-code)
+ (with-point-at-matcher
+ :lua-code lua-code
+ :with-point-at (lambda () "<1>")
+ :after-executing code-block
+ :to-end-up-at (lambda () "<2>")))
(defun lua--string-trim (string &optional trim-left trim-right)
;; Backport of string-trim for Emacs 24 that doesn't have subr-x lib.