1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-10 16:20:17 -08:00
Commit graph

566 commits

Author SHA1 Message Date
Jostein Kjønigsen
4bccb7b211
Make treesit-query-validate create a read-only buffer
* lisp/treesit.el: use view-mode for generated validation buffer.
2022-12-12 15:08:55 -08:00
Stefan Kangas
fcd0b377e0 Merge from origin/emacs-29
06ef030f93 use-package.texi: New section "Manual installation"
f4ce6fa7d3 Revert "Revert "Improve last change to xfaces.c" (05ece1e...
b8d2ec920f Revert "Improve last change to xfaces.c" (05ece1eb8b)
24c8c28ae6 Do not pare arguments unnecessarily.
9c0d7bb73b Add automated tests for Eglot
d3669cfe15 Eglot: allow skipping compile-time warnings about LSP int...
04b7e01885 ; project.el: Bump version.
f2876014ad Add customizale faces for tree-sitter explorer
3e349ee119 Fix error message when installing non-existent package
733cdeabfb Don't use diff-mode buffer as a patch when it's visiting ...
87475f4af2 Fix pcase rx patterns using rx-let bindings (bug#59814)
4893a15631 Fix use-package-defaults defcustom type (bug#59941)
074b7e6f4d ; * lisp/use-package/bind-key.el: Remove ineffective back...
864ed9dfa1 ; * lisp/progmodes/dockerfile-ts-mode.el: use \' instead ...
9f7e5584a4 * lisp/language/indian.el: Improve Brahmi composition rul...
78ad33bb05 ; Minor cleanup of last change in xfaces.c.
2024ade271 ; Improve docs of relaxing face-font attribute match (bug...
2022-12-12 09:05:53 +01:00
Yuan Fu
f2876014ad
Add customizale faces for tree-sitter explorer
* lisp/treesit.el (treesit-explorer-anonymous-node)
(treesit-explorer-field-name): New face.
(treesit--explorer-draw-node): Use the new faces.
(treesit-explore-mode): Change playground to explorer.
2022-12-11 14:51:33 -08:00
Theodor Thornhill
b889eced44 Add prog-fill-reindent-defun (bug#59664)
Introduce a new command that aims to reindent code in a defun, or fill
a paragraph of text.  The command uses treesit.el when available,
otherwise falls back to using syntax-ppss and regexps.  Treesit.el
needs a new variable that is intended to be set by the major modes so
that this and other future functions can know what kind of node we are
looking at.

* doc/emacs/programs.texi: Mention the new command.
* etc/NEWS: Mention the new command.
* lisp/progmodes/c-ts-mode.el (c++-ts-mode): Add regexp for the new
variable.
* lisp/progmodes/csharp-mode.el (csharp-ts-mode): Add regexp for the
new variable.
* lisp/progmodes/java-ts-mode.el (java-ts-mode): Add regexp for the
new variable.
* lisp/progmodes/js.el (js-ts-mode): Add regexp for the new variable.
* list/progmodes/prog-mode.el (prog-mode-map): Bind the new command by
default.
(prog-fill-reindent-defun): New command.
* lisp/progmodes/sh-script.el (bash-ts-mode): Add regexp for the new
variable.
* lisp/progmodes/typescript-ts-mode.el (typescript-ts-base-mode): Add
regexp for the new variable.
* lisp/treesit.el (treesit-text-type-regexp): New variable.
2022-12-11 22:31:31 +02:00
Eli Zaretskii
d3494f1bde Resurrect changes omitted by a recent merge from emacs-29 (bug#59921)
This includes the changes for the following commits:

  670daa8b
  b429e524
  c83c95634e
  6479691cf0
  b710ca62c0
  d31a253983
  a669d5fae5
  f7262b8f81
  fef1755736
  bf81df86e5
  bfc00f1c12
  d2411615e8
  dcf69a1d
2022-12-10 13:22:48 +02:00
Yuan Fu
670daa8b62
Add recursion limit to treesit--children-covering-range-recurse
* lisp/treesit.el (treesit--children-covering-range-recurse): Add
limit.
2022-12-07 16:36:07 -08:00
Yuan Fu
b429e52428
Improve tree-sitter fontification in edge-cases (bug#59738)
* lisp/treesit.el (treesit-font-lock-fontify-region): Use
treesit--children-covering-range-recurse.
2022-12-07 16:04:17 -08:00
Yuan Fu
c26fe45cb8
Fix treesit-query-capture
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).
2022-12-05 19:56:47 -08:00
Yuan Fu
ec00d292ec
Improve treesit-fontify-with-override
This also fixes fontification problem with c-ts-mode--fontify-defun.

Now treesit-fontify-with-override clips the fontification region for
the user, so no need for (max start node-start) shenanigans anymore.
More importantly it doesn't fontify unless the region between
node-start and node-end intersects with the region between start and
end, which fixes the problem with c-ts-mode--fontify-defun.

* lisp/treesit.el (treesit-fontify-with-override): Add optional
parameter BOUND-START and BOUND-END.  Wrap the function body in a
when-form.
* lisp/progmodes/c-ts-mode.el (c-ts-mode--fontify-declarator)
(c-ts-mode--fontify-variable)
(c-ts-mode--fontify-defun)
(c-ts-fontify-error)
* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string): Use the
new signature.
2022-12-04 20:03:28 -08:00
Yuan Fu
520a4e12f8
; * lisp/treesit.el (treesit-end-of-defun): Guard against nil value. 2022-12-03 14:49:37 -08:00
Eli Zaretskii
a86ccb5f9d ; Fix recently added treesit documentation
* lisp/treesit.el (treesit-defun-prefer-top-level):
* doc/lispref/positions.texi (List Motion): Fix wording.
2022-12-01 17:25:28 +02:00
Yuan Fu
01e7d4b2a1
Make treesit-defun-prefer-top-level more flexible
* doc/lispref/positions.texi (List Motion): Update manual.
* lisp/treesit.el (treesit-defun-prefer-top-level): Update docstring.
(treesit--defun-maybe-top-level): Change to accept new format.
2022-11-30 16:57:08 -08:00
Yuan Fu
d5dc1dbf7c
Remove treesit-comment-start/end and use comment-start/end-skip
treesit-comment-start/end is unnecessary because of
comment-start/end-skip, so they should be removed.

Cleanup and set comment-start/end-skip for tree-sitter C-like major
modes.

I replaced the [ \t]* part in comment-start-skip with (syntax
whitespace), which is what comment-end-skip uses.  I also added
grouping in comment-start-skip to match that of comment-end-skip.

* lisp/progmodes/c-ts-mode.el (c-ts-mode)
(c++-ts-mode)
* lisp/progmodes/csharp-mode.el (csharp-ts-mode)
* lisp/progmodes/java-ts-mode.el (java-ts-mode)
* lisp/progmodes/js.el (js-ts-mode)
* lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode): Setup
comment-start/end-skip.

* lisp/treesit.el (treesit-comment-start)
(treesit-comment-end): Remove variables.
(treesit-simple-indent-presets): Use comment-start/end-skip instead.
2022-11-27 14:20:56 -08:00
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