Function: treesit-major-mode-setup

treesit-major-mode-setup is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-major-mode-setup)

Documentation

Activate tree-sitter to power major-mode features.

If treesit-font-lock-settings is non-nil, set up fontification and enable font-lock-mode(var)/font-lock-mode(fun).

If treesit-simple-indent-rules is non-nil, set up indentation.

If treesit-defun-type-regexp is non-nil, set up beginning-of-defun-function and end-of-defun-function.

If treesit-defun-name-function is non-nil, set up add-log-current-defun.

If treesit-simple-imenu-settings is non-nil, set up Imenu.

Make sure necessary parsers are created for the current buffer before calling this function.

View in manual

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-major-mode-setup ()
  "Activate tree-sitter to power major-mode features.

If `treesit-font-lock-settings' is non-nil, set up fontification
and enable `font-lock-mode'.

If `treesit-simple-indent-rules' is non-nil, set up indentation.

If `treesit-defun-type-regexp' is non-nil, set up
`beginning-of-defun-function' and `end-of-defun-function'.

If `treesit-defun-name-function' is non-nil, set up
`add-log-current-defun'.

If `treesit-simple-imenu-settings' is non-nil, set up Imenu.

Make sure necessary parsers are created for the current buffer
before calling this function."
  ;; Font-lock.
  (when treesit-font-lock-settings
    ;; `font-lock-mode' wouldn't set up properly if
    ;; `font-lock-defaults' is nil, see `font-lock-specified-p'.
    (setq-local font-lock-defaults
                '( nil nil nil nil
                   (font-lock-fontify-syntactically-function
                    . treesit-font-lock-fontify-region)))
    (treesit-font-lock-recompute-features)
    (dolist (parser (treesit-parser-list))
      (treesit-parser-add-notifier
       parser #'treesit--font-lock-notifier))
    (add-hook 'pre-redisplay-functions #'treesit--pre-redisplay 0 t))
  ;; Syntax
  (dolist (parser (treesit-parser-list))
    (treesit-parser-add-notifier
     parser #'treesit--syntax-propertize-notifier))
  (add-hook 'syntax-propertize-extend-region-functions
            #'treesit--pre-syntax-ppss 0 t)
  ;; Indent.
  (when treesit-simple-indent-rules
    (setq-local treesit-simple-indent-rules
                (treesit--indent-rules-optimize
                 treesit-simple-indent-rules))
    (setq-local indent-line-function #'treesit-indent)
    (setq-local indent-region-function #'treesit-indent-region))
  ;; Navigation.
  (when treesit-defun-type-regexp
    (keymap-set (current-local-map) "<remap> <beginning-of-defun>"
                #'treesit-beginning-of-defun)
    (keymap-set (current-local-map) "<remap> <end-of-defun>"
                #'treesit-end-of-defun)
    ;; `end-of-defun' will not work completely correctly in nested
    ;; defuns due to its implementation.  However, many lisp programs
    ;; use `beginning/end-of-defun', so we should still set
    ;; `beginning/end-of-defun-function' so they still mostly work.
    ;; This is also what `cc-mode' does: rebind user commands and set
    ;; the variables.  In future we should update `end-of-defun' to
    ;; work with nested defuns.
    (setq-local beginning-of-defun-function #'treesit-beginning-of-defun)
    (setq-local end-of-defun-function #'treesit-end-of-defun))
  ;; Defun name.
  (when treesit-defun-name-function
    (setq-local add-log-current-defun-function
                #'treesit-add-log-current-defun))
  ;; Imenu.
  (when treesit-simple-imenu-settings
    (setq-local imenu-create-index-function
                #'treesit-simple-imenu)))