Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

712 lines
27 KiB

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