Function: treesit-simple-indent

treesit-simple-indent is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-simple-indent NODE PARENT BOL)

Documentation

Calculate indentation according to treesit-simple-indent-rules.

BOL is the position of the first non-whitespace character on the current line. NODE is the largest node that starts at BOL, PARENT is NODE's parent.

Return (ANCHOR . OFFSET) where ANCHOR is a node, OFFSET is the indentation offset, meaning indent to align with ANCHOR and add OFFSET.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-simple-indent (node parent bol)
  "Calculate indentation according to `treesit-simple-indent-rules'.

BOL is the position of the first non-whitespace character on the
current line.  NODE is the largest node that starts at BOL,
PARENT is NODE's parent.

Return (ANCHOR . OFFSET) where ANCHOR is a node, OFFSET is the
indentation offset, meaning indent to align with ANCHOR and add
OFFSET."
  (if (null parent)
      (progn (when treesit--indent-verbose
               (message "PARENT is nil, not indenting"))
             (cons nil nil))
    (let* ((language (treesit-node-language parent))
           (rules-list (list
                        (alist-get language
                                   treesit-simple-indent-override-rules)
                        (alist-get language
                                   treesit-simple-indent-rules))))
      (catch 'match
        (dolist (rules rules-list)
          (dolist (rule rules)
            (if (functionp rule)
                (let ((result (funcall rule node parent bol)))
                  (when result
                    (when treesit--indent-verbose
                      (message "Matched rule: %S" rule))
                    (throw 'match result)))
              (let ((pred (nth 0 rule))
                    (anchor (nth 1 rule))
                    (offset (nth 2 rule)))
                ;; Found a match.
                (when (treesit--simple-indent-eval
                       (list pred node parent bol))
                  (when treesit--indent-verbose
                    (message "Matched rule: %S" rule))
                  (let ((anchor-pos
                         (treesit--simple-indent-eval
                          (list anchor node parent bol)))
                        (offset-val
                         (cond ((numberp offset) offset)
                               ((and (symbolp offset)
                                     (boundp offset))
                                (symbol-value offset))
                               (t (treesit--simple-indent-eval
                                   (list offset node parent bol))))))
                    (throw 'match (cons anchor-pos offset-val))))))))
        ;; Didn't find any match.
        (when treesit--indent-verbose
          (message "No matched rule"))
        (cons nil nil)))))