1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 22:41:06 -08:00
Commit graph

270 commits

Author SHA1 Message Date
Yuan Fu
d2c4b926ac
Fix treesit-default-defun-skipper (bug#66711)
* lisp/treesit.el:
(treesit-default-defun-skipper): Add bol to the rx pattern.
2023-12-10 17:40:39 -08:00
Yuan Fu
9874561f39
Fix treesit-node-field-name and friends (bug#66674)
So turns out ts_node_field_name_for_child takes a named node index,
but we were passing it normal index that counts both named and
anonymous nodes.  That's what makes the field name all wrong in
treesit explorer.

* doc/lispref/parsing.texi:
(Accessing Node Information): Update docstring.
* lisp/treesit.el (treesit-node-index): Add some unrelated comment.
(treesit-node-field-name): Get named node index rather than all node
index.
* src/treesit.c (Ftreesit_node_field_name_for_child): Update
docstring, use ts_node_named_child_count.
2023-12-10 16:28:21 -08:00
Yuan Fu
103ca678ac
Fix treesit-simple-indent-presets docstring (bug#67007)
* lisp/treesit.el (treesit-simple-indent-presets): Fix docstring.
* doc/lispref/modes.texi (Parser-based Indentation): Fix example.
2023-11-08 23:49:15 -08:00
Yuan Fu
893c344b4e
Fix the use of adaptive-fill-regexp in treesit indent preset
* lisp/treesit.el (treesit-simple-indent-presets):
adaptive-fill-regexp don't have a capture group (the group in the
default value is supposed to be a non-capture group), so don't use the
group. Also, in the second diff hunk, replace looking-at with
looking-at-p so it doesn't override match data that we use later.
2023-10-21 21:12:59 -07:00
nverno
1098c114b7
Fix treesit-install-language-grammar (bug#66673)
* lisp/treesit.el (treesit-install-language-grammar): Take out the
language symbol when storing the recipe.
2023-10-21 20:39:30 -07:00
Yuan Fu
491ee428c0
Fix treesit-explore-mode (bug#66431)
* lisp/treesit.el (treesit-explore-mode): Reset
treesit--explorer-last-node before calling treesit--explorer-refresh,
so that in the rare case described in the bug report, the explorer
buffer don't show the outdated node.
2023-10-21 20:39:30 -07:00
Yuan Fu
73ccd9d295
Don't call font-lock-mode in treesit-major-mode-setup (bug#66223)
* lisp/treesit.el (treesit-major-mode-setup): Remove.

(cherry picked from commit a3a840c80a)
2023-10-08 22:00:36 -07:00
nverno
53292c5d81
Fix treesit-query-validate for string input (bug#66400)
* lisp/treesit.el (treesit-query-validate): Don't expand if QUERY is
string.
2023-10-08 22:00:32 -07:00
Yuan Fu
247743bd1e ; * lisp/treesit.el (treesit-language-at-point-function): Fix doc.
(cherry picked from commit 07ede5e92a)
(Bug#66183)
2023-10-07 12:03:37 +03:00
Augustin Chéneau
7447d3df94
Fix tree-sitter indentation conflict with multiple languages
* lisp/treesit.el (treesit--indent-1): Use bol instead of point.

Copyright-paperwork-exempt: yes
2023-09-24 14:06:53 -07:00
Yuan Fu
5cba5ee890
Fix tree-sitter range update function
* lisp/treesit.el (treesit-update-ranges): If an embedded language
doesn't have any range, don't set its range to nil (which means whole
buffer), instead, set its range to a dummy (1 . 1) zero range.
2023-09-18 20:48:54 -07:00
Yuan Fu
458442fe78
Escape percent character in treesit--inspect-name (bug#65540)
* lisp/treesit.el (treesit-inspect-node-at-point): Escape percent.
2023-08-27 14:50:43 -07:00
Yuan Fu
2c90ade09a
Tree-sitter use with-silent-modifications like jit-lock (bug#64321)
* lisp/treesit.el (treesit--font-lock-notifier): Use
with-silent-modifications when marking modified text to be fontified
by jit-lock.  This is what jit-lock itself does.
2023-06-27 20:58:34 -07:00
Basil L. Contovounesios
2847857496 Improve tree-sitter docs
* doc/lispref/positions.texi (List Motion): Incorporate more
accurate description of treesit-defun-type-regexp from
'(elisp) Tree-sitter Major Modes', replacing that duplicate
entry (bug#64018).

* doc/lispref/parsing.texi (Parsing Program Source)
(Language Grammar, Using Parser, Retrieving Nodes)
(Accessing Node Information, Pattern Matching, Multiple Languages):
(Tree-sitter Major Modes):
* doc/lispref/modes.texi (Parser-based Font Lock): Improve wording,
grammar, punctuation, and markup.  Fix typos.
(Parser-based Indentation): Ditto.  Document indent rule presets
field-is, catch-all, nth-sibling, grand-parent, and
great-grand-parent.

* lisp/treesit.el (treesit-simple-indent-presets): Mention field-is,
catch-all, nth-sibling, grand-parent, great-grand-parent in
docstring.
(treesit-major-mode-setup, treesit-explore-mode): Improve
docstring/commentary grammar.
2023-06-13 17:22:16 +01:00
Yuan Fu
3f44152567
Save the tree-sitter grammar recipe when installing a grammar
Raised in bug#63750, but not the main subject of it.

* lisp/treesit.el (treesit-install-language-grammar): Save the recipe
to treesit-language-source-alist when installation is successful.
2023-05-28 15:34:25 -07:00
Eli Zaretskii
33485fe8f7 ; Better warning message when loading tree-sitter fails
* lisp/treesit.el (treesit-ready-p): Improve diagnostics on
MS-Windows when the tree-sitter library cannot be loaded at run
time.
2023-05-15 18:39:49 +03:00
Eli Zaretskii
910a7b30df Fix beginning/end-of-defun with tree-sitter
* lisp/treesit.el (treesit-beginning-of-defun)
(treesit-end-of-defun): Push mark, as other beginning/end-of-defun
functions do.
2023-05-07 09:13:10 +03:00
Eli Zaretskii
0cf6e0998b * Makefile.in (distclean): Remove the 'native-lisp' directory. 2023-04-27 21:39:33 +03:00
Yuan Fu
8b6a0de964
Improve docstring of treesit-parent-while (bug#62301)
* doc/lispref/parsing.texi (Retrieving Nodes): Improve and fix
docstring for treesit-parent-until and treesit-parent-while.
* lisp/treesit.el (treesit-parent-while): Improve docstring.
2023-03-21 14:51:41 -07:00
Dmitry Gutov
7a1272168a * lisp/treesit.el (treesit-end-of-defun): Guard arg against nil (bug#62158). 2023-03-20 20:18:42 +02:00
Yuan Fu
f9b7913656
Fix empty line indentation in c-ts-mode (bug#61997)
* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--indent-styles): Handle the empty line case.
* test/lisp/progmodes/c-ts-mode-resources/indent.erts: New test.

* doc/lispref/modes.texi (Parser-based Indentation): Update manual.
* lisp/treesit.el (treesit-simple-indent-presets): Support null as
a value for NODE-TYPE in the 'match' matcher.
2023-03-07 16:42:44 -08:00
Yuan Fu
90504f9d89
Fix tree-sitter indent preset prev-line (bug#61998)
* lisp/treesit.el (treesit-simple-indent-presets): Fix return value.
2023-03-07 16:27:24 -08:00
Yuan Fu
4c16fd3a51
Change tree-sitter indent anchor 'point-min' to 'column-0'
Point-min isn't necessarily at column 0, using line-beginning-position
is better. column-0 is also more intuitive.

* doc/lispref/modes.texi (Parser-based Indentation): Update manual.
* lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-styles):
* lisp/progmodes/java-ts-mode.el (java-ts-mode--indent-rules):
* lisp/progmodes/rust-ts-mode.el (rust-ts-mode--indent-rules):
* lisp/progmodes/typescript-ts-mode.el:
(typescript-ts-mode--indent-rules): Change point-min to column-0.
* lisp/treesit.el (treesit-simple-indent-presets): Change point-min to
column-0.
2023-03-04 01:20:02 -08:00
Yuan Fu
6b2720778d
Improve tree-sitter's prev-sibling indent anchor
Now it handles the case where NODE is nil when indenting an empty
line: it tries to get the previous sibling nonetheless.

* lisp/progmodes/c-ts-mode.el (c-ts-mode--anchor-prev-sibling):
* lisp/treesit.el (treesit-simple-indent-presets): Add an or form to
handle more cases.
2023-03-01 14:07:01 -08:00
Yuan Fu
4a6db125b9
Fix treesit-indent-region
Fix it for the case where there is no suitable rule for the line.
Right now treesit-indent-region would indent the line to column 0.
After the change the indentation is not altered.

* lisp/treesit.el (treesit-indent-region): Handle the case where
ANCHOR or OFFSET is nil specially.
2023-03-01 00:55:51 -08:00
Yuan Fu
aee10ca1cb
Adjust tree-sitter defun navigation (bug#61617)
Before this change, when you use a tree-sitter navigation function to
move to the next beginning of a thing, it jumps over the immediate
next thing and lands you at the beginning of the next-next thing.

Eg, when point is at the "|", and we evaluate
(treesit--navigate-thing pos 1 'beg), we go from

|  (thing) (thing)

to

   (thing) |(thing)

But some might expect point to go to

   |(thing) (thing)

instead, which makes sense.  Also, that's how Emacs expect defun
navigation functions to work.  The discrepancy in expectation causes
bug#61617.

In this change I made tree-sitter navigation functions to work as what
Emacs expects.  And what I described for moving to the next beginning
of thing is similarly applicable to moving to the end of previous end
of thing.

* lisp/treesit.el (treesit-beginning-of-defun)
(treesit-end-of-defun): Handle the case where defun-skipper moves
point back to where we started, by adding a retry.

(treesit--navigate-thing): Add a single condition checking for
progress to the condition form responsible for checking whether to
skip the next defun.  Namely (eq pos (funcall advance next)))).

* test/src/treesit-tests.el:
(treesit--ert-defun-navigation-nested-master)
(treesit--ert-defun-navigation-top-level-master): Change tests to
reflect the new expectation.
2023-02-27 00:14:32 -08:00
Yuan Fu
0f15286c53
New tree-sitter indent anchor standalone-parent used by c-ts-mode
When writing c-ts-mode Theo used parent-bol which works well except
one case:

1 for (int i=0;
2      i < 5;
3      i++) {
4   func(i);
5 }

In this case, when indenting "func(i)", parent-bol returns the start
of "i++" on line 3, instead of the "correct" anchor, the start of
"for" on line 1.  parent-bol would have worked if the "for (...) {"
part is in one line.

To support this case I tried numerous things and added a bunch of
stuff, culminating in c-ts-common-statement-offset.  It's complicated,
requires extra setup, and slow.

Not anymore! I think the new anchor standalone-parent really captures
the logic behind how people expect indentation to work. It's simple
and fast, and requires no setup.

* doc/lispref/modes.texi (Parser-based Indentation): Update manual.

* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--standalone-grandparent): New anchor.
(c-ts-mode--indent-styles): Replace c-ts-common-statement-offset with
standalone-parent.
(c-ts-base-mode): Add comment.

* lisp/treesit.el:
(treesit-simple-indent-presets): New anchor standalone-parent.
2023-02-26 21:38:22 -08:00
Daniel Martín
5cf50d6004
Check the anchor along with the offset in treesit-indent-region
* lisp/treesit.el (treesit-indent-region): To compute a valid column,
having a non-nil offset is not enough, we need a non-nil anchor as
well.
2023-02-24 21:12:47 -08:00
Yuan Fu
afbce8bb46
Improve tree-sitter indent anchor prev-adaptive-prefix (bug#61314)
Now prev-adaptive-prefix looks at the current line and checks if it
begins with a prefix itself.  If it does, prev-adaptive-prefix tries
to place the anchor before the prefix on the previous line, rather
than after it.

 - prev line
 - this line -> This line starts with a "-", i.e., begins with a
                prefix, so we place the anchor at the beginning of the
                 "-" of the previous line, rather than after it

 - prev line
   this line -> This line doesn't start with a prefix, so the anchor
                is placed after the previous line's "-".

* doc/lispref/modes.texi (Parser-based Indentation): Update manual.
* lisp/treesit.el:
(treesit-simple-indent-presets): Add local variable
this-line-has-prefix, base what anchor to return on the value of
this-line-has-prefix and whether the prev line has a prefix.
2023-02-19 12:34:19 -08:00
Eli Zaretskii
cd05fca5f7 ; Improve documentation of 'native-comp-enable-subr-trampolines'
* doc/lispref/compile.texi (Native-Compilation Variables):
Document the interpretation of non-absolute directory names that
are the value of 'native-comp-enable-subr-trampolines'.
2023-02-19 11:04:57 +02:00
Dmitry Gutov
4c49452cde (treesit-query-validate): Fix reusing the output buffer
* lisp/treesit.el (treesit-query-validate): Fix the "Buffer is
read-only" error when an output buffer already exists.
2023-02-18 23:53:11 +02:00
Dmitry Gutov
5b34fc0708 * lisp/treesit.el (treesit-node-at): Update docstring (bug#61529). 2023-02-17 17:09:29 +02:00
Aleksandr Vityazev
17ab426670 * lisp/treesit.el (treesit): Fix shortdoc example form (bug#61318). 2023-02-06 16:37:32 +02:00
Dmitry Gutov
0c125fcc67 Make highlighting more regular across TS modes (bug#61205)
* doc/emacs/display.texi (Parser-based Font Lock):
Update description of treesit-font-lock-level, moving 'property' to
level 4.

* lisp/treesit.el (treesit-font-lock-level): Likewise, in docstring.

* lisp/progmodes/c-ts-mode.el (c-ts-base-mode): Do that here.

* lisp/progmodes/cmake-ts-mode.el (cmake-ts-mode): Add a comment.

* lisp/progmodes/go-ts-mode.el (go-ts-mode): Add 'definition' to
level 1.  Move 'function', 'property' and 'variable' to level 4.
(go-ts-mode--font-lock-settings): Move a bunch of existing rules
to 'definition'.  Add highlighting of function parameters.

* lisp/progmodes/rust-ts-mode.el (rust-ts-mode)
(rust-ts-mode--font-lock-settings): Same.  And also change "scoped
identifiers" highlights to only match capitalized names.
2023-02-05 15:50:16 +02:00
Eli Zaretskii
f13479d955 Fix installation of tree-sitter grammar on MS-Windows
* lisp/treesit.el (treesit--install-language-grammar-1): Fix a
failure on MS-Windows when the old DLL is still being used.
(Bug#61289)
2023-02-05 11:14:25 +02:00
Eli Zaretskii
2c33e2889b Fix byte-compilation of *-ts-mode.el files
* lisp/treesit.el (treesit-font-lock-settings): Move to before
use, to prevent failure in byte-compiling modes which require this
file.  (Bug#61282)
2023-02-05 08:24:32 +02:00
Yuan Fu
671e5d9fad
; * lisp/treesit.el (treesit--font-lock-level-setter): Minor change. 2023-02-04 15:33:26 -08:00
Eli Zaretskii
dda4baa58b ; Fix build and startup without tree-sitter
* lisp/treesit.el (treesit--font-lock-level-setter): Don't loop
over all the buffers if tree-sitter is not built-in, or else
initialization of defcustom will fail.  (Bug#61155)
* lisp/progmodes/rust-ts-mode.el (treesit-node-parent):
* lisp/progmodes/c-ts-common.el (treesit-node-parent): Declare, to
* avoid byte-compilation warnings.
2023-01-30 14:22:19 +02:00
Eli Zaretskii
197f994384 Document tree-sitter features in the user manual
* lisp/progmodes/c-ts-mode.el (c-ts-mode-map): Bind "C-c .", for
consistency with CC mode.
* lisp/treesit.el (treesit-font-lock-level): Doc fix.

* doc/emacs/programs.texi (C Indent, Custom C Indent): Document
the indentation features of 'c-ts-mode'.
(Moving by Defuns): Document 'treesit-defun-tactic'.
* doc/emacs/files.texi (Visiting): Document
'treesit-max-buffer-size'.
* doc/emacs/display.texi (Traditional Font Lock)
(Parser-based Font Lock): New subsections.
* doc/emacs/emacs.texi (Top): Update top-level menu.
2023-01-29 15:22:20 +02:00
Yuan Fu
b73539832d
; Remove treesit--font-lock-fast-mode-grace-count
* lisp/treesit.el:
(treesit--font-lock-fast-mode-grace-count): Remove. Should've been
removed in the last change.
2023-01-29 00:30:53 -08:00
Yuan Fu
1970726e26
Use treesit-subtree-stat to determine treesit--font-lock-fast-mode
* lisp/treesit.el:
(treesit--children-covering-range-recurse): Return nil if LIMIT is
exceeded.
(treesit--font-lock-fast-mode): Change to a ternary value.
(treesit-font-lock-fontify-region): Enable fast mode based on the
result of treesit-subtree-stat.
2023-01-29 00:12:57 -08:00
Yuan Fu
382e018856
Add treesit-subtree-stat
* src/treesit.c (Ftreesit_subtree_stat): New function.
* lisp/treesit.el (treesit): Add to shortdoc.
2023-01-29 00:12:57 -08:00
Theodor Thornhill
1de6ebf287 Make treesit-font-lock-level a defcustom
* lisp/treesit.el (treesit--font-lock-level-setter): Setter for the
new defcustom.
(treesit-font-lock-level): Turn it into a defcustom.
2023-01-28 19:40:04 +01:00
Dmitry Gutov
abb3becb9f treesit-install-language-grammar: Provide default repo url
* lisp/treesit.el (treesit--check-repo-url): New function.
(treesit--install-language-grammar-build-recipe): Use it (bug#61051).
2023-01-25 17:22:31 +02:00
Yuan Fu
7ca71d66dc
Fix various problems in treesit-explore-mode (bug#60800)
* lisp/treesit.el:
(treesit--explorer-kill-explorer-buffer): New function.
(treesit-explore-mode):
1. Move prompt for language earlier, and terminate early if language
not available.
2. Make sure desktop-save doesn't save the explorer buffer.
3. Kill the explorer buffer when the source buffer is killed.
2023-01-19 14:47:25 -08:00
Yuan Fu
b7d6bb47ee
; * lisp/treesit.el (treesit-font-lock-fontify-region): Minor fix.
The check for treesit--font-lock-fast-mode is not really necessary,
but anyway.
2023-01-19 14:47:25 -08:00
Dmitry Gutov
fb82d4e328 (treesit-simple-indent-presets): Have n-p-gp check for grandparent's presence
* lisp/treesit.el (treesit-simple-indent-presets): Have n-p-gp
check for grandparent's presence before checking its type.
2023-01-19 19:44:10 +02:00
Yuan Fu
faee7e1f1b
; * lisp/treesit.el (treesit-font-lock-fontify-region): Minor fix. 2023-01-17 22:52:22 -08:00
Yuan Fu
ac3bc775b6
Make it harder to misactivate tree-sitter font-lock fast mode
This has been brought up in bug#60691 and bug#60223.  I proposed a fix
by testing the size of the tree rather than measuring the query time.
But after some thought, I fear that just looking at the size will give
us false-negatives.  So I kept the time-based activation, just added a
grace count to reduce false-positives.

* lisp/treesit.el:
(treesit--font-lock-fast-mode-grace-count): New variable.
(treesit--font-lock-notifier): Only activate fast mode after 5
offenses.
2023-01-17 22:32:40 -08:00
Yuan Fu
9e7a5d58ee
; Fix tree-sitter indent anchor preset
* lisp/treesit.el:
(treesit-simple-indent-presets): Fix prev-adaptive-prefix.
2023-01-17 01:20:51 -08:00