[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
emacs-29 a2b77c79dcc 3/6: Use c-ts-common-statement-offset for closing b
From: |
Yuan Fu |
Subject: |
emacs-29 a2b77c79dcc 3/6: Use c-ts-common-statement-offset for closing brackets too |
Date: |
Thu, 2 Feb 2023 21:46:21 -0500 (EST) |
branch: emacs-29
commit a2b77c79dcca64b5e0ae58862206e7cc29640944
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
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)
- emacs-29 updated (88ccf78b206 -> 96ea27278b4), Yuan Fu, 2023/02/02
- emacs-29 b8009bbf2d8 1/6: ; Fix error where we pass t to treesit-node-type in c-ts-common.el, Yuan Fu, 2023/02/02
- emacs-29 8a6bdf88b4b 4/6: Call treesit_record_change in insert_from_gap_1, Yuan Fu, 2023/02/02
- emacs-29 a2b77c79dcc 3/6: Use c-ts-common-statement-offset for closing brackets too,
Yuan Fu <=
- emacs-29 74e715cb729 2/6: ; Go back to original point when filling comments in c-ts-mode, Yuan Fu, 2023/02/02
- emacs-29 d963a8f1355 5/6: Make c-ts-mode indent tests side-effect-free, Yuan Fu, 2023/02/02
- emacs-29 96ea27278b4 6/6: ; Fix c-ts-mode indent test, Yuan Fu, 2023/02/02