Commit graph

46 commits

Author SHA1 Message Date
Yuan Fu
bd13ce184b
* lisp/treesit.el (treesit-font-lock-rules): Warn about :feature. 2022-10-17 01:48:50 -07:00
Eli Zaretskii
9d3f8912b4 Support MinGW build on MS-Windows
* src/treesit.c [WINDOWSNT]: Add MS-Windows boilerplate for
dynamically-loaded optional libraries.
(init_treesit_functions) [WINDOWSNT]: New function.
(load_tree_sitter_if_necessary): New function.
(ts_initialize): Call 'load_tree_sitter_if_necessary'.
(ts_delete_parser, ts_delete_query, ts_named_node_p): Wrapper
functions for TS calls from outside treesit.c.
(Ftreesit_parser_root_node, Ftreesit_parser_set_included_ranges)
(Ftreesit_parser_included_ranges, Ftreesit_node_type)
(Ftreesit_node_start, Ftreesit_node_end, Ftreesit_node_string)
(Ftreesit_node_parent, Ftreesit_node_child, Ftreesit_node_check)
(Ftreesit_node_field_name_for_child, Ftreesit_node_child_count)
(Ftreesit_node_next_sibling, Ftreesit_node_prev_sibling)
(Ftreesit_node_first_child_for_pos)
(Ftreesit_node_descendant_for_range, Ftreesit_node_eq)
(Ftreesit_query_compile, Ftreesit_query_capture)
(Ftreesit_search_subtree, Ftreesit_search_forward)
(Ftreesit_induce_sparse_tree): Call 'ts_initialize' before any
other TS functions.
(Ftreesit_available_p): 'treesit-available-p' implemented in C, so
that on WINDOWSNT the library could be loaded dynamically.
* src/treesit.h (ts_delete_parser, ts_delete_query)
(ts_named_node_p): Add prototypes.
* src/print.c (print_vectorlike):
* src/alloc.c (cleanup_vector): Call tree-sitter function via
wrappers defined in treesit.c, not directly, because WINDOWSNT
redefines the TS functions to be called via function pointers.
* src/Makefile.in (base_obj): Add treesit.o
(TREE_SITTER_OBJ): Remove.

* lisp/treesit.el (treesit-available-p): Remove: now implemented
in C.
* lisp/term/w32-win.el (dynamic-library-alist): Add libtree-sitter
DLLs.

* configure.ac (TREE_SITTER): Support the MinGW build.
(TREE_SITTER_OBJ): Remove.
2022-10-16 16:22:09 +03:00
Yuan Fu
af288d813b
Rework tree-sitter font-lock
Remove :toggle and :level, add :feature.

* lisp/progmodes/js.el (js--treesit-settings): Add :feature.
(js--treesit-enable): Set treesit-font-lock-feature-list.
* lisp/progmodes/python.el: Replace :level with :feature.
(python-mode): Set treesit-font-lock-feature-list.
* lisp/treesit.el (treesit-font-lock-feature-list): New variable.
(treesit-font-lock-settings): Change format.
(treesit-font-lock-rules): Remove :toggle and :level, add :feature.
(treesit-font-lock-recompute-features): New function.
(treesit-font-lock-fontify-region): Change to work with the new
format.
(treesit-font-lock-enable): Add call to
treesit-font-lock-recompute-features.  And improve the font-lock-mode
workaround.
2022-10-13 14:44:42 -07:00
Yuan Fu
1bb7b43e1c
Make treesit-node-at faster
* lisp/treesit.el (treesit-node-at): We only need to use
treesit-node-first-child-for-pos.  This is both faster for large
buffers and simpler.  Also remove the TODO comment.
2022-10-10 11:57:24 -07:00
Theodor Thornhill
c8909fa368
Fix typo in treesit--check-manual-covarage
* lisp/treesit.el (treesit--check-manual-coverage): Fix typo in
function name and also lookup the correct library in
`find-library-name'
2022-10-10 11:31:20 -07:00
Yuan Fu
b341864d90
Improve treesit-search-forward-goto so it doens't stuck at EOF
* lisp/treesit.el (treesit-search-forward-goto): Handle the edge case.
2022-10-10 11:14:35 -07:00
Yuan Fu
b6568c1389
Rename custom group treesit to tree-sitter
* lisp/treesit.el (treesit): Rename.
2022-10-09 20:59:59 -07:00
Yuan Fu
aaaa4ca77e
Add :version tag for new tree-sitter custom options
* lisp/progmodes/python.el (python-use-tree-sitter): Add version tag.
* lisp/treesit.el (treesit):
(treesit-max-buffer-size): Add version tag.
2022-10-09 20:54:19 -07:00
Yuan Fu
c88a1631e3
Make tree-sitter font-lock support decoration levels
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html:
Update.
* admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html:
Update.
* doc/lispref/modes.texi: Mention the new :level option.
* lisp/treesit.el (treesit-font-lock-settings): Update docstring.
(treesit-font-lock-rules): Support :level.  Relayout the let form.
(treesit-font-lock-fontify-region): Support :level.
2022-10-09 17:20:28 -07:00
Yuan Fu
8bc6e31502
Add a :toggle option for tree-sitter font-lock
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html:
Update html manual.
* admin/notes/tree-sitter/html-manual/build-manual.sh: Fix manual
path.
* admin/notes/tree-sitter/starter-guide: Mention :toggle.
* doc/lispref/modes.texi: Mention :toggle.
* lisp/treesit.el (treesit-font-lock-settings): Update docstring.
(treesit-font-lock-rules): Handle :toggle.  Also change
some wrong-type-argument signal to treesit-font-lock-error which is
easier to understand.
(treesit-font-lock-fontify-region): Handle :toggle.
2022-10-07 01:21:09 -07:00
Yuan Fu
23bb724c92
Add :override flag for tree-sitter font-lock
* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.
* lisp/treesit.el (treesit-font-lock-settings): Update docstring.
(treesit-font-lock-rules): Handle :override.
(treesit-font-lock-fontify-region): Handle :override.  Also set
inhibit-point-motion-hooks to t.
2022-10-04 13:30:56 -07:00
Yuan Fu
10f96a8c57
Improve documentation of tree-sitter search functions
* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* src/treesit.c (treesit-search-subtree)
(treesit-search-forwardn)
(treesit-induce-sparse-tree): Mention case-insensitivity.
* lisp/treesit.el (treesit-simple-indent): Mention case-insensitivity.
2022-10-04 13:30:56 -07:00
Yuan Fu
9b5ecffeb0
Ignore some capture name in treesit-font-lock-fontify-region
* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.
* lisp/treesit.el: (treesit-font-lock-fontify-region): Ignore names
that are not face nor function.
(treesit-font-lock-rules): Update docstring.
2022-10-04 13:30:55 -07:00
Yuan Fu
6a3caeab58
Remove treesit-query-in
Because treesit-query-capture can now do everything it does.

* doc/lispref/parsing.texi (Pattern Matching): Update manual.
* lisp/treesit.el (treesit-query-in): Remove function.
* src/treesit.c (Ftreesit_query_capture): Accept parser and language
symbol as NODE.
2022-10-04 13:30:52 -07:00
Yuan Fu
e504eabe88
Change tree-sitter indent anchor preset 'prev-line'
* doc/lispref/modes.texi: Update manual.
* lisp/treesit.el (treesit-simple-indent-presets): Change prev-line to
mean bol of prev-line.
2022-09-30 16:54:42 -07:00
Yuan Fu
ef6e18a6b9
Improve treesit-search-forward-goto
* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-search-forward-goto): Instead of taking a
node, use the node at point, and make sure we make progress.
2022-09-24 21:11:31 -07:00
Yuan Fu
f071e61d10
; Fix docstrings in treesit.el
* lisp/treesit.el (treesit-font-lock-rules)
(treesit-inspect-mode): Fix docstrings.
2022-09-24 21:11:30 -07:00
Yuan Fu
eba6582436
Add the treesit-search functions that supplant the removed ones
The signatures also changed.

treesit-traverse-depth-first   -> treesit-search-subtree
treesit-traverse-breadth-first ->
treesit-traverse-forward       -> treesit-search-forward
treesit-search-forward         -> treesit-search-forward-goto
treesit-search-beginning/end   -> treesit-search-forward-goto
                               -> treesit-induce-sparse-tree

* doc/lispref/parsing.texi (Retrieving Node): Add relevant manual
sections.
* lisp/treesit.el (treesit-search-forward-goto): New function.
* src/treesit.c (ts_traverse_sibling_helper)
(ts_traverse_match_predicate)
(ts_search_dfs)
(ts_search_forward)
(treesit-search-subtree)
(treesit-search-forward)
(ts_build_sparse_tree)
(Ftreesit_induce_sparse_tree): Add functions.
* test/src/treesit-tests.el (treesit-node-supplemental): Add comments.
2022-09-24 21:11:30 -07:00
Yuan Fu
c957832cbf
Remove treesit-traverse functions
Remove before adding the replacements.

* doc/lispref/parsing.texi (Retrieving Node): Remove relevant sections.
* lisp/treesit.el (treesit-traverse-depth-first)
(treesit--traverse-breadth-first-1)
(treesit-traverse-breadth-first)
(treesit-next-sibling-or-up)
(treesit-traverse-forward)
(treesit-search-forward)
(treesit-search-beginning):
(treesit-search-end): Remove functions.
(treesit-defun-query): Remove variable.
(treesit-beginning-of-defun)
(treesit-end-of-defun): Remove functions.
* test/src/treesit-tests.el: Remove comments.
2022-09-24 21:11:30 -07:00
Yuan Fu
1575ee2eeb
Accept nil as NODE in treesit-node-text
* lisp/treesit.el (treesit-node-text): Just wrap function body
in (when ...).
2022-09-24 21:11:30 -07:00
Yuan Fu
cb0464bf42
Fix treesit-query-validate
* lisp/treesit.el (treesit-query-validate): Add a call to
treesit-query-expand so this function works on both sexp and string
query, as expected.
2022-09-20 18:36:52 -07:00
Yuan Fu
1d3234988a
Add node-only parameter to treesit-query-capture
* doc/lispref/parsing.texi (Pattern Matching): Mention the new
parameter.
* lisp/treesit.el (treesit-query-in): Add node-only.
* src/treesit.c (Ftreesit_query_capture): Add node-only.
2022-09-08 12:52:25 -07:00
Yuan Fu
56dbb5db3b
* lisp/treesit.el (treesit-can-enable-p): Minor fix. 2022-09-08 12:50:47 -07:00
Yuan Fu
a23aec59b3
Remove treesit-disabled-modes and change treesit-should-enable-p
Per emacs-devel discussion, remove treesit-disabled-modes and let
major modes to provide tree-sitter switches.  I also decided to add
treesit-max-buffer-size to elisp manual despite it being a user
option.  Though we should still add it to the user manual.

* doc/lispref/parsing.texi (Parsing Program Source): Update manual to
remove entries for treesit-diabled-modes and add
treesit-max-buffer-size.  Also update treesit-should-enable-p.
* lisp/treesit.el (treesit-disabled-modes): Remove user option.
(treesit-maximum-size): Change to treesit-max-buffer-size.
(treesit-should-enable-p): Change to treesit-can-enable-p and remove
checks of treesit-disabled-modes.
2022-09-07 13:20:37 -07:00
Yuan Fu
31ad906bd0
Add manual entry for tree-sitter search functions
* doc/lispref/parsing.texi (Retrieving Node): New subsection
"Searching for node".
* doc/lispref/positions.texi (List Motion): Add entries for
treesit-defun-query, treesit-beginning-of-defun, treesit-end-of-defun.
* lisp/treesit.el (treesit-search-forward, treesit-search-beginning)
(treesit-search-end): Minor docstring fix-up.
2022-09-07 11:52:13 -07:00
Yuan Fu
47a6c23751
Add tree-sitter font-lock settings helper function/macro
1. Add treesit-font-lock-rules that helps with settings
treesit-font-lock-settings.
2. Remove treesit-font-lock-defaults and with it, decoration levels.

Now major modes should set treesit-font-lock-settings with the output
of treesit-font-lock-rules rather than setting
treesit-font-lock-defaults.

* lisp/treesit.el (treesit-font-lock-settings): Update docstring.
(treesit-font-lock-rules): New function.
(treesit-font-lock-defaults): Remove variable.
(treesit-font-lock-enable): Remove code that interacts
treesit-font-lock-defaults.
* doc/lispref/modes.texi: Update manual for
treesit-font-lock-settings, treesit-font-lock-rules,
treesit-font-lock-defaults.
2022-09-07 11:46:07 -07:00
Yuan Fu
c8eeaa4ae4 ; * lisp/treesit.el (treesit-traverse-forward): Fix docstring. 2022-06-18 16:57:50 -07:00
Yuan Fu
246dbb540a Change treesit-parser-list from variable to function
Effectively making the list internal.  Now Emacs user cannot shoot
themselves in the foot by removing a parser from the list, make
chaanges to buffer and add that parser back to the list.

* doc/lispref/parsing.texi (Language Definitions, Using Parser)
(Retrieving Node, Multiple Languages): Change variable to function.
* lisp/treesit.el (treesit-language-at, treesit-node-on)
(treesit-buffer-root-node, treesit-indent, treesit-check-indent)
(treesit-search-forward, treesit-search-beginning)
(treesit-end-of-defun, treesit-inspect-mode): Change variable to
function.
* src/buffer.c (bset_ts_parser_list, reset_buffer, init_buffer_once):
Add ts_parser_list.
* src/buffer.h (struct buffer): Add ts_parser_list.
* src/treesit.c (ts_record_change, Ftreesit_parser_create): Use the
buffer field instead of the old buffer local variable.
(Ftreesit_parser_delete, Ftreesit_parser_list): New functions.
(syms_of_treesit): Remove treesit-parser-list.
* test/src/treesit-tests.el (treesit-basic-parsing): Use the new
function.
2022-06-16 17:55:07 -07:00
Yuan Fu
dd65d1c396 Consolidate treesit parser create functions
Merge treesit-parser-create, treesit-get-parser,
treesit-get-parser-create into one: treesit-parser-create.

* src/treesit.c (Ftreesit_parser_language): make BUFFER parameter
optional, add new parameter NO-REUSE.  Optionally reuse parser.
* test/src/treesit-tests.el: Change all parser creation to use
treesit-parser-create.  Remove tests for the removed functions.
* lisp/treesit.el (treesit-get-parser, treesit-get-parser-create):
Remove.
* lisp/treesit.el (treesit-set-ranges, treesit-get-ranges)
(treesit-buffer-root-node, treesit-query-string)
(treesit-font-lock-fontify-region, treesit-search-forward)
(treesit-query-validate): Change to use treesit-parser-create.
2022-06-16 11:52:04 -07:00
Yuan Fu
184d212042
Merge branch 'feature/tree-sitter-depth-control' into feature/tree-sitter 2022-06-14 15:52:21 -07:00
Yuan Fu
316bdc334c
Add manual for treesit-traverse-forward and friends
* doc/lispref/parsing.texi (Retrieving Node): Add manual entry for
treesit-traverse-depth-first, treesit-traverse-breadth-first,
treesit-traverse-forward.
* lisp/treesit.el (treesit-traverse-forward): Fix docstring.
2022-06-14 15:49:44 -07:00
Yuan Fu
a7288594f4
Change treesit-check-query and mention it in documentation
* doc/lispref/parsing.texi (Pattern Matching): Mention it.
* lisp/treesit.el (treesit-check-query): Rename to
treesit-query-validate.
* src/treesit.c (Ftreesit_query_capture, Ftreesit_query_compile):
Mention it.
2022-06-14 14:30:39 -07:00
Yuan Fu
e171ef933f
Support compiled queries in treesit-query-capture
Last commit added this new type, this commit adds functionalities.
treesit.el only has documentation changes.

* lisp/treesit.el (treesit-query-in, treesit-font-lock-settings,
treesit-defun-query): Update docstring.
* src/treesit.c (make_ts_query): New function.
(Ftreesit_query_compile): New function.
(Ftreesit_query_capture): Remove code that creates a query object and
instead either use make_ts_query or use the give compiled query.  Free
the query object conditonally.
(syms_of_treesit): New symbol.
2022-06-14 11:50:24 -07:00
Yuan Fu
8aa04aac65
; * lisp/treesit.el (treesit-defun-query): Improve docstring. 2022-06-13 14:20:40 -07:00
Yuan Fu
b3de8850e0
Use the up-only parameter in treesit navigation functions
* lisp/treesit.el(treesit-inspect-node-at-point,
treesit-end-of-defun): Set up-only to t.
2022-06-13 14:20:40 -07:00
Yuan Fu
c62473c31a
Add depth control for treesit traverse functions
* lisp/treesit.el (treesit-traverse-depth-first,
treesit-traverse-forward): Add depth parameter.
(treesit-search-forward, treesit-search-beginning,
treesit-search-end): Add up-only parameter.
2022-06-13 14:20:40 -07:00
Yuan Fu
a73f2b9990
Fix treesit-search-forward
Move the check for movement

    (if (> arg 0)
        ;; Make sure we moved forward.
        (> (funcall pos-fn node) starting-point)
      ;; Make sure we moved backward.
      (< (funcall pos-fn node) starting-point))

into cl-loop:

    if (treesit-node-eq cap-node node)

becomes

    if (and (treesit-node-eq cap-node node)
            (if (> arg 0)
                ;; Make sure we moved forward.
                (> (funcall pos-fn node)
                   starting-point)
              ;; Make sure we moved backward.
              (< (funcall pos-fn node)
                 starting-point)))

* lisp/treesit.el (treesit-search-forward): Move the check.
2022-06-13 14:20:25 -07:00
Yuan Fu
1dd8ddee12
Rename treesit-traverse-forward-depth-first
* lisp/treesit.el (treesit-traverse-forward): Rename to
'treesit-traverse-forward'.
(treesit-traverse-forward, treesit-search-forward): Use
the new name.
2022-06-11 20:24:38 -07:00
Theodor Thornhill
35e2786c93
Fix typo and argument in treesit-beginning-of-defun, etc
* lisp/treesit.el (treesit-beginning-of-defun, treesit-end-of-defun):
Fix typo, add shield for argument.
2022-05-19 19:06:12 -07:00
Yuan Fu
74f8572f6c
; * lisp/treesit.el (treesit-node-at): Fix typo. 2022-05-14 08:57:23 -07:00
Yuan Fu
b2b57eda04
Extract out treesit-search-forward
* lisp/treesit.el (treesit-search-forward, treesit-search-beginning,
treesit-search-end): New functions.
(treesit-traverse-defun): Remove function.
(treesit-beginning-of-defun, treesit-end-of-defun): Replace
'treesit-traverse-defun' with 'treesit-search-forward' and fiends.
* test/src/treesit-tests.el: Add reminder for tests.
2022-05-13 16:41:50 -07:00
Yuan Fu
750090fd07
* lisp/treesit.el (treesit-node-at): Add check for nil node. 2022-05-13 16:34:26 -07:00
Yuan Fu
d8c9b9c0fb
Add defun navigation
* lisp/treesit.el (treesit-defun-query): New variable.
(treesit-traverse-defun, treesit-beginning-of-defun,
treesit-end-of-defun): New functions.
* test/src/treesit-tests.el: Add reminders for tests.
2022-05-13 14:37:24 -07:00
Yuan Fu
d94c7076df
New node traversal functions
* lisp/treesit.el (treesit-traverse-parent): New alias.
(treesit-traverse-depth-first, treesit--traverse-breadth-first-1,
treesit-traverse-breadth-first, treesit-next-sibling-or-up,
treesit-traverse-forward-depth-first): New functions.
* test/src/treesit-tests.el (treesit-node-supplemental): Add reminders
for tests.
2022-05-13 14:37:24 -07:00
Yuan Fu
78df03329d
Redefine treesit-node-at
The old 'treesit-node-at' becomes 'treesit-node-on'.  The new
'treesit-node-at' has slightly different semantics.  Now
'treesit-node-on' gets the smallest node covering a range and
'treesit-node-at' gets the smallest node after a position.

The reason of change can be found in the docstring of
'treesit-node-on' (the BEWARE part): its result can be sometimes
surprising/unexpected.

* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-node-at): Change to new definition.
(treesit-node-on): Inherits the old definition of
'treesit-node-at'.  Parameter END is now mandatory.
(treesit-language-at, treesit-node-field-name): Use the new '-on'
function.
(treesit-font-lock-fontify-region, treesit-simple-indent-presets,
treesit-indent): Use the new '-at' function.
* test/src/treesit-tests.el (treesit-node-supplemental): Update tests.
2022-05-13 14:36:37 -07:00
Yuan Fu
84847cad82
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-05-07 01:11:39 -07:00