From 04cde74efe1909f69889cb65baea2ef9beb4dc11 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Tue, 21 May 2024 23:15:00 -0700 Subject: [PATCH] Improve treesit-major-mode-setup indentation setup (bug#71086) * lisp/treesit.el (treesit-major-mode-setup): Setup indentation when treesit-indent-function is set. * doc/lispref/modes.texi (Parser-based Indentation): Fix manual. (cherry picked from commit e153093f0a14fda47daa12b151b291e61d20b7e3) --- doc/lispref/modes.texi | 9 ++++++--- lisp/treesit.el | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi index ffede9e86f5..361a5e37d02 100644 --- a/doc/lispref/modes.texi +++ b/doc/lispref/modes.texi @@ -5186,9 +5186,12 @@ below: then the major mode needs only write some indentation rules, and the engine takes care of the rest. To enable the parser-based indentation engine, either set -@code{treesit-simple-indent-rules} and call -@code{treesit-major-mode-setup}, or equivalently, set the value of -@code{indent-line-function} to @code{treesit-indent}. +@code{treesit-simple-indent-rules} or set +@code{treesit-indent-function}, then call +@code{treesit-major-mode-setup}. (All that +@code{treesit-major-mode-setup} does is setting the value of +@code{indent-line-function} to @code{treesit-indent} and +@code{indent-region-function} to @code{treesit-indent-region}.) @defvar treesit-indent-function This variable stores the actual function called by diff --git a/lisp/treesit.el b/lisp/treesit.el index 86ed1bbae33..0475227c726 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -3008,7 +3008,12 @@ before calling this function." (when treesit-simple-indent-rules (setq-local treesit-simple-indent-rules (treesit--indent-rules-optimize - treesit-simple-indent-rules)) + treesit-simple-indent-rules))) + ;; Enable indent if simple indent rules are set, or the major mode + ;; sets a custom indent function. + (when (or treesit-simple-indent-rules + (and (not (eq treesit-indent-function #'treesit-simple-indent)) + treesit-indent-function)) (setq-local indent-line-function #'treesit-indent) (setq-local indent-region-function #'treesit-indent-region)) ;; Navigation. -- 2.39.5