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

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

[nongnu] elpa/lua-mode 724bfa5 409/468: Fix support for file-local vars


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode 724bfa5 409/468: Fix support for file-local vars in indentation tests, add tests for blocks
Date: Thu, 5 Aug 2021 04:59:19 -0400 (EDT)

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

    Fix support for file-local vars in indentation tests, add tests for blocks
---
 lua-mode.el                                       |   9 +-
 test/indentation-tests/assignment-indentation.lua |  61 +++++
 test/indentation-tests/continuation-lines.lua     |   2 +-
 test/indentation-tests/do-block.lua               |  30 +++
 test/indentation-tests/for-equals-block.lua       |  54 +++++
 test/indentation-tests/for-in-block.lua           |  69 ++++++
 test/indentation-tests/if-elseif-else-block.lua   | 240 +++++++++++++++++++
 test/indentation-tests/repeat-until-block.lua     |  57 +++++
 test/indentation-tests/while-block.lua            |  95 ++++++++
 test/test-indentation.el                          | 266 ++++------------------
 10 files changed, 653 insertions(+), 230 deletions(-)

diff --git a/lua-mode.el b/lua-mode.el
index bc54b2a..a60bc4c 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -406,21 +406,24 @@ Usually, stdin:XX line number points to nowhere."
   "If non-nil, contents of multiline string will be indented.
 Otherwise leading amount of whitespace on each line is preserved."
   :group 'lua
-  :type 'boolean)
+  :type 'boolean
+  :safe #'booleanp)
 
 (defcustom lua-indent-nested-block-content-align t
   "If non-nil, the contents of nested blocks are indented to
 align with the column of the opening parenthesis, rather than
 just forward by `lua-indent-level'."
   :group 'lua
-  :type 'boolean)
+  :type 'boolean
+  :safe #'booleanp)
 
 (defcustom lua-indent-close-paren-align t
   "If non-nil, close parenthesis are aligned with their open
 parenthesis.  If nil, close parenthesis are aligned to the
 beginning of the line."
   :group 'lua
-  :type 'boolean)
+  :type 'boolean
+  :safe #'booleanp)
 
 (defcustom lua-jump-on-traceback t
   "*Jump to innermost traceback location in *lua* buffer.  When this
diff --git a/test/indentation-tests/assignment-indentation.lua 
b/test/indentation-tests/assignment-indentation.lua
index fdf31a8..329542a 100644
--- a/test/indentation-tests/assignment-indentation.lua
+++ b/test/indentation-tests/assignment-indentation.lua
@@ -69,3 +69,64 @@ local
    y =
    10,
    20
+
+-- continuation after "local": 4
+
+local
+   x = 5
+
+-- XFAIL: it unindents close paren for arithmetical expression
+
+a = (
+   foo +
+   bar
+)
+
+-- XFAIL: it unindents close paren for arithmetical expression: local
+
+local a = (
+   foo +
+   bar
+)
+
+-- it unindents close paren for function call
+
+a = myfunc(
+   foo +
+   bar
+)
+
+-- it unindents close paren for function call: local
+
+local a = myfunc(
+   foo +
+   bar
+)
+
+-- it unindents close brace for table ctor
+
+a = {
+   foo,
+   bar
+}
+
+-- it unindents close brace for table ctor: local
+
+local a = {
+   foo,
+   bar
+}
+
+-- XFAIL: it unindents close bracket for indexing
+
+a = myobj[
+   foo +
+   bar
+]
+
+-- XFAIL: it unindents close bracket for indexing: local
+
+local a = myobj[
+   foo +
+   bar
+]
diff --git a/test/indentation-tests/continuation-lines.lua 
b/test/indentation-tests/continuation-lines.lua
index bf3a4e0..63ac9fb 100644
--- a/test/indentation-tests/continuation-lines.lua
+++ b/test/indentation-tests/continuation-lines.lua
@@ -241,7 +241,7 @@ function myfunc()
    end
 end
 
--- does not indents keywords after return: semicolon 1
+-- does not indents keywords after return: semicolon 2
 
 function myfunc()
    if true then
diff --git a/test/indentation-tests/do-block.lua 
b/test/indentation-tests/do-block.lua
new file mode 100644
index 0000000..9d237e2
--- /dev/null
+++ b/test/indentation-tests/do-block.lua
@@ -0,0 +1,30 @@
+-- works for do ... end blocks on separate lines
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for do ... end blocks: single line
+
+do a = a + 1 end
+
+a = 0
+
+-- works for do ... end blocks: body on the same line
+do a = a + 1
+end
+
+a = 0
+
+-- works for do ... end blocks: continuation inside body
+do a = a
+      + 1 end
+
+a = 0
+
+-- works for do ... end blocks: parentheses inside body
+do a = (a
+        + 1) end
+
+a = 0
diff --git a/test/indentation-tests/for-equals-block.lua 
b/test/indentation-tests/for-equals-block.lua
new file mode 100644
index 0000000..c3bc0b7
--- /dev/null
+++ b/test/indentation-tests/for-equals-block.lua
@@ -0,0 +1,54 @@
+-- works for for ... = ... do block: 1
+for y = 0, 10 do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... = ... do block: 2
+for y = 0, 10
+do
+   a = a + 1
+end
+
+a = 0
+
+-- XFAIL: works for for ... = ... do block: 3
+for y = 0,
+   10 do
+   a = a + 1
+end
+
+a = 0
+
+-- XFAIL: works for for ... = ... do block: 4
+for y = 0,
+   10
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... = ... do block: 5
+for y =
+   0, 10 do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... = ... do block: 6
+for y =
+   0, 10
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... = ... do block: single line
+
+for y = 0, 10 do a = a + 1 end
+
+a = 0
diff --git a/test/indentation-tests/for-in-block.lua 
b/test/indentation-tests/for-in-block.lua
new file mode 100644
index 0000000..5d10dd0
--- /dev/null
+++ b/test/indentation-tests/for-in-block.lua
@@ -0,0 +1,69 @@
+-- works for for ... do block: 1
+
+for k, v in pairs(bar) do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... do block: 2
+
+for
+   k, v in pairs(bar) do
+   a = a + 1
+end
+
+a = 0
+
+-- XFAIL: works for for ... do block: 3
+
+for
+   k,
+   v in pairs(bar) do
+   a = a + 1
+end
+
+a = 0
+
+-- XFAIL: works for for ... do block: 4
+
+for
+   k,
+   v in
+   pairs(bar) do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... do block: 5
+
+for k, v in
+   pairs(bar) do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... do block: 6
+
+for k, v in
+   pairs(bar)
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... do block: 7
+for k, v in pairs(bar)
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for for ... do block: single line
+for k, v in pairs(bar) do a = a + 1 end
+
+a = 0
diff --git a/test/indentation-tests/if-elseif-else-block.lua 
b/test/indentation-tests/if-elseif-else-block.lua
new file mode 100644
index 0000000..ef94cab
--- /dev/null
+++ b/test/indentation-tests/if-elseif-else-block.lua
@@ -0,0 +1,240 @@
+-- works for if/then block: 1
+
+if foo > bar then
+   a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: 2
+
+if
+   foo > bar then
+   a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: 3
+
+if foo >
+   bar then
+   a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: 4
+
+if foo > bar
+then
+   a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: 5
+
+if
+   foo > bar
+then
+   a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: 6
+
+if foo >
+   bar
+then
+   a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: single line 1
+
+if foo then a = a + 1 end
+
+a = 0
+
+-- works for if/then block: single line 2
+
+if foo
+then a = a + 1 end
+
+a = 0
+
+-- works for if/then block: single line 3
+
+if foo
+then a = a + 1
+end
+
+a = 0
+
+-- works for if/then block: single line 4
+
+if foo
+then a = a
+      + 1
+end
+
+a = 0
+
+-- works for if/else block: 1
+
+if foo then
+   a = a + 1
+else
+   a = a + 2
+end
+
+a = 0
+
+-- works for if/else block: 2
+
+if foo then a = a + 1 else
+   a = a + 2
+end
+
+a = 0
+
+-- works for if/else block: 3
+
+if foo then a = a + 1
+else
+   a = a + 2
+end
+
+a = 0
+
+-- works for if/else block: 4
+
+if foo then
+   a = a + 1
+else a = a + 2 end
+
+a = 0
+
+-- works for if/else block: 5
+
+if foo then
+   a = a + 1
+else a = a + 2
+end
+
+a = 0
+
+
+-- works for if/else block: single line 1
+
+if foo + bar then a = a + 1 else a = a + 2 end
+
+a = 0
+
+-- works for if/else block: single line 2
+
+if foo + bar
+then a = a + 1
+else a = a + 2
+end
+
+a = 0
+
+-- works for if/else block: single line 3
+
+if foo + bar then a = a + 1
+else a = a + 2
+end
+
+a = 0
+
+-- works for if/else block: single line 4
+
+if foo + bar
+then a = a + 1 else a = a + 2
+end
+
+a = 0
+
+-- XFAIL: works for if/else block: single line 5
+
+if foo + bar
+then a = a + 1 else a =
+      a + 2 -- this line should be indented by 2 levels: else+continuation
+end
+
+a = 0
+
+-- works for if/else block: single line 6
+
+if foo
+   + bar
+then a =
+      a + 1
+else a =
+      a + 2
+end
+
+a = 0
+
+
+-- XFAIL: works for if/else block: parentheses in conditional
+
+if (foo
+    + bar) then a = a + 1 else
+   a = a + 2
+end
+
+a = 0
+
+
+-- works for if/elseif/else block: 1
+
+if foo then
+   a = a + 1
+elseif bar then
+   a = a + 2
+elseif baz then
+   a = a + 3
+end
+
+a = 0
+
+-- works for if/elseif/else block: 2
+
+if foo then a = a + 1 elseif bar then
+   a = a + 2
+elseif baz then
+   a = a + 3
+else
+   a = a + 4
+end
+
+a = 0
+
+-- XFAIL: works for if/elseif/else block: 3
+
+if foo then
+   a = a + 1
+elseif bar then a = a + 2 elseif baz then
+   a = a + 3
+else
+   a = a + 4
+end
+
+a = 0
+
+-- XFAIL: works for if/elseif/else block: 4
+
+if foo then
+   a = a + 1
+elseif bar then
+   a = a + 2
+elseif baz then a = a + 3 else
+   a = a + 4
+end
+
+a = 0
diff --git a/test/indentation-tests/repeat-until-block.lua 
b/test/indentation-tests/repeat-until-block.lua
new file mode 100644
index 0000000..5b0bfed
--- /dev/null
+++ b/test/indentation-tests/repeat-until-block.lua
@@ -0,0 +1,57 @@
+-- works for repeat ... until blocks: 1
+
+repeat
+   a = a + 1
+until foo + bar
+
+a = 0
+
+-- works for repeat ... until blocks: 2
+
+repeat
+   a = a + 1
+until
+   foo
+
+a = 0
+
+-- works for repeat ... until blocks: 3
+
+repeat
+   a = a + 1
+until
+   not
+   foo
+
+a = 0
+
+-- works for repeat ... until blocks: 4
+
+repeat
+   a =
+      a + 1
+until
+   not
+   foo
+
+a = 0
+
+-- works for repeat ... until blocks: single line
+
+repeat a = a + 1 until not foo
+
+a = 0
+
+-- works for repeat ... until blocks: single line with continuation 1
+
+repeat a = a + 1 until
+   not foo
+
+a = 0
+
+-- XFAIL: works for repeat ... until blocks: single line with continuation 1
+
+repeat a =
+      a + 1 until not foo
+
+a = 0
diff --git a/test/indentation-tests/while-block.lua 
b/test/indentation-tests/while-block.lua
new file mode 100644
index 0000000..86ff45d
--- /dev/null
+++ b/test/indentation-tests/while-block.lua
@@ -0,0 +1,95 @@
+-- works for while ... do ... end blocks: 1
+
+while foo + bar do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 2
+
+while foo + bar
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 3
+
+while
+   foo + bar do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 4
+
+while foo +
+   bar do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 5
+
+while
+   foo
+   + bar
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 6
+
+while (
+   foo) do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 7
+
+while (
+   foo
+) do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: 8
+
+while (
+   foo
+)
+do
+   a = a + 1
+end
+
+a = 0
+
+-- works for while ... do ... end blocks: single line
+
+while foo + bar do a = a + 1 end
+
+a = 0
+
+-- works for while ... do ... end blocks: single line with continuation in body
+
+while foo + bar do a = a +
+      1 end
+
+a = 0
+
+-- works for while ... do ... end blocks: single line with parentheses in body
+
+while foo + bar do a = (a +
+                        1) end
+
+a = 0
diff --git a/test/test-indentation.el b/test/test-indentation.el
index 33248d9..fc81ec8 100644
--- a/test/test-indentation.el
+++ b/test/test-indentation.el
@@ -6,42 +6,50 @@
 
 (require 'buttercup)
 (require 'subr-x)
+(require 'cl-lib)
 
-(defun file-contents (path)
-  (with-temp-buffer
-    (insert-file-contents-literally path)
-    (buffer-substring-no-properties (point-min) (point-max))))
-
-(defun string-trim-safe (str)
+(defun lua--string-trim-safe (str)
   (save-match-data (string-trim str)))
 
-(defun indentation-test-sections (file-path)
-  (with-temp-buffer
-     (insert-file-contents-literally file-path)
-     (let (results
-           section-name
-           (begin (point-min))
-           end
-           cur-str
-           (next-section-name "start"))
-       (goto-char (point-min))
-       (while next-section-name
-         (setq next-section-name
-               (when (re-search-forward "^--\\(.*\\)" nil 'noerror) 
(string-trim-safe (match-string-no-properties 1))))
-         (setq end (if next-section-name (match-beginning 0) (point-max)))
-         (setq cur-str (string-trim-safe (buffer-substring-no-properties begin 
end)))
-         (if (> (length cur-str) 0)
-             (push (cons (or section-name (format "section %d" (1+ (length 
results))))
-                         cur-str)
-                   results))
-         (setq section-name next-section-name)
-         (setq begin (point)))
-       (nreverse results))))
 
-(defun make-indentation-it-or-xit-clause (x)
+(defun lua--get-indentation-test-sections (file-path)
+  (with-temp-buffer
+    (insert-file-contents-literally file-path)
+    (hack-local-variables)
+    (let (results
+          section-name
+          (begin (point-min))
+          end
+          cur-str
+          (next-section-name "start"))
+      (goto-char (point-min))
+      (while next-section-name
+        ;; Scan towards the next comment or end of file, save the comment as
+        ;; the name for the section that comes AFTER the current one.
+        (setq next-section-name
+              (when (re-search-forward "^--\\(.*\\)" nil 'noerror) 
(lua--string-trim-safe (match-string-no-properties 1))))
+        ;; Record current section bounds and contents
+        (setq end (if next-section-name (match-beginning 0) (point-max)))
+        (setq cur-str (lua--string-trim-safe (buffer-substring-no-properties 
begin end)))
+        ;; Save current section to be returned
+        (if (> (length cur-str) 0)
+            (push (list (or section-name (format "section %d" (1+ (length 
results))))
+                        cur-str
+                        file-local-variables-alist)
+                  results))
+        ;; Transition to the next iteration of the loop.
+        (setq section-name next-section-name)
+        (setq begin (point)))
+      (nreverse results))))
+
+(defun lua--indentation-test-make-it-or-xit-clause (x)
   (let ((it-or-xit (if (string-match "XFAIL" (car x)) 'xit 'it)))
     (eval `(,it-or-xit ,(format "%s" (car x))
-                       (let ((lua-code ,(cdr x)))
+                       (let ((lua-code ,(cadr x))
+                             ,@(mapcar (lambda (alist-cons)
+                                         (list (car alist-cons) (cdr 
alist-cons)))
+                                       ;; cl-caddr here is to support Emacs<26 
that don't have caddr.
+                                       (cl-caddr x)))
                          (expect lua-code :to-be-reindented-the-same-way))))))
 
 (let* ((current-path (or load-file-name (buffer-file-name) default-directory))
@@ -50,11 +58,10 @@
   (mapcar (lambda (test-file)
             (let ((file-path (expand-file-name test-file 
indentation-tests-dir)))
               (describe (format "Indentation test `%s'" test-file)
-                (mapcar #'make-indentation-it-or-xit-clause
-                        (indentation-test-sections file-path)))))
+                (mapcar #'lua--indentation-test-make-it-or-xit-clause
+                        (lua--get-indentation-test-sections file-path)))))
           indentation-tests))
 
-
 (describe "Continuation lines"
   (it "are indented before/after binary operators"
     (let ((binops '("+"  "-"  "*"  "/"  "^"  "%"  ".."
@@ -101,181 +108,6 @@ x = {
 
 
 
-(describe "Block indentation"
-  (it "works for do ... end blocks"
-    ;; FIXME: test split block-intro indentations
-    (expect (lua--reindent-like "\
-do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-do a = a + 1 end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-do a = a + 1
-end
-
-a = 0")))
-
-
-  (it "works for while ... do ... end blocks"
-    (expect (lua--reindent-like "\
-while foo do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-while foo
-do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-while
-   foo
-do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-while foo do a = a + 1 end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-while
-   x +
-   y > 0
-do
-   a = a + 1
-end
-
-a = 0")))
-
-
-  (it "works for repeat ... until blocks"
-    (expect (lua--reindent-like "\
-repeat
-   a = a + 1
-until foo
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-repeat
-   a = a + 1
-until
-   foo
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-repeat
-   a = a + 1
-until
-   not
-   foo
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-repeat a = a + 1 until not foo
-
-a = 0")))
-
-
-
-  (it "works for \"for ... do\" block "
-    (expect (lua--reindent-like "\
-for k, v in pairs(bar) do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for k, v in pairs(bar)
-do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for k, v in pairs(bar) do a = a + 1 end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for y = 0, 10 do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for y = 0, 10
-do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for y = 0, 10 do a = a + 1 end
-
-a = 0")))
-
-  (it "works for conditionals"
-    (expect (lua--reindent-like "\
-if foo then
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-if foo then a = a + 1 end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-if foo then
-   a = a + 1
-else
-   a = a + 2
-end
-
-a = 0"))
-
-
-    (expect (lua--reindent-like "\
-if foo then
-   a = a + 1
-elseif bar then
-   a = a + 2
-elseif baz then
-   a = a + 3
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-if foo then a = a + 1 else
-   a = a + 2
-end"))))
-
 (describe "Function indentation"
   (it "indents function call arguments"
     (expect (lua--reindent-like "\
@@ -385,24 +217,6 @@ foobar(
 )"))
       )))
 
-(ert-deftest lua-indentation-defun ()
-  ;;    [local] function funcname funcbody
-  ;; FIXME: add
-  )
-
-(ert-deftest lua-indentation-alignment ()
-  ;; FIXME: add
-  )
-
-(ert-deftest lua-indentation-tablector ()
-  ;; FIXME: add
-  )
-
-(ert-deftest lua-indentation-continuation-spans-over-empty-lines ()
-  ;; FIXME: add
-  ;; FIXME: check comment-only lines too
-  )
-
 
 (ert-deftest lua-indentation-keywords-with-special-characters ()
   (expect (lua--reindent-like "\



reply via email to

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