]> git.eshelyaron.com Git - emacs.git/commitdiff
Address a common pitfall in tree-sitter's manual section (bug#71048)
authorYuan Fu <casouri@gmail.com>
Wed, 22 May 2024 05:40:32 +0000 (22:40 -0700)
committerEshel Yaron <me@eshelyaron.com>
Thu, 23 May 2024 08:29:51 +0000 (10:29 +0200)
* doc/lispref/parsing.texi (Multiple Languages): Add example for
treesit-language-at-point-function.

(cherry picked from commit e947e63b066680bbc7e027f73c9e68e26a47d0dd)

doc/lispref/parsing.texi

index 1bc0acfacd08eee4d6f8670c9cd4bc22cc2c6e87..6afdce377285ba32e1a10322e044d8c6d0f5b3c5 100644 (file)
@@ -1894,12 +1894,29 @@ directly translate into operations shown above.
        :host 'html
        '((script_element (raw_text) @@capture))
 @end group
-
 @group
        :embed 'css
        :host 'html
        '((style_element (raw_text) @@capture))))
 @end group
+@group
+;; Major modes with multiple languages should always set
+`treesit-language-at-point-function' (which see).
+(setq treesit-language-at-point-function
+      (lambda (pos)
+        (let* ((node (treesit-node-at pos 'html))
+               (parent (treesit-node-parent node)))
+          (cond
+           ((and node parent
+                 (equal (treesit-node-type node) "raw_text")
+                 (equal (treesit-node-type parent) "script_element"))
+            'javascript)
+           ((and node parent
+                 (equal (treesit-node-type node) "raw_text")
+                 (equal (treesit-node-type parent) "style_element"))
+            'css)
+           (t 'html)))))
+@end group
 @end example
 
 @defun treesit-range-rules &rest query-specs