unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: Wilhelm Kirschbaum <wkirschbaum@gmail.com>,
	62333@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>
Subject: bug#62333: 30.0.50; Issue with tree-sitter syntax tree during certain changes
Date: Wed, 22 Mar 2023 20:03:20 -0700	[thread overview]
Message-ID: <09539C5E-23DA-4B00-A3F6-873A41D6A2CE@gmail.com> (raw)
In-Reply-To: <2fd8f2b8-d9c4-c825-a789-f2d42324859f@yandex.ru>

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



> On Mar 22, 2023, at 6:03 PM, Dmitry Gutov <dgutov@yandex.ru> wrote:
> 
> On 21/03/2023 16:00, Wilhelm Kirschbaum wrote:
>> With the following text in a buffer with elixir-ts-mode:
>> def foo() do
>> end
>> When I delete the `)` and re-insert it, the tree-sitter syntax tree does
>> not fully recover and the `do end` part is absent when observing using
>> treesit-explore-mode.
>> Before removing the `)`:
>> (source
>> (call target: (identifier)
>>  (arguments
>>   (call target: (identifier)
>>    (arguments ( ))))
>>  (do_block do end)))
>> after re-inserting `)`:
>> (source
>> (call target: (identifier)
>>  (arguments
>>   (call target: (identifier)
>>    (arguments ( ))))))
>> I can see the same behaviour using ruby-ts-mode when I remove the
>> following expression:
>> (setq-local syntax-propertize-function #'ruby-ts--syntax-propertize)
>> The ruby syntax I tested with:
>> def foo()
>> end
>> When I add a local `syntax-propertize-function' function to elixir-ts-mode
>> it also solves the problem of not fully recovering the tree-sitter
>> syntax tree.  It does not seem to matter what the function achieves
>> thought, so am a bit confused.
> 
> I can repro both scenarios. Except with ruby-ts-mode, the difference in printed AST is the added wrapping in (program ...). But the visible result is similar: "end" not being highlighted.

The problem seems to be caused by narrowing during fontification: when parsing, tree-sitter sees that BUF_ZV_BYTE is smaller than it’s visible_end, and removed the part after the parenthesis from the parse tree. I suspect this has to do with the narrowed fontification we added for dealing with long lines. 

CC’ed Eli because I don’t really know anything about the locked narrow thing besides that it exists. In particular, how does it determine when and where to narrow?


To see it in action, apply this patch, and repeat Wilhelm’s experiment: remove the “)” and reinsert it. You should see

start: 9, oend: 10, nend: 9
start: 9, oend: 10, nend: 9
start: 9, oend: 9, nend: 10
start: 9, oend: 9, nend: 10
2.5 start: 10, oend: 18, nend: 10 —> culprit

(Make sure to turn off electric-pair-mode to reduce noise.)

Yuan


[-- Attachment #2: print.diff --]
[-- Type: application/octet-stream, Size: 2389 bytes --]

diff --git a/src/treesit.c b/src/treesit.c
index 5a4fe3e8803..9b46dcef2d1 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -806,6 +806,7 @@ treesit_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte,
 	  eassert (start_offset <= old_end_offset);
 	  eassert (start_offset <= new_end_offset);
 
+	  printf("start: %ld, oend: %ld, nend: %ld\n", start_offset, old_end_offset, new_end_offset);
 	  treesit_tree_edit_1 (tree, start_offset, old_end_offset,
 			       new_end_offset);
 	  XTS_PARSER (lisp_parser)->need_reparse = true;
@@ -925,6 +926,7 @@ treesit_sync_visible_region (Lisp_Object parser)
   if (visible_beg > BUF_BEGV_BYTE (buffer))
     {
       /* Tree-sitter sees: insert at the beginning.  */
+      printf("1 start: %d, oend: %d, nend: %ld\n", 0, 0, visible_beg - BUF_BEGV_BYTE (buffer));
       treesit_tree_edit_1 (tree, 0, 0, visible_beg - BUF_BEGV_BYTE (buffer));
       visible_beg = BUF_BEGV_BYTE (buffer);
       eassert (visible_beg <= visible_end);
@@ -933,6 +935,9 @@ treesit_sync_visible_region (Lisp_Object parser)
   if (visible_end < BUF_ZV_BYTE (buffer))
     {
       /* Tree-sitter sees: insert at the end.  */
+      printf("2 start: %ld, oend: %ld, nend: %ld\n", visible_end - visible_beg,
+	     visible_end - visible_beg,
+	     BUF_ZV_BYTE (buffer) - visible_beg);
       treesit_tree_edit_1 (tree, visible_end - visible_beg,
 			   visible_end - visible_beg,
 			   BUF_ZV_BYTE (buffer) - visible_beg);
@@ -942,6 +947,9 @@ treesit_sync_visible_region (Lisp_Object parser)
   else if (visible_end > BUF_ZV_BYTE (buffer))
     {
       /* Tree-sitter sees: delete at the end.  */
+      printf("2.5 start: %ld, oend: %ld, nend: %ld\n", BUF_ZV_BYTE (buffer) - visible_beg,
+			   visible_end - visible_beg,
+			   BUF_ZV_BYTE (buffer) - visible_beg);
       treesit_tree_edit_1 (tree, BUF_ZV_BYTE (buffer) - visible_beg,
 			   visible_end - visible_beg,
 			   BUF_ZV_BYTE (buffer) - visible_beg);
@@ -952,6 +960,7 @@ treesit_sync_visible_region (Lisp_Object parser)
   if (visible_beg < BUF_BEGV_BYTE (buffer))
     {
       /* Tree-sitter sees: delete at the beginning.  */
+      printf("3 start: %d, oend: %ld, nend: %d\n", 0, BUF_BEGV_BYTE (buffer) - visible_beg, 0);
       treesit_tree_edit_1 (tree, 0, BUF_BEGV_BYTE (buffer) - visible_beg, 0);
       visible_beg = BUF_BEGV_BYTE (buffer);
       eassert (visible_beg <= visible_end);

  reply	other threads:[~2023-03-23  3:03 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-21 14:00 bug#62333: 30.0.50; Issue with tree-sitter syntax tree during certain changes Wilhelm Kirschbaum
2023-03-23  1:03 ` Dmitry Gutov
2023-03-23  3:03   ` Yuan Fu [this message]
2023-03-23  4:20     ` Dmitry Gutov
2023-03-23  7:13       ` Eli Zaretskii
2023-03-23 21:18         ` Yuan Fu
2023-03-23 21:30           ` Dmitry Gutov
2023-03-23 22:06           ` Dmitry Gutov
2023-03-23 23:59             ` Yuan Fu
2023-03-24  6:05               ` Eli Zaretskii
2023-03-24  7:34                 ` Yuan Fu
2023-03-25  1:51                   ` Dmitry Gutov
2023-03-25 12:34                     ` Eli Zaretskii
2023-03-25 13:00                       ` Dmitry Gutov
2023-03-25 13:14                         ` Eli Zaretskii
2023-03-25 13:44                           ` Dmitry Gutov
2023-03-25 14:09                             ` Eli Zaretskii
2023-03-25 14:18                               ` Dmitry Gutov
2023-03-25 14:41                                 ` Eli Zaretskii
2023-03-25 15:25                                   ` Dmitry Gutov
2023-03-25 15:57                                     ` Eli Zaretskii
2023-03-25 16:03                                       ` Dmitry Gutov
2023-03-25 16:24                                         ` Eli Zaretskii
2023-03-25 17:05                                           ` Dmitry Gutov
2023-03-25 17:40                                             ` Eli Zaretskii
2023-03-25 19:31                                               ` Yuan Fu
2023-03-25 23:29                                                 ` Dmitry Gutov
2023-03-26 22:52                                                   ` Yuan Fu
2023-03-27  1:29                                                     ` Dmitry Gutov
2023-03-26  4:28                                                 ` Eli Zaretskii
2023-03-26 22:57                                                   ` Yuan Fu
2023-03-27 13:32                                                     ` Eli Zaretskii
2023-03-27 18:43                                                       ` Yuan Fu
2023-03-25 22:57                                               ` Dmitry Gutov
2023-03-26  5:04                                                 ` Eli Zaretskii
2023-03-26  9:25                                                   ` Dmitry Gutov
2023-03-26 10:01                                                     ` Eli Zaretskii
2023-03-26 22:00                                                       ` Dmitry Gutov
2023-03-27  8:24                                                         ` Gregory Heytings
2023-03-27 13:39                                                           ` Eli Zaretskii
2023-03-27 20:05                                                             ` Gregory Heytings
2023-03-28 11:30                                                               ` Eli Zaretskii
2023-03-28 11:39                                                                 ` Gregory Heytings
2023-03-28 12:11                                                                   ` Eli Zaretskii
2023-03-28 12:25                                                                     ` Gregory Heytings
2023-03-28 12:36                                                                       ` Eli Zaretskii
2023-03-27 23:06                                                             ` Dmitry Gutov
2023-03-28 11:32                                                               ` Eli Zaretskii
2023-03-28 21:08                                                                 ` Dmitry Gutov
2023-03-29 11:08                                                                   ` Eli Zaretskii
2023-03-31  1:10                                                                     ` Dmitry Gutov
2023-03-31  1:27                                                                       ` Dmitry Gutov
2023-03-31  6:19                                                                       ` Eli Zaretskii
2023-03-31  7:46                                                                         ` Eli Zaretskii
2023-03-31 12:38                                                                           ` Dmitry Gutov
2023-03-31 13:03                                                                             ` Eli Zaretskii
2023-03-31 13:26                                                                               ` Dmitry Gutov
2023-03-31 12:46                                                                         ` Dmitry Gutov
2023-03-31 13:06                                                                           ` Eli Zaretskii
2023-03-31 18:43                                                                           ` Yuan Fu
2023-04-01  1:53                                                                             ` Dmitry Gutov
2023-04-01  5:47                                                                             ` Eli Zaretskii
2023-04-01 16:12                                                                               ` Dmitry Gutov
2023-04-02 22:08                                                                               ` Yuan Fu
2023-04-03 12:31                                                                                 ` Eli Zaretskii
2023-03-27 23:20                                                           ` Dmitry Gutov
2023-03-27 13:29                                                         ` Eli Zaretskii
2023-03-27 23:33                                                           ` Dmitry Gutov
2023-03-28 11:38                                                             ` Eli Zaretskii
2023-03-28 21:19                                                               ` Dmitry Gutov
2023-03-29 11:17                                                                 ` Eli Zaretskii
2023-03-30 15:50                                                                   ` Gregory Heytings
2023-03-30 16:04                                                                     ` Eli Zaretskii
2023-03-30 16:28                                                                       ` Gregory Heytings
2023-03-30 16:40                                                                         ` Eli Zaretskii
2023-03-30 17:27                                                                           ` Gregory Heytings
2023-03-30 17:47                                                                             ` Eli Zaretskii
2023-03-30 20:14                                                                               ` Gregory Heytings
2023-03-30 22:08                                                                                 ` Gregory Heytings
2023-03-31  6:15                                                                                   ` Eli Zaretskii
2023-03-31  1:25                                                                       ` Dmitry Gutov
2023-03-31  6:22                                                                         ` Eli Zaretskii
2023-03-31  1:17                                                                   ` Dmitry Gutov

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=09539C5E-23DA-4B00-A3F6-873A41D6A2CE@gmail.com \
    --to=casouri@gmail.com \
    --cc=62333@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    --cc=eliz@gnu.org \
    --cc=wkirschbaum@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).