Function: web-mode-javascript-indentation

web-mode-javascript-indentation is a natively compiled function defined in web-mode.el.

Signature

(web-mode-javascript-indentation POS INITIAL-COLUMN LANGUAGE-OFFSET LANGUAGE &optional LIMIT)

Source Code

;; Defined in /nix/store/2qgnz1rp30nzfz6w0apx4kdm14if1bbs-emacs-packages-deps/share/emacs/site-lisp/elpa/web-mode-20260831.219/web-mode.el
(defun web-mode-javascript-indentation (pos initial-column language-offset language &optional limit)
  (let (open-ctx open-pos indentation offset sub block-pos)
    (setq open-ctx (web-mode-bracket-up pos language limit))
    ;;(message "%S" open-ctx)
    ;;(message "pos(%S) initial-column(%S) language-offset(%S) language(%S) limit(%S)" pos initial-column language-offset language limit)
    ;;(message "javascript-indentation: %S\nchar=%c" open-ctx (plist-get open-ctx :char))
    (setq indentation (plist-get open-ctx :indentation))
    (when (and initial-column (> initial-column indentation))
      (setq indentation initial-column))
    (setq case-fold-search nil) ;#1006
    (when open-ctx
      (setq open-pos (plist-get open-ctx :pos)))
    (setq block-pos (web-mode-inside-block-control pos))
    (when (and block-pos (> limit block-pos)) ;#1275
      (setq block-pos nil))
    ;;(message "bracket-pos=%S block-pos=%S" open-pos block-pos)
    (cond
      ((and block-pos (or (null open-pos) (> block-pos open-pos))) ;#1230
       (setq offset (+ indentation language-offset)))
      ((null open-pos)
       (setq offset initial-column))
      ((and (member language '("javascript" "jsx" "ejs"))
            (eq (plist-get open-ctx :char) ?\{)
            (web-mode-looking-back "switch[ ]*" (plist-get open-ctx :pos)))
       (setq sub (if (cdr (assoc "case-extra-offset" web-mode-indentation-params)) 0 1))
       (cond
         ((looking-at-p "case\\|default")
          (setq offset (+ indentation (* language-offset (- 1 sub)))))
         (t
          (setq offset (+ indentation (* language-offset (- 2 sub)))))
         ) ;cond switch
       )
      (t
       (setq offset (+ indentation language-offset)))
      ) ;cond
    (setq case-fold-search t)
    (cons (if (< offset initial-column) initial-column offset) open-ctx)
    ))