"Separate" tree-sitter parser list for indirect buffers

When create a parser for the indirect buffer, set the buffer field of
the parser to the indirect buffer, but add the parser to the base
buffer's parser list.  This way, all the parsers still get buffer
updates, but indirect buffer's parsers can have different narrowing than
the parsers of the base buffer.

When returning the parser list of a buffer, do filtering and only return
the parser for that buffer.

From user's POV, indirect buffers appear to have their own parser list.

* doc/lispref/parsing.texi (Using Parser): Remove the text describing
indirect buffer's special case.
* src/treesit.c (Ftreesit_parser_create): When create a parser for the
indirect buffer, set the buffer field of the parser to the indirect
buffer, but add the parser to the base buffer's parser list.
(Ftreesit_parser_list): Filter parser list, only return parsers for this
buffer.

xx
This commit is contained in:
Yuan Fu 2024-07-26 22:33:17 -07:00
parent a2c439db68
commit e4cd26defc
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
3 changed files with 51 additions and 24 deletions

View file

@ -413,13 +413,6 @@ is non-@code{nil}, this function always creates a new parser.
@var{tag} can be any symbol except @code{t}, and defaults to
@code{nil}. Different parsers can have the same tag.
If that buffer is an indirect buffer, its base buffer is used instead.
That is, indirect buffers use their base buffer's parsers. If the
base buffer is narrowed, an indirect buffer might not be able to
retrieve information of the portion of the buffer text that is
invisible in the base buffer. Lisp programs should widen as necessary
should they want to use a parser in an indirect buffer.
@end defun
Given a parser, we can query information about it.
@ -458,9 +451,7 @@ tree incrementally.
@defun treesit-parser-list &optional buffer language tag
This function returns the parser list of @var{buffer}, filtered by
@var{language} and @var{tag}. If @var{buffer} is @code{nil} or
omitted, it defaults to the current buffer. If that buffer is an
indirect buffer, its base buffer is used instead. That is, indirect
buffers use their base buffer's parsers.
omitted, it defaults to the current buffer.
If @var{language} is non-@var{nil}, only include parsers for that
language, and only include parsers with @var{tag}. @var{tag} defaults

View file

@ -194,6 +194,23 @@ authorize the invoked D-Bus method (for example via polkit).
** The customization group 'wp' has been removed.
It has been obsolete since Emacs 26.1. Use the group 'text' instead.
** Tree-sitter changes
+++
*** Indirect buffers can have their own parser list.
Before, indirect buffers share their base buffers parser list and
parsers. Now they can have their own parser list.
+++
*** New variable 'treesit-language-remap-alist'.
This variable allows a user to remap one language into another, such
that creating a parser for language A actually creates a parser for
language B. By extension, any font-lock rules or indentation rules for
language A will be applied to language B instead.
This is useful for reusing font-lock rules and indentation rules of
language A for language B, when language B is a strict superset of
language A.
* Changes in Emacs 31.1 on Non-Free Operating Systems

View file

@ -392,16 +392,20 @@ init_treesit_functions (void)
These are all imaginary scenarios but they are not impossible
:-)
Parsers in indirect buffers: We make indirect buffers to share the
parser of its base buffer. Indirect buffers and their base buffer
Parsers in indirect buffers: We make indirect buffers share the
parser of their base buffer. Indirect buffers and their base buffer
share the same buffer content but not other buffer attributes. If
they have separate parser lists, changes made in an indirect buffer
will only update parsers of that indirect buffer, and not parsers
in the base buffer or other indirect buffers, and vice versa. We
could keep track of all the base and indirect buffers, and update
all of their parsers, but ultimately decide to take a simpler
approach, which is to make indirect buffers share their base
buffer's parser list. The discussion can be found in bug#59693. */
will only update parsers of that indirect buffer, and not parsers in
the base buffer or other indirect buffers, and vice versa. For that
reason, the base buffer and all ot its indirect buffers share a
single parser list. But each parser in this shared parser list still
points to their own buffer. On top of that, treesit-parser-list only
return parsers that belongs to the calling buffer. So ultimately,
from the user's POV, each buffer, regardless of indirect or not,
appears to have their own parser list. A discussion can be found in
bug#59693. Note that that discussion led to an earlier design, which
is different from the current one. */
/*** Initialization */
@ -1416,13 +1420,20 @@ an indirect buffer. */)
CHECK_SYMBOL (language);
CHECK_SYMBOL (tag);
struct buffer *buf;
Lisp_Object buf_orig;
if (NILP (buffer))
buf = current_buffer;
{
buf = current_buffer;
XSETBUFFER (buf_orig, current_buffer);
}
else
{
CHECK_BUFFER (buffer);
buf = XBUFFER (buffer);
buf_orig = buffer;
}
if (buf->base_buffer)
buf = buf->base_buffer;
@ -1457,9 +1468,7 @@ an indirect buffer. */)
ts_parser_set_language (parser, lang);
/* Create parser. */
Lisp_Object lisp_buf;
XSETBUFFER (lisp_buf, buf);
Lisp_Object lisp_parser = make_treesit_parser (lisp_buf,
Lisp_Object lisp_parser = make_treesit_parser (buf_orig,
parser, NULL,
language, tag);
@ -1505,13 +1514,20 @@ tag. */)
(Lisp_Object buffer, Lisp_Object language, Lisp_Object tag)
{
struct buffer *buf;
Lisp_Object buf_orig;
if (NILP (buffer))
buf = current_buffer;
{
buf = current_buffer;
XSETBUFFER (buf_orig, current_buffer);
}
else
{
CHECK_BUFFER (buffer);
buf = XBUFFER (buffer);
buf_orig = buffer;
}
if (buf->base_buffer)
buf = buf->base_buffer;
@ -1526,7 +1542,10 @@ tag. */)
{
struct Lisp_TS_Parser *parser = XTS_PARSER (XCAR (tail));
if ((NILP (language) || EQ (language, parser->language_symbol))
&& (EQ (tag, Qt) || EQ (tag, parser->tag)))
&& (EQ (tag, Qt) || EQ (tag, parser->tag))
/* Indirect buffers and base buffer shares the same parser
* list, so we need the filtering here. */
&& (EQ (parser->buffer, buf_orig)))
return_list = Fcons (XCAR (tail), return_list);
}