unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#60256: 29.0.60; Maybe improve tree sitter default treesit-language-at function
@ 2022-12-22 10:30 Wilhelm H Kirschbaum
  2022-12-24  7:25 ` Eli Zaretskii
  2022-12-24 22:43 ` Yuan Fu
  0 siblings, 2 replies; 5+ messages in thread
From: Wilhelm H Kirschbaum @ 2022-12-22 10:30 UTC (permalink / raw)
  To: 60256


We can detect the language from treesit-parser-included-ranges, so
instead of taking the first parser from the list what about taking 
the
first parser within known ranges?

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 6407669118..f08bb1cd40 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -136,8 +136,20 @@ treesit-language-at
 parser in `treesit-parser-list', or nil if there is no parser."
   (if treesit-language-at-point-function
       (funcall treesit-language-at-point-function position)
-    (when-let ((parser (car (treesit-parser-list))))
-      (treesit-parser-language parser))))
+    (let ((language-in-range
+         (cl-loop
+          for parser in (treesit-parser-list)
+          do (setq range
+                   (cl-loop
+                    for range in (treesit-parser-included-ranges 
parser)
+                    if (and (>= point (car range)) (<= point (cdr 
range)))
+                    return parser))
+          if range
+          return (treesit-parser-language parser))))
+    (if (null language-in-range)
+        (when-let ((parser (car (treesit-parser-list))))
+          (treesit-parser-language parser))
+      language-in-range))))


Maybe there is a better way of doing this, but seems like it can 
be
generic enough to make this work without having to set
treesit-language-at-point-function for the majority of the cases.

Wilhelm





^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-09-02 16:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-22 10:30 bug#60256: 29.0.60; Maybe improve tree sitter default treesit-language-at function Wilhelm H Kirschbaum
2022-12-24  7:25 ` Eli Zaretskii
2022-12-24 22:43 ` Yuan Fu
2022-12-25  6:40   ` Eli Zaretskii
2023-09-02 16:41     ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).