diff options
| author | Yuan Fu | 2023-11-26 17:42:51 -0800 |
|---|---|---|
| committer | Yuan Fu | 2023-11-26 17:42:51 -0800 |
| commit | dab7cc241f426af3c9fe901954fe48e415dcf75a (patch) | |
| tree | 759bd6a4a57d2764dc05dc1d48ae0dce0cf18efc | |
| parent | f8d9dc26c78366e1f2de9dd847fe4744f607748d (diff) | |
| download | emacs-dab7cc241f426af3c9fe901954fe48e415dcf75a.tar.gz emacs-dab7cc241f426af3c9fe901954fe48e415dcf75a.zip | |
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.
| -rw-r--r-- | lisp/progmodes/c-ts-mode.el | 16 |
1 files changed, 16 insertions, 0 deletions
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." | |||
| 355 | (apply (alist-get 'standalone-parent treesit-simple-indent-presets) | 355 | (apply (alist-get 'standalone-parent treesit-simple-indent-presets) |
| 356 | parent (treesit-node-parent parent) bol args)) | 356 | parent (treesit-node-parent parent) bol args)) |
| 357 | 357 | ||
| 358 | (defun c-ts-mode--prev-line-match (regexp) | ||
| 359 | "An indentation matcher that matches if prev line matches REGEXP." | ||
| 360 | (lambda (_n _p bol &rest _) | ||
| 361 | (save-excursion | ||
| 362 | (goto-char bol) | ||
| 363 | (forward-line -1) | ||
| 364 | (back-to-indentation) | ||
| 365 | (looking-at-p regexp)))) | ||
| 366 | |||
| 358 | (defun c-ts-mode--indent-styles (mode) | 367 | (defun c-ts-mode--indent-styles (mode) |
| 359 | "Indent rules supported by `c-ts-mode'. | 368 | "Indent rules supported by `c-ts-mode'. |
| 360 | MODE is either `c' or `cpp'." | 369 | MODE is either `c' or `cpp'." |
| 361 | (let ((common | 370 | (let ((common |
| 362 | `((c-ts-mode--for-each-tail-body-matcher prev-line c-ts-mode-indent-offset) | 371 | `((c-ts-mode--for-each-tail-body-matcher prev-line c-ts-mode-indent-offset) |
| 372 | ;; If the user types "if (...)" and hit return, they expect | ||
| 373 | ;; the point on the empty line to be indented, this rule | ||
| 374 | ;; does that. | ||
| 375 | ((and no-node | ||
| 376 | (c-ts-mode--prev-line-match | ||
| 377 | ,(rx (or "if" "else" "while" "do" "for")))) | ||
| 378 | prev-line c-ts-mode-indent-offset) | ||
| 363 | 379 | ||
| 364 | ((parent-is "translation_unit") column-0 0) | 380 | ((parent-is "translation_unit") column-0 0) |
| 365 | ((query "(ERROR (ERROR)) @indent") column-0 0) | 381 | ((query "(ERROR (ERROR)) @indent") column-0 0) |