Commit graph

87202 commits

Author SHA1 Message Date
Yuan Fu
62c8c8e51a
Plug tree-sitter-simple-indent into c-offset-alist
Now tree-sitter indentation can produce a cc-engine syntax symbol and
use c-offset-alist to compute the offset.  Catch: line-up functions
don't work with tree-sitter.

* lisp/progmodes/js.el (js--treesit-cc-indent-rules): New variable.
(js-mode): Use cc-indent rules by default.
* lisp/treesit.el (treesit-simple-indent-presets): Consider types as
regexp now.  New matchers: n-p-gp, field-is, top-level, catch-all.
New anchors: nth-sibling, grand-parent, and, or, not, list.
first-sibling now returns the actual first sibling rather than the
first named sibling.
2022-10-26 18:41:29 -07:00
Yuan Fu
5532ae81cf
* lisp/treesit.el (treesit-node-top-level-p): New argument TYPE. 2022-10-26 18:31:07 -07:00
Yuan Fu
76b86d9853
Generalize js--treesit-imenu-top-level-p
* lisp/progmodes/js.el (js--treesit-imenu-top-level-p): Move.
(js--treesit-font-lock-settings): Fix usage.
* lisp/treesit.el (treesit-node-top-level-p): Move to here.
2022-10-26 18:06:07 -07:00
Yuan Fu
c352392420
Add tree-sitter navigation support to python-mode
* lisp/progmodes/python.el (python-mode): Set
treesit-defun-type-regexp.
2022-10-26 15:39:02 -07:00
Yuan Fu
6fb6cb075f
Add tree-sitter imenu support for js-mode and ts-mode
js-mode's current imenu is pretty plain and incomplete, so I took the
liberty to add a bit more flair to it.

* lisp/progmodes/js.el (js--treesit-imenu-type-alist): New variable.
(js--treesit-imenu-top-level-p)
(js--treesit-imenu-label)
(js--treesit-imenu-1)
(js--treesit-imenu): New functions.
2022-10-26 15:36:15 -07:00
Yuan Fu
734df28368
Fix tree-sitter navigation
* lisp/treesit.el (treesit-search-forward-goto): Track current
position with a local variable instead of (point).  Clean up the
condition forms.
(treesit-beginning-of-defun): Fix traverse direction.
2022-10-26 15:34:02 -07:00
Yuan Fu
1f74e9112e
Don't disable parse cache in tree-sitter activated js-mode
* lisp/progmodes/js.el (js-mode): Move parse cache setup back.
2022-10-25 19:46:58 -07:00
Yuan Fu
5070278539
* lisp/treesit.el (treesit-simple-indent-rules): Make buffer-local. 2022-10-25 14:27:43 -07:00
Yuan Fu
e5043db2f1
Fix treesit-language-at
* lisp/treesit.el (treesit-language-at-point-function): New local
variable.
(treesit-language-at): Change to use
treesit-language-at-point-function rather than trying each parser
one-by-one.
* doc/lispref/parsing.texi (Multiple Languages): Update manual.
2022-10-25 14:26:41 -07:00
Yuan Fu
7c5d434833
New tree-sitter toggle scheme
This version: central variable, everything controlled by
treesit-settings.  Major mode sets up tree-sitter/non-tree-sitter
in a conditional branch, based on the setting.

* lisp/treesit.el (treesit-settings): New option.
(treesit-defun-type-regexp): Change docstring.
(treesit-mode-supported)
(treesit-required-languages)
(treesit--local-variable-backup): Remove variables.
(treesit--backup-local-variable)
(treesit-mode)
(global-treesit-mode--turn-on)
(global-treesit-mode): Remove functions.
(treesit--setting-for-mode): New function.
(treesit-ready-p): New argument MODE, changed REPORT to QUIET, and
LANGUAGEs to LANGUAGE (now it can be a single symbol or a list of
them).
(treesit-major-mode-setup): New function.  Mostly comes from
treesit-mode.

* test/src/treesit-tests.el (treesit-misc): New test.

* lisp/progmodes/python.el (python-mode): Move some setup code into
the conditional branch at the end.

* lisp/progmodes/js.el (js-json-mode)
(js-mode): Move some setup code into the conditional branch at the
end.

* lisp/progmodes/ts-mode.el: Move tree-sitter setup into the
conditional branch.
2022-10-25 14:07:47 -07:00
Yuan Fu
06b5ec4bbd
* lisp/progmodes/ts-mode.el (ts-mode): Fix font-lock setup. 2022-10-24 15:02:41 -07:00
Yuan Fu
6203b9db34
* lisp/treesit.el (treesit-node-at): Remove argument LARGEST.
LARGEST returns the root node at BOB, finding the smallest node at
point and search upward is a better practice.
2022-10-23 18:54:37 -07:00
Yuan Fu
1787e651d3
* lisp/treesit.el (treesit-parser-range-on): New function. 2022-10-23 18:51:35 -07:00
Yuan Fu
c120b0ebdb
Fix treesit-beginning/end-of-defun
Now they always move to the top-level defun (i.e., skips nested ones).

* lisp/treesit.el (treesit--find-top-level-match): New function
(treesit-beginning-of-defun): Goes to the top-level match.
(treesit-end-of-defun): Simply move to the end of current match.
2022-10-23 18:47:02 -07:00
Yuan Fu
6cf2a9c55d
Make treesit-search-forward-goto accept a NODE argument
With NODE argument we can do

(setq node (treesit-search-forward-goto node))

And we can choose what node to pass to it (maybe we want to pass it
the largest node at point, rather than the smallest node, and in case
of multiple parsers, we can choose which parser to use).

* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-search-forward-goto): Accept a NODE
argument.
2022-10-23 18:29:02 -07:00
Yuan Fu
524d10c86b
Make treesit-node-at return the last leaf node at EOB
* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-node-at): Add a (when node) form just to be
explicit, return the last leaf node if there is no node after POS.
2022-10-23 18:23:17 -07:00
Yuan Fu
e45d8dbeea
; * lisp/treesit.el (treesit-range-functions): Fix typo. 2022-10-23 16:45:54 -07:00
Yuan Fu
3114eb8078
* lisp/treesit.el (treesit-node-at): Add LARGEST argument. 2022-10-23 16:16:27 -07:00
Yuan Fu
fa9bede36c
Change function signiture of treesit search functions
Justification: We want to make the SIDE argument in
treesit-search-forward-goto optional, so I changed it to START.
It makes more sense for BACKWARD to follow START so two common
case, search for end forward and beginning backwards are

(treesit-search-forward-goto node pred)
(treesit-search-forward-goto node pred t t)

Then since we swapped BACKWARD and ALL for
treesit-search-forward-goto, it's best to swap them for
treesit-search-forward and treesit-search-subtree, too.  And BACKWARD
will probably be used more frequently than ALL anyway.

* doc/lispref/parsing.texi (Retrieving Node): Resolve FIXME and update
function signitures.
* lisp/treesit.el (treesit-search-forward-goto): Change SIDE to
START, swap BACKWARD and ALL.
(treesit-beginning-of-defun)
(treesit-end-of-defun): Update use of treesit-search-forward-goto
* src/treesit.c (Ftreesit_search_subtree)
(Ftreesit_search_forward): Swap BACKWARD and ALL.
2022-10-23 15:19:00 -07:00
Yuan Fu
57b904f4ba
Fix infinite loop in treesit-search-forward-goto
* lisp/treesit.el (treesit-search-forward-goto): Remove UP argument.
* src/treesit.c (treesit_traverse_child_helper): New function.
(treesit_search_forward): Remove UP_ONLY and SKIP_START argument.
Don't traverse subtree of START.  And after we've found the next
sibling/parent, go down to the first leaf node.  Also change recursion
to loop.
(Ftreesit_search_forward): Change docstring, remove UP argument.
2022-10-22 22:50:56 -07:00
Eli Zaretskii
6f28810f6b Clean up tree-sitter sections of the ELisp manual
* doc/lispref/parsing.texi (Parsing Program Source):
* doc/lispref/modes.texi (Font Lock Mode)
(Parser-based Font Lock): Fix wording, punctuation, and markup.
Add index entries.

* lisp/treesit.el (treesit-node-at, treesit-language-at): Rename
argument POINT to POS.
2022-10-22 18:48:42 +03:00
Yuan Fu
7c750343be
* lisp/treesit.el (treesit-font-lock-enable): Add function back. 2022-10-21 18:50:24 -07:00
Yuan Fu
5159789e55
Revise the toggle scheme of tree-sitter (again)
Now instead of a toggle function (major-mode-backend-function), we let
major mode set local variables like treesit-font-lock-settings,
treesit-imenu-function, then treesit-mode takes care of activating
those things (clearing font-lock-keywords, setting
imenu-create-index-function to treesit-imenu-function, etc).

js.el and python.el: I've returned js-mode and python-mode to exactly
what they were before tree-sitter change, plus lines at the end
setting up tree-sitter variables.  Sorry about all these fuss :-D

* lisp/treesit.el (treesit-mode-inhibit-message): Remove option.
(major-mode-backend-function)
(treesit-remapped-major-mode-alist): Remove variables.
(treesit-mode): Move down to the end of buffer.  Do more things.
(global-treesit-mode): Move down to the end of buffer.  Don't handle
major-mode-remap-alist anymore.
(global-treesit-mode--turn-on): Move down to the end of buffer.
(treesit-ready-p): Move down to the end of buffer.  Changed signature.
(treesit-font-lock-enable): Remove function.
(treesit-defun-type-regexp): New variable.
(treesit-beginning-of-defun)
(treesit-end-of-defun): New function.
(treesit-imenu-function): New variable.
(treesit-mode-supported)
(treesit-required-languages)
(treesit--local-variable-backup): New variables.
(treesit--backup-local-variable): New function

* lisp/progmodes/js.el (js-use-tree-sitter): Remove option.
(js--treesit-defun-type-regexp): Remove variable.  (Now set inline in
js-mode.)
(js--treesit-beginning-of-defun)
(js--treesit-end-of-defun): Remove functions.  (Now use
treesit-beginning/end-of-defun.)
(js--backend-toggle)
(js--json-backend-toggle): Remove function.
(js-mode)
(js-json-mode): Restore back to before tree-sitter changes.  Add
tree-sitter setup at the end.

* lisp/progmodes/python.el (python--backend-toggle): Remove function.
(python-mode): Restore back to before tree-sitter changes.  Add
tree-sitter setup at the end.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): Use
js--fontify-template-string.
(ts-mode--fontify-template-string): Remove function (because we can
just use js--fontify-template-string).
(ts-mode--defun-type-regexp): Remove variable (now set inline in
ts-mode).
(ts-mode--beginning-of-defun)
(ts-mode--end-of-defun): Remove functions (now using
treesit-beginning/end-of-defun).
(ts-mode): Setup tree-sitter variables and then turn on treesit-mode
or move to js-mode.
2022-10-19 16:44:04 -07:00
Yuan Fu
6b475da972
; * lisp/progmodes/python.el (python--backend-toggle): Use cond. 2022-10-18 14:06:13 -07:00
Theodor Thornhill
c4ad36cd2d
Add more granular features in font-locking
There is now support for three font-locking levels, 'minimal',
'moderate' and 'full'.  The richest experience is to be expected from
the 'full', and all levels are enabled by default.

* lisp/progmodes/js.el (js--treesit-font-lock-settings): New defvar
renamed from 'js--treesit-settings'.

(js--treesit-font-lock-settings): New defvar renamed from
'js--json-treesit-settings'.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): New defvar
renamed from 'ts-mode--settings'.
2022-10-18 13:42:42 -07:00
Yuan Fu
85073998cb
Rename tree-sitter group to treesit
* lisp/treesit.el (tree-sitter): Rename to treesit.
(treesit-mode)
(global-treesit-mode): Use treesit group.
2022-10-18 13:31:42 -07:00
Yuan Fu
1ee7c3041a
* lisp/treesit.el: Add commentary. 2022-10-18 13:21:55 -07:00
Yuan Fu
17b65f5292
Install new toggle scheme for tree-sitter
Basically we now have treesit-mode and global-treesit-mode.  Major
modes set major-mode-backend-function which treesit-mode calls to
activate/deactivate tree-sitter.

js.el needs a bit explanation: I'm so sorry for messing up the
history, but basically now js-mode and js-json-mode should be exactly
the same as before any tree-sitter change was introduced, sans some
initialization code that are moved to js(-json)--backend-toggle.
js-mode and js-json-mode now just sets major-mode-backend-function and
initialize with js(-json)--backend-toggle.

* lisp/treesit.el (treesit-mode-inhibit-message): New option.
(treesit-can-enable-p): Remove function.
(major-mode-backend-function)
(treesit-remapped-major-mode-alist): New variables.
(treesit-mode)
(global-treesit-mode): New minor modes.
(global-treesit-mode--turn-on)
(treesit-ready-p): New functions.

* lisp/progmodes/python.el: Remove option.
(python--backend-toggle): New function.
(python-mode): Remove the if-form, all the initialization code are
moved to python--backend-toggle, python-mode now just sets
major-mode-backend-function and initialize with
python--backend-toggle.
* lisp/progmodes/js.el (js--treesit-can-enable-p)
(js--json-treesit-can-enable-p)
(js--treesit-enable)
(js--json-treesit-enable): Remove functions.
(js--backend-toggle)
(js-json--backend-toggle): New function.

* lisp/progmodes/ts-mode.el (ts-mode): Use treesit-ready-p.
2022-10-18 13:17:12 -07:00
Yuan Fu
170924e945
Fix js/ts tree-sitter template_string font-lock
* lisp/progmodes/js.el (js--treesit-settings): Fontify
template_strings with js--fontify-template-string.
(js--fontify-template-string): New function.
(js--json-treesit-settings): Add missing :feature flag.
* lisp/progmodes/ts-mode.el (ts-mode--settings): Fontify
template_strings with js--fontify-template-string.
2022-10-17 01:48:50 -07:00
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
f1e058d46b
Update ts-mode font-lock to work with tree-sitter's :feature
* lisp/progmodes/ts-mode.el (ts-mode--settings): Add :feature flag.
(ts-mode): Set treesit-font-lock-feature-list.
2022-10-15 16:09:35 -07: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
77f1533ccb
; Comment and docstring fixup in ts-mode.el
* lisp/progmodes/ts-mode.el: Add comment section and docstrings.
2022-10-11 23:49:04 -07:00
Theodor Thornhill
45b8204e09
Add TypeScript support with tree-sitter
* lisp/progmodes/ts-mode.el (ts-mode): New major mode for TypeScript
with support for tree-sitter.  It uses the TSX parser, so that we get
support for TSX as well as TypeScript.  If we cannot find tree-sitter,
we default to using js-mode.
2022-10-11 23:41:53 -07:00
Theodor Thornhill
c4179117af
Fix byte-compile warning for js--treesit-defun-type-regexp
* lisp/progmodes/js.el (js--treesit-defun-type-regexp): Move before
js--treesit-beginning-of-defun.
2022-10-11 11:45:50 -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
2a762336da
Fix js--treesit-beginning/end-of-defun
* lisp/progmodes/js.el (require): Add rx.
(js--treesit-move-to-node): Remove function.
(js--treesit-beginning-of-defun)
(js--treesit-end-of-defun): Change to use treesit-search-forward-goto.
(js-treesit--defun-query): Remove variable.
(js--treesit-defun-type-regexp): Add variable.
2022-10-10 10:46:47 -07:00
Theodor Thornhill
ceb9591e5d
Add tree-sitter functionality to js-mode
The diff for js-mode, js-json-mode, js-jsx-mode are a bit strange, but
all we did is to wrap some the old code in

(if tree-sitter
  (tree-sitter-enable)
 ...some old-code)

...other old-code (that’s tree-sitter-agnostic)

* lisp/progmodes/js.el (treesit): Add tree-sitter dependency
(js-use-treesitter)
(js-json-use-treesitter): New custom options.

(js--treesit-keywords)
(js--treesit-settings)
(js--treesit-indent-rules)
(js--json-treesit-settings)
(js--json-treesit-indent-rules): New variables.

(js--treesit-backward-up-list)
(js-treesit-current-defun)
(js--treesit-move-to-node)
(js--treesit-beginning-of-defun, js--treesit-end-of-defun)
(js--treesit-enable)
(js--json-treesit-enable): New functions.

(js-mode, js-json-mode, js-jsx-mode): Add support for tree-sitter
functionalities.
2022-10-10 10:25:50 -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
bb8376145a
Make python tree-sitter font-lock support decoration levels
* lisp/progmodes/python.el (python--treesit-settings): Stratify into
three levels.
2022-10-09 17:20:40 -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
cea1e95d38
Merge branch 'master' into feature/tree-sitter 2022-10-06 11:06:07 -07:00
Arash Esbati
0c65c9dfb3 Pass correct environment name to `reftex-label'
* lisp/textmodes/tex-mode.el (latex-block-body-alist): Pass
"figure" to `reftex-label' when inserting a figure float.
(bug#58329)
2022-10-06 19:45:44 +02:00
Justus Winter
82388dff8e Fix epg's reliance on an implicit GnuPG command.
Previously, epg invoked GnuPG without an explicit command to verify
signatures.  Make that operation explicit.  Happily, this aligns how
gpg and gpgsm is invoked, so it actually makes the code simpler.
2022-10-06 14:53:36 +02:00
Manuel Giraud
07b2e9b909 Fix BSD battery mode over TRAMP (bug#58307)
* lisp/battery.el (battery--call-process-to-string): Simple wrapper
to `call-process' that outputs to a string.
(battery-bsd-apm): Use it instead of `shell-command-to-string'
(bug#58307).
2022-10-06 14:46:39 +02:00