unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 64830@debbugs.gnu.org, Alan Mackenzie <acm@muc.de>,
	Stefan Kangas <stefankangas@gmail.com>
Subject: bug#64830: 30.0.50 C++ treesitter mode no coloration
Date: Tue, 27 Aug 2024 22:36:46 -0700	[thread overview]
Message-ID: <29FFAE70-8B01-47CF-A45A-D01C82C56247@gmail.com> (raw)
In-Reply-To: <86wmk2i2vp.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 656 bytes --]



> On Aug 27, 2024, at 5:09 AM, Eli Zaretskii <eliz@gnu.org> wrote:
> 
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Mon, 26 Aug 2024 18:58:13 -0700
>> Cc: Alan Mackenzie <acm@muc.de>,
>> Eli Zaretskii <eliz@gnu.org>,
>> 64830@debbugs.gnu.org
>> 
>> I also want a lisp function that can return the location of a loaded grammar file, would that be an overkill if we have the aforementioned printing?
> 
> Where/when would such a function be used?

For debugging. Eg, to verify that Emacs loaded the grammar file I think it loaded. Something like

(treesit-grammar-location 'c) ; => “/opt/local/lib/libtree-sitter-c.dylib”.

Yuan


[-- Attachment #2: show-location.patch --]
[-- Type: application/octet-stream, Size: 6726 bytes --]

From c64dc3e287e3db009589fb27d07d658baca9fe66 Mon Sep 17 00:00:00 2001
From: Yuan Fu <casouri@gmail.com>
Date: Tue, 27 Aug 2024 22:31:42 -0700
Subject: [PATCH] Add Ftreesit_grammar_location

* src/treesit.c (treesit_loaded_lang): New struct.
(treesit_load_language): Return a struct instead of just the language
object.  The struct contains both the language object and the path to
the shared library.
(Ftreesit_language_available_p, Ftreesit_language_abi_version)
(treesit_ensure_query_compiled, Ftreesit_parser_create): Update
call of treesit_load_language.
(Ftreesit_grammar_location): New function.
---
 src/treesit.c | 68 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 55 insertions(+), 13 deletions(-)

diff --git a/src/treesit.c b/src/treesit.c
index 5aedca44489..56cf8a1075a 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -19,6 +19,7 @@ Copyright (C) 2021-2024 Free Software Foundation, Inc.
 You should have received a copy of the GNU General Public License
 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
 
+#include <dlfcn.h>
 #include <config.h>
 #include "lisp.h"
 #include "buffer.h"
@@ -491,6 +492,14 @@ treesit_initialize (void)
 \f
 /*** Loading language library  */
 
+struct treesit_loaded_lang
+{
+  /* The language object.  */
+  TSLanguage *lang;
+  /* The path of the shared library.  */
+  const char *path;
+};
+
 /* Translate a symbol treesit-<lang> to a C name treesit_<lang>.  */
 static void
 treesit_symbol_to_c_name (char *symbol_name)
@@ -575,7 +584,7 @@ treesit_load_language_push_for_each_suffix (Lisp_Object lib_base_name,
 
    If error occurs, return NULL and fill SIGNAL_SYMBOL and SIGNAL_DATA
    with values suitable for xsignal.  */
-static TSLanguage *
+static struct treesit_loaded_lang
 treesit_load_language (Lisp_Object language_symbol,
 		       Lisp_Object *signal_symbol, Lisp_Object *signal_data)
 {
@@ -626,6 +635,7 @@ treesit_load_language (Lisp_Object language_symbol,
   dynlib_handle_ptr handle;
   const char *error;
   Lisp_Object error_list = Qnil;
+  struct treesit_loaded_lang loaded_lang = { NULL, NULL };
 
   tail = path_candidates;
   error = NULL;
@@ -650,7 +660,7 @@ treesit_load_language (Lisp_Object language_symbol,
          mismatch.  */
       *signal_symbol = Qtreesit_load_language_error;
       *signal_data = Fcons (Qnot_found, Fnreverse (error_list));
-      return NULL;
+      return loaded_lang;
     }
 
   /* Load TSLanguage.  */
@@ -672,7 +682,7 @@ treesit_load_language (Lisp_Object language_symbol,
     {
       *signal_symbol = Qtreesit_load_language_error;
       *signal_data = list2 (Qsymbol_error, build_string (error));
-      return NULL;
+      return loaded_lang;
     }
   TSLanguage *lang = (*langfn) ();
 
@@ -685,9 +695,15 @@ treesit_load_language (Lisp_Object language_symbol,
       *signal_symbol = Qtreesit_load_language_error;
       *signal_data = list2 (Qversion_mismatch,
 			    make_fixnum (ts_language_version (lang)));
-      return NULL;
+      return loaded_lang;
     }
-  return lang;
+
+  Dl_info info;
+  if (dladdr(langfn, &info))
+    loaded_lang.path = info.dli_fname;
+
+  loaded_lang.lang = lang;
+  return loaded_lang;
 }
 
 DEFUN ("treesit-language-available-p", Ftreesit_language_available_p,
@@ -704,7 +720,9 @@ DEFUN ("treesit-language-available-p", Ftreesit_language_available_p,
   treesit_initialize ();
   Lisp_Object signal_symbol = Qnil;
   Lisp_Object signal_data = Qnil;
-  if (treesit_load_language (language, &signal_symbol, &signal_data) == NULL)
+  struct treesit_loaded_lang loaded_lang
+    = treesit_load_language (language, &signal_symbol, &signal_data);
+  if (loaded_lang.lang == NULL)
     {
       if (NILP (detail))
 	return Qnil;
@@ -750,9 +768,9 @@ DEFUN ("treesit-language-abi-version", Ftreesit_language_abi_version,
     {
       Lisp_Object signal_symbol = Qnil;
       Lisp_Object signal_data = Qnil;
-      TSLanguage *ts_language = treesit_load_language (language,
-						       &signal_symbol,
-						       &signal_data);
+      struct treesit_loaded_lang lang
+	= treesit_load_language (language, &signal_symbol, &signal_data);
+      TSLanguage *ts_language = lang.lang;
       if (ts_language == NULL)
 	return Qnil;
       uint32_t version =  ts_language_version (ts_language);
@@ -760,6 +778,27 @@ DEFUN ("treesit-language-abi-version", Ftreesit_language_abi_version,
     }
 }
 
+DEFUN ("treesit-grammar-location", Ftreesit_grammar_location,
+       Streesit_grammar_location,
+       1, 1, 0,
+       doc: /* Return the path to the grammar file for LANGUAGE.
+
+If LANGUAGE isn't loaded yet, load it first.  If the langauge can't be
+loaded or the path couldn't be found, return nil.  */)
+  (Lisp_Object language)
+{
+  CHECK_SYMBOL (language);
+
+  Lisp_Object signal_symbol = Qnil;
+  Lisp_Object signal_data = Qnil;
+  struct treesit_loaded_lang lang
+    = treesit_load_language (language, &signal_symbol, &signal_data);
+
+  if (!lang.lang || !lang.path) return Qnil;
+
+  return build_string (lang.path);
+}
+
 \f
 /*** Parsing functions  */
 
@@ -1305,8 +1344,9 @@ treesit_ensure_query_compiled (Lisp_Object query, Lisp_Object *signal_symbol,
   Lisp_Object language = XTS_COMPILED_QUERY (query)->language;
   /* This is the main reason why we compile query lazily: to avoid
      loading languages early.  */
-  TSLanguage *treesit_lang = treesit_load_language (language, signal_symbol,
-						    signal_data);
+  struct treesit_loaded_lang lang
+    = treesit_load_language (language, signal_symbol, signal_data);
+  TSLanguage *treesit_lang = lang.lang;
   if (treesit_lang == NULL)
     return NULL;
 
@@ -1477,8 +1517,9 @@ DEFUN ("treesit-parser-create",
   Lisp_Object signal_symbol = Qnil;
   Lisp_Object signal_data = Qnil;
   TSParser *parser = ts_parser_new ();
-  TSLanguage *lang = treesit_load_language (language, &signal_symbol,
-					    &signal_data);
+  struct treesit_loaded_lang loaded_lang
+    = treesit_load_language (language, &signal_symbol, &signal_data);
+  TSLanguage *lang = loaded_lang.lang;
   if (lang == NULL)
     xsignal (signal_symbol, signal_data);
   /* We check language version when loading a language, so this should
@@ -4275,6 +4316,7 @@ cons (REGEXP . FN), which is a combination of a regexp and a predicate
   defsubr (&Streesit_language_available_p);
   defsubr (&Streesit_library_abi_version);
   defsubr (&Streesit_language_abi_version);
+  defsubr (&Streesit_grammar_location);
 
   defsubr (&Streesit_parser_p);
   defsubr (&Streesit_node_p);
-- 
2.39.5 (Apple Git-151)


  reply	other threads:[~2024-08-28  5:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24  9:13 bug#64830: 29.1; C++ treesitter mode no coloration David Come
2023-07-24 12:32 ` Eli Zaretskii
2024-08-14 17:35 ` bug#64830: 30.0.50 " Alan Mackenzie
2024-08-15  5:25   ` Eli Zaretskii
2024-08-16 16:44     ` Alan Mackenzie
2024-08-16 17:40       ` Eli Zaretskii
2024-08-16 17:45         ` Eli Zaretskii
2024-08-16 18:06           ` Alan Mackenzie
2024-08-16 18:27             ` Eli Zaretskii
2024-08-20  3:46               ` Yuan Fu
2024-08-24 18:35                 ` Yuan Fu
2024-08-24 19:38                   ` Alan Mackenzie
2024-08-24 20:43                     ` Yuan Fu
2024-08-25  2:19                       ` Alan Mackenzie
2024-08-25  4:54                         ` Eli Zaretskii
2024-08-25 12:08                           ` Alan Mackenzie
2024-08-25 12:17                             ` Eli Zaretskii
2024-08-25 22:40                         ` Yuan Fu
2024-08-26 17:25                           ` Alan Mackenzie
2024-08-26 17:51                             ` Eli Zaretskii
2024-08-26 19:50                               ` Alan Mackenzie
2024-08-26 22:25                                 ` Stefan Kangas
2024-08-27  1:58                                   ` Yuan Fu
2024-08-27 12:09                                     ` Eli Zaretskii
2024-08-28  5:36                                       ` Yuan Fu [this message]
2024-08-28 12:33                                         ` Eli Zaretskii
2024-08-29  4:54                                           ` Yuan Fu
2024-08-29  6:01                                             ` Eli Zaretskii
2024-09-11  5:09                                               ` Yuan Fu
2024-09-11 12:09                                                 ` Eli Zaretskii
2024-09-12  8:06                                                   ` Yuan Fu
2024-08-27 12:01                                 ` Eli Zaretskii
2024-08-27 11:03                             ` Eli Zaretskii

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=29FFAE70-8B01-47CF-A45A-D01C82C56247@gmail.com \
    --to=casouri@gmail.com \
    --cc=64830@debbugs.gnu.org \
    --cc=acm@muc.de \
    --cc=eliz@gnu.org \
    --cc=stefankangas@gmail.com \
    /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).