Fix c-ts-mode indent heuristic (bug#67417)
This is a continuation of the first two patches for bug#67417. The c-ts-mode--prev-line-match heuristic we added is too broad, so for now we are just adding a very specific heuristic for the else case. * lisp/progmodes/c-ts-mode.el: (c-ts-mode--prev-line-match): Remove function. (c-ts-mode--else-heuristic): New function. (c-ts-mode--indent-styles): Use c-ts-mode--else-heuristic.
This commit is contained in:
parent
08fc6bace2
commit
f0734e1c0d
1 changed files with 15 additions and 15 deletions
|
@ -356,14 +356,15 @@ PARENT, BOL, ARGS are the same as other anchor functions."
|
||||||
(apply (alist-get 'standalone-parent treesit-simple-indent-presets)
|
(apply (alist-get 'standalone-parent treesit-simple-indent-presets)
|
||||||
parent (treesit-node-parent parent) bol args))
|
parent (treesit-node-parent parent) bol args))
|
||||||
|
|
||||||
(defun c-ts-mode--prev-line-match (regexp)
|
(defun c-ts-mode--else-heuristic (node parent bol &rest _)
|
||||||
"An indentation matcher that matches if previous line matches REGEXP."
|
"Heuristic matcher for when else is followed by a closing bracket.
|
||||||
(lambda (_n _p bol &rest _)
|
NODE, PARENT, BOL are the same as other matchers."
|
||||||
|
(and (null node)
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char bol)
|
|
||||||
(forward-line -1)
|
(forward-line -1)
|
||||||
(back-to-indentation)
|
(looking-at (rx (* whitespace) "else" (* whitespace) eol)))
|
||||||
(looking-at-p regexp))))
|
(let ((next-node (treesit-node-first-child-for-pos parent bol)))
|
||||||
|
(equal (treesit-node-type next-node) "}"))))
|
||||||
|
|
||||||
(defun c-ts-mode--first-sibling (node parent &rest _)
|
(defun c-ts-mode--first-sibling (node parent &rest _)
|
||||||
"Matches when NODE is the \"first sibling\".
|
"Matches when NODE is the \"first sibling\".
|
||||||
|
@ -383,13 +384,12 @@ PARENT is its parent."
|
||||||
MODE is either `c' or `cpp'."
|
MODE is either `c' or `cpp'."
|
||||||
(let ((common
|
(let ((common
|
||||||
`((c-ts-mode--for-each-tail-body-matcher prev-line c-ts-mode-indent-offset)
|
`((c-ts-mode--for-each-tail-body-matcher prev-line c-ts-mode-indent-offset)
|
||||||
;; If the user types "if (...)" and hits RET, they expect
|
;; If the user types "else" and hits RET, they expect point
|
||||||
;; point on the empty line to be indented; this rule
|
;; on the empty line to be indented; this rule does that.
|
||||||
;; does that.
|
;; This heuristic is intentionally very specific because
|
||||||
((and no-node
|
;; more general heuristic is very error-prone, see
|
||||||
(c-ts-mode--prev-line-match
|
;; discussion in bug#67417.
|
||||||
,(rx (or "if" "else" "while" "do" "for"))))
|
(c-ts-mode--else-heuristic prev-line c-ts-mode-indent-offset)
|
||||||
prev-line c-ts-mode-indent-offset)
|
|
||||||
|
|
||||||
((parent-is "translation_unit") column-0 0)
|
((parent-is "translation_unit") column-0 0)
|
||||||
((query "(ERROR (ERROR)) @indent") column-0 0)
|
((query "(ERROR (ERROR)) @indent") column-0 0)
|
||||||
|
|
Loading…
Add table
Reference in a new issue