Commit Graph

412 Commits

Author SHA1 Message Date
3b6084ddf4 fix: type fixes
Type fixes caught by emmylua
2025-06-06 15:36:48 +01:00
744674900f refactor(treesitter): avoid unnecessarily copying tree
node:tree() now already copies the tree before returning it, for memory
safety reasons.
2025-06-06 15:35:52 +01:00
533cc0ab35 fix(vim.diagnostic): improve typing
Problem:

`vim.diagnostic.set()` doesn't actually accept a list of
`vim.Diagnostic` as internally `vim.diagnostic.set()` normalizes the
diagnostics and this normalization is assumed throughout the module.

Solution:

- Add a new type `vim.Diagnostic.Set` which is the input to `vim.diagnostic.set()`

- `col` is now an optional field and defaults to `0` to be consistent
  with `vim.diagnostic.match()`.

- Change `table.insert(t, x)` to `table[#table + 1] = x` for improved
  type checking.
2025-06-03 20:25:07 +01:00
f82219c490 fix(treesitter): parser metadata annotations
Problem: `TSLangInfo` annotation does not reflect the structure returned
by `vim.treesitter.language.inspect()`.

Solution: Move version information under new (optional since ABI 15 only)
`TSLangMetadata` field.
2025-05-29 16:57:51 +02:00
d22fcf2917 fix(gO): use non-breaking space #34197 2025-05-27 05:38:45 -07:00
baabc35987 fix(treesitter): properly clip nested injections 2025-05-24 11:51:11 +01:00
0412527a40 feat(outline): smaller indentation #34005 2025-05-22 02:21:34 -07:00
de45b8e275 fix(treesitter): proper tree contains() logic with combined injections
**Problem:** `LanguageTree:contains()` considers any range within the
start of the first tree and end of the last tree as "within" the
language tree. In the case of combined injections, this is problematic
because we only want to consider ranges within any of the combined trees
as "contained" (as opposed to any range within the entire range spanned
by all combined trees).

**Solution:** Use a more discriminative check in
`LanguageTree:contains()`.
2025-05-11 08:04:57 +01:00
bee45fc0e7 refactor(docs): remove unnecessary @private/@nodoc annotations (#33951)
* refactor(docs): remove `@private` annotations from local functions

* refactor(docs): remove unnecessary `@nodoc` annotations
2025-05-10 14:42:48 -07:00
2aa7948266 fix(treesitter): invalidate conceal_lines cache earlier #33875
Problem:  conceal_lines cache is invalidated in `on_buf`
          which is too late for code calculating text height after a
          buffer change but before a redraw (like `lsp/util.lua`).

Solution: Replace `on_buf` with `on_bytes` handler that invalidates
          the cache and clears the marks.
2025-05-06 18:03:54 -07:00
8d75910ef9 fix(treesitter): eliminate flicker for single windows #33842
This commit will eliminate flicker while editing one open window. It
works by querying previously calculated trees for highlight marks, in
the case that we are still asynchronously parsing in `on_win`.

It will not fully solve the case of flicker when there are multiple open
windows, since the parser will drop previously parsed injection trees
whenever it receives a call to parse a different region of the buffer.
This will require a refactor of `languagetree.lua`.
2025-05-05 19:05:16 -07:00
9274532615 fix(treesitter): invalidate conceal_lines marks #33828
Problem:  Spliced conceal_lines marks after changing the buffer text are
          left valid, concealing lines that shouldn't be.

Solution: Set the `invalidate` extmark property.
2025-05-03 22:37:02 +00:00
f048298e9a fix(runtime): conceal paths in help, man ToC loclist #33764
Problem:
The check for concealing paths in TOCs in the qf syntax file fails
because the TOC tile has changed.

Solution:
Force the qf syntax file to be reloaded after the qf_toc variable
has been set, so that the it can apply the correct settings.

Using the explicit qf_toc key, already used in the syntax file, instead
of the title is less prone to breaking.

It was also already being set for man pages but it had no effect because
the syntax file had already been loaded when the variable was set.

Fixes #33733
2025-05-03 07:06:22 -07:00
5a2edc483d fix(treesitter): close :InspectTree with q
Problem: `:InspectTree` window does not follow precedent for focused
"info windows" (like `checkhealth`, `Man`, etc.).

Solution: Map `q` to `<C-w>c`.
2025-05-02 21:39:11 +02:00
284b0e4fa2 fix(treesitter): fix :InspectTree incorrect injections 2025-04-16 15:03:28 +01:00
0977f70f4d fix(treesitter): injected lang ranges may cross capture boundaries #32549
Problem:
treesitter injected language ranges sometimes cross over the capture
boundaries when `@combined`.

Solution:
Clip child regions to not spill out of parent regions within
languagetree.lua, and only apply highlights within those regions in
highlighter.lua.


Co-authored-by: Cormac Relf <web@cormacrelf.net>
2025-04-13 14:22:17 -07:00
cf59631f65 fix(treesitter): not refreshing virtualtext contents #33361
Problem: In some cases, when treesitter is enabled, deleting a
line below virtualtext will not refresh all updated lines.
https://github.com/neovim/neovim/issues/33358

Solution: Revert a part of https://github.com/neovim/neovim/pull/31324
to ensure that the full range (with virtual lines) is refreshed.
2025-04-07 03:56:40 -07:00
28eaec5e15 fix(treesitter): don't memoize modified headings (#33186)
Problem: repeated gO in markdown etc. adds extra toc indentation

Solution: don't memoize heading table which gets modified
2025-03-31 11:22:57 +02:00
f4fc769c81 refactor(treesitter): migrate to ts parser callback API #33141
Remove the `set_timeout` functions for `TSParser` and instead add a timeout
parameter to the regular parse function. Remove these deprecated tree-sitter
API functions and replace them with the preferred `TSParseOptions` style.
2025-03-29 10:57:22 -07:00
75cbd9a8ae refactor(treesitter): simplify injection retrieval #33104
Simplify the logic for retrieving the injection ranges for the language
tree. The trees are now also sorted by starting position, regardless of
whether they are part of a combined injection or not. This would be
helpful if ranges are ever to be stored in an interval tree or other
kind of sorted tree structure.
2025-03-28 04:38:47 -07:00
fa85543e3b docs(treesitter): document TSNode:named_children()
This commit also gives an internal documentation description for
`TSNode:__has_ancestor()`.
2025-03-21 09:01:47 +01:00
74fcc9452c fix(runtime): gO always says "Help TOC" #32971
Problem:
gO always says "Help TOC".

Solution:
Use a generic title instead.
2025-03-19 04:17:00 -07:00
44ccd9ca24 feat(treesitter): allow iter_captures to accept opts
This matches the `iter_captures` functionality to the `iter_matches`
functionality, allowing it to specify a match limit and start depth for
the query iterator.
2025-03-12 09:41:40 +01:00
8b5a0a00c8 feat(treesitter): allow disabling captures and patterns on TSQuery (#32790)
Problem: Cannot disable individual captures and patterns in treesitter queries.

Solution: 
* Expose the corresponding tree-sitter API functions for `TSQuery` object. 
* Add documentation for `TSQuery`.
* Return the pattern ID from `get_captures_at_pos()` (and hence `:Inspect!`).
2025-03-11 14:45:01 +01:00
0829e7575d fix: warn when :InspectTree on buffer with no parser #32783
Problem: `InspectTree` error on buffer without ts parser attached.

Solution: show a more correct warning.
2025-03-11 06:15:22 -07:00
e00d67ef31 feat(checkhealth): group parsers by name and path in output
Problem:
  :checkhealth vim.treesitter sorts parser entries solely by path,
  splitting duplicates and reducing clarity.

Solution:
  Sort entries first by name, then by path, so that duplicates are grouped.
2025-03-10 21:15:19 +01:00
65c7033cbe feat(comment): allow commentstring to be determined from node metadata
**Problem:** Some weird languages have different comment syntax
depending on the location in the code, and we do not have a way to
determine the correct `commentstring` for these special cases.

**Solution:** Allow queries to specify `commentstring` values in
metadata, allowing users/`nvim-treesitter` to provide a better
commenting experience without hugely increasing the scope of the code in
core.
2025-03-02 18:38:13 +01:00
ec8922978e feat(treesitter): add more metadata to language.inspect() (#32657)
Problem: No way to check the version of a treesitter parser.

Solution: Add version metadata (ABI 15 parsers only) as well as parser state count and supertype information (ABI 15) in `vim.treesitter.language.inspect()`. Also graduate the `abi_version` field, as this is now the official upstream name.

---------

Co-authored-by: Christian Clason <c.clason@uni-graz.at>
2025-03-01 15:51:09 +00:00
86046c5a31 fix(marks): ineffective conceal_line callback optimization (#32662)
Problem:  _on_conceal_line callbacks are not invoked if callback has not
          let Nvim know it wants to receive them. But this may change on
          factors other than what is currently checked (changed buffer).
Solution: Forego this optimization, callback is still guarded behind
          'conceallevel'.
2025-02-28 13:36:25 +01:00
9b25c68db2 fix(treesitter): correctly parse queries with combined injections 2025-02-28 09:33:02 +01:00
c3337e357a fix(treesitter): nil check query for has_conceal_line 2025-02-25 16:21:16 +01:00
8ba047e33f feat(treesitter): vertical conceal support for highlighter
TSHighlighter now places marks for conceal_lines metadata. A new
internal decor provider callback _on_conceal_line was added that
instructs the highlighter to place conceal_lines marks whenever the
editor needs to know whether a line is concealed. The bundled markdown
queries use conceal_lines metadata to conceal code block fence lines.
2025-02-25 13:09:01 +01:00
0c9c140f91 refactor(treesitter): simplify parsing coroutine logic
Lua coroutines can yield across non-coroutine function boundaries,
meaning that we don't need to wrap each helper function in a coroutine
and resume it within `_parse()`. If we just have them yield when
appropriate, this will be caught by the top level `_parse()` coroutine,
and resuming the `_parse()` will resume from the position in the helper
function where we yielded last.
2025-02-23 17:12:58 +01:00
2e5b560482 feat(treesitter): table of contents for checkhealth, markdown (#32282)
Problem: It's difficult to navigate large structured text files (vim
help, checkhealth, Markdown).

Solution: Support `gO` for table of contents and `]]`/`[[` for moving
between headings for all these filetypes using treesitter queries.

Refactor: colorization of highlight groups is moved to the `help` ftplugin
while headings-related functionality is implemented in a private
`vim.treesitter` module for possible future use for other filetypes.
2025-02-22 13:07:21 +01:00
cbad2c6628 perf(treesitter): don't block when finding injection ranges
**Problem:** Currently, parsing is asynchronous, but it involves a
(sometimes lengthy) step which finds all injection ranges for a tree by
iterating over that language's injection queries. This causes edits in
large files to be extremely slow, and also causes a long stutter during
the initial parse of a large file.

**Solution:** Break up the injection query iteration over multiple event
loop iterations.
2025-02-21 18:10:24 +01:00
55b165ac15 fix(treesitter): TSNode:field() returns all children with the given field 2025-02-21 09:47:02 +00:00
562056c875 perf(treesitter): only search for injections within the parse range
Co-authored-by: Jaehwang Jung <tomtomjhj@gmail.com>
2025-02-21 09:56:21 +01:00
b0bbe25c48 fix(treesitter): don't spam query errors in the highlighter
**Problem:** An erroneous query in the treesitter highlighter gives a
deluge of errors that makes the editor almost unusable.

**Solution:** Detach the highlighter after an error is detected, so that
it only gets displayed once (per highlighter instance).
2025-02-19 19:13:16 +01:00
bc1018a8d3 fix(treesitter): avoid computing fold levels for empty buffer
Problem:  Computing fold levels for an empty buffer (somehow) breaks the
          parser state, resulting in a broken highlighter and foldexpr.
          Cached foldexpr parser is invalid after filetype has changed.
Solution: Avoid computing fold levels for empty buffer.
          Clear cached foldinfos upon `FileType`.
2025-02-19 19:11:55 +01:00
1827ab7a1f fix(treesitter): separately track the number of valid regions
We need to add a separate variable to keep track of this information,
since we cannot read the length of the valid regions table itself, since
it has holes.
2025-02-17 15:13:26 +00:00
3abfaafad2 fix(treesitter): detect trees with outdated regions in is_valid() 2025-02-11 09:17:08 +00:00
09f9f0a946 feat(treesitter): show which nodes are missing in InspectTree
Now `:InspectTree` will show missing nodes as e.g. `(MISSING identifier)`
or `(MISSING ";")` rather than just `(identifier)` or `";"`. This is
doable because the `MISSING` keyword is now valid query syntax.

Co-authored-by: Christian Clason <c.clason@uni-graz.at>
2025-02-05 09:29:31 +01:00
8543aa406c feat(treesitter): allow LanguageTree:is_valid() to accept a range
When given, only that range will be checked for validity rather than the
entire tree. This is used in the highlighter to save CPU cycles since we
only need to parse a certain region at a time anyway.
2025-02-02 12:13:25 -08:00
9508d6a814 refactor(treesitter): use coroutines for resuming _parse() logic
This means that all work previously done by a `_parse()` iteration will
be kept in future iterations. This prevents it from running indefinitely
in some cases where the file is very large and there are 2+ injections.
2025-02-02 11:51:24 -08:00
77be44563a refactor(treesitter): always return valid range from parse() #32273
Problem:
When running an initial parse, parse() returns an empty table rather
than an actual range. In `languagetree.lua`, we manually check if
a parse was incremental to determine the changed parse region.

Solution:
- Always return a range (in the C side) from parse().
- Simplify the language tree code a bit.
- Logger no longer shows empty ranges on the initial parse.
2025-02-02 03:46:26 -08:00
02ea0e77a1 refactor(treesitter): drop LanguageTree._has_regions #32274
This simplifies some logic in `languagetree.lua`, removing the need for
`_has_regions`, and removing side effects in `:included_regions()`.

Before:
- Edit is made which sets `_regions = nil`
- Upon the next call to `included_regions()` (usually right after we
  marked `_regions` as `nil` due to an `_iter_regions()` call), if
  `_regions` is nil, we repopulate the table (as long as the tree
  actually has regions)

After:
- Edit is made which resets `_regions` if it exists
- `included_regions()` no longer needs to perform this logic itself, and
  also no longer needs to read a `_has_regions` variable
2025-02-02 03:42:47 -08:00
096ae3bfd7 fix(treesitter): nil access when running string parser async 2025-02-01 17:02:52 +01:00
e71d2c817d docs: misc
Co-authored-by: Dustin S. <dstackmasta27@gmail.com>
Co-authored-by: Ferenc Fejes <fejes@inf.elte.hu>
Co-authored-by: Maria José Solano <majosolano99@gmail.com>
Co-authored-by: Yochem van Rosmalen <git@yochem.nl>
Co-authored-by: brianhuster <phambinhanctb2004@gmail.com>
Co-authored-by: zeertzjq <zeertzjq@outlook.com>
2025-01-30 13:46:06 +01:00
e7ebc5c13d fix(treesitter): stop async parsing if buffer is invalid
Problem: Error occurs if delete buffer in the middle of parsing.
Solution: Check if buffer is valid in parsing.
2025-01-29 09:15:13 +01:00
19f00bf32c fix(treesitter) Set modeline=false in TSHighlighter:destroy (#32234)
Problem:  `TSHighlighter:destroy()` causes double-processing of the modeline
and failure of `b:undo_ftplugin`.

Solution: Disable modeline in `TSHighlighter:destroy()` by setting `modeline=false`
if executing `syntaxset` autocommands for the `FileType` event.

Co-authored-by: Daniel Petrovic <daniel.petrovic@ebcont.com>
2025-01-29 09:02:49 +01:00