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)
committerYuan Fu <casouri@gmail.com>
Wed, 22 May 2024 05:47:20 +0000 (22:47 -0700)
* doc/lispref/parsing.texi (Multiple Languages): Add example for
treesit-language-at-point-function.

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