diff options
| author | john muhl | 2024-05-03 15:51:01 -0500 |
|---|---|---|
| committer | Yuan Fu | 2024-05-08 20:36:37 -0700 |
| commit | 73d2b829f06124fec8b65eebc68e87da48808086 (patch) | |
| tree | ff90d3d6472c1b4f8627e688ae20d87540ed5021 | |
| parent | 4eb363acc825ef3aaa5468ab5e206ecab5883acb (diff) | |
| download | emacs-73d2b829f06124fec8b65eebc68e87da48808086.tar.gz emacs-73d2b829f06124fec8b65eebc68e87da48808086.zip | |
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.
| -rw-r--r-- | lisp/progmodes/lua-ts-mode.el | 63 | ||||
| -rw-r--r-- | test/lisp/progmodes/lua-ts-mode-resources/indent.erts | 40 |
2 files changed, 97 insertions, 6 deletions
diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el index 06663e5bd0e..f15edd040cc 100644 --- a/lisp/progmodes/lua-ts-mode.el +++ b/lisp/progmodes/lua-ts-mode.el | |||
| @@ -292,6 +292,14 @@ values of OVERRIDE." | |||
| 292 | (node-is "]]")) | 292 | (node-is "]]")) |
| 293 | no-indent 0) | 293 | no-indent 0) |
| 294 | ((and (n-p-gp "field" "table_constructor" "arguments") | 294 | ((and (n-p-gp "field" "table_constructor" "arguments") |
| 295 | lua-ts--multi-arg-function-call-matcher | ||
| 296 | lua-ts--last-arg-function-call-matcher) | ||
| 297 | standalone-parent lua-ts-indent-offset) | ||
| 298 | ((and (n-p-gp "}" "table_constructor" "arguments") | ||
| 299 | lua-ts--multi-arg-function-call-matcher | ||
| 300 | lua-ts--last-arg-function-call-matcher) | ||
| 301 | standalone-parent 0) | ||
| 302 | ((and (n-p-gp "field" "table_constructor" "arguments") | ||
| 295 | lua-ts--multi-arg-function-call-matcher) | 303 | lua-ts--multi-arg-function-call-matcher) |
| 296 | parent lua-ts-indent-offset) | 304 | parent lua-ts-indent-offset) |
| 297 | ((and (n-p-gp "}" "table_constructor" "arguments") | 305 | ((and (n-p-gp "}" "table_constructor" "arguments") |
| @@ -311,10 +319,15 @@ values of OVERRIDE." | |||
| 311 | (and (parent-is "parameters") lua-ts--first-child-matcher) | 319 | (and (parent-is "parameters") lua-ts--first-child-matcher) |
| 312 | (and (parent-is "table_constructor") lua-ts--first-child-matcher)) | 320 | (and (parent-is "table_constructor") lua-ts--first-child-matcher)) |
| 313 | standalone-parent lua-ts-indent-offset) | 321 | standalone-parent lua-ts-indent-offset) |
| 322 | ((and (not lua-ts--comment-first-sibling-matcher) | ||
| 323 | (or (parent-is "arguments") | ||
| 324 | (parent-is "parameters") | ||
| 325 | (parent-is "table_constructor"))) | ||
| 326 | lua-ts--first-real-sibling-anchor 0) | ||
| 314 | ((or (parent-is "arguments") | 327 | ((or (parent-is "arguments") |
| 315 | (parent-is "parameters") | 328 | (parent-is "parameters") |
| 316 | (parent-is "table_constructor")) | 329 | (parent-is "table_constructor")) |
| 317 | (nth-sibling 1) 0) | 330 | standalone-parent lua-ts-indent-offset) |
| 318 | ((and (n-p-gp "block" "function_definition" "parenthesized_expression") | 331 | ((and (n-p-gp "block" "function_definition" "parenthesized_expression") |
| 319 | lua-ts--nested-function-block-matcher | 332 | lua-ts--nested-function-block-matcher |
| 320 | lua-ts--nested-function-block-include-matcher) | 333 | lua-ts--nested-function-block-include-matcher) |
| @@ -337,6 +350,9 @@ values of OVERRIDE." | |||
| 337 | lua-ts--nested-function-end-matcher | 350 | lua-ts--nested-function-end-matcher |
| 338 | lua-ts--nested-function-last-function-matcher) | 351 | lua-ts--nested-function-last-function-matcher) |
| 339 | parent 0) | 352 | parent 0) |
| 353 | ((and (n-p-gp "end" "function_definition" "arguments") | ||
| 354 | lua-ts--top-level-function-call-matcher) | ||
| 355 | standalone-parent 0) | ||
| 340 | ((n-p-gp "end" "function_definition" "arguments") parent 0) | 356 | ((n-p-gp "end" "function_definition" "arguments") parent 0) |
| 341 | ((or (match "end" "function_definition") | 357 | ((or (match "end" "function_definition") |
| 342 | (node-is "end")) | 358 | (node-is "end")) |
| @@ -385,24 +401,39 @@ values of OVERRIDE." | |||
| 385 | "Return t if NODE is a function_definition." | 401 | "Return t if NODE is a function_definition." |
| 386 | (equal "function_definition" (treesit-node-type node))) | 402 | (equal "function_definition" (treesit-node-type node))) |
| 387 | 403 | ||
| 404 | (defun lua-ts--g-parent (node) | ||
| 405 | "Return the grand-parent of NODE." | ||
| 406 | (let ((parent (treesit-node-parent node))) | ||
| 407 | (treesit-node-parent parent))) | ||
| 408 | |||
| 409 | (defun lua-ts--g-g-parent (node) | ||
| 410 | "Return the great-grand-parent of NODE." | ||
| 411 | (treesit-node-parent (lua-ts--g-parent node))) | ||
| 412 | |||
| 388 | (defun lua-ts--g-g-g-parent (node) | 413 | (defun lua-ts--g-g-g-parent (node) |
| 389 | "Return the great-great-grand-parent of NODE." | 414 | "Return the great-great-grand-parent of NODE." |
| 390 | (let* ((parent (treesit-node-parent node)) | 415 | (treesit-node-parent (lua-ts--g-g-parent node))) |
| 391 | (g-parent (treesit-node-parent parent)) | ||
| 392 | (g-g-parent (treesit-node-parent g-parent))) | ||
| 393 | (treesit-node-parent g-g-parent))) | ||
| 394 | 416 | ||
| 395 | (defun lua-ts--multi-arg-function-call-matcher (_n parent &rest _) | 417 | (defun lua-ts--multi-arg-function-call-matcher (_n parent &rest _) |
| 396 | "Matches if PARENT has multiple arguments." | 418 | "Matches if PARENT has multiple arguments." |
| 397 | (> (treesit-node-child-count (treesit-node-parent parent)) 3)) | 419 | (> (treesit-node-child-count (treesit-node-parent parent)) 3)) |
| 398 | 420 | ||
| 421 | (defun lua-ts--last-arg-function-call-matcher (node parent &rest _) | ||
| 422 | "Matches if NODE's PARENT is the last argument in a function call." | ||
| 423 | (let* ((g-parent (lua-ts--g-parent node)) | ||
| 424 | (last (1- (treesit-node-child-count g-parent t)))) | ||
| 425 | (treesit-node-eq parent (seq-elt (treesit-node-children g-parent t) last)))) | ||
| 426 | |||
| 399 | (defun lua-ts--nested-function-argument-matcher (node &rest _) | 427 | (defun lua-ts--nested-function-argument-matcher (node &rest _) |
| 400 | "Matches if NODE is in a nested function argument." | 428 | "Matches if NODE is in a nested function argument." |
| 401 | (save-excursion | 429 | (save-excursion |
| 402 | (goto-char (treesit-node-start node)) | 430 | (goto-char (treesit-node-start node)) |
| 403 | (treesit-beginning-of-defun) | 431 | (treesit-beginning-of-defun) |
| 404 | (backward-char 2) | 432 | (backward-char 2) |
| 405 | (not (looking-at ")(")))) | 433 | (and (not (looking-at ")(")) |
| 434 | (not (equal "chunk" | ||
| 435 | (treesit-node-type | ||
| 436 | (lua-ts--g-parent (treesit-node-at (point))))))))) | ||
| 406 | 437 | ||
| 407 | (defun lua-ts--nested-function-block-matcher (node &rest _) | 438 | (defun lua-ts--nested-function-block-matcher (node &rest _) |
| 408 | "Matches if NODE is in a nested function block." | 439 | "Matches if NODE is in a nested function block." |
| @@ -438,6 +469,26 @@ values of OVERRIDE." | |||
| 438 | (treesit-induce-sparse-tree parent #'lua-ts--function-definition-p))) | 469 | (treesit-induce-sparse-tree parent #'lua-ts--function-definition-p))) |
| 439 | (= 1 (length (cadr sparse-tree))))) | 470 | (= 1 (length (cadr sparse-tree))))) |
| 440 | 471 | ||
| 472 | (defun lua-ts--comment-first-sibling-matcher (node &rest _) | ||
| 473 | "Matches if NODE if it's previous sibling is a comment." | ||
| 474 | (let ((sibling (treesit-node-prev-sibling node))) | ||
| 475 | (equal "comment" (treesit-node-type sibling)))) | ||
| 476 | |||
| 477 | (defun lua-ts--top-level-function-call-matcher (node &rest _) | ||
| 478 | "Matches if NODE is within a top-level function call." | ||
| 479 | (let* ((g-g-p (lua-ts--g-g-parent node)) | ||
| 480 | (g-g-g-p (lua-ts--g-g-g-parent node))) | ||
| 481 | (and (equal "function_call" (treesit-node-type g-g-p)) | ||
| 482 | (equal "chunk" (treesit-node-type g-g-g-p))))) | ||
| 483 | |||
| 484 | (defun lua-ts--first-real-sibling-anchor (_n parent _) | ||
| 485 | "Return the start position of the first non-comment child of PARENT." | ||
| 486 | (treesit-node-start | ||
| 487 | (seq-first | ||
| 488 | (seq-filter | ||
| 489 | (lambda (n) (not (equal "comment" (treesit-node-type n)))) | ||
| 490 | (treesit-node-children parent t))))) | ||
| 491 | |||
| 441 | (defun lua-ts--variable-declaration-continuation (node &rest _) | 492 | (defun lua-ts--variable-declaration-continuation (node &rest _) |
| 442 | "Matches if NODE is part of a multi-line variable declaration." | 493 | "Matches if NODE is part of a multi-line variable declaration." |
| 443 | (treesit-parent-until node | 494 | (treesit-parent-until node |
diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts index 48184160b4d..ba7bad1b452 100644 --- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts | |||
| @@ -66,6 +66,10 @@ end | |||
| 66 | return f | 66 | return f |
| 67 | end | 67 | end |
| 68 | 68 | ||
| 69 | f6(function() | ||
| 70 | print'ok' | ||
| 71 | end) | ||
| 72 | |||
| 69 | ;(function () | 73 | ;(function () |
| 70 | return true | 74 | return true |
| 71 | end)() | 75 | end)() |
| @@ -118,6 +122,10 @@ function f6(...) | |||
| 118 | return f | 122 | return f |
| 119 | end | 123 | end |
| 120 | 124 | ||
| 125 | f6(function() | ||
| 126 | print'ok' | ||
| 127 | end) | ||
| 128 | |||
| 121 | ;(function () | 129 | ;(function () |
| 122 | return true | 130 | return true |
| 123 | end)() | 131 | end)() |
| @@ -406,6 +414,15 @@ a = 1, | |||
| 406 | b = 2, | 414 | b = 2, |
| 407 | }, | 415 | }, |
| 408 | nil) | 416 | nil) |
| 417 | |||
| 418 | Test(nil, { | ||
| 419 | a = 1, | ||
| 420 | b = 2, | ||
| 421 | }) | ||
| 422 | |||
| 423 | fn( -- comment | ||
| 424 | 1, | ||
| 425 | 2) | ||
| 409 | =-= | 426 | =-= |
| 410 | h( | 427 | h( |
| 411 | "string", | 428 | "string", |
| @@ -443,6 +460,15 @@ Test({ | |||
| 443 | b = 2, | 460 | b = 2, |
| 444 | }, | 461 | }, |
| 445 | nil) | 462 | nil) |
| 463 | |||
| 464 | Test(nil, { | ||
| 465 | a = 1, | ||
| 466 | b = 2, | ||
| 467 | }) | ||
| 468 | |||
| 469 | fn( -- comment | ||
| 470 | 1, | ||
| 471 | 2) | ||
| 446 | =-=-= | 472 | =-=-= |
| 447 | 473 | ||
| 448 | Name: Parameter Indent | 474 | Name: Parameter Indent |
| @@ -464,6 +490,9 @@ local f3 = function( a, b, | |||
| 464 | c, d ) | 490 | c, d ) |
| 465 | print(a,b,c,d) | 491 | print(a,b,c,d) |
| 466 | end | 492 | end |
| 493 | |||
| 494 | local f4 = function(-- comment | ||
| 495 | a, b, c) | ||
| 467 | =-= | 496 | =-= |
| 468 | function f1( | 497 | function f1( |
| 469 | a, | 498 | a, |
| @@ -481,6 +510,9 @@ local f3 = function( a, b, | |||
| 481 | c, d ) | 510 | c, d ) |
| 482 | print(a,b,c,d) | 511 | print(a,b,c,d) |
| 483 | end | 512 | end |
| 513 | |||
| 514 | local f4 = function(-- comment | ||
| 515 | a, b, c) | ||
| 484 | =-=-= | 516 | =-=-= |
| 485 | 517 | ||
| 486 | Name: Table Indent | 518 | Name: Table Indent |
| @@ -506,6 +538,10 @@ a = 1, | |||
| 506 | b = 2, | 538 | b = 2, |
| 507 | c = 3, | 539 | c = 3, |
| 508 | } | 540 | } |
| 541 | |||
| 542 | local a = { -- hello world! | ||
| 543 | b = 10 | ||
| 544 | } | ||
| 509 | =-= | 545 | =-= |
| 510 | local Other = { | 546 | local Other = { |
| 511 | First={up={Step=true,Jump=true}, | 547 | First={up={Step=true,Jump=true}, |
| @@ -527,6 +563,10 @@ local Other = { | |||
| 527 | b = 2, | 563 | b = 2, |
| 528 | c = 3, | 564 | c = 3, |
| 529 | } | 565 | } |
| 566 | |||
| 567 | local a = { -- hello world! | ||
| 568 | b = 10 | ||
| 569 | } | ||
| 530 | =-=-= | 570 | =-=-= |
| 531 | 571 | ||
| 532 | Name: Continuation Indent | 572 | Name: Continuation Indent |