Improve indentation in 'lua-ts-mode' (bug#70785)

* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
- Ignore comments when aligning arguments, parameters and fields.
- Apply simpler rules to simpler usage of anonymous functions.
- Better handling of table as a function argument.
(lua-ts--comment-first-sibling-matcher):
(lua-ts--first-real-sibling-anchor):
(lua-ts--last-arg-function-call-matcher):
(lua-ts--top-level-function-call-matcher): New function.
(lua-ts--g-parent):
(lua-ts--g-g-parent): New function.
(lua-ts--g-g-g-parent): Use it.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts:
Add tests.
This commit is contained in:
john muhl 2024-05-03 15:51:01 -05:00 committed by Yuan Fu
parent 4eb363acc8
commit 73d2b829f0
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 97 additions and 6 deletions

View file

@ -291,6 +291,14 @@ values of OVERRIDE."
(parent-is "string_content")
(node-is "]]"))
no-indent 0)
((and (n-p-gp "field" "table_constructor" "arguments")
lua-ts--multi-arg-function-call-matcher
lua-ts--last-arg-function-call-matcher)
standalone-parent lua-ts-indent-offset)
((and (n-p-gp "}" "table_constructor" "arguments")
lua-ts--multi-arg-function-call-matcher
lua-ts--last-arg-function-call-matcher)
standalone-parent 0)
((and (n-p-gp "field" "table_constructor" "arguments")
lua-ts--multi-arg-function-call-matcher)
parent lua-ts-indent-offset)
@ -311,10 +319,15 @@ values of OVERRIDE."
(and (parent-is "parameters") lua-ts--first-child-matcher)
(and (parent-is "table_constructor") lua-ts--first-child-matcher))
standalone-parent lua-ts-indent-offset)
((and (not lua-ts--comment-first-sibling-matcher)
(or (parent-is "arguments")
(parent-is "parameters")
(parent-is "table_constructor")))
lua-ts--first-real-sibling-anchor 0)
((or (parent-is "arguments")
(parent-is "parameters")
(parent-is "table_constructor"))
(nth-sibling 1) 0)
standalone-parent lua-ts-indent-offset)
((and (n-p-gp "block" "function_definition" "parenthesized_expression")
lua-ts--nested-function-block-matcher
lua-ts--nested-function-block-include-matcher)
@ -337,6 +350,9 @@ values of OVERRIDE."
lua-ts--nested-function-end-matcher
lua-ts--nested-function-last-function-matcher)
parent 0)
((and (n-p-gp "end" "function_definition" "arguments")
lua-ts--top-level-function-call-matcher)
standalone-parent 0)
((n-p-gp "end" "function_definition" "arguments") parent 0)
((or (match "end" "function_definition")
(node-is "end"))
@ -385,24 +401,39 @@ values of OVERRIDE."
"Return t if NODE is a function_definition."
(equal "function_definition" (treesit-node-type node)))
(defun lua-ts--g-parent (node)
"Return the grand-parent of NODE."
(let ((parent (treesit-node-parent node)))
(treesit-node-parent parent)))
(defun lua-ts--g-g-parent (node)
"Return the great-grand-parent of NODE."
(treesit-node-parent (lua-ts--g-parent node)))
(defun lua-ts--g-g-g-parent (node)
"Return the great-great-grand-parent of NODE."
(let* ((parent (treesit-node-parent node))
(g-parent (treesit-node-parent parent))
(g-g-parent (treesit-node-parent g-parent)))
(treesit-node-parent g-g-parent)))
(treesit-node-parent (lua-ts--g-g-parent node)))
(defun lua-ts--multi-arg-function-call-matcher (_n parent &rest _)
"Matches if PARENT has multiple arguments."
(> (treesit-node-child-count (treesit-node-parent parent)) 3))
(defun lua-ts--last-arg-function-call-matcher (node parent &rest _)
"Matches if NODE's PARENT is the last argument in a function call."
(let* ((g-parent (lua-ts--g-parent node))
(last (1- (treesit-node-child-count g-parent t))))
(treesit-node-eq parent (seq-elt (treesit-node-children g-parent t) last))))
(defun lua-ts--nested-function-argument-matcher (node &rest _)
"Matches if NODE is in a nested function argument."
(save-excursion
(goto-char (treesit-node-start node))
(treesit-beginning-of-defun)
(backward-char 2)
(not (looking-at ")("))))
(and (not (looking-at ")("))
(not (equal "chunk"
(treesit-node-type
(lua-ts--g-parent (treesit-node-at (point)))))))))
(defun lua-ts--nested-function-block-matcher (node &rest _)
"Matches if NODE is in a nested function block."
@ -438,6 +469,26 @@ values of OVERRIDE."
(treesit-induce-sparse-tree parent #'lua-ts--function-definition-p)))
(= 1 (length (cadr sparse-tree)))))
(defun lua-ts--comment-first-sibling-matcher (node &rest _)
"Matches if NODE if it's previous sibling is a comment."
(let ((sibling (treesit-node-prev-sibling node)))
(equal "comment" (treesit-node-type sibling))))
(defun lua-ts--top-level-function-call-matcher (node &rest _)
"Matches if NODE is within a top-level function call."
(let* ((g-g-p (lua-ts--g-g-parent node))
(g-g-g-p (lua-ts--g-g-g-parent node)))
(and (equal "function_call" (treesit-node-type g-g-p))
(equal "chunk" (treesit-node-type g-g-g-p)))))
(defun lua-ts--first-real-sibling-anchor (_n parent _)
"Return the start position of the first non-comment child of PARENT."
(treesit-node-start
(seq-first
(seq-filter
(lambda (n) (not (equal "comment" (treesit-node-type n))))
(treesit-node-children parent t)))))
(defun lua-ts--variable-declaration-continuation (node &rest _)
"Matches if NODE is part of a multi-line variable declaration."
(treesit-parent-until node

View file

@ -66,6 +66,10 @@ end
return f
end
f6(function()
print'ok'
end)
;(function ()
return true
end)()
@ -118,6 +122,10 @@ function f6(...)
return f
end
f6(function()
print'ok'
end)
;(function ()
return true
end)()
@ -406,6 +414,15 @@ a = 1,
b = 2,
},
nil)
Test(nil, {
a = 1,
b = 2,
})
fn( -- comment
1,
2)
=-=
h(
"string",
@ -443,6 +460,15 @@ Test({
b = 2,
},
nil)
Test(nil, {
a = 1,
b = 2,
})
fn( -- comment
1,
2)
=-=-=
Name: Parameter Indent
@ -464,6 +490,9 @@ local f3 = function( a, b,
c, d )
print(a,b,c,d)
end
local f4 = function(-- comment
a, b, c)
=-=
function f1(
a,
@ -481,6 +510,9 @@ local f3 = function( a, b,
c, d )
print(a,b,c,d)
end
local f4 = function(-- comment
a, b, c)
=-=-=
Name: Table Indent
@ -506,6 +538,10 @@ a = 1,
b = 2,
c = 3,
}
local a = { -- hello world!
b = 10
}
=-=
local Other = {
First={up={Step=true,Jump=true},
@ -527,6 +563,10 @@ local Other = {
b = 2,
c = 3,
}
local a = { -- hello world!
b = 10
}
=-=-=
Name: Continuation Indent