This can be nil, meaning such special handling is not needed.")
-(defun c-ts-common-statement-offset (node parent &rest _)
+(defun c-ts-common-statement-offset (node parent bol &rest _)
"This anchor is used for children of a statement inside a block.
This function basically counts the number of block nodes (i.e.,
checks whether the opening bracket { is on its own line, if so,
it adds an extra level, except for the top-level.
-PARENT is NODE's parent."
+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 (and node
- (string-match-p c-ts-common-indent-block-type-regexp
- (treesit-node-type node)))
+ (or (string-match-p c-ts-common-indent-block-type-regexp
+ (treesit-node-type node))
+ (save-excursion (goto-char bol) (looking-at-p "}"))))
(cl-decf level))
;; If point is on an empty line, NODE would be nil, but we pretend
;; there is a statement node.
(treesit-node-parent node))))
;; Case (2).
(and parent-type
- (or (string-match-p
- c-ts-common-indent-block-type-regexp
- parent-type))))
+ (string-match-p
+ c-ts-common-indent-block-type-regexp
+ parent-type)))
nil)
;; Add a level.
((looking-back (rx bol (* whitespace))
(1+ level)
level)))
-(defun c-ts-mode--close-bracket-offset (node parent &rest _)
- "Offset for the closing bracket, NODE.
-It's basically one level less that the statements in the block.
-PARENT is NODE's parent."
- (- (c-ts-common-statement-offset node parent)
- (symbol-value c-ts-common-indent-offset)))
-
(provide 'c-ts-common)
;;; c-ts-common.el ends here
;; int[5] a = { 0, 0, 0, 0 };
((parent-is "initializer_list") parent-bol c-ts-mode-indent-offset)
+ ;; Statement in enum.
((parent-is "enumerator_list") point-min c-ts-common-statement-offset)
+ ;; Statement in struct and union.
((parent-is "field_declaration_list") point-min c-ts-common-statement-offset)
- ;; {} blocks.
- ((node-is "}") point-min c-ts-mode--close-bracket-offset)
+ ;; Statement in {} blocks.
((parent-is "compound_statement") point-min c-ts-common-statement-offset)
+ ;; Closing bracket.
+ ((node-is "}") point-min c-ts-common-statement-offset)
+ ;; Opening bracket.
((node-is "compound_statement") point-min c-ts-common-statement-offset)
,@(when (eq mode 'cpp)