Skip to content

Commit f79ec29

Browse files
committed
Prioritize patterns that appear earlier in the highlighting query
1 parent 3c82a48 commit f79ec29

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lisp/tree-sitter-hl.el

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,22 @@ It also expects VALUE to be a single value, not a list."
245245
(ts--point-from-position beg)
246246
(ts--point-from-position end))
247247
(let* ((root-node (ts-root-node tree-sitter-tree))
248-
;; TODO: Use `ts-query-matches', for pattern priority.
249-
(captures (ts-query-captures
248+
(matches (ts-query-matches
250249
tree-sitter-hl--query
251250
root-node
252251
tree-sitter-hl--query-cursor
253252
nil
254253
#'ts--node-text)))
254+
(sort matches (lambda (m1 m2)
255+
(< (car m1) (car m2))))
255256
;; TODO: Handle quitting.
256-
(with-silent-modifications
257-
(font-lock-unfontify-region beg end)
258-
;; TODO: Handle uncaptured nodes.
259-
(seq-do #'tree-sitter-hl--highlight-capture captures))
257+
(let ((inhibit-point-motion-hooks t))
258+
(with-silent-modifications
259+
(font-lock-unfontify-region beg end)
260+
;; TODO: Handle uncaptured nodes.
261+
(seq-doseq (match matches)
262+
(pcase-let ((`(_ . ,captures) match))
263+
(seq-do #'tree-sitter-hl--highlight-capture captures)))))
260264
;; TODO: Return the actual region being fontified.
261265
`(jit-lock-bounds ,beg . ,end))))
262266

0 commit comments

Comments
 (0)