summaryrefslogtreecommitdiff
path: root/src/treesit.c
Commit message (Collapse)AuthorAge
* Support treesit-thing-settings in search functionsYuan Fu2023-04-14
| | | | | | | | | | | | * src/treesit.c (safe_assq) (treesit_traverse_get_predicate): New functions. (treesit_traverse_validate_predicate) (treesit_traverse_match_predicate): Support symbols. (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree) (Ftreesit_node_match_p): Move validation down so we can pass LANGUAGE to it.
* Add a recursion level limit for tree-sitter search predicatesYuan Fu2023-04-14
| | | | | | | | | | * src/treesit.c: (treesit_traverse_validate_predicate): Check for recursion level. (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree) (Ftreesit_node_match_p): Update uses of treesit_traverse_validate_predicate.
* ; Minor fixes in treesit.cYuan Fu2023-04-14
| | | | | | | | | | | | | | | | * src/treesit.c: (treesit_initialized): Make static. (treesit_find_override_name): Add check for XCAR (tail). (Ftreesit_parser_set_included_ranges): Fix comment. (treesit_recursion_limit): Change to a compile time constant. (treesit_symbol_to_c_name): Precompute the length. (Ftreesit_pattern_expand): Use predefined symbols. (treesit_cursor_helper) (Ftreesit_search_subtree) (Ftreesit_induce_sparse_tree): Update treesit_recursion_limit. (syms_of_treesit): New symbols.
* Add treesit-thing-settingsYuan Fu2023-04-14
| | | | | | | | | * lisp/treesit.el (treesit--things-around) (treesit--navigate-thing) (treesit-thing-at-point): Update docstring. * src/treesit.c (treesit_traverse_validate_predicate): Refer to treesit-thing-settings. (syms_of_treesit): Add Vtreesit_thing_settings.
* Add treesit-node-match-pYuan Fu2023-04-14
| | | | * src/treesit.c (Ftreesit_node_match_p): New function.
* Use BASE_EQ in treesit.cMattias EngdegÄrd2023-04-14
| | | | | | | * src/treesit.c (Ftreesit_node_check, Ftreesit_pattern_expand) (Ftreesit_query_capture, treesit_traverse_validate_predicate) (treesit_traverse_match_predicate): Use BASE_EQ instead of EQ where this is obviously correct.
* Fix previous commit on tree-sitterYuan Fu2023-04-13
| | | | | | | | | | * src/treesit.c: (treesit_traverse_validate_predicate): Don't accept symbols. (treesit_traverse_match_predicate): Don't accept symbols, and use correct variable for the regexp and pred check. * test/src/treesit-tests.el: (treesit-search-forward-predicate): Fix the test.
* Fix bugs in treesit.oPo Lu2023-04-14
| | | | | | | | | * src/treesit.c (treesit_traverse_match_predicate): Remove redundant cast. (treesit_search_forward, treesit_traverse_cleanup_cursor) (Ftreesit_search_subtree, Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Fix coding style and specpdl unwinding.
* Support more predicates in tree-sitter search functionsYuan Fu2023-04-13
| | | | | | | | | | | | | | | | | | | | | | | | | Right now we support regexp strings and predicate functions for the PRED argument. This change adds support for (not ...) (or ...) and (regexp . pred) predicates. I still need to find a place to document the supported shapes of a predicate. * src/treesit.c (treesit_traverse_validate_predicate): New function. (treesit_traverse_match_predicate): Support more predicate shapes. (treesit_search_dfs): (treesit_search_forward) (treesit_build_sparse_tree): Fix docstring (unrelated to this change). (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Use the new function to validate predicate shape. (syms_of_treesit): New error Qtreesit_invalid_predicate. * test/src/treesit-tests.el: (treesit--ert-search-setup): Add edebug declaration. (treesit-search-forward-predicate) (treesit-search-forward-predicate-invalid-predicate): New tests.
* Catch signals produced by PRED in tree-sitter search functionsYuan Fu2023-04-13
| | | | | | | | | | | | | | | Earlier we switched to using cursors rather than nodes to traverse the parse tree. Because cursors need cleanup, we have to catch signals thrown by the predicate functions and free the cursor. Failing to do this will result in leaking the cursor whenever the predicate function signals in a search function. This change fixes the leak. * src/treesit.c (treesit_traverse_cleanup_cursor): New function. (Ftreesit_search_subtree) (Ftreesit_search_forward) (Ftreesit_induce_sparse_tree): Catch signals.
* Merge from origin/emacs-29Stefan Monnier2023-03-27
|\ | | | | | | | | | | | | | | | | | | | | | | | | 8f42db010d1 Improve indenting "case" in Python c4d490490dc ; * test/src/fns-tests.el: Fix last change 875e77a66a6 * test/infra/Dockerfile.emba (emacs-native-comp): Add zli... 64a2b0d36fe Fix failure of fns-tests-collate-strings on Cygwin 90fc6b987ad * lisp/savehist.el (savehist-save): Preserve shared struc... 08fbc133756 Adapt Tramp manual accb3871668 Fix system time sampling on MS-Windows 33d436eefa1 Fix treesit_ensure_parsed (bug#62333) d93a439846f * lisp/help-fns.el (find-lisp-object-file-name): Fix bug#... be8147c53f9 Improve "Bugs" in the Emacs manual
| * Fix treesit_ensure_parsed (bug#62333)Yuan Fu2023-03-24
| | | | | | | | | | | | * src/treesit.c (treesit_ensure_parsed): Check for need_reparse after treesit_sync_visible_region runs, because as the comment says, treesit_sync_visible_region might modify need_reparse.
* | Switch buffer in Ftreesit_query_captureYuan Fu2023-03-24
| | | | | | | | | | | | | | | | | | | | | | This way both #pred and #match predicates runs in the node's buffer by default. * src/treesit.c: (treesit_predicate_match): No need to switch buffer anymore. (Ftreesit_query_capture): Switch buffer. * doc/lispref/parsing.texi (Pattern Matching): Update manual.
* | Handle signals gracefully in tree-sitter query predicatesYuan Fu2023-03-24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before this change, predicate functions can signal, which will cause Ftreesit_query_capture to skip freeing the query and cursor object. We make predicate functions return the signal data rather than directly signal. * src/treesit.c (treesit_predicate_capture_name_to_node) (treesit_predicate_capture_name_to_text) (treesit_predicate_equal) (treesit_predicate_match) (treesit_predicate_pred) (treesit_eval_predicates): Return signal rather than signaling directly. (Ftreesit_query_capture): Check for returned signal data.
* | ; Minor refactor of Ftreesit_query_captureYuan Fu2023-03-24
| | | | | | | | | | * src/treesit.c (Ftreesit_query_capture): Move around some variable initialization.
* | Refactor Ftreesit_query_captureYuan Fu2023-03-24
|/ | | | | | | | | Refactor some part of Ftreesit_query_capture out into separate functions, to pave the way for other query-based functions. * src/treesit.c (treesit_resolve_node): New function. (treesit_initialize_query): New function. (Ftreesit_query_capture): Refactor some part into new functions.
* ; * src/treesit.c (treesit_predicate_match): Fix typo.Yuan Fu2023-02-26
|
* Fix comment in treesit_record_change (bug#61369)Yuan Fu2023-02-18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Turns out the previous commit message and comment is not entirely correct: the old behavior is in fact wrong, not just "correct but has problems". Here is why the old code is wrong: |visible range| -> markup for visible range updated range -> markup for updated range ------------- First we have some text |aaaaaa| Now we insert something at the beginning, because we clip new_end_offset to visible_end, out of eight b's inserted, only the first six are known to tree-sitter. |bbbbbbbbaaaa|aa start: 0, old_end: 0, new_end: 6 ------ In treesit_sync_visible_region, we sync up visible region, but the two missing b's are not in the updated range. |bbbbbbbbaaaaaa| start: 12, old_end: 12, new_end: 14 -- The old behavior not only is wrong, but also doesn't make much sense. * src/treesit.c (treesit_record_change): Update comment.
* ; * src/treesit.c: Improve sectioning.Po Lu2023-02-18
|
* Don't completely clip into visible range in treesit_record_changeYuan Fu2023-02-17
| | | | | | | | | | | | | | | (Bug#61369) From min (visible_end, max (visible_beg, new_end_byte)) - visible_beg to max (visible_beg, new_end_byte) - visible_beg * src/treesit.c (treesit_record_change): We don't clip the new end into the visible range anymore. If you think of it, when inserting in a narrowed region, the visible region is always extended to accommodate more text, rather than pushing text at the end to keep the size of the visible region.
* Rename LIMIT to DEPTH in tree-sitter functions (bug#61231)Yuan Fu2023-02-09
| | | | | | | | | I only changed the Lisp functions, internal functions are left unchanged. * doc/lispref/parsing.texi (Retrieving Nodes): Update manual. * src/treesit.c (Ftreesit_search_subtree) (Ftreesit_induce_sparse_tree): Change LIMIT to DEPTH.
* Add 'live' property to treesit-node-check (bug#61235)Yuan Fu2023-02-09
| | | | | | | * doc/lispref/parsing.texi (Accessing Node Information): Document. * src/treesit.c (treesit_parser_live_p): New function. (Ftreesit_node_check): Add 'live' property. * test/src/treesit-tests.el (treesit-node-api): Add tests.
* ; Fix memory leak in treesit.cEli Zaretskii2023-02-08
| | | | | * src/treesit.c (treesit_load_language): Fix a memory leak. Reported by Eric Gillespie <epg@pretzelnet.org>.
* Fix crashes inside 'xfree' called from treesit.cEli Zaretskii2023-02-07
| | | | | * src/treesit.c (treesit_load_language): Always xstrdup 'c_name', to avoid crashes inside xfree. (Bug#61351)
* ; * src/treesit.c (treesit_predicate_match): Simplify last change.Eli Zaretskii2023-02-02
|
* (treesit_predicate_match): Match node text against regexp without consingDmitry Gutov2023-02-02
| | | | | | | | | | | | | | * src/treesit.c (treesit_predicate_match): Match node text against regexp without creating a new string object (bug#60953). * src/search.c (search_buffer): Make not static. Delete declaration near the beginning of the file. * src/lisp.h: Declare it here. * lisp/progmodes/ruby-ts-mode.el (ruby-ts--builtin-method-p): Remove function. (ruby-ts--font-lock-settings): Use the regexp with :match directly.
* Unbreak the MS-Windows buildEli Zaretskii2023-02-01
| | | | | * src/treesit.c (ts_query_pattern_count) [WINDOWSNT]: Load from the library and define as macro.
* ; (Ftreesit_query_capture): Fix typoDmitry Gutov2023-02-01
|
* (Ftreesit_query_capture): Cache list of predicates for given pattern indexDmitry Gutov2023-02-01
| | | | | * src/treesit.c (Ftreesit_query_capture): Cache list of predicates for given pattern index (bug#60953).
* Add treesit-subtree-statYuan Fu2023-01-29
| | | | | * src/treesit.c (Ftreesit_subtree_stat): New function. * lisp/treesit.el (treesit): Add to shortdoc.
* ; * src/treesit.c: Remove unused boilerplate.Yuan Fu2023-01-17
| | | | These two functions are not used after 7c61a304104.
* Fix treesit-node-first-child-for-pos (bug#60127)Yuan Fu2023-01-17
| | | | | | | | | The problem is due to a bug in ts_node_first_child_for_pos, but tree-sitter is moving pretty slowly right now so I reimplemented a correct version of it in treesit.c. * src/treesit.c (treesit_cursor_first_child_for_byte): New function. (Ftreesit_node_first_child_for_pos): Use the new function.
* ; * src/treesit.c (Ftreesit_induce_sparse_tree): Minor change.Yuan Fu2023-01-17
|
* Fix use of build_pure_c_string in treesit.cYuan Fu2023-01-13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is brought up in bug#60691. build_pure_c_string should only be used in places such as syms_of_treesit, which are called just once, during dumping. * src/treesit.c (Vtreesit_str_libtree_sitter): (Vtreesit_str_tree_sitter): (Vtreesit_str_dot): (Vtreesit_str_question_mark): (Vtreesit_str_star): (Vtreesit_str_plus): (Vtreesit_str_pound_equal): (Vtreesit_str_pound_match): (Vtreesit_str_pound_pred): (Vtreesit_str_open_bracket): (Vtreesit_str_close_bracket): (Vtreesit_str_open_paren): (Vtreesit_str_close_paren): (Vtreesit_str_space): (Vtreesit_str_equal): (Vtreesit_str_match): (Vtreesit_str_pred): New variables. (treesit_load_language): (Ftreesit_pattern_expand): (Ftreesit_query_expand): (treesit_eval_predicates): Use new varaibles. (treesit_check_buffer_size): (treesit_compose_query_signal_data): (treesit_check_range_argument): (Ftreesit_parser_set_included_ranges): (treesit_predicate_capture_name_to_node): (treesit_predicate_equal): (treesit_predicate_match): (treesit_predicate_pred): Use build_string for signal message. (syms_of_treesit): Initialize new variables.
* ; Improve documentation of tree-sitter node comparisonEli Zaretskii2023-01-13
| | | | | | * doc/lispref/parsing.texi (Accessing Node Information): * src/treesit.c (Ftreesit_node_eq): Improve documentation of node comparison.
* Equal now recognizes tree-sitter nodes (bug#60659)Yuan Fu2023-01-12
| | | | | | | | | | | Now equal uses ts_node_eq to check equality between nodes. * doc/lispref/parsing.texi: (Accessing Node Information): Update manual. * src/fns.c (internal_equal): Handle tree-sitter nodes. * src/treesit.c (treesit_node_eq): New function. (Ftreesit_node_eq): Factor out. Update docstring. * src/treesit.h (treesit_node_eq): Declare new function.
* ; Add 2023 to copyright years.Eli Zaretskii2023-01-01
|
* ; Fix recent treesit-related changesEli Zaretskii2022-12-31
| | | | | | | | | | | | * 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-30
| | | | | | | * 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-30
| | | | | | | | | 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-29
| | | | | | | * 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-28
| | | | | | * 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-28
| | | | | | | | 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-26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-26
| | | | | * lisp/treesit.el: * src/treesit.c: Add maintainer.
* ; Add treesit_recursion_limitYuan Fu2022-12-24
| | | | | | | * 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-24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-18
| | | | | | | * 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-17
| | | | | | | | 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-17
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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.