unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Wilhelm H Kirschbaum <wilhelm@floatpays.co.za>
To: 60256@debbugs.gnu.org
Subject: bug#60256: 29.0.60; Maybe improve tree sitter default treesit-language-at function
Date: Thu, 22 Dec 2022 12:30:09 +0200	[thread overview]
Message-ID: <87h6xnenhl.fsf@gmail.com> (raw)


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





             reply	other threads:[~2022-12-22 10:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-22 10:30 Wilhelm H Kirschbaum [this message]
2022-12-24  7:25 ` bug#60256: 29.0.60; Maybe improve tree sitter default treesit-language-at function Eli Zaretskii
2022-12-24 22:43 ` Yuan Fu
2022-12-25  6:40   ` Eli Zaretskii
2023-09-02 16:41     ` Stefan Kangas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h6xnenhl.fsf@gmail.com \
    --to=wilhelm@floatpays.co.za \
    --cc=60256@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).