emacs/test/src
Yuan Fu 1897da0b59
Add line-column tracking for tree-sitter
Add line-column tracking for tree-sitter parsers.  Copied from
comments in treesit.c:

   Technically we had to send tree-sitter the line and column
   position of each edit.  But in practice we just send it dummy
   values, because tree-sitter doesn't use it for parsing and
   mostly just carries the line and column positions around and
   return it when e.g. reporting node positions[1].  This has
   been working fine until we encountered grammars that actually
   utilizes the line and column information for
   parsing (Haskell)[2].

   [1] https://github.com/tree-sitter/tree-sitter/issues/445
   [2] https://github.com/tree-sitter/tree-sitter/issues/4001

   So now we have to keep track of line and column positions and
   pass valid values to tree-sitter.  (It adds quite some
   complexity, but only linearly; one can ignore all the linecol
   stuff when trying to understand treesit code and then come
   back to it later.)  Eli convinced me to disable tracking by
   default, and only enable it for languages that needs it.  So
   the buffer starts out not tracking linecol.  And when a
   parser is created, if the language is in
   treesit-languages-require-line-column-tracking, we enable
   tracking in the buffer, and enable tracking for the parser.
   To simplify things, once a buffer starts tracking linecol, it
   never disables tracking, even if parsers that need tracking
   are all deleted; and for parsers, tracking is determined at
   creation time, if it starts out tracking/non-tracking, it
   stays that way, regardless of later changes to
   treesit-languages-require-line-column-tracking.

   To make calculating line/column positons fast, we store
   linecol caches for begv, point, and zv in the
   buffer (buf->ts_linecol_cache_xxx); and in the parser object,
   we store linecol cache for visible beg/end of that parser.

   In buffer editing functions, we need the linecol for
   start/old_end/new_end, those can be calculated by scanning
   newlines (treesit_linecol_of_pos) from the buffer point
   cache, which should be always near the point.  And we usually
   set the calculated linecol of new_end back to the buffer
   point cache.

   We also need to calculate linecol for the visible_beg/end for
   each parser, and linecol for the buffer's begv/zv, these
   positions are usually far from point, so we have caches for
   all of them (in either the parser object or the buffer).
   These positions are far from point, so it's inefficient to
   scan newlines from point to there to get up-to-date linecol
   for them; but in the same time, because they're far and
   outside the changed region, we can calculate their change in
   line and column number by simply counting how much newlines
   are added/removed in the changed
   region (compute_new_linecol_by_change).

* doc/lispref/parsing.texi (Using Parser): Mention line-column
tracking in manual.
* etc/NEWS: Add news.
* lisp/treesit.el:
(treesit-languages-need-line-column-tracking): New variable.
* src/buffer.c: Include treesit.h (for TREESIT_EMPTY_LINECOL).
(Fget_buffer_create):
(Fmake_indirect_buffer): Initialize new buffer fields.
(Fbuffer_swap_text): Add new buffer fields.
* src/buffer.h (ts_linecol): New struct.
(buffer): New buffer fields.
(BUF_TS_LINECOL_BEGV):
(BUF_TS_LINECOL_POINT):
(BUF_TS_LINECOL_ZV):
(SET_BUF_TS_LINECOL_BEGV):
(SET_BUF_TS_LINECOL_POINT):
(SET_BUF_TS_LINECOL_ZV): New inline functions.
* src/casefiddle.c (casify_region): Record linecol info.
* src/editfns.c (Fsubst_char_in_region):
(Ftranslate_region_internal):
(Ftranspose_regions): Record linecol info.
* src/insdel.c (insert_1_both):
(insert_from_string_1):
(insert_from_gap_1):
(insert_from_buffer):
(replace_range):
(del_range_2): Record linecol info.
* src/treesit.c (TREESIT_BOB_LINECOL):
(TREESIT_EMPTY_LINECOL):
(TREESIT_TS_POINT_1_0): New constants.
(treesit_debug_print_linecol):
(treesit_buf_tracks_linecol_p):
(restore_restriction_and_selective_display):
(treesit_count_lines):
(treesit_debug_validate_linecol):
(treesit_linecol_of_pos):
(treesit_make_ts_point):
(Ftreesit_tracking_line_column_p):
(Ftreesit_parser_tracking_line_column_p): New functions.
(treesit_tree_edit_1): Accept real TSPoint and pass to
tree-sitter.
(compute_new_linecol_by_change): New function.
(treesit_record_change_1): Rename from treesit_record_change,
handle linecol if tracking is enabled.
(treesit_linecol_maybe): New function.
(treesit_record_change): New wrapper around
treesit_record_change_1 that handles some boilerplate and sets
buffer state.
(treesit_sync_visible_region): Handle linecol if tracking is
enabled.
(make_treesit_parser): Setup parser's linecol cache if tracking
is enabled.
(Ftreesit_parser_create): Enable tracking if the parser's
language requires it.
(Ftreesit__linecol_at):
(Ftreesit__linecol_cache_set):
(Ftreesit__linecol_cache): New functions for debugging and
testing.
(syms_of_treesit): New variable
Vtreesit_languages_require_line_column_tracking.
* src/treesit.h (Lisp_TS_Parser): New fields.
(TREESIT_BOB_LINECOL):
(TREESIT_EMPTY_LINECOL): New constants.
* test/src/treesit-tests.el (treesit-linecol-basic):
(treesit-linecol-search-back-across-newline):
(treesit-linecol-col-same-line):
(treesit-linecol-enable-disable): New tests.
* src/lisp.h: Declare display_count_lines.
* src/xdisp.c (display_count_lines): Remove static keyword.
2025-05-03 22:14:03 -07:00
..
comp-resources Update copyright year to 2025 2025-01-02 18:39:42 +01:00
emacs-module-resources Update copyright year to 2025 2025-01-02 18:39:42 +01:00
lread-resources Make force-load-doc-strings work again 2022-08-07 10:25:03 +02:00
regex-resources * src/regex-emacs.c (analyze_first): Fix incorrect optimization 2023-09-27 13:29:50 -04:00
syntax-resources Enhance syntax-tests.el to test nestable comments (Lisp style) 2020-11-17 16:51:49 +00:00
alloc-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
buffer-tests.el Prefer ert-with-temp-directory in tests 2025-02-23 03:46:57 +01:00
callint-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
callproc-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
casefiddle-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
character-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
charset-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
chartab-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
cmds-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
coding-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
comp-tests.el Fix a nativecomp type propagation bug (bug#74771) 2025-03-23 17:33:27 +01:00
data-tests.el Prefer incf to cl-incf in tests 2025-02-23 00:38:22 +01:00
decompress-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
doc-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
editfns-tests.el (decode_coding): Avoid nested *-change-functions (bug#78042) 2025-05-02 17:06:09 -04:00
emacs-module-tests.el Prefer plusp/minusp to cl-plusp/cl-minusp 2025-02-17 22:46:56 +01:00
emacs-tests.el Port allows-stdout test to overly-suspicious bwrap 2025-01-22 11:04:57 -08:00
eval-tests.el Correctly unload variable aliases. 2025-03-16 12:52:40 -04:00
fileio-tests.el Prefer ert-with-temp-directory in tests 2025-02-23 03:46:57 +01:00
filelock-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
floatfns-tests.el Prefer oddp/evenp to free-coding them in tests 2025-02-17 05:37:10 +01:00
fns-tests.el Add basic and low-level tests for hash tables 2025-03-24 00:57:47 +01:00
font-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
image-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
indent-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
inotify-tests.el Guarantee delivery of inotify special events 2025-02-26 10:39:46 +08:00
json-tests.el Respect narrowed buffers when parsing JSON (bug#77325) 2025-03-30 11:52:07 +00:00
keyboard-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
keymap-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
lcms-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
lread-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
marker-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
minibuf-tests.el Prefer incf to cl-incf in tests 2025-02-23 00:38:22 +01:00
print-tests.el Don't escape "." in `prin1' when followed by a letter 2025-04-28 13:46:40 -04:00
process-tests.el Adapt process-tests to Android 2025-02-28 21:06:48 +08:00
regex-emacs-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
search-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
sqlite-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
syntax-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
textprop-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
thread-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
timefns-tests.el Use eabs in Fcurrent_time_zone 2025-01-13 01:05:42 +01:00
treesit-tests.el Add line-column tracking for tree-sitter 2025-05-03 22:14:03 -07:00
undo-tests.el Prefer oddp/evenp to free-coding them in tests 2025-02-17 05:37:10 +01:00
xdisp-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
xfaces-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00
xml-tests.el Update copyright year to 2025 2025-01-02 18:39:42 +01:00