emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

emacs-29 1238fa8e49 2/4: Fix label indent of GNU and Linux style in c-ts


From: Yuan Fu
Subject: emacs-29 1238fa8e49 2/4: Fix label indent of GNU and Linux style in c-ts-mode (bug#60543)
Date: Mon, 9 Jan 2023 01:15:14 -0500 (EST)

branch: emacs-29
commit 1238fa8e49bdb12db66d856dcb4b192db5d026ff
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    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 e1b45b06e1..772b259d59 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]