diff options
| author | Yuan Fu | 2022-11-25 15:06:55 -0800 |
|---|---|---|
| committer | Yuan Fu | 2022-11-25 19:00:22 -0800 |
| commit | 245366b18a0675dc56d3236895cf0e099385d720 (patch) | |
| tree | 6cd9b4487ee0aecbc359d5afe01514128af21e3f /src | |
| parent | 73c94d5a9f00a98944516d86e4efcf50e20b2d48 (diff) | |
| download | emacs-245366b18a0675dc56d3236895cf0e099385d720.tar.gz emacs-245366b18a0675dc56d3236895cf0e099385d720.zip | |
; Add comments in treesit.c and treesit.h
* src/treesit.c
* src/treesit.h: Add (and fix) comments.
Diffstat (limited to 'src')
| -rw-r--r-- | src/treesit.c | 49 | ||||
| -rw-r--r-- | src/treesit.h | 13 |
2 files changed, 51 insertions, 11 deletions
diff --git a/src/treesit.c b/src/treesit.c index 3df53f2179d..66fd884efc3 100644 --- a/src/treesit.c +++ b/src/treesit.c | |||
| @@ -707,6 +707,8 @@ treesit_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte, | |||
| 707 | Lisp_Object lisp_parser = XCAR (parser_list); | 707 | Lisp_Object lisp_parser = XCAR (parser_list); |
| 708 | treesit_check_parser (lisp_parser); | 708 | treesit_check_parser (lisp_parser); |
| 709 | TSTree *tree = XTS_PARSER (lisp_parser)->tree; | 709 | TSTree *tree = XTS_PARSER (lisp_parser)->tree; |
| 710 | /* See comment (ref:visible-beg-null) if you wonder why we don't | ||
| 711 | update visible_beg/end when tree is NULL. */ | ||
| 710 | if (tree != NULL) | 712 | if (tree != NULL) |
| 711 | { | 713 | { |
| 712 | eassert (start_byte <= old_end_byte); | 714 | eassert (start_byte <= old_end_byte); |
| @@ -742,7 +744,7 @@ treesit_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte, | |||
| 742 | XTS_PARSER (lisp_parser)->timestamp++; | 744 | XTS_PARSER (lisp_parser)->timestamp++; |
| 743 | 745 | ||
| 744 | /* VISIBLE_BEG/END records tree-sitter's range of view in | 746 | /* VISIBLE_BEG/END records tree-sitter's range of view in |
| 745 | the buffer. Ee need to adjust them when tree-sitter's | 747 | the buffer. We need to adjust them when tree-sitter's |
| 746 | view changes. */ | 748 | view changes. */ |
| 747 | ptrdiff_t visi_beg_delta; | 749 | ptrdiff_t visi_beg_delta; |
| 748 | if (old_end_byte > new_end_byte) | 750 | if (old_end_byte > new_end_byte) |
| @@ -765,6 +767,44 @@ treesit_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte, | |||
| 765 | } | 767 | } |
| 766 | } | 768 | } |
| 767 | 769 | ||
| 770 | /* Comment (ref:visible-beg-null) So the purpose of visible_beg/end | ||
| 771 | are to keep track of "which part of the buffer does the tree-sitter | ||
| 772 | tree sees", in order to update the tree correctly. Visible_beg/end | ||
| 773 | has two purposes: "clips" buffer changes within them, and translate | ||
| 774 | position in buffer to position in the tree (buf position - visi_beg | ||
| 775 | = tree position). | ||
| 776 | |||
| 777 | Conceptually, visible_beg/end marks the visible region of the | ||
| 778 | buffer when we last reparsed. In between two reparse, we don't | ||
| 779 | really care if the visible region of the buffer changes. | ||
| 780 | |||
| 781 | Right before we reparse, we make tree-sitter's visible region | ||
| 782 | matches that of the buffer, and update visible_beg/end. | ||
| 783 | |||
| 784 | That is, the whole purpose of visible_beg/end (and | ||
| 785 | treesit_record_change and treesit_ensure_position_synced) is to | ||
| 786 | update the tree (by ts_tree_edit). So if the tree is NULL, we | ||
| 787 | don't update the tree and there is no need to keep tracking of | ||
| 788 | them. Only when we already have a tree, do we need to keep track | ||
| 789 | of position changes and update it correctly, so it can be feed into | ||
| 790 | ts_parser_parse as the old tree, so that tree-sitter only parses | ||
| 791 | the changed part (aka incremental). | ||
| 792 | |||
| 793 | In a nutshell, tree-sitter incremental parsing in Emacs looks like: | ||
| 794 | |||
| 795 | treesit_record_change(tree) \ | ||
| 796 | treesit_record_change(tree) | user edits buffer | ||
| 797 | ... / | ||
| 798 | |||
| 799 | treesit_ensure_position_synced(tree) \ treesit_ensure_parsed | ||
| 800 | ts_parser_parse(tree) -> tree / | ||
| 801 | |||
| 802 | treesit_record_change(tree) \ | ||
| 803 | treesit_record_change(tree) | user edits buffer | ||
| 804 | ... / | ||
| 805 | |||
| 806 | and so on. */ | ||
| 807 | |||
| 768 | /* Make sure PARSER's visible_beg and visible_end are in sync with | 808 | /* Make sure PARSER's visible_beg and visible_end are in sync with |
| 769 | BUF_BEGV_BYTE and BUG_ZV_BYTE. When calling this function you must | 809 | BUF_BEGV_BYTE and BUG_ZV_BYTE. When calling this function you must |
| 770 | make sure the current buffer's size is not larger than UINT32_MAX. | 810 | make sure the current buffer's size is not larger than UINT32_MAX. |
| @@ -1365,9 +1405,10 @@ treesit_check_range_argument (Lisp_Object ranges) | |||
| 1365 | CHECK_LIST_END (tail, ranges); | 1405 | CHECK_LIST_END (tail, ranges); |
| 1366 | } | 1406 | } |
| 1367 | 1407 | ||
| 1368 | /* Generate a list of ranges in Lisp from RANGES. This function | 1408 | /* Generate a list of ranges in Lisp from RANGES. Assumes tree-sitter |
| 1369 | doesn't take ownership of RANGES. BUFFER is used to convert | 1409 | tree and the buffer has the same visible region (w.r.t narrowing). |
| 1370 | between tree-sitter buffer offset and buffer position. */ | 1410 | This function doesn't take ownership of RANGES. BUFFER is used to |
| 1411 | convert between tree-sitter buffer offset and buffer position. */ | ||
| 1371 | static Lisp_Object | 1412 | static Lisp_Object |
| 1372 | treesit_make_ranges (const TSRange *ranges, uint32_t len, | 1413 | treesit_make_ranges (const TSRange *ranges, uint32_t len, |
| 1373 | struct buffer *buffer) | 1414 | struct buffer *buffer) |
diff --git a/src/treesit.h b/src/treesit.h index 1473126c5bc..6f6423ff472 100644 --- a/src/treesit.h +++ b/src/treesit.h | |||
| @@ -56,13 +56,12 @@ struct Lisp_TS_Parser | |||
| 56 | this field to true to force tree-sitter to re-parse. */ | 56 | this field to true to force tree-sitter to re-parse. */ |
| 57 | bool need_reparse; | 57 | bool need_reparse; |
| 58 | /* These two positions record the buffer byte position (1-based) of | 58 | /* These two positions record the buffer byte position (1-based) of |
| 59 | the "visible region" that tree-sitter sees. Unlike markers, | 59 | the "visible region" that tree-sitter sees. Before re-parse, we |
| 60 | These two positions do not change as the user inserts and deletes | 60 | move these positions to match BUF_BEGV_BYTE and BUF_ZV_BYTE. |
| 61 | text around them. Before re-parse, we move these positions to | 61 | Note that we don't need to synchronize these positions when |
| 62 | match BUF_BEGV_BYTE and BUF_ZV_BYTE. Note that we don't need to | 62 | retrieving them in a function that involves a node: if the node |
| 63 | synchronize these positions when retrieving them in a function | 63 | is not outdated, these positions are synchronized. See comment |
| 64 | that involves a node: if the node is not outdated, these | 64 | (ref:visible-beg-null) in treesit.c for more explanation. */ |
| 65 | positions are synchronized. */ | ||
| 66 | ptrdiff_t visible_beg; | 65 | ptrdiff_t visible_beg; |
| 67 | ptrdiff_t visible_end; | 66 | ptrdiff_t visible_end; |
| 68 | /* This counter is incremented every time a change is made to the | 67 | /* This counter is incremented every time a change is made to the |