Function: c-ts-common-statement-offset
c-ts-common-statement-offset is a byte-compiled function defined in
c-ts-common.el.gz.
Signature
(c-ts-common-statement-offset NODE PARENT &rest _)
Documentation
Return an indent offset for a statement inside a block.
Assumes the anchor is (point-min), i.e., the 0th column.
This function basically counts the number of block nodes (i.e.,
brackets) (defined by c-ts-common-indent-block-type-regexp)
between NODE and the root node (not counting NODE itself), and
multiply that by c-ts-common-indent-offset.
To support GNU style, on each block level, this function also checks whether the opening bracket { is on its own line, if so, it adds an extra level, except for the top-level.
It also has special handling for bracketless statements and else-if statements, which see.
PARENT is NODE's parent, BOL is the beginning of non-whitespace characters on the current line.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/c-ts-common.el.gz
(defun c-ts-common-statement-offset (node parent &rest _)
"Return an indent offset for a statement inside a block.
Assumes the anchor is (point-min), i.e., the 0th column.
This function basically counts the number of block nodes (i.e.,
brackets) (defined by `c-ts-common-indent-block-type-regexp')
between NODE and the root node (not counting NODE itself), and
multiply that by `c-ts-common-indent-offset'.
To support GNU style, on each block level, this function also
checks whether the opening bracket { is on its own line, if so,
it adds an extra level, except for the top-level.
It also has special handling for bracketless statements and
else-if statements, which see.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters on the current line."
(let ((level 0))
;; If NODE is a opening/closing bracket on its own line, take off
;; one level because the code below assumes NODE is a statement
;; _inside_ a {} block.
(when (c-ts-common--node-is node 'block 'close-bracket)
(cl-decf level))
;; If point is on an empty line, NODE would be nil, but we pretend
;; there is a statement node.
(when (null node)
(setq node t))
;; Go up the tree and compute indent level.
(while (if (eq node t)
(setq node parent)
node)
(let ((parent (treesit-node-parent node)))
;; Increment level for every bracket (with exception).
(when (c-ts-common--node-is node 'block)
(cl-incf level)
(save-excursion
(goto-char (treesit-node-start node))
;; Add an extra level if the opening bracket is on its own
;; line, except (1) it's at top-level, or (2) it's immediate
;; parent is another block.
(cond ((bolp) nil) ; Case (1).
((c-ts-common--node-is parent 'block) ; Case (2).
nil)
;; Add a level.
((looking-back (rx bol (* whitespace))
(line-beginning-position))
(cl-incf level)))))
;; Fix bracketless statements.
(when (and (c-ts-common--node-is parent
'if 'do 'while 'for)
(not (c-ts-common--node-is node 'block)))
(cl-incf level))
;; Flatten "else if" statements.
(when (and (c-ts-common--node-is node 'else)
(c-ts-common--node-is node 'if)
;; But if the "if" is on it's own line, still
;; indent a level.
(not (save-excursion
(goto-char (treesit-node-start node))
(looking-back (rx bol (* whitespace))
(line-beginning-position)))))
(cl-decf level)))
;; Go up the tree.
(setq node (treesit-node-parent node)))
(* level (symbol-value c-ts-common-indent-offset))))