emacs-diffs
[Top][All Lists]
Advanced

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

emacs-29 dab7cc241f4: Fix c-ts-mode indentation after if/else (bug#67417


From: Yuan Fu
Subject: emacs-29 dab7cc241f4: Fix c-ts-mode indentation after if/else (bug#67417)
Date: Sun, 26 Nov 2023 20:44:48 -0500 (EST)

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

    Fix c-ts-mode indentation after if/else (bug#67417)
    
    * lisp/progmodes/c-ts-mode.el:
    (c-ts-mode--prev-line-match): New function.
    (c-ts-mode--indent-styles): Add a rule for the empty line after
    if/else/for/etc.
---
 lisp/progmodes/c-ts-mode.el | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 5975057518e..1a7424db5d9 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -355,11 +355,27 @@ PARENT, BOL, ARGS are the same as other anchor functions."
   (apply (alist-get 'standalone-parent treesit-simple-indent-presets)
          parent (treesit-node-parent parent) bol args))
 
+(defun c-ts-mode--prev-line-match (regexp)
+  "An indentation matcher that matches if prev line matches REGEXP."
+  (lambda (_n _p bol &rest _)
+    (save-excursion
+      (goto-char bol)
+      (forward-line -1)
+      (back-to-indentation)
+      (looking-at-p regexp))))
+
 (defun c-ts-mode--indent-styles (mode)
   "Indent rules supported by `c-ts-mode'.
 MODE is either `c' or `cpp'."
   (let ((common
          `((c-ts-mode--for-each-tail-body-matcher prev-line 
c-ts-mode-indent-offset)
+           ;; If the user types "if (...)" and hit return, they expect
+           ;; the point on the empty line to be indented, this rule
+           ;; does that.
+           ((and no-node
+                 (c-ts-mode--prev-line-match
+                  ,(rx (or "if" "else" "while" "do" "for"))))
+            prev-line c-ts-mode-indent-offset)
 
            ((parent-is "translation_unit") column-0 0)
            ((query "(ERROR (ERROR)) @indent") column-0 0)



reply via email to

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