diff options
| author | Jackson Ray Hamilton | 2019-04-09 18:44:36 -0700 |
|---|---|---|
| committer | Jackson Ray Hamilton | 2019-04-09 18:44:36 -0700 |
| commit | 5772971f255c7031753e02492ae979c501018a50 (patch) | |
| tree | fd79d0b01e84787dcb61fd97c1c695fec60ae598 /lisp | |
| parent | d82603747564063f908c9c877449c827a9808528 (diff) | |
| download | emacs-5772971f255c7031753e02492ae979c501018a50.tar.gz emacs-5772971f255c7031753e02492ae979c501018a50.zip | |
Add new defcustom js-jsx-indent-level
* lisp/progmodes/js.el (js-jsx-indent-level): New variable for users
to set JSX indentation differently than JS, like before.
(js-jsx--contextual-indentation): Respect js-jsx-indent-level when
it’s set.
* test/manual/indent/jsx-indent-level.jsx: New test for
js-jsx-indent-level.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/progmodes/js.el | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 70998245818..90f857c96fa 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el | |||
| @@ -600,6 +600,42 @@ It is set to be buffer-local (and t) when in `js-jsx-mode'." | |||
| 600 | :safe 'booleanp | 600 | :safe 'booleanp |
| 601 | :group 'js) | 601 | :group 'js) |
| 602 | 602 | ||
| 603 | (defcustom js-jsx-indent-level nil | ||
| 604 | "When non-nil, indent JSX by this value, instead of like JS. | ||
| 605 | |||
| 606 | Let `js-indent-level' be 4. When this variable is also set to | ||
| 607 | nil, JSX indentation looks like this (consistent): | ||
| 608 | |||
| 609 | return ( | ||
| 610 | <element> | ||
| 611 | <element> | ||
| 612 | Hello World! | ||
| 613 | </element> | ||
| 614 | </element> | ||
| 615 | ) | ||
| 616 | |||
| 617 | Alternatively, when this variable is also set to 2, JSX | ||
| 618 | indentation looks like this (different): | ||
| 619 | |||
| 620 | return ( | ||
| 621 | <element> | ||
| 622 | <element> | ||
| 623 | Hello World! | ||
| 624 | </element> | ||
| 625 | </element> | ||
| 626 | )" | ||
| 627 | :version "27.1" | ||
| 628 | :type 'integer | ||
| 629 | :safe (lambda (x) (or (null x) (integerp x))) | ||
| 630 | :group 'js) | ||
| 631 | ;; This is how indentation behaved out-of-the-box until Emacs 27. JSX | ||
| 632 | ;; indentation was controlled with `sgml-basic-offset', which defaults | ||
| 633 | ;; to 2, whereas `js-indent-level' defaults to 4. Users who had the | ||
| 634 | ;; same values configured for both their HTML and JS indentation would | ||
| 635 | ;; luckily get consistent JSX indentation; most others were probably | ||
| 636 | ;; unhappy. I’d be surprised if anyone actually wants different | ||
| 637 | ;; indentation levels, but just in case, here’s a way back to that. | ||
| 638 | |||
| 603 | (defcustom js-jsx-attribute-offset 0 | 639 | (defcustom js-jsx-attribute-offset 0 |
| 604 | "Specifies a delta for JSXAttribute indentation. | 640 | "Specifies a delta for JSXAttribute indentation. |
| 605 | 641 | ||
| @@ -2706,7 +2742,7 @@ The column calculation is based off of `sgml-calculate-indent'." | |||
| 2706 | (current-column) | 2742 | (current-column) |
| 2707 | ;; This is the first attribute: indent. | 2743 | ;; This is the first attribute: indent. |
| 2708 | (goto-char (+ (nth 1 context) js-jsx-attribute-offset)) | 2744 | (goto-char (+ (nth 1 context) js-jsx-attribute-offset)) |
| 2709 | (+ (current-column) js-indent-level)))) | 2745 | (+ (current-column) (or js-jsx-indent-level js-indent-level))))) |
| 2710 | 2746 | ||
| 2711 | ('text | 2747 | ('text |
| 2712 | ;; Indent to reflect nesting. | 2748 | ;; Indent to reflect nesting. |
| @@ -2715,7 +2751,7 @@ The column calculation is based off of `sgml-calculate-indent'." | |||
| 2715 | ;; The last line isn’t nested, but the rest are. | 2751 | ;; The last line isn’t nested, but the rest are. |
| 2716 | (if (or (not (nth 2 context)) ; Unclosed. | 2752 | (if (or (not (nth 2 context)) ; Unclosed. |
| 2717 | (< line (line-number-at-pos (nth 2 context)))) | 2753 | (< line (line-number-at-pos (nth 2 context)))) |
| 2718 | js-indent-level | 2754 | (or js-jsx-indent-level js-indent-level) |
| 2719 | 0))) | 2755 | 0))) |
| 2720 | 2756 | ||
| 2721 | )) | 2757 | )) |