emacs-31 9d1b0c2d7f6: Fix markdown-inline incremental parsing issue (bug#81019)

Yuan Fu <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: emacs-31
commit 9d1b0c2d7f64d8367a7d700041d2c4a3605663e7
Author: Yuan Fu <[email protected]>
Commit: Yuan Fu <[email protected]>

    Fix markdown-inline incremental parsing issue (bug#81019)
    
    * lisp/treesit.el:
    (treesit--embed-languages-need-full-parse): New variable.
    (treesit--set-embed-ranges): Reparse full range if the langauge
    needs it.
---
 lisp/treesit.el | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index e2e62bb71a2..dbb39b19226 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -680,6 +680,11 @@ If none are valid, return nil."
 
 ;;; Range API supplement
 
+;; See bug#81019.
+(defvar treesit--embed-languages-need-full-parse '(markdown-inline)
+  "Languages that requires a full parse when used as embedded languages.
+This variable is not intended for general use.")
+
 (defvar treesit--range-verbose nil
   "If non-nil, print verbose debugging info for setting ranges.
 Useful when your multi-parser setup doesn't seem to work.")
@@ -1094,6 +1099,14 @@ RANGES is a list of (START . END) or just (START . END)."
     (when (and (null new-ranges) treesit--range-verbose)
       (message "Setting empty ranges to %s\nRanges for embedded parser :%s\nRanges for host parser: %s\nIntersection is empty"
                new-ranges-1 embed-parser host-parser))
+    ;; Due to some tree-sitter bug[1], we need to force a full reparse for
+    ;; some languages to get a correct parse tree.
+    ;; [1] https://github.com/tree-sitter/tree-sitter/issues/5636
+    (when (memq (treesit-parser-language embed-parser)
+                treesit--embed-languages-need-full-parse)
+      (treesit-parser-set-included-ranges
+       embed-parser `((,(point-min) . ,(point-min))))
+      (treesit-parser-root-node embed-parser))
     ;; When there's no range for the embedded language, set it's range
     ;; to a dummy (1 . 1), otherwise it would be set to the whole
     ;; buffer, which is not what we want.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.