From a2b77c79dcca64b5e0ae58862206e7cc29640944 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 2 Feb 2023 14:57:41 -0800 Subject: [PATCH] Use c-ts-common-statement-offset for closing brackets too Merge c-ts-mode--close-bracket-offset into c-ts-common-statement-offset. * lisp/progmodes/c-ts-common.el: (c-ts-common-statement-offset): Handle closing brackets too. (c-ts-mode--close-bracket-offset): Remove function. * lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-styles): Use c-ts-common-statement-offset for closing brackets. --- lisp/progmodes/c-ts-common.el | 23 +++++++++-------------- lisp/progmodes/c-ts-mode.el | 8 ++++++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 05997e8bd20..8729cae4ba7 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -281,7 +281,7 @@ special handling from our bracket-counting indent algorithm. 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., @@ -293,14 +293,16 @@ 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. -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. @@ -323,9 +325,9 @@ PARENT is NODE's parent." (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)) @@ -352,13 +354,6 @@ the bracket in the body." (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 diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 9cbba92a194..00704337cd9 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -254,12 +254,16 @@ MODE is either `c' or `cpp'." ;; 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) -- 2.39.2