[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
feature/tree-sitter 98f5b74808 2/3: Add treesit--indent-rules-optimize
From: |
Yuan Fu |
Subject: |
feature/tree-sitter 98f5b74808 2/3: Add treesit--indent-rules-optimize |
Date: |
Mon, 7 Nov 2022 05:15:05 -0500 (EST) |
branch: feature/tree-sitter
commit 98f5b74808a54ad9093888b72b2c04e085f4b369
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Add treesit--indent-rules-optimize
* lisp/treesit.el (treesit--indent-rules-optimize): New function.
(treesit-major-mode-setup): Optimize indent rules when setting up for
major mode.
---
lisp/treesit.el | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 37eee929d7..83f9dd1b7f 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1327,6 +1327,35 @@ indentation (target) is in green, current indentation is
in red."
(indent-region (point-min) (point-max))
(diff-buffers source-buf (current-buffer)))))
+(defun treesit--indent-rules-optimize (rules)
+ "Optimize simple indent RULES.
+RULES should be a value suitable for
+`treesit-simple-indent-rules'. Return the optimized version of
+RULES."
+ ;; Right now this function just compiles queries. it doesn't
+ ;; byte-compile matchers and anchors because it doesn't make much
+ ;; difference.
+ (cl-loop for setting in rules
+ for lang = (car setting)
+ for indent-rules = (cdr setting)
+ collect
+ (cl-labels
+ ;; Optimize a matcher or anchor.
+ ((optimize-func (func)
+ (pcase func
+ (`(query ,qry)
+ (list 'query (treesit-query-compile lang qry)))
+ (_ func)))
+ ;; Optimize a rule (MATCHER ANCHOR OFFSET).
+ (optimize-rule (rule)
+ (let ((matcher (nth 0 rule))
+ (anchor (nth 1 rule))
+ (offset (nth 2 rule)))
+ (list (optimize-func matcher)
+ (optimize-func anchor)
+ offset))))
+ (cons lang (mapcar #'optimize-rule indent-rules)))))
+
;;; Search
(defun treesit-search-forward-goto
@@ -1539,6 +1568,9 @@ If `treesit-defun-type-regexp' is non-nil, setup
(treesit-font-lock-recompute-features))
;; Indent.
(when treesit-simple-indent-rules
+ (setq-local treesit-simple-indent-rules
+ (treesit--indent-rules-optimize
+ treesit-simple-indent-rules))
(setq-local indent-line-function #'treesit-indent)
(setq-local indent-region-function #'treesit-indent-region))
;; Navigation.