diff options
| author | john muhl | 2024-11-10 11:26:33 -0600 |
|---|---|---|
| committer | Eli Zaretskii | 2024-11-14 10:14:46 +0200 |
| commit | d592832504554c6ee30bea263e55dc84feaec18d (patch) | |
| tree | c0f14eddc82e6c74d45cbeec99ad46d1af7c4c42 | |
| parent | 6bc44ccf287a398d47563ddf60d13784cd64720f (diff) | |
| download | emacs-d592832504554c6ee30bea263e55dc84feaec18d.tar.gz emacs-d592832504554c6ee30bea263e55dc84feaec18d.zip | |
Improve comment indenting in 'lua-ts-mode'
* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
Align single line comments with the surrounding context.
(lua-ts--comment-first-sibling-matcher): Check that comment is
the first sibling.
(lua-ts--multi-line-comment-start): New function.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts:
Add tests. (Bug#74298)
| -rw-r--r-- | lisp/progmodes/lua-ts-mode.el | 17 | ||||
| -rw-r--r-- | test/lisp/progmodes/lua-ts-mode-resources/indent.erts | 42 |
2 files changed, 56 insertions, 3 deletions
diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el index 4ea453c9b65..836a62b2dac 100644 --- a/lisp/progmodes/lua-ts-mode.el +++ b/lisp/progmodes/lua-ts-mode.el | |||
| @@ -289,7 +289,8 @@ values of OVERRIDE." | |||
| 289 | 289 | ||
| 290 | (defvar lua-ts--simple-indent-rules | 290 | (defvar lua-ts--simple-indent-rules |
| 291 | `((lua | 291 | `((lua |
| 292 | ((or (node-is "comment") | 292 | ((or (and (node-is "comment") (parent-is "chunk")) |
| 293 | lua-ts--multi-line-comment-start | ||
| 293 | (parent-is "comment_content") | 294 | (parent-is "comment_content") |
| 294 | (parent-is "string_content") | 295 | (parent-is "string_content") |
| 295 | (node-is "]]")) | 296 | (node-is "]]")) |
| @@ -473,9 +474,10 @@ values of OVERRIDE." | |||
| 473 | (= 1 (length (cadr sparse-tree))))) | 474 | (= 1 (length (cadr sparse-tree))))) |
| 474 | 475 | ||
| 475 | (defun lua-ts--comment-first-sibling-matcher (node &rest _) | 476 | (defun lua-ts--comment-first-sibling-matcher (node &rest _) |
| 476 | "Matches if NODE if it's previous sibling is a comment." | 477 | "Matches NODE if its previous sibling is a comment." |
| 477 | (let ((sibling (treesit-node-prev-sibling node))) | 478 | (let ((sibling (treesit-node-prev-sibling node))) |
| 478 | (equal "comment" (treesit-node-type sibling)))) | 479 | (and (= 0 (treesit-node-index sibling t)) |
| 480 | (equal "comment" (treesit-node-type sibling))))) | ||
| 479 | 481 | ||
| 480 | (defun lua-ts--top-level-function-call-matcher (node &rest _) | 482 | (defun lua-ts--top-level-function-call-matcher (node &rest _) |
| 481 | "Matches if NODE is within a top-level function call." | 483 | "Matches if NODE is within a top-level function call." |
| @@ -508,6 +510,15 @@ values of OVERRIDE." | |||
| 508 | (line-beginning-position)) | 510 | (line-beginning-position)) |
| 509 | (point)))) | 511 | (point)))) |
| 510 | 512 | ||
| 513 | (defun lua-ts--multi-line-comment-start (node &rest _) | ||
| 514 | "Matches if NODE is the beginning of a multi-line comment." | ||
| 515 | (and node | ||
| 516 | (equal "comment" (treesit-node-type node)) | ||
| 517 | (save-excursion | ||
| 518 | (goto-char (treesit-node-start node)) | ||
| 519 | (forward-char 2) ; Skip the -- part. | ||
| 520 | (looking-at "\\[\\[")))) | ||
| 521 | |||
| 511 | (defvar lua-ts--syntax-table | 522 | (defvar lua-ts--syntax-table |
| 512 | (let ((table (make-syntax-table))) | 523 | (let ((table (make-syntax-table))) |
| 513 | (modify-syntax-entry ?+ "." table) | 524 | (modify-syntax-entry ?+ "." table) |
diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts index ba7bad1b452..b0ece4cc261 100644 --- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts | |||
| @@ -360,6 +360,10 @@ multi-line | |||
| 360 | ]] | 360 | ]] |
| 361 | return true | 361 | return true |
| 362 | end | 362 | end |
| 363 | |||
| 364 | --[[ | ||
| 365 | Long comment. | ||
| 366 | ]] | ||
| 363 | =-= | 367 | =-= |
| 364 | --[[ | 368 | --[[ |
| 365 | Multi-line | 369 | Multi-line |
| @@ -373,6 +377,44 @@ multi-line | |||
| 373 | ]] | 377 | ]] |
| 374 | return true | 378 | return true |
| 375 | end | 379 | end |
| 380 | |||
| 381 | --[[ | ||
| 382 | Long comment. | ||
| 383 | ]] | ||
| 384 | =-=-= | ||
| 385 | |||
| 386 | Name: Comment Indent | ||
| 387 | |||
| 388 | =-= | ||
| 389 | local fn1 = function (a, b) | ||
| 390 | -- comment | ||
| 391 | return a + b | ||
| 392 | end | ||
| 393 | |||
| 394 | local tb1 = { | ||
| 395 | first = 1, | ||
| 396 | -- comment | ||
| 397 | second = 2, | ||
| 398 | } | ||
| 399 | |||
| 400 | local tb9 = { one = 1, | ||
| 401 | -- comment | ||
| 402 | two = 2 } | ||
| 403 | =-= | ||
| 404 | local fn1 = function (a, b) | ||
| 405 | -- comment | ||
| 406 | return a + b | ||
| 407 | end | ||
| 408 | |||
| 409 | local tb1 = { | ||
| 410 | first = 1, | ||
| 411 | -- comment | ||
| 412 | second = 2, | ||
| 413 | } | ||
| 414 | |||
| 415 | local tb9 = { one = 1, | ||
| 416 | -- comment | ||
| 417 | two = 2 } | ||
| 376 | =-=-= | 418 | =-=-= |
| 377 | 419 | ||
| 378 | Name: Argument Indent | 420 | Name: Argument Indent |