diff options
| author | Vincenzo Pupillo | 2025-04-19 22:58:39 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2025-04-20 09:14:29 +0300 |
| commit | 4428077c2bea73570cfe9f9d6b40809aeda7f5c9 (patch) | |
| tree | fe635639189800c8fdecdebda2bd4dd58ffb8b44 | |
| parent | 372ce97180930b63fdd15d1d89fb1a519d4cf85f (diff) | |
| download | emacs-4428077c2bea73570cfe9f9d6b40809aeda7f5c9.tar.gz emacs-4428077c2bea73570cfe9f9d6b40809aeda7f5c9.zip | |
Fix indentation of "{" on a new line of a function declaration
* lisp/progmodes/js.el (js--treesit-switch-body-helper): New
anchor helper function for the switch_body.
(js--treesit-member-chained-expression-helper): New anchor
helper function for chained calls in member_expression.
(js--treesit-arrow-function-helper): New anchor helper
function for arrow_function.
(js--treesit-indent-rules): Fix rule for the indentation of
"{" when of a new line of a function declaration. (Bug#76704)
Fix the indentation of the parent of arrow_function, member_expression,
switch_body, ternary_expression and sequence_expression.
| -rw-r--r-- | lisp/progmodes/js.el | 89 |
1 files changed, 77 insertions, 12 deletions
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 243329de7ae..aa0d35efab8 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el | |||
| @@ -3449,25 +3449,86 @@ Check if a node type is available, then return the right indent rules." | |||
| 3449 | `(((match "<" "jsx_text") parent 0) | 3449 | `(((match "<" "jsx_text") parent 0) |
| 3450 | ((parent-is "jsx_text") parent js-indent-level))))) | 3450 | ((parent-is "jsx_text") parent js-indent-level))))) |
| 3451 | 3451 | ||
| 3452 | (defun js--treesit-switch-body-helper (_node parent _bol &rest _args) | ||
| 3453 | "Anchor helper for the switch body.. | ||
| 3454 | If \"{\" is on a newline return the PARENT bol plus an offset, | ||
| 3455 | otherwise return the usual `parent-bol' value. If `js-switch-indent-offset' > 0 | ||
| 3456 | this is the offset, otherwise is `js-indent-level'." | ||
| 3457 | (save-excursion | ||
| 3458 | (goto-char (treesit-node-start parent)) | ||
| 3459 | (back-to-indentation) | ||
| 3460 | (if (not (eq ?{ (char-after))) | ||
| 3461 | (+ (point) js-switch-indent-offset) | ||
| 3462 | (let ((offset ; offset relative to "{" | ||
| 3463 | (if (= js-switch-indent-offset 0) | ||
| 3464 | js-indent-level | ||
| 3465 | js-switch-indent-offset)) | ||
| 3466 | (pos ; position relative to the "statement_block" | ||
| 3467 | (treesit-node-start | ||
| 3468 | (treesit-node-parent | ||
| 3469 | parent)))) | ||
| 3470 | (+ pos offset))))) | ||
| 3471 | |||
| 3472 | (defun js--treesit-member-chained-expression-helper (_node parent _bol &rest _args) | ||
| 3473 | "Anchor helper for member chained expressions. | ||
| 3474 | Returns a position relative to PARENT context and the value of | ||
| 3475 | `js-chain-indent'. | ||
| 3476 | See `js-chain-indent' and `js--chained-expression-p'." | ||
| 3477 | (let ((parent-start (treesit-node-start parent))) | ||
| 3478 | (if (not js-chain-indent) | ||
| 3479 | (if-let* ((ancestor-node | ||
| 3480 | (treesit-parent-until | ||
| 3481 | parent | ||
| 3482 | "variable_declarator"))) | ||
| 3483 | (treesit-node-start ancestor-node) | ||
| 3484 | (save-excursion | ||
| 3485 | (goto-char parent-start) | ||
| 3486 | (back-to-indentation) | ||
| 3487 | (if (eq parent-start (point)) | ||
| 3488 | (+ parent-start js-indent-level) | ||
| 3489 | parent-start))) | ||
| 3490 | (save-excursion | ||
| 3491 | (goto-char parent-start) | ||
| 3492 | (let ((pos (search-forward "." (pos-eol) t 1))) | ||
| 3493 | (if (and pos (> pos 0)) | ||
| 3494 | (- pos 1) | ||
| 3495 | parent-start)))))) | ||
| 3496 | |||
| 3497 | (defun js--treesit-arrow-function-helper (node parent bol &rest args) | ||
| 3498 | "Anchor helper for arrow_function, return a position based on context. | ||
| 3499 | If and arrow function has a variable_declarator ancestor, returns the | ||
| 3500 | same value of `grand-parent' otherwise return `parent-bol' plus | ||
| 3501 | `js-indent-level'. | ||
| 3502 | PARENT is NODE's parent, BOL is the beginning of non-whitespace | ||
| 3503 | characters of the current line." | ||
| 3504 | (if-let* ((ancestor-node | ||
| 3505 | (treesit-parent-until | ||
| 3506 | parent | ||
| 3507 | "variable_declarator"))) | ||
| 3508 | (apply (alist-get 'grand-parent treesit-simple-indent-presets) | ||
| 3509 | node parent bol args) | ||
| 3510 | (+ (apply (alist-get 'parent-bol treesit-simple-indent-presets) | ||
| 3511 | node parent bol args) | ||
| 3512 | js-indent-level))) | ||
| 3513 | |||
| 3452 | (defvar js--treesit-indent-rules | 3514 | (defvar js--treesit-indent-rules |
| 3453 | (let ((switch-case (rx "switch_" (or "case" "default")))) | ||
| 3454 | `((javascript | 3515 | `((javascript |
| 3455 | ((parent-is "program") parent-bol 0) | 3516 | ((parent-is "program") parent-bol 0) |
| 3456 | ((node-is "}") parent-bol 0) | 3517 | ((node-is "}") standalone-parent 0) |
| 3457 | ((node-is ")") parent-bol 0) | 3518 | ((node-is ")") parent-bol 0) |
| 3458 | ((node-is "]") parent-bol 0) | 3519 | ((node-is "]") parent-bol 0) |
| 3459 | ((node-is ">") parent-bol 0) | 3520 | ((node-is ">") parent-bol 0) |
| 3460 | ((and (parent-is "comment") c-ts-common-looking-at-star) | 3521 | ((and (parent-is "comment") c-ts-common-looking-at-star) |
| 3461 | c-ts-common-comment-start-after-first-star -1) | 3522 | c-ts-common-comment-start-after-first-star -1) |
| 3462 | ((parent-is "comment") prev-adaptive-prefix 0) | 3523 | ((parent-is "comment") prev-adaptive-prefix 0) |
| 3524 | ((n-p-gp "identifier" "ternary_expression" "parenthesized_expression") | ||
| 3525 | parent 0) | ||
| 3463 | ((parent-is "ternary_expression") parent-bol js-indent-level) | 3526 | ((parent-is "ternary_expression") parent-bol js-indent-level) |
| 3464 | ((parent-is "member_expression") parent-bol js-indent-level) | 3527 | ((parent-is "sequence_expression") parent 0) |
| 3465 | ((node-is ,switch-case) parent-bol 0) | 3528 | ((parent-is "member_expression") js--treesit-member-chained-expression-helper 0) |
| 3466 | ;; "{" on the newline. | ||
| 3467 | ((node-is "statement_block") parent-bol js-indent-level) | ||
| 3468 | ((parent-is "named_imports") parent-bol js-indent-level) | 3529 | ((parent-is "named_imports") parent-bol js-indent-level) |
| 3469 | ((parent-is "statement_block") parent-bol js-indent-level) | 3530 | ((parent-is "statement_block") standalone-parent js-indent-level) |
| 3470 | ((parent-is "variable_declarator") parent-bol js-indent-level) | 3531 | ((parent-is "variable_declarator") parent 0) |
| 3471 | ((parent-is "arguments") parent-bol js-indent-level) | 3532 | ((parent-is "arguments") parent-bol js-indent-level) |
| 3472 | ((parent-is "array") parent-bol js-indent-level) | 3533 | ((parent-is "array") parent-bol js-indent-level) |
| 3473 | ((parent-is "formal_parameters") parent-bol js-indent-level) | 3534 | ((parent-is "formal_parameters") parent-bol js-indent-level) |
| @@ -3476,12 +3537,16 @@ Check if a node type is available, then return the right indent rules." | |||
| 3476 | ((parent-is "object_pattern") parent-bol js-indent-level) | 3537 | ((parent-is "object_pattern") parent-bol js-indent-level) |
| 3477 | ((parent-is "object") parent-bol js-indent-level) | 3538 | ((parent-is "object") parent-bol js-indent-level) |
| 3478 | ((parent-is "pair") parent-bol js-indent-level) | 3539 | ((parent-is "pair") parent-bol js-indent-level) |
| 3479 | ((parent-is "arrow_function") parent-bol js-indent-level) | 3540 | ((parent-is "arrow_function") js--treesit-arrow-function-helper 0) |
| 3480 | ((parent-is "parenthesized_expression") parent-bol js-indent-level) | 3541 | ((parent-is "parenthesized_expression") parent-bol js-indent-level) |
| 3481 | ((parent-is "binary_expression") parent-bol js-indent-level) | 3542 | ((parent-is "binary_expression") parent-bol js-indent-level) |
| 3543 | ((parent-is "assignment_expression") parent-bol js-indent-level) | ||
| 3482 | ((parent-is "class_body") parent-bol js-indent-level) | 3544 | ((parent-is "class_body") parent-bol js-indent-level) |
| 3483 | ((parent-is ,switch-case) parent-bol js-indent-level) | 3545 | ;; "{" on the newline, should stay here. |
| 3484 | ((parent-is "statement_block") parent-bol js-indent-level) | 3546 | ((node-is "statement_block") parent-bol 0) |
| 3547 | ((parent-is "switch_statement") parent-bol 0) | ||
| 3548 | ((parent-is "switch_body") js--treesit-switch-body-helper 0) | ||
| 3549 | ((parent-is ,(rx "switch_" (or "case" "default"))) parent-bol js-indent-level) | ||
| 3485 | ((match "while" "do_statement") parent-bol 0) | 3550 | ((match "while" "do_statement") parent-bol 0) |
| 3486 | ((match "else" "if_statement") parent-bol 0) | 3551 | ((match "else" "if_statement") parent-bol 0) |
| 3487 | ((parent-is ,(rx (or (seq (or "if" "for" "for_in" "while" "do") "_statement") | 3552 | ((parent-is ,(rx (or (seq (or "if" "for" "for_in" "while" "do") "_statement") |
| @@ -3502,7 +3567,7 @@ Check if a node type is available, then return the right indent rules." | |||
| 3502 | (no-node parent-bol 0)) | 3567 | (no-node parent-bol 0)) |
| 3503 | (jsdoc | 3568 | (jsdoc |
| 3504 | ((and (parent-is "document") c-ts-common-looking-at-star) | 3569 | ((and (parent-is "document") c-ts-common-looking-at-star) |
| 3505 | c-ts-common-comment-start-after-first-star -1))))) | 3570 | c-ts-common-comment-start-after-first-star -1)))) |
| 3506 | 3571 | ||
| 3507 | (defvar js--treesit-keywords | 3572 | (defvar js--treesit-keywords |
| 3508 | '("as" "async" "await" "break" "case" "catch" "class" "const" "continue" | 3573 | '("as" "async" "await" "break" "case" "catch" "class" "const" "continue" |