summaryrefslogtreecommitdiff
path: root/src/treesit.c
Commit message (Collapse)AuthorAgeFilesLines
* ; Fix recent treesit-related changesEli Zaretskii2022-12-311-5/+5
| | | | | | | | | | | | * lisp/treesit.el (treesit-language-source-alist) (treesit--install-language-grammar-1): Doc fixes. * src/treesit.c (Ftreesit_language_abi_version): Fix a typo in function's Lisp name. Doc fix. (Ftreesit_language_available_p): Fix a typo in the function's C name. * doc/lispref/parsing.texi (Language Grammar): Fix wording.
* Show tree-sitter query source when signaling query errorYuan Fu2022-12-301-4/+7
| | | | | | | * src/treesit.c (treesit_compose_query_signal_data): Add QUERY_SOURCE parameter. (treesit_ensure_query_compiled) (Ftreesit_query_capture): Add query source.
* Add treesit-language-abi-versionYuan Fu2022-12-301-4/+27
| | | | | | | | | Also rename treesit-language-version to treesit-library-abi-version, because the old name is somewhat misleading. * doc/lispref/parsing.texi (Language Grammar): Update. * src/treesit.c (Ftreesit_library_abi_version): Rename. (Ftreesit_language_abi_version): New function.
* Avoid assertion violations in treesit.c with --enable-checkingEli Zaretskii2022-12-291-4/+3
| | | | | | | * src/treesit.c (Ftreesit_node_first_child_for_pos) (Ftreesit_node_descendant_for_range): Check validity of buffer positions before converting them to byte-positions, to avoid assertion violations in buf_charpos_to_bytepos.
* Call tree-sitter parser notifier on the first parseYuan Fu2022-12-281-9/+19
| | | | | | * src/treesit.c (treesit_call_after_change_functions): Handle NULL old_tree. (treesit_ensure_parsed): Remove check for NULL tree.
* Fix tree-sitter parser notifier recursionYuan Fu2022-12-281-5/+14
| | | | | | | | See the comment for detail. * src/treesit.c (treesit_ensure_parsed): Move the need_reparse short circuit to the very beginning. Move the call to treesit_call_after_change_functions to the very end.
* Add a new tree-sitter query predicate 'pred'Yuan Fu2022-12-261-12/+45
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I realized that using an arbitrary function as the predicate in queries is very helpful for some queries I'm writing for python and javascript, and presumably most other languages[1]. Granted, we can already filter out unwanted nodes by using a function instead of a face for the capture name, and (1) determine whether the captured node is valid and (2) fontify that node if it's valid. However, such approach is a bit more cumbersome and more importantly gets in the way of another potential use of the fontification queries: context extraction. For example, I could use the query for the 'variable' feature to get all the variables in a certain region. In this use-case, we want the filtering happen before returning the captured nodes. Besides, the change is relatively small and straightforward: most code are already there, I just need to add some boilerplate. [1] For a code like aa.bb(cc), we want bb to be in function face, because obviously its a function. But for aa.bb, we want bb to be in property face, because it's a property. In the AST, bb is always a property, the difference between the two cases is the enclosing node: in the first case, aa.bb is in a "call_expression" node, indicating that bb is used as a function (a method). So we want a predicate function that checks whether bb is used as a function or a property, and determine whether it should be in function or property face. * doc/lispref/parsing.texi (Pattern Matching): Update manual. * src/treesit.c (Ftreesit_pattern_expand): Handle :pred. (treesit_predicate_capture_name_to_node): A new function extracted from treesit_predicate_capture_name_to_text. (treesit_predicate_capture_name_to_text): Use the newly extracted function. (treesit_predicate_pred): New predicate function. (treesit_eval_predicates): Add new predicate. Also fix a bug: we want to AND the results of each predicate. * test/src/treesit-tests.el (treesit--ert-pred-last-sibling): New helper function. (treesit-query-api): Test #pred predicate.
* Add maintainer stub for tree-sitter filesYuan Fu2022-12-261-0/+2
| | | | | * lisp/treesit.el: * src/treesit.c: Add maintainer.
* ; Add treesit_recursion_limitYuan Fu2022-12-241-3/+8
| | | | | | | * src/treesit.c (treesit_recursion_limit): New constant. (treesit_cursor_helper) (Ftreesit_search_subtree) (Ftreesit_induce_sparse_tree): Use the new constant.
* Fix treesit_cursor_helper (bug#60267)Yuan Fu2022-12-241-53/+76
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The cause of that bug is that in a particular parse tree, the node treesit_cursor_helper tries to go to is a missing node, not only is it a missing node, it is the first node of a subtree. So when treesit_cursor_helper follows the algorithm and goes down the tree, it goes down the previous subtree (because that subtree's end = end_pos, because the target node has zero width). o | o--+-o | | +-+ +-+-+ | | | | | o x t o o (We ended up in x when the target is t, because t has zero width.) One way to solve it is to go back up the tree if we are at a leaf node and still haven't matched the target node. That's too ugly and finicky so I resorted to recursion. Now one more functions will return give up (treesit_node_parent) if we are in a werid parse tree that is super deep. But since we already kind of give up on this kind of parse trees (bug#59426), it doesn't really hurt. * src/treesit.c (treesit_cursor_helper_1): New function. (treesit_cursor_helper): Use the new function. Change return type to bool, and accept a cursor pointer. (Ftreesit_node_parent) (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Use the new signature.
* Fix MS-Windows build broken by recent treesit.c changesEli Zaretskii2022-12-181-4/+8
| | | | | | | * src/treesit.c (init_treesit_functions, ts_tree_cursor_copy) (ts_tree_cursor_delete): Add boilerplate for using new tree-sitter functions. (ts_node_parent): Delete boilerplate of unused function.
* Use cursor API in treesit-node-parentYuan Fu2022-12-171-6/+13
| | | | | | | | This is the last part of the change that fixes bug#60054. The previous change fixes it for searching functions, this fixes for treesit-node-parent. * src/treesit.c (Ftreesit_node_parent): Use the new cursor API.
* Switch to use cursor API in treesit.cYuan Fu2022-12-171-125/+187
| | | | | | | | | | | | | | | | | | | | | | | | | | | ts_node_parent has bugs (bug#60054), using cursor API avoids that. Tree-sitter's author might remove ts_node_parent in the future, so might as well switch to use cursors now. We are basically reimplementing some of the logic of ts_node_prev_sibling and ts_node_parent in the sibling helper and cursor helper functions. See also https://github.com/tree-sitter/tree-sitter/issues/1992 * src/treesit.c (treesit_traverse_sibling_helper) (treesit_traverse_child_helper) (treesit_traverse_match_predicate): Reimplemented to use the cursor API. (treesit_search_dfs) (treesit_search_forward): Use the new cursor helper functions. (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Use cursors. * test/src/treesit-tests.el (treesit-search-subtree): New test. (treesit--ert-search-setup): New macro. (treesit-search-forward) (treesit-search-forward-named-only) (treesit-search-backward) (treesit-search-backward-named-only) (treesit-cursor-helper-with-missing-node): New tests.
* Add treesit_assume_true and treesit_cursor_helperYuan Fu2022-12-171-0/+67
| | | | | | | | | This is part 1 of the change to change node API to cursor API. See the second part for more detail. (I splitted the change to make the diff more sane.) * src/treesit.c (treesit_assume_true) (treesit_cursor_helper): New functions.
* Make indirect buffers use tree-sitter parsers of their base bufferYuan Fu2022-12-091-17/+44
| | | | | | | | | | | | Fix the problem described in bug#59693. * src/treesit.c (treesit_record_change): Always use the base buffer. (Ftreesit_parser_create): Always use the base buffer. Also change the for loop into FOR_EACH_TAIL (stylistic change). (Ftreesit_parser_list): Always use the base buffer. * doc/lispref/parsing.texi (Using Parser): Update manual. * test/src/treesit-tests.el (treesit-indirect-buffer): New test.
* Improve parameter checking in tree-sitter functionsYuan Fu2022-12-071-19/+31
| | | | | | | | | | | | | * src/treesit.c (treesit_check_position): Extract out new function. (Ftreesit_node_first_child_for_pos) (Ftreesit_node_descendant_for_range): Replace code with the new function. (Ftreesit_query_capture): Add missing check for node and parser. Add check for range for BEG and END. Move treesit_initialize to the beginning of the function. * test/src/treesit-tests.el (treesit-node-api) (treesit-query-api): Add tests for out-of-range error.
* ; Minor cleanup in treesit.cEli Zaretskii2022-12-061-6/+4
| | | | | * src/treesit.c (Ftreesit_query_capture) (Ftreesit_parser_set_included_ranges): Minor cleanups.
* Fix treesit-query-captureYuan Fu2022-12-051-4/+7
| | | | | | | | | | | | | | | | | | | Before this change Ftreesit_query_capture doesn't convert character position to byte position for BEG and END parameters. I observed fontification issue in css files but couldn't figure out why, now I know :-) I decide to keep treesit--font-lock-query-expand-range, since it might provide a escape hatch for problems we discover in the future, and it should be very cheap so no downside of keeping it. * lisp/textmodes/css-mode.el (css-ts-mode): Stop setting treesit--font-lock-query-expand-range. * lisp/treesit.el (treesit--font-lock-query-expand-range): Update docstring. * src/treesit.c (Ftreesit_query_capture): Convert BEG and END to byte position. Also added parentheses wround "beg_byte - visible_beg" in the call to ts_query_cursor_set_byte_range (i.e., style change).
* Reparse tree-sitter tree when buffer restriction changesYuan Fu2022-11-281-6/+20
| | | | | | | | * src/treesit.c (treesit_sync_visible_region): Set nee_reparse flag to true if buffer range changes. Add some assertion. * src/treesit.c (treesit_ensure_parsed): Move treesit_sync_visible_region in front of the check for need_reparse.
* ; * src/treesit.c (Ftreesit_parser_included_ranges): Doc fix.Eli Zaretskii2022-11-271-2/+2
|
* ; Fix typo (Bug#59634)Stefan Kangas2022-11-271-2/+2
| | | | * src/treesit.c (Ftreesit_parser_included_ranges): Fix typo.
* ; * src/treesit.c (Ftreesit_induce_sparse_tree): Doc fix.Eli Zaretskii2022-11-271-1/+1
|
* Add default limit for tree-sitter recursive tree-traversing functionYuan Fu2022-11-261-28/+27
| | | | | | | | | | | This fixes bug#59426. * src/treesit.c (treesit_search_dfs) (treesit_build_sparse_tree): Remove no_limit parameter. (Ftreesit_search_forward, Ftreesit_induce_sparse_tree): Use default limit of 1000. * doc/lispref/parsing.texi (Retrieving Nodes): Update manual.
* ; * src/treesit.c: Fix typos and wording in comments.Eli Zaretskii2022-11-261-25/+25
|
* Fix tree-sitter assertion error (bug#59574)Yuan Fu2022-11-251-9/+15
| | | | | * src/treesit.c (treesit_sync_visible_region): Initialize visible_beg/end when tree is NULL.
* Rename treesit_ensure_position_synced to treesit_sync_visible_regionYuan Fu2022-11-251-13/+14
| | | | * src/treesit.c: Rename to better convey the purpose of the function.
* ; Add comments in treesit.c and treesit.hYuan Fu2022-11-251-4/+45
| | | | | * src/treesit.c * src/treesit.h: Add (and fix) comments.
* Avoid assertion violations in treesit.c when editing non-ASCIIEli Zaretskii2022-11-241-2/+2
| | | | | | * src/treesit.c (make_treesit_parser): Use byte positions when initializing 'lisp_parser'. This avoids assertion violations when the buffer has non-ASCII characters.
* ; * src/treesit.c (treesit_predicate_match): Move use below check.Yuan Fu2022-11-231-2/+3
|
* ; * src/treesit.c: Minor comment improvement.Yuan Fu2022-11-231-1/+2
|
* ; Fix typosStefan Kangas2022-11-231-2/+2
|
* Fix 'treesit-max-buffer-size' and its useEli Zaretskii2022-11-221-3/+3
| | | | | | | | | | | * lisp/treesit.el (treesit-max-buffer-size): Avoid overflow in computing buffer-size limit. Account for 32-but systems built "--with-wide-int". Extend doc string. (treesit-ready-p): Compare the limit with the size of the buffer in bytes, not in characters. * src/treesit.c (treesit_check_buffer_size): Measure buffer size in bytes.
* ; Fix comparisons in treesit.cEli Zaretskii2022-11-221-2/+2
| | | | | | * src/treesit.c (Ftreesit_parser_add_notifier) (Ftreesit_parser_remove_notifier): Fix comparison with Lisp objects. (Bug#59483)
* ; Fix typosStefan Kangas2022-11-221-5/+5
|
* ; * src/treesit.c (treesit_load_language): Move call to eassume.Yuan Fu2022-11-211-3/+1
| | | | | If handle is ever going to be NULL, it will be when error != NULL, so we should only eassume handle != NULL after the check for error.
* ; Repair build without tree-sitterMattias EngdegÄrd2022-11-211-2/+2
|
* * src/treesit.c (treesit_load_language): Fix uninitialized uses.Po Lu2022-11-211-1/+6
|
* Avoid usage of intern_c_string in treesit.cPo Lu2022-11-211-4/+8
| | | | | | * src/treesit.c (Ftreesit_pattern_expand): Use DEFSYM'd symbols when the naming makes sense. (syms_of_treesit): Add new defsyms QCanchor, QCequal, QCmatch.
* Stylistic fixes to treesit.cPo Lu2022-11-211-6/+6
| | | | | * src/treesit.c (treesit_make_ranges): (Ftreesit_parser_set_included_ranges): Fix coding style.
* Fix documentation of recent treesit changesEli Zaretskii2022-11-171-5/+5
| | | | | | | | | | * src/treesit.c (Ftreesit_parser_notifiers) (Ftreesit_parser_add_notifier, Ftreesit_parser_remove_notifier): Doc string fixes. * doc/lispref/parsing.texi (Accessing Node Information): Fix wording and punctuation. (Using Parser): Improve indexing and wording.
* Fix treesit-search-forward not matching leaf nodesYuan Fu2022-11-161-0/+9
| | | | * src/treesit.c: Match against NODE at start of the loop.
* Allow checking for outdated nodes in tree-sitterYuan Fu2022-11-161-3/+12
| | | | | | | | | Now you can run (treesit-node-check node 'outdated). * doc/lispref/parsing.texi (Accessing Node Information): Update manual. * src/treesit.c (Ftreesit_node_check): Add new property 'outdated'. * test/src/treesit-tests.el (treesit-node-check): Add tests.
* ; Add documentation for tree-sitter parser after-change notifiersYuan Fu2022-11-161-3/+11
| | | | | | * doc/lispref/parsing.texi (Using Parser): Update manual. * lisp/treesit.el (treesit): Add shordocs. * src/treesit.c: Augment docstrings.
* ; * src/treesit.c: Minor stylistic changes.Yuan Fu2022-11-151-13/+8
|
* Fix the MS-Windows build due to use of a new TS functionEli Zaretskii2022-11-151-4/+5
| | | | | * src/treesit.c (ts_tree_get_changed_ranges) [WINDOWSNT]: Define, load from the library, and call through a function pointer.
* Allow tree-sitter to notify parse-tree changesYuan Fu2022-11-151-1/+85
| | | | | | | | | | | * src/treesit.c (treesit_call_after_change_functions): New function. (treesit_ensure_parsed): Call treesit_call_after_change_functions right after re-parse. (make_treesit_parser): Initialize after_change_functions. (Ftreesit_parser_notifiers) (Ftreesit_parser_add_notifier) (Ftreesit_parser_remove_notifier): New functions. * src/treesit.h (Lisp_TS_Parser): New field after_change_functions.
* Extract out treesit_make_rangesYuan Fu2022-11-151-17/+27
| | | | | * src/treesit.c (treesit_make_ranges): New function. (Ftreesit_parser_included_ranges): Use treesit_make_ranges.
* Remove feature that checks whether tree-sitter node "has changes"Yuan Fu2022-11-141-8/+2
| | | | | | | | | | | | | | First of all, we signal error on using an outdated node, so if a node has changes, calling treesit-node-check would only raise an error. Besides, in order to properly answer whether a node has changed, we would have to update the node as the buffer is edited, which we don't do right now. * doc/lispref/parsing.texi (Accessing Node Information): Remove relevant manual text. * src/treesit.c (Ftreesit_node_check): Remove docstring mentions, remove the branch for "has-changes". (syms_of_treesit): Remove has-changes.
* Fix crash on MS-Windows due to memory-allocation problem in treesit.cEli Zaretskii2022-11-131-2/+2
| | | | | | | | * src/treesit.c (treesit_load_language): Use 'xstrdup'/'xfree' instead of 'strdup'/'free', to prevent crashes on MS-Windows, where we must use our own implementation of 'malloc'/'free', whereas 'strdup' uses the default implementation in the MS-Windows C runtime library.
* ; Fix docstring graph alignment in Ftreesit_search_forwardYuan Fu2022-11-101-4/+4
| | | | * src/treesit.c (Ftreesit_search_forward): Replace tabs with spaces.