Function: js-ts-mode

js-ts-mode is an autoloaded, interactive and byte-compiled function defined in js.el.gz.

Signature

(js-ts-mode)

Documentation

Major mode for editing JavaScript.

In addition to any hooks its parent mode js-base-mode might have run, this mode runs the hook js-ts-mode-hook, as the final or penultimate step during initialization.

M-. js-find-symbol

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/js.el.gz
;;;###autoload
(define-derived-mode js-ts-mode js-base-mode "JavaScript"
  "Major mode for editing JavaScript.

\\<js-ts-mode-map>"
  :group 'js
  :syntax-table js-mode-syntax-table
  (when (treesit-ready-p 'javascript)
    ;; Borrowed from `js-mode'.
    (setq-local prettify-symbols-alist js--prettify-symbols-alist)
    (setq-local parse-sexp-ignore-comments t)
    ;; Which-func.
    (setq-local which-func-imenu-joiner-function #'js--which-func-joiner)
    ;; Comment.
    (c-ts-common-comment-setup)
    (setq-local comment-multi-line t)
    ;; Electric-indent.
    (setq-local electric-indent-chars
                (append "{}():;,<>/" electric-indent-chars)) ;FIXME: js2-mode adds "[]*".
    (setq-local electric-layout-rules
                '((?\; . after) (?\{ . after) (?\} . before)))
    (setq-local syntax-propertize-function #'js-ts--syntax-propertize)

    ;; Tree-sitter setup.
    (treesit-parser-create 'javascript)
    ;; Indent.
    (setq-local treesit-simple-indent-rules js--treesit-indent-rules)
    ;; Navigation.
    (setq-local treesit-defun-prefer-top-level t)
    (setq-local treesit-defun-type-regexp
                (rx (or "class_declaration"
                        "method_definition"
                        "function_declaration"
                        "lexical_declaration")))
    (setq-local treesit-defun-name-function #'js--treesit-defun-name)
    ;; Fontification.
    (setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
    (setq-local treesit-font-lock-feature-list
                '(( comment definition)
                  ( keyword string)
                  ( assignment constant escape-sequence jsx number
                    pattern string-interpolation)
                  ( bracket delimiter function operator property)))
    ;; Imenu
    (setq-local treesit-simple-imenu-settings
                `(("Function" "\\`function_declaration\\'" nil nil)
                  ("Variable" "\\`lexical_declaration\\'"
                   js--treesit-valid-imenu-entry nil)
                  ("Class" ,(rx bos (or "class_declaration"
                                        "method_definition")
                                eos)
                   nil nil)))
    (treesit-major-mode-setup)

    (add-to-list 'auto-mode-alist
                 '("\\(\\.js[mx]\\|\\.har\\)\\'" . js-ts-mode))))