X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/b1234ca57f7d043fff8c14eb2857d2d435a40fde..bb0acf5972e8ff505cb37a38ea9edf11cb1bd741:/js2-old-indent.el diff --git a/js2-old-indent.el b/js2-old-indent.el index bb605bcbc..f336005e4 100644 --- a/js2-old-indent.el +++ b/js2-old-indent.el @@ -54,6 +54,8 @@ ;;; Code: +(require 'sgml-mode) + (defvar js2-language-version) (declare-function js2-mark-safe-local "js2-mode") @@ -129,13 +131,13 @@ switch statement body are indented one additional level." followed by an opening brace.") (defconst js2-indent-operator-re - (concat "[-+*/%<>&^|?:.]\\([^-+*/]\\|$\\)\\|!?=\\|" - (regexp-opt '("in" "instanceof") 'words)) + (concat "[-+*/%<>&^|?:.]\\([^-+*/.]\\|$\\)\\|!?=\\|" + (regexp-opt '("in" "instanceof") 'symbols)) "Regular expression matching operators that affect indentation of continued expressions.") (defconst js2-declaration-keyword-re - (regexp-opt '("var" "let" "const") 'words) + (regexp-opt '("var" "let" "const") 'symbols) "Regular expression matching variable declaration keywords.") (defun js2-re-search-forward-inner (regexp &optional bound count) @@ -223,31 +225,30 @@ and comments have been removed." (eq (char-after) ??)))) (not (and (eq (char-after) ?*) - (looking-at (concat "\\* *" js2-mode-identifier-re " *(")) + ;; Generator method (possibly using computed property). + (looking-at (concat "\\* *\\(?:\\[\\|" + js2-mode-identifier-re + " *(\\)")) (save-excursion - (goto-char (1- (match-end 0))) - (let (forward-sexp-function) (forward-sexp)) - (js2-forward-sws) - (eq (char-after) ?{)))))) + (js2-backward-sws) + ;; We might misindent some expressions that would + ;; return NaN anyway. Shouldn't be a problem. + (memq (char-before) '(?, ?} ?{))))))) (defun js2-continued-expression-p () "Return non-nil if the current line continues an expression." (save-excursion (back-to-indentation) - (or (js2-looking-at-operator-p) - (when (catch 'found - (while (and (re-search-backward "\n" nil t) - (let ((state (syntax-ppss))) - (when (nth 4 state) - (goto-char (nth 8 state))) ;; skip comments - (skip-chars-backward " \t") - (if (bolp) - t - (throw 'found t)))))) - (backward-char) - (when (js2-looking-at-operator-p) - (backward-char) - (not (looking-at "\\*\\|\\+\\+\\|--\\|/[/*]"))))))) + (if (js2-looking-at-operator-p) + (or (not (memq (char-after) '(?- ?+))) + (progn + (forward-comment (- (point))) + (not (memq (char-before) '(?, ?\[ ?\())))) + (forward-comment (- (point))) + (or (bobp) (backward-char)) + (when (js2-looking-at-operator-p) + (backward-char) + (not (looking-at "\\*\\|\\+\\+\\|--\\|/[/*]")))))) (defun js2-end-of-do-while-loop-p () "Return non-nil if word after point is `while' of a do-while @@ -440,7 +441,7 @@ indentation is aligned to that column." (goto-char bracket) (cond ((looking-at "[({[][ \t]*\\(/[/*]\\|$\\)") - (when (save-excursion (skip-chars-backward " \t)") + (when (save-excursion (skip-chars-backward " \t\n)") (looking-at ")")) (backward-list)) (back-to-indentation) @@ -493,6 +494,215 @@ indentation is aligned to that column." (when (cl-plusp offset) (forward-char offset))))) +;;; JSX Indentation + +;; The following JSX indentation code is copied basically verbatim from js.el at +;; 958da7f, except that the prefixes on the functions/variables are changed. + +(defsubst js2--jsx-find-before-tag () + "Find where JSX starts. + +Assume JSX appears in the following instances: +- Inside parentheses, when returned or as the first argument + to a function, and after a newline +- When assigned to variables or object properties, but only + on a single line +- As the N+1th argument to a function + +This is an optimized version of (re-search-backward \"[(,]\n\" +nil t), except set point to the end of the match. This logic +executes up to the number of lines in the file, so it should be +really fast to reduce that impact." + (let (pos) + (while (and (> (point) (point-min)) + (not (progn + (end-of-line 0) + (when (or (eq (char-before) 40) ; ( + (eq (char-before) 44)) ; , + (setq pos (1- (point)))))))) + pos)) + +(defconst js2--jsx-end-tag-re + (concat "" sgml-name-re ">\\|/>") + "Find the end of a JSX element.") + +(defconst js2--jsx-after-tag-re "[),]" + "Find where JSX ends. +This complements the assumption of where JSX appears from +`js--jsx-before-tag-re', which see.") + +(defun js2--jsx-indented-element-p () + "Determine if/how the current line should be indented as JSX. + +Return `first' for the first JSXElement on its own line. +Return `nth' for subsequent lines of the first JSXElement. +Return `expression' for an embedded JS expression. +Return `after' for anything after the last JSXElement. +Return nil for non-JSX lines. + +Currently, JSX indentation supports the following styles: + +- Single-line elements (indented like normal JS): + + var element =
; + +- Multi-line elements (enclosed in parentheses): + + function () { + return ( +