From: Yuan Fu Date: Mon, 20 Jan 2025 06:34:11 +0000 (-0800) Subject: Make treesit-language-remap-alist completely transparent (bug#72388) X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=f8a7c982b138b892fb94e7dbf7f9ba7348a3065d;p=emacs.git Make treesit-language-remap-alist completely transparent (bug#72388) * doc/lispref/parsing.texi (Using Parser): Update manual. * src/treesit.c (Ftreesit_parser_create): Use the LANGUAGE argument given as the language for the parser, not the actual language. (cherry picked from commit 458135155675a29a2c064998afc0cb416cd38b52) --- diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index bd8b0b1f561..8f1ee614ff2 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi @@ -570,10 +570,27 @@ The value of this variable should be an alist of in the alist, creating a parser for @var{language-a} actually creates a parser for @var{language-b}. By extension, anything that creates a node or makes a query of @var{language-a} will be redirected to use -@var{language-b} instead. +@var{language-b} instead. This mapping is completely transparent, the +parser created will report as @var{language-b}, and the sames goes for +nodes created by this parser. -Note that calling @code{treesit-parser-language} on a parser for -@var{language-a} still returns @var{language-a}. +Specifically, the parser created by @code{treesit-parser-create} will +report to use whatever @var{language} was given to it. For example, +if language @code{cpp} is mapped to @code{cuda}: + +@example +@group +(setq treesit-language-remap-alist '((cpp . cuda))) + +(treesit-parser-language (treesit-parser-create 'cpp)) +;; => 'cpp + +(treesit-parser-language (treesit-parser-create 'cuda)) +;; => 'cuda +@end group +@end example + +Even though both parser are actually @code{cuda} parser. @end defvar @node Retrieving Nodes diff --git a/src/treesit.c b/src/treesit.c index 2f06de67ad8..f179e4561cf 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -1657,8 +1657,8 @@ an indirect buffer. */) treesit_check_buffer_size (buf); - language = resolve_language_symbol (language); - CHECK_SYMBOL (language); + Lisp_Object remapped_lang = resolve_language_symbol (language); + CHECK_SYMBOL (remapped_lang); /* See if we can reuse a parser. */ if (NILP (no_reuse)) @@ -1679,7 +1679,7 @@ an indirect buffer. */) Lisp_Object signal_data = Qnil; TSParser *parser = ts_parser_new (); struct treesit_loaded_lang loaded_lang - = treesit_load_language (language, &signal_symbol, &signal_data); + = treesit_load_language (remapped_lang, &signal_symbol, &signal_data); TSLanguage *lang = loaded_lang.lang; if (lang == NULL) xsignal (signal_symbol, signal_data); @@ -1687,7 +1687,10 @@ an indirect buffer. */) always succeed. */ ts_parser_set_language (parser, lang); - /* Create parser. */ + /* Create parser. Use the unmapped LANGUAGE symbol, so the nodes + created by this parser (and this parser) self identify as the + unmapped language. This makes the grammar mapping completely + transparent. */ Lisp_Object lisp_parser = make_treesit_parser (buf_orig, parser, NULL, language, tag);