1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00
Commit graph

324 commits

Author SHA1 Message Date
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
Eli Zaretskii
d4e8c90b66 ; Minor fixes to documentation
* lisp/treesit.el (treesit-ready-p, treesit-inspect-mode)
(treesit-range-settings, treesit-range-rules)
(treesit--merge-ranges, treesit-update-ranges)
(treesit-font-lock-rules, treesit-font-lock-feature-list)
(treesit-font-lock-contextual-post-process)
(treesit-font-lock-recompute-features): Doc fixes.
* lisp/progmodes/js.el (js--fontify-template-string): Doc fix.

* doc/lispref/modes.texi (Parser-based Indentation):
* doc/lispref/positions.texi (List Motion):
* doc/lispref/parsing.texi (Tree-sitter major modes):
* doc/lispref/parsing.texi (Multiple Languages): Fix wording and
markup.
2022-11-05 10:49:46 +02:00
Yuan Fu
951dd2a4d9
; * lisp/treesit.el (treesit-inspect-node-at-point): Minor fix. 2022-11-04 10:50:55 -07:00
Yuan Fu
c924b6afb6
; * lisp/treesit.el (treesit-ready-p): Fix typo. 2022-11-04 10:16:33 -07:00
Yuan Fu
ce9e720413
; Minor improvement in treesit-inspect-mode
* lisp/treesit.el (treesit-inspect-node-at-point): Show the current
node in bold.
(treesit-inspect-mode): Update docstring.
2022-11-04 10:15:42 -07:00
Yuan Fu
e0e3299c01
; * lisp/treesit.el (treesit-update-ranges): Update all parsers. 2022-11-04 10:03:17 -07:00
Yuan Fu
784f53a215
; Replace error with signal in treesit.el
(or (treesit-parser-create language)
    (error "Cannot find a parser for %s" language))

is turned into

(treesit-parser-create language)

because treesit-parser-create never returns nil.  (We used to use
treesit-get-parser which don't create parser automatically.)

* lisp/treesit.el (treesit-buffer-root-node): Replace error with signal.
2022-11-04 10:01:51 -07:00
Yuan Fu
673a55780d
; Remove unused tree-sitter functions
They didn't turn out to be very useful.

* lisp/treesit.el (treesit-set-ranges)
(treesit-get-ranges): Remove functions.
2022-11-04 09:57:32 -07:00
Yuan Fu
8ad4c4009e
Add treesit-indent-error
* lisp/treesit.el (treesit-indent-error): New error.
(treesit--simple-indent-eval): Signal treesit-indent-error.
2022-11-04 09:57:11 -07:00
Yuan Fu
d9d66764e2
Revise tree-sitter facility for multi-language buffers
* doc/lispref/parsing.texi (Multiple Languages): Extend and update
manual.
* lisp/treesit.el (treesit-range-functions): Remove variable.
(treesit-range-settings): New variable.
(treesit-range-rules): New function.
(treesit--merge-ranges): New function.
(treesit-update-ranges): Use treesit-range-settings instead of
treesit-range-functions.
(treesit-font-lock-rules): Fix docstring.
(treesit-indent)
(treesit-indent-region): Only update ranges in a region.
* test/src/treesit-tests.el (treesit-range): New test.
2022-11-04 09:39:57 -07:00
Yuan Fu
b028d4f7df
; Tree-sitter: Don't refontify if entity contained in the region
* lisp/treesit.el (treesit-font-lock-contextual-post-process): Only
refontify when NODE-START < region START.
2022-11-03 19:37:55 -07:00
Yuan Fu
69ab588bb5
Pass region start and end to tree-sitter fontification functions
* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.
* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string): Update
function to only fontify within the region.
* lisp/treesit.el (treesit-font-lock-rules): Update docstring.
(treesit-font-lock-fontify-region): Pass START and END to
fontification functions.
2022-11-02 17:03:03 -07:00
Yuan Fu
f331be1f07
Add handling of contextual entities in tree-sitter font-lock
* lisp/progmodes/python.el: Remove function.
(python--treesit-settings): Capture contextual node.
* lisp/treesit.el (treesit--set-nonsticky):
(treesit-font-lock-contextual-post-process): New functions.
(treesit-font-lock-fontify-region): Change local variable START and
END to NODE-START and NODE-END, handle special capture name
"contextual".
* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.
2022-11-02 16:47:53 -07:00
Yuan Fu
040991a469
Change signature of tree-sitter font-lock functions
Change from

(START END NODE OVERRIDE &rest _)

to

(NODE OVERRIDE &rest _)

START and END aren't used frequently enough to justify.  If a
fontification function needs them, it can get them from NODE.

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

* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string)
(python--treesit-fontify-string-end): Change signature.

* lisp/treesit.el (treesit-font-lock-rules): Update docstring.
(treesit-font-lock-fontify-region): Remove START and END arguments.
2022-11-02 11:26:38 -07:00
Yuan Fu
6d5b34d9de
; Tree-sitter font-lock debugging: print node type not language
* lisp/treesit.el (treesit-font-lock-fontify-region): Print node type.
2022-11-01 12:54:55 -07:00
Yuan Fu
f6e92035a7
Allow user to add/remove tree-sitter font-lock features
* lisp/treesit.el (treesit-font-lock-recompute-features): Add
ADD-LIST, REMOVE-LIST argument.
2022-11-01 12:54:55 -07:00
Yuan Fu
a3651d3237
Query on the root node in tree-sitter font-lock
Rather than querying on the smallest node that spans START to END, we
query on the root node between START to END.

* lisp/progmodes/python.el (python--treesit-fontify-string): Accept
the string rather than the quote node.
(python--treesit-settings): Capture the string rather than the quote
node.
* lisp/treesit.el (treesit-font-lock-fontify-region): Query the root
node rather than the smallest node.
2022-11-01 12:54:51 -07:00
Yuan Fu
eeeae5e9ee
Add an argument OVERRIDE to tree-sitter font-lock functions
* doc/lispref/modes.texi (Parser-based Font Lock): Reflect this change
in manual.
* lisp/progmodes/js.el (js--fontify-template-string): Add _OVERRIDE
argument.
* lisp/progmodes/python.el (python--treesit-fontify-string): Add
_OVERRIDE argument.
* lisp/treesit.el (treesit-font-lock-rules): Update docstring.
(treesit-fontify-with-override): New function.
(treesit-font-lock-fontify-region): Extract out into
treesit-fontify-with-override.
2022-10-31 15:03:49 -07:00
Yuan Fu
9fab83ed7a
; * lisp/treesit.el (tresit--font-lock-verbose): Fix typo. 2022-10-31 14:26:03 -07:00
Yuan Fu
18947103fa
Use tree-sitter for font-lock's syntactic function (optionally)
Before this change, we rebind font-lock-fontify-region-function and
call font-lock's function in tree-sitter's function, and the order of
fontification is tree-sitter, font-lock syntax, font-lock regexp.

Now we make font-lock's syntax function customizable and replace it
with tree-sitter's function, and the order of fontification is
tree-sitter, font-lock regexp.

* doc/lispref/modes.texi (Parser-based Font Lock): Reflect the change
in manual.
* lisp/font-lock.el (font-lock-fontify-syntactically-function): New
varaible.
(font-lock-default-fontify-region): Call
font-lock-fontify-syntactically-function rather.
(font-lock-fontify-syntactically-region): Rename to
font-lock-default-fontify-syntactically
(font-lock-default-fontify-syntactically): Rename to this.

* lisp/treesit.el (treesit-font-lock-fontify-region): Don't call
font-lock functions.
(treesit-font-lock-enable): Remove this function.  It is not used even
before this change.
(treesit-major-mode-setup): Instead of binding
font-lock-fontify-region-function, now bind to
font-lock-fontify-syntactically-function.  And we can let font-lock do
it's thing.
2022-10-31 00:23:31 -07:00
Yuan Fu
377ee8158b
Add tresit--font-lock-verbose
* lisp/treesit.el (tresit--font-lock-verbose): New variable.
(treesit-font-lock-fontify-region): Take tresit--font-lock-verbose
into account.  Return jit-lock-bounds.
2022-10-30 20:56:21 -07:00
Yuan Fu
52c8fdde16
Set jit-lock-contextually in treesit-major-mode-setup
* lisp/treesit.el (treesit-major-mode-setup): Set jit-lock-contextually.
2022-10-30 20:53:44 -07:00
Yuan Fu
f6483526cd
Fix byte-compile warning in treesit.el
Originally treesit-simple-indent-presets was

'((parent-is . (lambda (type)
                 `(lambda (n p b)
                    (equal (treesit-node-type p)
                           ,type)))))

Then I changed it to

`((parent-is . ,(byte-compile
                 (lambda (type)
                   (lambda (n p b)
                     (equal (treesit-node-type p)
                            type))))))

but that generates "function already compiled" warning when
byte-compiling treesit.el.  The latest form shouldn't have any problem
anymore.

* lisp/treesit.el (treesit-simple-indent-presets): Change byte-compile
to plain evaluation.
2022-10-29 14:50:52 -07:00
Yuan Fu
f6e3de3f63
Not return noindent in treesit-indent
* lisp/treesit.el (treesit-indent): Don't return noindent.
(treesit-defun-type-regexp): Return (nil . nil) rather than nil.
2022-10-29 14:49:31 -07:00
Yuan Fu
da87895df2
Add treesit-node-top-level
Merge treesit-node-top-level-p and treesit--find-top-level-match into
treesit-node-top-level.

* doc/lispref/parsing.texi (Retrieving Node): Add manual entry for it.
* lisp/progmodes/js.el (js--treesit-imenu-1): Replace
treesit-node-top-level-p with treesit-node-top-level.
* lisp/treesit.el (treesit-node-top-level-p)
(treesit--find-top-level-match): Remove functions.
(treesit-node-top-level): New function.
* lisp/treesit.el (treesit-beginning-of-defun)
(treesit-end-of-defun): Replace treesit--find-top-level-match with
treesit-node-top-level.
2022-10-29 14:47:05 -07:00
Yuan Fu
3a784d1321
Fix parent-bol preset for tree-sitter indent
* lisp/treesit.el (treesit-simple-indent-presets): Return point rather
than column.
2022-10-29 13:05:52 -07:00
Yuan Fu
a915d836fc
Keep cursor position in treesit-indent
* lisp/treesit.el (treesit-indent): Create a marker and jump back to
that marker after indentation.
2022-10-29 11:59:45 -07:00
Yuan Fu
ce5ae0117e
Refine handling of node at point in treesit-indent
* doc/lispref/modes.texi (Parser-based Indentation): Document the
semantic clearly.
* lisp/treesit.el (treesit-simple-indent-presets): Replace with
current-indentation.
(treesit--indent-1): Now NODE always starts at BOL, and parent is
always the smallest node above NODE that spans BOL.
(treesit-indent): Don't use save-excurtion, no one else does.
2022-10-29 09:43:50 -07:00
Yuan Fu
bde8e87cb7
Fix treesit-indent-region
* lisp/treesit.el (treesit-simple-indent-presets): Remove extra comma.
(treesit--indent-1): Return (ANCHOR . OFFSET) instead of column.
(treesit-indent): Accept (ANCHOR . OFFSET) from treesit--indent-1.
(treesit--indent-region-batch-size): Reduce to 400.
(treesit-indent-region): Put a marker on each line's ANCHOR and
compute the indentation with ANCHOR + OFFSET.  Precomputing column
gives wrong indentation when the ANHOR line doesn't have correct
indent yet.
2022-10-29 09:09:09 -07:00
Yuan Fu
f46eb3d3ca
Byte-compile treesit-simple-indent-presets
* lisp/treesit.el (treesit-simple-indent-presets): Byte-compile these
functions.
2022-10-28 17:56:05 -07:00