emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to add pseudo vector types


From: Yuan Fu
Subject: Re: How to add pseudo vector types
Date: Fri, 30 Jul 2021 10:17:22 -0400

> 
> That's not the whole code, that's a patch against some previous
> version of the code.  So I cannot answer your questions with 100%
> certainty, until I see the entire code of the TS support.  For
> example, I'm not sure I have a clear idea when are the two functions
> ts_ensure_parsed and ts_record_change called.

Oops, I thought you have all prior patches. You can clone the “ts” branch from 

https://github.com/casouri/emacs.git

If this is ok, I’ll push to this branch instead of sending patches from now on.

> 
> That said, it looks like the code is correct: you should record the
> changes in the entire buffer, but only pass to TS the changes inside
> the restriction BEGV..ZV that is in effect at the time of the re-parse
> call.  Btw, I don't see the code that filters changes reported to TS
> by their positions against the restriction; did I miss something?

Yes, I do clip the change to the visible portion:

ts_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte,
                  ptrdiff_t new_end_byte)
{
  eassert(start_byte <= old_end_byte);
  eassert(start_byte <= new_end_byte);

  Lisp_Object parser_list = Fsymbol_value (Qtree_sitter_parser_list);

  while (!NILP (parser_list))
    {
      Lisp_Object lisp_parser = Fcar (parser_list);
      TSTree *tree = XTS_PARSER (lisp_parser)->tree;
      if (tree != NULL)
        {
          /* We "clip" the change to between visible_beg and
             visible_end.  It is okay if visible_end ends up larger
             than BUF_Z, tree-sitter only access buffer text during
             re-parse, and we will adjust visible_beg/end before
             re-parse.  */
          ptrdiff_t visible_beg = XTS_PARSER (lisp_parser)->visible_beg;
          ptrdiff_t visible_end = XTS_PARSER (lisp_parser)->visible_end;

          ptrdiff_t visible_start =
            max (visible_beg, start_byte) - visible_beg;
          ptrdiff_t visible_old_end =
            min (visible_end, old_end_byte) - visible_beg;
          ptrdiff_t visible_new_end =
            min (visible_end, new_end_byte) - visible_beg;

          ts_tree_edit_1 (tree, visible_start, visible_old_end,
                          visible_new_end);
          XTS_PARSER (lisp_parser)->need_reparse = true;

          parser_list = Fcdr (parser_list);
        }
    }
}

> And one more question: I understand that ts_read_buffer doesn't check
> against BUF_BEGV_BYTE because TS never reads before the "visible beg"
> position, is that right?  

Yes, we always update visible_beg and visible_end to match BUF_BEGV_BYTE and 
BUF_ZV_BYTE before we instruct tree-sitter to re-parse. So when tree-sitter 
reads at byte position 0, it translates to buffer byte position 0 + visible_beg 
= BUF_BEGV_BYTE. 

> But if so, why do we need the similar test
> for BUF_ZV_BYTE? could TS attempt to read beyond the "visible end”?

Tree-sitter doesn’t know the size of the buffer, it just keeps reading until 
the read function sets bytes_read to 0, signaling that it has reached the end. 

Yuan





reply via email to

[Prev in Thread] Current Thread [Next in Thread]