Skip to content

Commit

Permalink
perf(indicators): Render only in next redisplay in timer
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jul 6, 2024
1 parent 442f6c5 commit 2a0210d
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions ts-fold-indicators.el
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ Argument FOLDED holds folding state; it's a boolean."
(let ((folded (ts-fold-overlay-at node)))
(ts-fold-indicators--create-overlays beg end folded))))

(defun ts-fold-indicators--schedule-render (&optional window &rest _)
"Schedule the render on the next redisplay in WINDOW."
(let ((timer (window-parameter window 'ts-fold-indicators-render-timer)))
(when (timerp timer)
(cancel-timer timer))
(set-window-parameter window 'ts-fold-indicators-render-timer
(run-with-timer 0.0 nil #'ts-fold-indicators-refresh window))))

(defun ts-fold-indicators--size-change (&optional frame &rest _)
"Render indicators for all visible windows from FRAME."
(ts-fold--with-no-redisplay
Expand All @@ -320,8 +328,7 @@ Argument FOLDED holds folding state; it's a boolean."

(defun ts-fold-indicators--render-window (window)
"Render indicators for WINDOW."
(ts-fold--with-selected-window window
(ignore-errors (ts-fold-indicators-refresh))))
(ts-fold-indicators--schedule-render window))

(defun ts-fold-indicators--trigger-render (&rest _)
"Trigger rendering on the next redisplay."
Expand All @@ -330,7 +337,7 @@ Argument FOLDED holds folding state; it's a boolean."
(defun ts-fold-indicators--post-command ()
"Post command."
(when ts-fold-indicators--render-this-command-p
(ts-fold-indicators-refresh)
(ts-fold-indicators--render-window (selected-window))
(setq ts-fold-indicators--render-this-command-p nil)))

(defun ts-fold-indicators--within-window (node wend wstart)
Expand All @@ -353,34 +360,35 @@ Arguments WEND and WSTART are the range for caching."
node))

;;;###autoload
(defun ts-fold-indicators-refresh (&rest _)
"Refresh indicators for all folding range."
(when (and tree-sitter-mode ts-fold-indicators-mode)
(ts-fold--ensure-ts
(when-let*
((node (ignore-errors (tsc-root-node tree-sitter-tree)))
(patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) @name))
(alist-get major-mode ts-fold-range-alist)
'vector))
(query (ignore-errors
(tsc-make-query tree-sitter-language patterns)))
(nodes-to-fold (tsc-query-captures query node #'ignore))
(wend (window-end nil t))
(wstart (window-start))
(nodes-to-fold
(cl-remove-if-not (lambda (node)
(ts-fold-indicators--within-window (cdr node) wend wstart))
nodes-to-fold))
(mode-ranges (alist-get major-mode ts-fold-range-alist))
(nodes-to-fold
(cl-remove-if (lambda (node)
(ts-fold--non-foldable-node-p (cdr node) mode-ranges))
nodes-to-fold)))
(ts-fold-indicators--remove-ovs)
(thread-last nodes-to-fold
(mapcar #'cdr)
(mapc #'ts-fold-indicators--create))
(run-hooks 'ts-fold-indicators-refresh-hook)))))
(defun ts-fold-indicators-refresh (&optional window &rest _)
"Refresh indicators for all folding range in WINDOW."
(ts-fold--with-selected-window (or window (selected-window))
(when (and tree-sitter-mode ts-fold-indicators-mode)
(ts-fold--ensure-ts
(when-let*
((node (ignore-errors (tsc-root-node tree-sitter-tree)))
(patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) @name))
(alist-get major-mode ts-fold-range-alist)
'vector))
(query (ignore-errors
(tsc-make-query tree-sitter-language patterns)))
(nodes-to-fold (tsc-query-captures query node #'ignore))
(wend (window-end nil t))
(wstart (window-start))
(nodes-to-fold
(cl-remove-if-not (lambda (node)
(ts-fold-indicators--within-window (cdr node) wend wstart))
nodes-to-fold))
(mode-ranges (alist-get major-mode ts-fold-range-alist))
(nodes-to-fold
(cl-remove-if (lambda (node)
(ts-fold--non-foldable-node-p (cdr node) mode-ranges))
nodes-to-fold)))
(ts-fold-indicators--remove-ovs)
(thread-last nodes-to-fold
(mapcar #'cdr)
(mapc #'ts-fold-indicators--create))
(run-hooks 'ts-fold-indicators-refresh-hook))))))

(defun ts-fold-indicators--remove-ovs (&optional window)
"Remove all indicators overlays in this WINDOW."
Expand Down

0 comments on commit 2a0210d

Please sign in to comment.