Skip to content

Commit 895cd05

Browse files
committed
hl: Make fontification function fallthrough outside of tree-sitter-hl
1 parent b78de56 commit 895cd05

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## [Unreleased]
77
- Added customization option `tsc-dyn-get-from`, which is a list of sources to get the dynamic module `tsc-dyn` from. Its default value is `(:github :compilation)`.
8+
- Made `tree-sitter-hl`'s region-fontification function fall back to the underlying non-tree-sitter function when called outside of `tree-sitter-hl-mode`. This fixes an issue where `jupyter-repl-mode`'s [input cells are not highlighted](https://github.com/nnicandro/emacs-jupyter/issues/363).
89

910
## [0.16.1] - 2021-12-11
1011
- Modified CI pipelines to publish additional pre-built dynamic modules. Their filenames include the platform they are built for. The files without platform in name will eventually be deprecated.

lisp/tree-sitter-hl.el

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,18 @@ If LOUDLY is non-nil, print debug messages."
520520
;; HL-REGION.
521521
`(jit-lock-bounds ,beg . ,end)))))
522522

523+
(defun tree-sitter-hl--highlight-region-with-fallback (old-fontify-fn beg end &optional loudly)
524+
"Highlight the region (BEG . END).
525+
526+
This is a wrapper around `tree-sitter-hl--highlight-region' that falls back to
527+
OLD-FONTIFY-FN when the current buffer doesn't have `tree-sitter-hl-mode'
528+
enabled. An example is `jupyter-repl-mode', which copies and uses other major
529+
modes' fontification functions to highlight its input cells. See
530+
https://github.com/emacs-tree-sitter/elisp-tree-sitter/issues/78#issuecomment-1005987817."
531+
(if tree-sitter-hl--query
532+
(tree-sitter-hl--highlight-region beg end loudly)
533+
(funcall old-fontify-fn beg end loudly)))
534+
523535
(defun tree-sitter-hl--invalidate (&optional old-tree)
524536
"Mark regions of text to be rehighlighted after a text change.
525537
Installed on `tree-sitter-after-change-functions'.
@@ -590,8 +602,8 @@ This assumes both `tree-sitter-mode' and `font-lock-mode' were already enabled."
590602
;; highlighting without setting `font-lock-defaults'. At the moment,
591603
;; `font-lock-mode' somehow helps with making sure that fontification is
592604
;; updated in-time, instead of eventually.
593-
(add-function :override (local 'font-lock-fontify-region-function)
594-
#'tree-sitter-hl--highlight-region)
605+
(add-function :around (local 'font-lock-fontify-region-function)
606+
#'tree-sitter-hl--highlight-region-with-fallback)
595607
(tree-sitter-hl--minimize-font-lock-keywords)
596608
;; XXX: We used to have a hack that calls`font-lock-turn-on-thing-lock',
597609
;; which allows turning on tree-based syntax highlighting by temporarily
@@ -611,7 +623,7 @@ This assumes both `tree-sitter-mode' and `font-lock-mode' were already enabled."
611623
(defun tree-sitter-hl--teardown ()
612624
"Tear down `tree-sitter-hl' in the current buffer."
613625
(remove-function (local 'font-lock-fontify-region-function)
614-
#'tree-sitter-hl--highlight-region)
626+
#'tree-sitter-hl--highlight-region-with-fallback)
615627
(remove-hook 'tree-sitter-after-change-functions
616628
#'tree-sitter-hl--invalidate
617629
:local)

0 commit comments

Comments
 (0)