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.
Probably introduced at or before Emacs version 30.1.
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.
(setq-local treesit-primary-parser (treesit-parser-create 'javascript))
;; Indent.
(setq-local treesit-simple-indent-rules js--treesit-indent-rules)
;; Navigation.
(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)
(setq-local treesit-thing-settings
`((javascript
(sexp ,(js--regexp-opt-symbol js--treesit-sexp-nodes))
(sentence ,(js--regexp-opt-symbol js--treesit-sentence-nodes))
(text ,(js--regexp-opt-symbol '("comment"
"template_string"))))))
;; Fontification.
(setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
(setq-local treesit-font-lock-feature-list
'(( comment document definition)
( keyword string)
( assignment constant escape-sequence jsx number
pattern string-interpolation)
( bracket delimiter function operator property)))
(when (treesit-ready-p 'jsdoc t)
(setq-local treesit-range-settings
(treesit-range-rules
:embed 'jsdoc
:host 'javascript
:local t
`(((comment) @capture (:match ,js--treesit-jsdoc-beginning-regexp @capture)))))
(setq c-ts-common--comment-regexp (rx (or "comment" "line_comment" "block_comment" "description"))))
;; 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))))