]> code.delx.au - gnu-emacs-elpa/blob - js2-old-indent.el
Fix computed property generator methods indentation
[gnu-emacs-elpa] / js2-old-indent.el
1 ;;; js2-old-indent.el --- Indentation code kept for compatibility
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Commentary:
21
22 ;; All features of this indentation code have been ported to Emacs's
23 ;; built-in `js-mode' by now, so we derive from it. An older
24 ;; commentary follows.
25
26 ;; This code is kept for Emacs 24.5 and ealier.
27
28 ;; This indenter is based on Karl Landström's "javascript.el" indenter.
29 ;; Karl cleverly deduces that the desired indentation level is often a
30 ;; function of paren/bracket/brace nesting depth, which can be determined
31 ;; quickly via the built-in `parse-partial-sexp' function. His indenter
32 ;; then does some equally clever checks to see if we're in the context of a
33 ;; substatement of a possibly braceless statement keyword such as if, while,
34 ;; or finally. This approach yields pretty good results.
35
36 ;; The indenter is often "wrong", however, and needs to be overridden.
37 ;; The right long-term solution is probably to emulate (or integrate
38 ;; with) cc-engine, but it's a nontrivial amount of coding. Even when a
39 ;; parse tree from `js2-parse' is present, which is not true at the
40 ;; moment the user is typing, computing indentation is still thousands
41 ;; of lines of code to handle every possible syntactic edge case.
42
43 ;; In the meantime, the compromise solution is that we offer a "bounce
44 ;; indenter", configured with `js2-bounce-indent-p', which cycles the
45 ;; current line indent among various likely guess points. This approach
46 ;; is far from perfect, but should at least make it slightly easier to
47 ;; move the line towards its desired indentation when manually
48 ;; overriding Karl's heuristic nesting guesser.
49
50 ;; I've made miscellaneous tweaks to Karl's code to handle some Ecma
51 ;; extensions such as `let' and Array comprehensions. Major kudos to
52 ;; Karl for coming up with the initial approach, which packs a lot of
53 ;; punch for so little code. -- Steve
54
55 ;;; Code:
56
57 (require 'sgml-mode)
58
59 (defvar js2-language-version)
60
61 (declare-function js2-mark-safe-local "js2-mode")
62 (declare-function js2-backward-sws "js2-mode")
63 (declare-function js2-forward-sws "js2-mode")
64 (declare-function js2-same-line "js2-mode")
65
66 (defcustom js2-basic-offset (if (and (boundp 'c-basic-offset)
67 (numberp c-basic-offset))
68 c-basic-offset
69 4)
70 "Number of spaces to indent nested statements.
71 Similar to `c-basic-offset'."
72 :group 'js2-mode
73 :safe 'integerp
74 :type 'integer)
75
76 (defcustom js2-pretty-multiline-declarations t
77 "Non-nil to line up multiline declarations vertically:
78
79 var a = 10,
80 b = 20,
81 c = 30;
82
83 If the value is t, and the first assigned value in the
84 declaration is a function/array/object literal spanning several
85 lines, it won't be indented additionally:
86
87 var o = { var bar = 2,
88 foo: 3 vs. o = {
89 }, foo: 3
90 bar = 2; };
91
92 If the value is `all', it will always be indented additionally:
93
94 var o = {
95 foo: 3
96 };
97
98 var o = {
99 foo: 3
100 },
101 bar = 2;
102
103 If the value is `dynamic', it will be indented additionally only
104 if the declaration contains more than one variable:
105
106 var o = {
107 foo: 3
108 };
109
110 var o = {
111 foo: 3
112 },
113 bar = 2;"
114 :group 'js2-mode
115 :safe 'symbolp
116 :type 'symbol)
117
118 (defcustom js2-indent-switch-body nil
119 "When nil, case labels are indented on the same level as the
120 containing switch statement. Otherwise, all lines inside
121 switch statement body are indented one additional level."
122 :type 'boolean
123 :safe 'booleanp
124 :group 'js2-mode)
125
126 (defconst js2-possibly-braceless-keywords-re
127 (concat "else[ \t]+if\\|for[ \t]+each\\|"
128 (regexp-opt '("catch" "do" "else" "finally" "for" "if"
129 "try" "while" "with" "let")))
130 "Regular expression matching keywords that are optionally
131 followed by an opening brace.")
132
133 (defconst js2-indent-operator-re
134 (concat "[-+*/%<>&^|?:.]\\([^-+*/]\\|$\\)\\|!?=\\|"
135 (regexp-opt '("in" "instanceof") 'words))
136 "Regular expression matching operators that affect indentation
137 of continued expressions.")
138
139 (defconst js2-declaration-keyword-re
140 (regexp-opt '("var" "let" "const") 'words)
141 "Regular expression matching variable declaration keywords.")
142
143 (defun js2-re-search-forward-inner (regexp &optional bound count)
144 "Auxiliary function for `js2-re-search-forward'."
145 (let (parse saved-point)
146 (while (> count 0)
147 (re-search-forward regexp bound)
148 (setq parse (if saved-point
149 (parse-partial-sexp saved-point (point))
150 (syntax-ppss (point))))
151 (cond ((nth 3 parse)
152 (re-search-forward
153 (concat "\\(\\=\\|[^\\]\\|^\\)" (string (nth 3 parse)))
154 (save-excursion (end-of-line) (point)) t))
155 ((nth 7 parse)
156 (forward-line))
157 ((or (nth 4 parse)
158 (and (eq (char-before) ?\/) (eq (char-after) ?\*)))
159 (re-search-forward "\\*/"))
160 (t
161 (setq count (1- count))))
162 (setq saved-point (point))))
163 (point))
164
165 (defun js2-re-search-forward (regexp &optional bound noerror count)
166 "Search forward but ignore strings and comments.
167 Invokes `re-search-forward' but treats the buffer as if strings
168 and comments have been removed."
169 (let ((saved-point (point)))
170 (condition-case err
171 (cond ((null count)
172 (js2-re-search-forward-inner regexp bound 1))
173 ((< count 0)
174 (js2-re-search-backward-inner regexp bound (- count)))
175 ((> count 0)
176 (js2-re-search-forward-inner regexp bound count)))
177 (search-failed
178 (goto-char saved-point)
179 (unless noerror
180 (error (error-message-string err)))))))
181
182 (defun js2-re-search-backward-inner (regexp &optional bound count)
183 "Auxiliary function for `js2-re-search-backward'."
184 (let (parse)
185 (while (> count 0)
186 (re-search-backward regexp bound)
187 (setq parse (syntax-ppss (point)))
188 (cond ((nth 3 parse)
189 (re-search-backward
190 (concat "\\([^\\]\\|^\\)" (string (nth 3 parse)))
191 (line-beginning-position) t))
192 ((nth 7 parse)
193 (goto-char (nth 8 parse)))
194 ((or (nth 4 parse)
195 (and (eq (char-before) ?/) (eq (char-after) ?*)))
196 (re-search-backward "/\\*"))
197 (t
198 (setq count (1- count))))))
199 (point))
200
201 (defun js2-re-search-backward (regexp &optional bound noerror count)
202 "Search backward but ignore strings and comments.
203 Invokes `re-search-backward' but treats the buffer as if strings
204 and comments have been removed."
205 (let ((saved-point (point)))
206 (condition-case err
207 (cond ((null count)
208 (js2-re-search-backward-inner regexp bound 1))
209 ((< count 0)
210 (js2-re-search-forward-inner regexp bound (- count)))
211 ((> count 0)
212 (js2-re-search-backward-inner regexp bound count)))
213 (search-failed
214 (goto-char saved-point)
215 (unless noerror
216 (error (error-message-string err)))))))
217
218 (defun js2-looking-at-operator-p ()
219 "Return non-nil if text after point is a non-comma operator."
220 (defvar js2-mode-identifier-re)
221 (and (looking-at js2-indent-operator-re)
222 (or (not (eq (char-after) ?:))
223 (save-excursion
224 (and (js2-re-search-backward "[?:{]\\|\\_<case\\_>" nil t)
225 (eq (char-after) ??))))
226 (not (and
227 (eq (char-after) ?*)
228 ;; Generator method (possibly using computed property).
229 (looking-at (concat "\\* *\\(?:\\[\\|"
230 js2-mode-identifier-re
231 " *(\\)"))
232 (save-excursion
233 (js2-backward-sws)
234 ;; We might misindent some expressions that would
235 ;; return NaN anyway. Shouldn't be a problem.
236 (memq (char-before) '(?, ?} ?{)))))))
237
238 (defun js2-continued-expression-p ()
239 "Return non-nil if the current line continues an expression."
240 (save-excursion
241 (back-to-indentation)
242 (or (js2-looking-at-operator-p)
243 (when (catch 'found
244 (while (and (re-search-backward "\n" nil t)
245 (let ((state (syntax-ppss)))
246 (when (nth 4 state)
247 (goto-char (nth 8 state))) ;; skip comments
248 (skip-chars-backward " \t")
249 (if (bolp)
250 t
251 (throw 'found t))))))
252 (backward-char)
253 (when (js2-looking-at-operator-p)
254 (backward-char)
255 (not (looking-at "\\*\\|\\+\\+\\|--\\|/[/*]")))))))
256
257 (defun js2-end-of-do-while-loop-p ()
258 "Return non-nil if word after point is `while' of a do-while
259 statement, else returns nil. A braceless do-while statement
260 spanning several lines requires that the start of the loop is
261 indented to the same column as the current line."
262 (interactive)
263 (save-excursion
264 (when (looking-at "\\s-*\\_<while\\_>")
265 (if (save-excursion
266 (skip-chars-backward "[ \t\n]*}")
267 (looking-at "[ \t\n]*}"))
268 (save-excursion
269 (backward-list) (backward-word 1) (looking-at "\\_<do\\_>"))
270 (js2-re-search-backward "\\_<do\\_>" (point-at-bol) t)
271 (or (looking-at "\\_<do\\_>")
272 (let ((saved-indent (current-indentation)))
273 (while (and (js2-re-search-backward "^[ \t]*\\_<" nil t)
274 (/= (current-indentation) saved-indent)))
275 (and (looking-at "[ \t]*\\_<do\\_>")
276 (not (js2-re-search-forward
277 "\\_<while\\_>" (point-at-eol) t))
278 (= (current-indentation) saved-indent))))))))
279
280 (defun js2-multiline-decl-indentation ()
281 "Return the declaration indentation column if the current line belongs
282 to a multiline declaration statement. See `js2-pretty-multiline-declarations'."
283 (let (forward-sexp-function ; use Lisp version
284 at-opening-bracket)
285 (save-excursion
286 (back-to-indentation)
287 (when (not (looking-at js2-declaration-keyword-re))
288 (when (looking-at js2-indent-operator-re)
289 (goto-char (match-end 0))) ; continued expressions are ok
290 (while (and (not at-opening-bracket)
291 (not (bobp))
292 (let ((pos (point)))
293 (save-excursion
294 (js2-backward-sws)
295 (or (eq (char-before) ?,)
296 (and (not (eq (char-before) ?\;))
297 (prog2 (skip-syntax-backward ".")
298 (looking-at js2-indent-operator-re)
299 (js2-backward-sws))
300 (not (eq (char-before) ?\;)))
301 (js2-same-line pos)))))
302 (condition-case _
303 (backward-sexp)
304 (scan-error (setq at-opening-bracket t))))
305 (when (looking-at js2-declaration-keyword-re)
306 (goto-char (match-end 0))
307 (1+ (current-column)))))))
308
309 (defun js2-ctrl-statement-indentation ()
310 "Return the proper indentation of current line if it is a control statement.
311 Returns an indentation if this line starts the body of a control
312 statement without braces, else returns nil."
313 (let (forward-sexp-function)
314 (save-excursion
315 (back-to-indentation)
316 (when (and (not (js2-same-line (point-min)))
317 (not (looking-at "{"))
318 (js2-re-search-backward "[[:graph:]]" nil t)
319 (not (looking-at "[{([]"))
320 (progn
321 (forward-char)
322 (when (= (char-before) ?\))
323 ;; scan-sexps sometimes throws an error
324 (ignore-errors (backward-sexp))
325 (skip-chars-backward " \t" (point-at-bol)))
326 (let ((pt (point)))
327 (back-to-indentation)
328 (when (looking-at "}[ \t]*")
329 (goto-char (match-end 0)))
330 (and (looking-at js2-possibly-braceless-keywords-re)
331 (= (match-end 0) pt)
332 (not (js2-end-of-do-while-loop-p))))))
333 (+ (current-indentation) js2-basic-offset)))))
334
335 (defun js2-indent-in-array-comp (parse-status)
336 "Return non-nil if we think we're in an array comprehension.
337 In particular, return the buffer position of the first `for' kwd."
338 (let ((bracket (nth 1 parse-status))
339 (end (point)))
340 (when bracket
341 (save-excursion
342 (goto-char bracket)
343 (when (looking-at "\\[")
344 (forward-char 1)
345 (js2-forward-sws)
346 (if (looking-at "[[{]")
347 (let (forward-sexp-function) ; use Lisp version
348 (forward-sexp) ; skip destructuring form
349 (js2-forward-sws)
350 (if (and (/= (char-after) ?,) ; regular array
351 (looking-at "for"))
352 (match-beginning 0)))
353 ;; to skip arbitrary expressions we need the parser,
354 ;; so we'll just guess at it.
355 (if (and (> end (point)) ; not empty literal
356 (re-search-forward "[^,]]* \\(for\\) " end t)
357 ;; not inside comment or string literal
358 (let ((state (parse-partial-sexp bracket (point))))
359 (not (or (nth 3 state) (nth 4 state)))))
360 (match-beginning 1))))))))
361
362 (defun js2-array-comp-indentation (parse-status for-kwd)
363 (if (js2-same-line for-kwd)
364 ;; first continuation line
365 (save-excursion
366 (goto-char (nth 1 parse-status))
367 (forward-char 1)
368 (skip-chars-forward " \t")
369 (current-column))
370 (save-excursion
371 (goto-char for-kwd)
372 (current-column))))
373
374 (defun js2-maybe-goto-declaration-keyword-end (bracket)
375 "Helper function for `js2-proper-indentation'.
376 Depending on the value of `js2-pretty-multiline-declarations',
377 move point to the end of a variable declaration keyword so that
378 indentation is aligned to that column."
379 (cond
380 ((eq js2-pretty-multiline-declarations 'all)
381 (when (looking-at js2-declaration-keyword-re)
382 (goto-char (1+ (match-end 0)))))
383 ((eq js2-pretty-multiline-declarations 'dynamic)
384 (let (declaration-keyword-end
385 at-closing-bracket-p
386 comma-p)
387 (when (looking-at js2-declaration-keyword-re)
388 ;; Preserve the match data lest it somehow be overridden.
389 (setq declaration-keyword-end (match-end 0))
390 (save-excursion
391 (goto-char bracket)
392 (setq at-closing-bracket-p
393 ;; Handle scan errors gracefully.
394 (condition-case nil
395 (progn
396 ;; Use the regular `forward-sexp-function' because the
397 ;; normal one for this mode uses the AST.
398 (let (forward-sexp-function)
399 (forward-sexp))
400 t)
401 (error nil)))
402 (when at-closing-bracket-p
403 (js2-forward-sws)
404 (setq comma-p (looking-at-p ","))))
405 (when comma-p
406 (goto-char (1+ declaration-keyword-end))))))))
407
408 (cl-defun js2-proper-indentation (parse-status)
409 "Return the proper indentation for the current line."
410 (save-excursion
411 (back-to-indentation)
412 (when (nth 4 parse-status)
413 (cl-return-from js2-proper-indentation (js2--comment-indent parse-status)))
414 (let* ((at-closing-bracket (looking-at "[]})]"))
415 (same-indent-p (or at-closing-bracket
416 (looking-at "\\_<case\\_>[^:]")
417 (and (looking-at "\\_<default:")
418 (save-excursion
419 (js2-backward-sws)
420 (not (memq (char-before) '(?, ?{)))))))
421 (continued-expr-p (js2-continued-expression-p))
422 (declaration-indent (and js2-pretty-multiline-declarations
423 (js2-multiline-decl-indentation)))
424 (bracket (nth 1 parse-status))
425 beg indent)
426 (cond
427 ;; indent array comprehension continuation lines specially
428 ((and bracket
429 (>= js2-language-version 170)
430 (not (js2-same-line bracket))
431 (setq beg (js2-indent-in-array-comp parse-status))
432 (>= (point) (save-excursion
433 (goto-char beg)
434 (point-at-bol)))) ; at or after first loop?
435 (js2-array-comp-indentation parse-status beg))
436
437 ((js2-ctrl-statement-indentation))
438
439 ((and declaration-indent continued-expr-p)
440 (+ declaration-indent js2-basic-offset))
441
442 (declaration-indent)
443
444 (bracket
445 (goto-char bracket)
446 (cond
447 ((looking-at "[({[][ \t]*\\(/[/*]\\|$\\)")
448 (when (save-excursion (skip-chars-backward " \t\n)")
449 (looking-at ")"))
450 (backward-list))
451 (back-to-indentation)
452 (js2-maybe-goto-declaration-keyword-end bracket)
453 (setq indent
454 (cond (same-indent-p
455 (current-column))
456 (continued-expr-p
457 (+ (current-column) (* 2 js2-basic-offset)))
458 (t
459 (+ (current-column) js2-basic-offset))))
460 (if (and js2-indent-switch-body
461 (not at-closing-bracket)
462 (looking-at "\\_<switch\\_>"))
463 (+ indent js2-basic-offset)
464 indent))
465 (t
466 (unless same-indent-p
467 (forward-char)
468 (skip-chars-forward " \t"))
469 (current-column))))
470
471 (continued-expr-p js2-basic-offset)
472
473 (t 0)))))
474
475 (defun js2--comment-indent (parse-status)
476 "Indentation inside a multi-line block comment continuation line."
477 (save-excursion
478 (goto-char (nth 8 parse-status))
479 (if (looking-at "/\\*")
480 (+ 1 (current-column))
481 0)))
482
483 (defun js2-indent-line (&optional bounce-backwards)
484 "Indent the current line as JavaScript source text."
485 (interactive)
486 (let (parse-status offset
487 ;; Don't whine about errors/warnings when we're indenting.
488 ;; This has to be set before calling parse-partial-sexp below.
489 (inhibit-point-motion-hooks t))
490 (setq parse-status (save-excursion
491 (syntax-ppss (point-at-bol)))
492 offset (- (point) (save-excursion
493 (back-to-indentation)
494 (point))))
495 ;; Don't touch multiline strings.
496 (unless (nth 3 parse-status)
497 (indent-line-to (js2-proper-indentation parse-status))
498 (when (cl-plusp offset)
499 (forward-char offset)))))
500
501 ;;; JSX Indentation
502
503 ;; The following JSX indentation code is copied basically verbatim from js.el at
504 ;; 958da7f, except that the prefixes on the functions/variables are changed.
505
506 (defsubst js2--jsx-find-before-tag ()
507 "Find where JSX starts.
508
509 Assume JSX appears in the following instances:
510 - Inside parentheses, when returned or as the first argument
511 to a function, and after a newline
512 - When assigned to variables or object properties, but only
513 on a single line
514 - As the N+1th argument to a function
515
516 This is an optimized version of (re-search-backward \"[(,]\n\"
517 nil t), except set point to the end of the match. This logic
518 executes up to the number of lines in the file, so it should be
519 really fast to reduce that impact."
520 (let (pos)
521 (while (and (> (point) (point-min))
522 (not (progn
523 (end-of-line 0)
524 (when (or (eq (char-before) 40) ; (
525 (eq (char-before) 44)) ; ,
526 (setq pos (1- (point))))))))
527 pos))
528
529 (defconst js2--jsx-end-tag-re
530 (concat "</" sgml-name-re ">\\|/>")
531 "Find the end of a JSX element.")
532
533 (defconst js2--jsx-after-tag-re "[),]"
534 "Find where JSX ends.
535 This complements the assumption of where JSX appears from
536 `js--jsx-before-tag-re', which see.")
537
538 (defun js2--jsx-indented-element-p ()
539 "Determine if/how the current line should be indented as JSX.
540
541 Return `first' for the first JSXElement on its own line.
542 Return `nth' for subsequent lines of the first JSXElement.
543 Return `expression' for an embedded JS expression.
544 Return `after' for anything after the last JSXElement.
545 Return nil for non-JSX lines.
546
547 Currently, JSX indentation supports the following styles:
548
549 - Single-line elements (indented like normal JS):
550
551 var element = <div></div>;
552
553 - Multi-line elements (enclosed in parentheses):
554
555 function () {
556 return (
557 <div>
558 <div></div>
559 </div>
560 );
561 }
562
563 - Function arguments:
564
565 React.render(
566 <div></div>,
567 document.querySelector('.root')
568 );"
569 (let ((current-pos (point))
570 (current-line (line-number-at-pos))
571 last-pos
572 before-tag-pos before-tag-line
573 tag-start-pos tag-start-line
574 tag-end-pos tag-end-line
575 after-tag-line
576 parens paren type)
577 (save-excursion
578 (and
579 ;; Determine if we're inside a jsx element
580 (progn
581 (end-of-line)
582 (while (and (not tag-start-pos)
583 (setq last-pos (js2--jsx-find-before-tag)))
584 (while (forward-comment 1))
585 (when (= (char-after) 60) ; <
586 (setq before-tag-pos last-pos
587 tag-start-pos (point)))
588 (goto-char last-pos))
589 tag-start-pos)
590 (progn
591 (setq before-tag-line (line-number-at-pos before-tag-pos)
592 tag-start-line (line-number-at-pos tag-start-pos))
593 (and
594 ;; A "before" line which also starts an element begins with js, so
595 ;; indent it like js
596 (> current-line before-tag-line)
597 ;; Only indent the jsx lines like jsx
598 (>= current-line tag-start-line)))
599 (cond
600 ;; Analyze bounds if there are any
601 ((progn
602 (while (and (not tag-end-pos)
603 (setq last-pos (re-search-forward js2--jsx-end-tag-re nil t)))
604 (while (forward-comment 1))
605 (when (looking-at js2--jsx-after-tag-re)
606 (setq tag-end-pos last-pos)))
607 tag-end-pos)
608 (setq tag-end-line (line-number-at-pos tag-end-pos)
609 after-tag-line (line-number-at-pos after-tag-line))
610 (or (and
611 ;; Ensure we're actually within the bounds of the jsx
612 (<= current-line tag-end-line)
613 ;; An "after" line which does not end an element begins with
614 ;; js, so indent it like js
615 (<= current-line after-tag-line))
616 (and
617 ;; Handle another case where there could be e.g. comments after
618 ;; the element
619 (> current-line tag-end-line)
620 (< current-line after-tag-line)
621 (setq type 'after))))
622 ;; They may not be any bounds (yet)
623 (t))
624 ;; Check if we're inside an embedded multi-line js expression
625 (cond
626 ((not type)
627 (goto-char current-pos)
628 (end-of-line)
629 (setq parens (nth 9 (syntax-ppss)))
630 (while (and parens (not type))
631 (setq paren (car parens))
632 (cond
633 ((and (>= paren tag-start-pos)
634 ;; Curly bracket indicates the start of an embedded expression
635 (= (char-after paren) 123) ; {
636 ;; The first line of the expression is indented like sgml
637 (> current-line (line-number-at-pos paren))
638 ;; Check if within a closing curly bracket (if any)
639 ;; (exclusive, as the closing bracket is indented like sgml)
640 (cond
641 ((progn
642 (goto-char paren)
643 (ignore-errors (let (forward-sexp-function)
644 (forward-sexp))))
645 (< current-line (line-number-at-pos)))
646 (t)))
647 ;; Indicate this guy will be indented specially
648 (setq type 'expression))
649 (t (setq parens (cdr parens)))))
650 t)
651 (t))
652 (cond
653 (type)
654 ;; Indent the first jsx thing like js so we can indent future jsx things
655 ;; like sgml relative to the first thing
656 ((= current-line tag-start-line) 'first)
657 ('nth))))))
658
659 (defmacro js2--as-sgml (&rest body)
660 "Execute BODY as if in sgml-mode."
661 `(with-syntax-table sgml-mode-syntax-table
662 (let (forward-sexp-function
663 parse-sexp-lookup-properties)
664 ,@body)))
665
666 (defun js2--expression-in-sgml-indent-line ()
667 "Indent the current line as JavaScript or SGML (whichever is farther)."
668 (let* (indent-col
669 (savep (point))
670 ;; Don't whine about errors/warnings when we're indenting.
671 ;; This has to be set before calling parse-partial-sexp below.
672 (inhibit-point-motion-hooks t)
673 (parse-status (save-excursion
674 (syntax-ppss (point-at-bol)))))
675 ;; Don't touch multiline strings.
676 (unless (nth 3 parse-status)
677 (setq indent-col (save-excursion
678 (back-to-indentation)
679 (if (>= (point) savep) (setq savep nil))
680 (js2--as-sgml (sgml-calculate-indent))))
681 (if (null indent-col)
682 'noindent
683 ;; Use whichever indentation column is greater, such that the sgml
684 ;; column is effectively a minimum
685 (setq indent-col (max (js2-proper-indentation parse-status)
686 (+ indent-col js2-basic-offset)))
687 (if savep
688 (save-excursion (indent-line-to indent-col))
689 (indent-line-to indent-col))))))
690
691 (defun js2-jsx-indent-line ()
692 "Indent the current line as JSX (with SGML offsets).
693 i.e., customize JSX element indentation with `sgml-basic-offset'
694 et al."
695 (interactive)
696 (let ((indentation-type (js2--jsx-indented-element-p)))
697 (cond
698 ((eq indentation-type 'expression)
699 (js2--expression-in-sgml-indent-line))
700 ((or (eq indentation-type 'first)
701 (eq indentation-type 'after))
702 ;; Don't treat this first thing as a continued expression (often a "<" or
703 ;; ">" causes this misinterpretation)
704 (cl-letf (((symbol-function #'js2-continued-expression-p) 'ignore))
705 (js2-indent-line)))
706 ((eq indentation-type 'nth)
707 (js2--as-sgml (sgml-indent-line)))
708 (t (js2-indent-line)))))
709
710 (provide 'js2-old-indent)
711
712 ;;; js2-old-indent.el ends here