Skip to content

Instantly share code, notes, and snippets.

@th0rex
Created March 31, 2020 16:07
Show Gist options
  • Save th0rex/64a48204f5129c0cd293bd90932bd018 to your computer and use it in GitHub Desktop.
Save th0rex/64a48204f5129c0cd293bd90932bd018 to your computer and use it in GitHub Desktop.
> PS: You want to hook into font-lock rather than into
> `window-scroll-functions`. More about this is my next email.
Here's one way to do it.
[ Note: like the previous, this is guarantee 100% untested. ]
Stefan
diff --git a/lisp/tree-sitter-highlight.el b/lisp/tree-sitter-highlight.el
index 53ecb32..3246bd6 100644
--- a/lisp/tree-sitter-highlight.el
+++ b/lisp/tree-sitter-highlight.el
@@ -167,7 +167,6 @@ to faces. Each function takes no arguments."
(defvar-local tree-sitter-highlight--injections nil)
(defvar-local tree-sitter-highlight--injections-query nil)
(defvar-local tree-sitter-highlight--jit-function nil)
-(defvar-local tree-sitter-highlight--orig-scroll-functions nil)
(defvar-local tree-sitter-highlight--query nil)
(defvar-local tree-sitter-highlight--query-cursor nil)
@@ -240,19 +239,11 @@ to faces. Each function takes no arguments."
(defun tree-sitter-highlight--jit (old-tree)
(when old-tree
- (let ((changes (ts-changed-ranges old-tree tree-sitter-tree))
- (wstart (window-start))
- (wend (window-end)))
- ;; TODO: Remember what we've highlighted, similar to how font-lock does it.
- ;; Already highlighted regions shouldn't be re-highlighted.
-
- ;; Find changes that are within the current window
- (mapc #'(lambda (range)
- (let ((start (aref range 0))
- (end (aref range 1)))
- ;; TODO: Improve this
- (tree-sitter-highlight--highlight (max wstart start) (min wend end))))
- changes))))
+ (mapc #'(lambda (range)
+ (let ((start (aref range 0))
+ (end (aref range 1)))
+ (font-lock-flush start end)))
+ (ts-changed-ranges old-tree tree-sitter-tree))))
(defun tree-sitter-highlight--highlight (start end)
(ts--save-context
@@ -282,9 +273,6 @@ to faces. Each function takes no arguments."
;; (ts-byte-from-position start)
;; (ts-byte-from-position end)))))
-(defun tree-sitter-highlight--highlight-window (_window start)
- (tree-sitter-highlight--highlight start (window-end nil t)))
-
(defun tree-sitter-highlight--enable ()
"Enable `tree-sitter-highlight' in this buffer."
(run-hooks 'tree-sitter-highlight-setup-functions)
@@ -301,10 +289,8 @@ to faces. Each function takes no arguments."
tree-sitter-highlight--injections-query (cadr x)))
(setq tree-sitter-highlight--capture-names (ts-query-capture-names tree-sitter-highlight--query))
(setq tree-sitter-highlight--query-cursor (ts-make-query-cursor))
- (make-variable-buffer-local 'window-scroll-functions)
- (setq tree-sitter-highlight--orig-scroll-functions window-scroll-functions)
- (setq window-scroll-functions (cons #'tree-sitter-highlight--highlight-window window-scroll-functions))
- (tree-sitter-highlight--highlight-window nil (window-start))
+ (add-function :override (local 'font-lock-fontify-region-function)
+ #'tree-sitter-highlight--highlight)
(add-hook 'tree-sitter-after-change-functions #'tree-sitter-highlight--jit nil t)
)
@@ -314,7 +300,8 @@ to faces. Each function takes no arguments."
(remove-text-properties (point-min)
(point-max)
'(face nil)))
- (setq window-scroll-functions tree-sitter-highlight--orig-scroll-functions)
+ (remove-function (local 'font-lock-fontify-region-function)
+ #'tree-sitter-highlight--highlight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment