1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-16 02:50:26 -08:00
Commit graph

153 commits

Author SHA1 Message Date
Eli Zaretskii
80dcd78ff1 ; Fix recent documentation changes in treesit.el
* lisp/treesit.el (treesit-font-lock-level)
(treesit-font-lock-recompute-features): Doc fixes.
2022-11-27 10:06:02 +02:00
Yuan Fu
447b9d48d9
Add treesit-font-lock-level
This replaces font-lock-maximum-decoration and allows us to disable
the busiest fontification level by default.

* lisp/treesit.el (treesit-font-lock-level): New variable.
(treesit-font-lock-feature-list)
(treesit-font-lock-settings): Change docstring.
(treesit-font-lock-recompute-features): Use the new variable.
2022-11-26 15:40:24 -08:00
Yuan Fu
73c94d5a9f
; Fix comment-end in treesit-simple-indent-presets
* lisp/treesit.el (treesit-simple-indent-presets): Fix comment-end.
2022-11-25 19:00:22 -08:00
Stefan Kangas
4ee5618c6b ; Remove unused variable treesit-imenu-function
* lisp/treesit.el (treesit-imenu-function): Remove unused
variable.  (Bug#59475)
2022-11-25 08:58:57 +01:00
Yuan Fu
183c66be97
; Relayout comments in treesit-font-lock-fontify-region
; * lisp/treesit.el (treesit-font-lock-fontify-region): Relayout
comments.
2022-11-23 19:48:28 -08:00
Yuan Fu
00e7da76a7
Don't always filter out captured tree-sitter nodes outside of region
* lisp/treesit.el (treesit-font-lock-fontify-region): If the capture
name is a function, don't filter.
2022-11-23 19:48:28 -08:00
Yuan Fu
6785273a82
More flexible tree-sitter defun navigation
Before this change, treesit-beginning-of-defun skips nested defuns.
Now user can decide whether to skip nested defuns.

* lisp/treesit.el (treesit-search-forward-goto): Improve docstring.
(treesit-defun-prefer-top-level): New variable.
(treesit--defun-maybe-top-level): New function.
(treesit-beginning-of-defun)
(treesit-end-of-defun): Use treesit--defun-maybe-top-level.
2022-11-23 13:27:56 -08:00
Stefan Kangas
5e8c62ffca ; Fix typos 2022-11-23 05:49:22 +01:00
Yuan Fu
17ed39cc8d
; Remove obsolete use of inhibit-point-motion-hooks
* lisp/treesit.el (treesit-font-lock-fontify-region): Remove it.
2022-11-22 19:57:44 -08:00
Yuan Fu
c69858b3f0
; * lisp/treesit.el (treesit-ready-p): Guard against empty buffers. 2022-11-22 17:29:26 -08:00
Yuan Fu
057901f55a
Fix treesit-update-ranges
* lisp/treesit.el (treesit--clip-ranges): New function.
(treesit-update-ranges): Now clips the range within (point-min)
and (point-max), so the new range we use are not out-of-range.
2022-11-22 12:38:20 -08:00
Yuan Fu
2fa6ccab01
; Fix treesit--children-covering-range
* lisp/treesit.el (treesit--children-covering-range): Handle the case
when NODE doesn't have children, and when CHILD is nil.
2022-11-22 12:38:14 -08:00
Eli Zaretskii
368d2531be Fix 'treesit-max-buffer-size' and its use
* 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.
2022-11-22 20:22:41 +02:00
Yuan Fu
6e07838051
treesit-font-lock-recompute-features now has two modes of operation
1. Set activation of each feature (changes every feature)
2. Add/remove features (only change those explicitly configured by
ADD-LIST and REMOVE-LIST)

This is useful for enabling/disabling certain features for all
modes (without resetting others) by calling this function in
prog-mode-hook

* lisp/treesit.el (treesit-font-lock-recompute-features): See above
description.
2022-11-22 01:06:51 -08:00
Yuan Fu
c7202e7f4a
; Fix docstrings in treesit.el
* lisp/treesit.el (treesit--explorer--nodes-to-highlight)
(treesit--explorer-draw-node): Fix docstrings.
2022-11-22 01:06:51 -08:00
Yuan Fu
74892eefc8
* lisp/treesit.el (treesit-max-buffer-size): Enlarge value. 2022-11-22 01:06:51 -08:00
Juanma Barranquero
ea73fd69f0 ; Fix typos in tree-sitter files
* admin/notes/tree-sitter/starter-guide (Font-lock)
(Debugging queries, Indent, Navigation, Which-func)
(More features?):
* lisp/treesit.el (treesit--merge-ranges)
(treesit-font-lock-feature-list, treesit-font-lock-rules)
(treesit-font-lock-fontify-region, treesit--font-lock-notifier)
(treesit-simple-indent-presets, treesit--font-lock-fast-mode)
(treesit--indent-region-batch-size)
(treesit--indent-rules-optimize, treesit-ready-p): Fix typos.
2022-11-22 04:40:49 +01:00
Stefan Kangas
40539c7587 ; Fix typos 2022-11-22 02:24:20 +01:00
Yuan Fu
6cf8bf5042
Allow major modes to tweak tree-sitter fontification
treesit--font-lock-query-expand-range allows a major mode to fix
fontification problems temporarily before the parser can be fixed.

* lisp/treesit.el (treesit--font-lock-query-expand-range): New variable.
(treesit-font-lock-fontify-region): Use the new variable.
* lisp/textmodes/css-mode.el (css-ts-mode): Use the new variable.
2022-11-21 11:26:46 -08:00
Yuan Fu
0cbb7d3bd0
Further tweak tree-sitter fontification heuristics
So it turns out the slowness observed in bug#59415 is not due to the
size, but the strangely tall tree.  Adjust the heuristic to DTRT:
don't enable the heuristic by default or when buffer is large, enable
when query is abnormally slow.  We could do some clever thing that
calibrates a base reading for the query time instead of using a
hard-coded value, but it doesn't seem necessary.

* lisp/treesit.el (treesit--font-lock-fast-mode): New variable.
(treesit-font-lock-fontify-region): Don't activate heuristic by
default (reasons in comments).  Measure the query time and activate
the fast mode if query time is long.
2022-11-21 10:52:55 -08:00
Eli Zaretskii
27e89ff116 ; * lisp/treesit.el (treesit-parser-add-notifier): Add declaration. 2022-11-21 17:24:42 +02:00
Yuan Fu
938e68bb28
Fix tree-sitter fontification heuristic
Previously applied heuristic 2 sometimes invalidates heuristic 1, add
a guard so it doesn't.

The new function is just for clearity of the code and has nothing to
do with the change itself.

* lisp/treesit.el (treesit--node-length): New function
(treesit-font-lock-fontify-region): Use the new function.  Only do
heuristic 2 when the node is large.
2022-11-21 01:29:31 -08:00
Yuan Fu
76eb88df02
Add another heuristic to speed up tree-sitter fontification
* lisp/treesit.el (treesit-font-lock-fontify-region): Use
treesit--children-covering-range to reduce the node size.
2022-11-20 17:15:42 -08:00
Yuan Fu
625ea08652
Improve tree-sitter fontification on large buffers
* lisp/treesit.el (treesit--children-covering-range)
(treesit--children-covering-range-recurse): New functions.  They are
not currently used but could be useful in the future, so I left them
in place.
(treesit-font-lock-fontify-region):
* lisp/treesit.el (treesit-font-lock-fontify-region): Use the result
of treesit-node-on instead of the root node.
2022-11-20 17:04:58 -08:00
Eli Zaretskii
657947dc7c ; Improve recently-modified documentation
* lisp/progmodes/python.el (python-base-mode)
(python--treesit-fontify-string):
* lisp/textmodes/css-mode.el (css-base-mode):
* lisp/treesit.el (treesit-ready-p, treesit-comment-end)
(treesit-simple-indent-presets): Fix typos and wording in doc strings.

* doc/lispref/parsing.texi (Tree-sitter major modes):
* doc/lispref/modes.texi (Parser-based Indentation): Fix wording
and add cross-references.
2022-11-20 10:30:38 +02:00
Yuan Fu
f17ca55a0a
Remove treesit-settings
Remove it since we are using separate major modes for tree-sitter and
native variant now.

* doc/lispref/parsing.texi (Tree-sitter major modes): Update manual.
* lisp/treesit.el (treesit-settings): Remove option.
(treesit--setting-for-mode): Remove function.
(treesit-ready-p): Don't check for user preference in treesit-settings.
2022-11-19 18:54:39 -08:00
Yuan Fu
ac9bc819bc
Fix tree-sitter comment indentation for C-like languages
The goal is to indent like this:

/* comment
   comment  --> This line aligns with the beginning of the first line
 */         --> This line aligns with the opening comment token

* lisp/treesit.el (treesit-comment-start)
(treesit-comment-end): New variables.
(treesit-simple-indent-presets): New preset comment-end,
comment-start, comment-start-skip

* lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-styles)
(c-ts-mode)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--indent-rules)
(java-ts-mode)
* lisp/progmodes/js.el (js--treesit-indent-rules)
(js-ts-mode)
* lisp/progmodes/ts-mode.el (ts-mode--indent-rules)
(ts-mode): Add identical indent rules to each mode, and set identical
treesit-comment-start/end's.

* doc/lispref/modes.texi (Parser-based Indentation)
* doc/lispref/parsing.texi (Tree-sitter major modes): Update manual.
2022-11-19 18:36:08 -08:00
Yuan Fu
e41af3971d
Change tree-sitter indent preset 'match' to count on all nodes
I changed other presets to count all nodes a while ago, but forgot to
change 'match' too.

* lisp/treesit.el (treesit-simple-indent-presets): Change to count on
both named and anonymous nodes.
2022-11-19 15:25:06 -08:00
Yuan Fu
5d77eb4b7c
; * lisp/treesit.el (treesit--font-lock-notifier): Add messaging. 2022-11-19 00:41:19 -08:00
Yuan Fu
b440fa0a6b
; Elaborate on tree-sitter fontification features documentation
* doc/lispref/modes.texi (Parser-based Font Lock)
* lisp/treesit.el (treesit-font-lock-feature-list): Explain in more
detail, and change some features.
2022-11-17 10:11:20 -08:00
Yuan Fu
75b65b3f67
; Add documentation for tree-sitter parser after-change notifiers
* doc/lispref/parsing.texi (Using Parser): Update manual.
* lisp/treesit.el (treesit): Add shordocs.
* src/treesit.c: Augment docstrings.
2022-11-16 14:40:40 -08:00
Yuan Fu
306e49285a
Add treesit-explore-mode
This mode is basically the tree-sitter playground[1] in Emacs:
displays the syntax tree with the source side-by-side, kept in sync in
real-time.

[1] https://tree-sitter.github.io/tree-sitter/playground

* doc/lispref/parsing.texi (Language Definitions): Mention in manual.
* lisp/treesit.el (treesit--explorer-buffer)
(treesit--explorer-source-buffer)
(treesit--explorer-language)
(treesit--explorer-refresh-timer)
(treesit--explorer-highlight-overlay)
(treesit--explorer-last-node): New variables.
* lisp/treesit.el (treesit--explorer--nodes-to-highlight)
(treesit--explorer-refresh)
(treesit--explorer-post-command)
(treesit--explorer-jump)
(treesit--explorer-highlight-node)
(treesit--explorer-draw-node): New functions.
(treesit--explorer-tree-mode)
(treesit-explore-mode): New modes.
2022-11-16 14:40:37 -08:00
Yuan Fu
63f8a838ec
Make tree-sitter fontification automatically update
Now nodes that are affected and changed during re-parse will be
correctly refontified.  Nodes can change even if it corresponding text
wasn't edited: additional text can complete the parse tree and resolve
error nodes, for example.

* lisp/progmodes/python.el (python-mode): Create Python parser before
calling treesit-major-mode-setup.
* lisp/treesit.el (treesit--font-lock-notifier): New function.
(treesit-major-mode-setup): Register fontifier with every existing
parser.
2022-11-15 02:30:50 -08:00
Yuan Fu
e0760599b0
Remove the contextual hack in tree-sitter fontification
We now have a better facility that can replace the contextual
hack. The C part is in the previous commit, and the Lisp part work
will be in the next commit.

* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.
* lisp/progmodes/js.el (js--treesit-font-lock-settings)
* lisp/progmodes/python.el (python--treesit-settings)
* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): Stop marking
contextual nodes.
* lisp/treesit.el (treesit-font-lock-contextual-post-process): Remove
function.
(treesit-font-lock-fontify-region): Remove code processing contextual
nodes.
2022-11-15 02:22:41 -08:00
Eli Zaretskii
faf44e2c61 Fix recent changes in documentation
* lisp/treesit.el (treesit-node-at): Doc fix.

* doc/lispref/parsing.texi (Language Definitions): Add
cross-reference.
(Retrieving Nodes): Renamed from "Retrieving Node"; all references
changed.
(Retrieving Nodes): Fix wording and markup.
2022-11-12 10:57:56 +02:00
Yuan Fu
e333f040e9
; * lisp/treesit.el (treesit--indent-1): Fix treesit-node-at usage. 2022-11-11 21:37:29 -08:00
Yuan Fu
5cd3db73be
Improve treesit-node-at
* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-node-at): Change semantic.  It tries to
return the node that a user would expect in various circumstances.
* test/src/treesit-tests.el (treesit-node-at): New test.
2022-11-11 20:04:38 -08:00
Robert Pluim
a5a047a07f ; * lisp/treesit.el (treesit-font-lock-rules): Improve docstring 2022-11-10 12:45:14 +01:00
Yuan Fu
2332c3eb55
Add tree-sitter shortdoc
* lisp/treesit.el (treesit--generate-shortdoc-examples): New function.
(treesit): New shortdoc group.
2022-11-09 15:51:13 -08:00
Yuan Fu
077c664052
; Fix treesit-query-range docstring
* lisp/treesit.el (treesit-query-range): Change signature, change
reference of treesit-query-in to that of treesit-query-capture.
2022-11-09 15:51:12 -08:00
Yuan Fu
29418f954e
; Improve tree-sitter documentation
* doc/lispref/modes.texi (Parser-based Font Lock)
* doc/lispref/parsing.texi (Multiple Languages)
* lisp/treesit.el (treesit-range-rules)
(treesit-font-lock-rules): Improve phrasing around queries and keyword
and values.
2022-11-08 10:39:53 -08:00
Yuan Fu
07292b1344
; Improve docstring of treesit-simple-indent-presets
* lisp/treesit.el (treesit-simple-indent-presets): Improve docstring.
2022-11-07 02:27:59 -08:00
Yuan Fu
1bed1f0b8e
Add tree-sitter indent preset "point-min"
* doc/lispref/modes.texi (Parser-based Indentation): Update manual.
* lisp/treesit.el (treesit-simple-indent-presets): Add preset.  Update
docstring.
2022-11-07 02:00:04 -08:00
Yuan Fu
98f5b74808
Add treesit--indent-rules-optimize
* lisp/treesit.el (treesit--indent-rules-optimize): New function.
(treesit-major-mode-setup): Optimize indent rules when setting up for
major mode.
2022-11-07 01:59:21 -08:00
Eli Zaretskii
488a6efe32 ; More documentation improvements for treesit
* lisp/treesit.el (treesit-language-at, treesit-range-rules)
(treesit-font-lock-rules): Doc fixes.

* doc/lispref/parsing.texi (Multiple Languages):
* doc/lispref/modes.texi (Parser-based Font Lock): Fix wording.
2022-11-06 11:59:27 +02:00
Yuan Fu
9fbbb23552
Fix incorrect tree-sitter fontification
* lisp/treesit.el (treesit-font-lock-fontify-region): If the captured
node is outside of the region between START and END, don't fontify it.
Wrap fontification code in a when form.
2022-11-05 19:00:17 -07:00
Yuan Fu
076a258101
Allow tree-sitter indent offset to be a variable
* doc/lispref/modes.texi (Parser-based Indentation): Update manual.
* lisp/progmodes/js.el (js--treesit-indent-rules): Change all
occurance of ,js-indent-level to js-indent-level.

* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Change all
occurance of ,ts-mode-indent-offset to ts-mode-indent-offset.
* lisp/treesit.el (treesit-simple-indent-rules): Change docstring.
(treesit-simple-indent): Allow offset to be a variable.
2022-11-05 18:32:01 -07:00
Yuan Fu
f847ecc40b
Don't indent empty lines in treesit-indent-region
* lisp/treesit.el (treesit-indent-region): Wrap the old code in an if
form that checks whether we are at an empty line.  Add comments.
2022-11-05 17:56:24 -07:00
Yuan Fu
b471f7f614
Make treesit-language-at work in more situations
; * lisp/treesit.el (treesit-language-at): Return sometime sensible
even if treesit-language-at-point-function is nil.
2022-11-05 17:41:10 -07:00
Yuan Fu
5fbb7a126d
; Fix documentation for tree-sitter
* doc/lispref/modes.texi (Parser-based Font Lock): Fix the signature
style.
* doc/lispref/parsing.texi (Language Definitions): Rephrase the
section, remove documentation for deleted functions, fix signature
style.
* lisp/treesit.el (treesit-range-rules)
(treesit-font-lock-rules): Change ARGS to QUERY-SPECS, fix docstring.
* lisp/treesit.el (treesit-inspect-mode): Fix docstring.
2022-11-05 17:41:06 -07:00