From 1238fa8e49bdb12db66d856dcb4b192db5d026ff Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Sun, 8 Jan 2023 16:57:29 -0800 Subject: [PATCH] Fix label indent of GNU and Linux style in c-ts-mode (bug#60543) The previous fix isn't correct. * lisp/progmodes/c-ts-mode.el: (c-ts-mode--indent-styles): New indent rule. Fix the rule for Linux style. (c-ts-mode--top-level-label-matcher): New function. --- lisp/progmodes/c-ts-mode.el | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index e1b45b06e1a..772b259d59e 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -125,6 +125,7 @@ MODE is either `c' or `cpp'." ((and (parent-is "comment") c-ts-mode--looking-at-star) c-ts-mode--comment-start-after-first-star -1) ((parent-is "comment") prev-adaptive-prefix 0) + (c-ts-mode--top-level-label-matcher point-min 1) ((node-is "labeled_statement") parent-bol 0) ((parent-is "labeled_statement") parent-bol c-ts-mode-indent-offset) ((match "preproc_ifdef" "compound_statement") point-min 0) @@ -171,7 +172,10 @@ MODE is either `c' or `cpp'." ,@common) (k&r ,@common) (linux - ((node-is "labeled_statement") point-min 1) + ;; Reference: + ;; https://www.kernel.org/doc/html/latest/process/coding-style.html, + ;; and script/Lindent in Linux kernel repository. + ((node-is "labeled_statement") point-min 0) ,@common) (bsd ((parent-is "if_statement") parent-bol 0) @@ -195,6 +199,17 @@ MODE is either `c' or `cpp'." ('linux (alist-get 'linux (c-ts-mode--indent-styles mode))))))) `((,mode ,@style)))) +(defun c-ts-mode--top-level-label-matcher (node &rest _) + "A matcher that matches a top-level label. +NODE should be a labeled_statement." + (let ((func (treesit-parent-until + node (lambda (n) + (equal (treesit-node-type n) + "function_definition"))))) + (and (equal (treesit-node-type node) + "labeled_statement") + (not (treesit-node-top-level func "function_definition"))))) + (defun c-ts-mode--bracket-children-anchor (_n parent &rest _) "This anchor is used for children of a compound_statement. So anything inside a {} block. PARENT should be the -- 2.39.5