Skip to content

Commit a3a71b1

Browse files
authored
Merge pull request #151 from edam/indent-nested-blocks
Enhance indentation of blocks
2 parents 5dfc5be + 0cf8000 commit a3a71b1

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ modules to your Emacs and keeping them up-to-date. Once you have **el-get** set
2020
and updating is no more than
2121

2222
<M-x> el-get-update "lua-mode"`
23-
23+
2424
Please, consult with [el-get documentation](https://github.com/dimitri/el-get/blob/master/README.md) for further information.
2525

2626
### MANUAL INSTALLATION
@@ -53,6 +53,8 @@ The following variables are available for customization (see more via `M-x custo
5353

5454
- Var `lua-indent-level` (default `3`): indentation offset in spaces
5555
- Var `lua-indent-string-contents` (default `nil`): set to `t` if you like to have contents of multiline strings to be indented like comments
56+
- Var `lua-indent-nested-block-content-align` (default `t`) set to `nil` to stop aligning the content of nested blocks with the open parenthesis
57+
- Var `lua-indent-close-paren-align` (defaut `t`) set to `t` to align close parenthesis with the open parenthesis rather than with the beginning of the line
5658
- Var `lua-mode-hook`: list of functions to execute when lua-mode is initialized
5759
- Var `lua-documentation-url` (default `"http://www.lua.org/manual/5.1/manual.html#pdf-"`): base URL for documentation lookup
5860
- Var `lua-documentation-function` (default `browse-url`): function used to show documentation (`eww` is a viable alternative for Emacs 25)

lua-mode.el

+24-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
;; - Var `lua-indent-string-contents':
5454
;; set to `t` if you like to have contents of multiline strings to be
5555
;; indented like comments
56+
;; - Var `lua-indent-nested-block-content-align':
57+
;; set to `nil' to stop aligning the content of nested blocks with the
58+
;; open parenthesis
59+
;; - Var `lua-indent-close-paren-align':
60+
;; set to `t' to align close parenthesis with the open parenthesis,
61+
;; rather than with the beginning of the line
5662
;; - Var `lua-mode-hook':
5763
;; list of functions to execute when lua-mode is initialized
5864
;; - Var `lua-documentation-url':
@@ -399,6 +405,20 @@ Otherwise leading amount of whitespace on each line is preserved."
399405
:group 'lua
400406
:type 'boolean)
401407

408+
(defcustom lua-indent-nested-block-content-align t
409+
"If non-nil, the contents of nested blocks are indented to
410+
align with the column of the opening parenthesis, rather than
411+
just forward by `lua-indent-level'."
412+
:group 'lua
413+
:type 'boolean)
414+
415+
(defcustom lua-indent-close-paren-align t
416+
"If non-nil, close parenthesis are aligned with their open
417+
parenthesis. If nil, close parenthesis are aligned to the
418+
beginning of the line."
419+
:group 'lua
420+
:type 'boolean)
421+
402422
(defcustom lua-jump-on-traceback t
403423
"*Jump to innermost traceback location in *lua* buffer. When this
404424
variable is non-nil and a traceback occurs when running Lua code in a
@@ -1306,7 +1326,8 @@ Don't use standalone."
13061326
(cons 'relative lua-indent-level))
13071327

13081328
;; block openers
1309-
((member found-token (list "{" "(" "["))
1329+
((and lua-indent-nested-block-content-align
1330+
(member found-token (list "{" "(" "[")))
13101331
(save-excursion
13111332
(let ((found-bol (line-beginning-position)))
13121333
(forward-comment (point-max))
@@ -1580,7 +1601,8 @@ If not, return nil."
15801601
(when (lua-goto-matching-block-token block-token-pos 'backward)
15811602
;; Exception cases: when the start of the line is an assignment,
15821603
;; go to the start of the assignment instead of the matching item
1583-
(if (lua-point-is-after-left-shifter-p)
1604+
(if (or (not lua-indent-close-paren-align)
1605+
(lua-point-is-after-left-shifter-p))
15841606
(current-indentation)
15851607
(current-column)))))))
15861608

test/test-indentation.el

+82-1
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,91 @@ foobar(
501501
b
502502
},
503503
c, d
504-
)"))))
504+
)")))
505+
506+
(it "indent blocks with lua-indent-nested-block-content-align"
507+
(let ((lua-indent-nested-block-content-align nil))
508+
(expect (lua--reindent-like "\
509+
call_some_fn( something, {
510+
val = 5,
511+
another = 6,
512+
} )"))
513+
(expect (lua--reindent-like "\
514+
local def = {
515+
some_very_long_name = { fn =
516+
function()
517+
return true
518+
end
519+
}
520+
}"))
521+
))
522+
523+
(it "indent blocks with lua-indent-close-paren-align"
524+
(let ((lua-indent-close-paren-align nil))
525+
(expect (lua--reindent-like "\
526+
local foo = setmetatable( {
527+
a = 4,
528+
b = 5,
529+
}, {
530+
__index = some_func,
531+
} )"))
532+
))
533+
534+
(it "indents nested tables with alternative block indenting"
535+
(let ((lua-indent-nested-block-content-align nil)
536+
(lua-indent-close-paren-align nil))
537+
(expect (lua--reindent-like "\
538+
foobar({
539+
a, b, c
540+
})"))
541+
542+
(expect (lua--reindent-like "\
543+
foobar(a, {
544+
b,
545+
c
546+
})"))
547+
548+
(expect (lua--reindent-like "\
549+
foobar(
550+
a,
551+
{
552+
b,
553+
c
554+
})"))
555+
556+
(expect (lua--reindent-like "\
557+
foobar(
558+
a,
559+
{
560+
b,
561+
c
562+
}
563+
)"))
505564

565+
(expect (lua--reindent-like "\
566+
foobar(a,
567+
{
568+
b,
569+
c
570+
})"))
506571

572+
(expect (lua--reindent-like "\
573+
foobar(a,
574+
{
575+
b,
576+
c
577+
}
578+
)"))
507579

580+
(expect (lua--reindent-like "\
581+
foobar(
582+
{
583+
a,
584+
b
585+
},
586+
c, d
587+
)"))
588+
)))
508589

509590
(ert-deftest lua-indentation-defun ()
510591
;; [local] function funcname funcbody

0 commit comments

Comments
 (0)