Commit Graph

163 Commits

Author SHA1 Message Date
Lewis Russell
46b73bf22c perf(treesitter): more efficient foldexpr 2023-03-10 11:51:33 +00:00
Lewis Russell
c5b9643bf1 fix(treesitter): better lang handling of get_parser() 2023-03-10 10:43:35 +00:00
Lewis Russell
adfa9de8eb fix(treesitter): do not error on empty filetype
Ignore instead
2023-03-10 10:41:19 +00:00
Lewis Russell
be0461e3c2
fix(treesitter): is_in_node_range (#22582)
TS ranges are end column exclusive, so fix is_in_node_range
to account for that.
2023-03-08 23:45:43 +00:00
Lewis Russell
6d4f481821
fix(treesitter): disallow empty filetypes
Fixes #22473
2023-03-03 09:44:02 +00:00
zeertzjq
fb1db80f5a
test(treesitter/parser_spec): correct time unit (#22471) 2023-03-02 14:42:15 +08:00
Lewis Russell
c57af5d41c
feat(treesitter)!: remove silent option from language.add()
Simply use `pcall` if you want to silence an error.
2023-02-24 09:50:59 +00:00
Lewis Russell
1df3f5ec6a
feat(treesitter): upstream foldexpr from nvim-treesitter 2023-02-23 17:05:20 +00:00
Lewis Russell
75e53341f3
perf(treesitter): smarter languagetree invalidation
Problem:
  Treesitter injections are slow because all injected trees are invalidated on every change.

Solution:
    Implement smarter invalidation to avoid reparsing injected regions.

    - In on_bytes, try and update self._regions as best we can. This PR just offsets any regions after the change.
    - Add valid flags for each region in self._regions.
    - Call on_bytes recursively for all children.
       - We still need to run the query every time for the top level tree. I don't know how to avoid this. However, if the new injection ranges don't change, then we re-use the old trees and avoid reparsing children.

This should result in roughly a 2-3x reduction in tree parsing when the comment injections are enabled.
2023-02-23 15:19:52 +00:00
Lewis Russell
8714a4009c
feat(treesitter): add filetype -> lang API
Problem:

  vim.treesitter does not know how to map a specific filetype to a parser.

  This creates problems since in a few places (including in vim.treesitter itself), the filetype is incorrectly used in place of lang.

Solution:

  Add an API to enable this:

  - Add vim.treesitter.language.add() as a replacement for vim.treesitter.language.require_language().
    - Optional arguments are now passed via an opts table.
    - Also takes a filetype (or list of filetypes) so we can keep track of what filetypes are associated with which langs.
    - Deprecated vim.treesitter.language.require_language().
  - Add vim.treesitter.language.get_lang() which returns the associated lang for a given filetype.
  - Add vim.treesitter.language.register() to associate filetypes to a lang without loading the parser.
2023-02-21 17:09:18 +00:00
Lewis Russell
8a985d12dd fix(treesitter): don't trample parsers when filetype!=lang
This allows vim.treesitter.show_tree() to work on buffers where the
filetype does not match the parser language name e.g, bash/sh.
2023-02-10 16:15:56 +00:00
figsoda
e1d5ad1cb8 feat(treesitter): add metadata option for get_node_text 2023-02-04 21:15:03 -05:00
figsoda
bb8845340b feat(treesitter): allow capture text to be transformed
Co-authored-by: Lewis Russell <lewis6991@gmail.com>
2023-02-04 21:04:45 -05:00
Christian Clason
c032e83b22 fix(treesitter): validate language name
Problem: Some injections (like markdown) allow specifying arbitrary
language names for code blocks, which may be lead to errors when
looking for a corresponding parser in runtime path.

Solution: Validate that the language name only contains alphanumeric
characters and `_` (e.g., for `c_sharp`) and error otherwise.
2023-01-28 11:28:52 +01:00
Matthieu Coudron
151b9fc52e
feat(treesitter): show filetype associated with parser (#17633)
to ease debug. At one point I had an empty filetype and the current message was not helpful enough
2023-01-22 16:51:17 +01:00
Folke Lemaitre
ef91146efc
feat: vim.inspect_pos, vim.show_pos, :Inspect 2022-12-17 13:05:31 +01:00
dundargoc
5eb5f49488
test: simplify platform detection (#21020)
Extend the capabilities of is_os to detect more platforms such as
freebsd and openbsd. Also remove `iswin()` helper function as it can be
replaced by `is_os("win")`.
2022-11-22 08:13:30 +08:00
dundargoc
211c568e64
test: don't skip parser_spec on windows (#20294) 2022-11-15 19:33:30 +08:00
Lewis Russell
e8cc489acc
feat(test): add Lua forms for API methods (#20152) 2022-11-14 10:01:35 +00:00
dundargoc
736c36c02f
test: introduce skip() #21010
This is essentially a convenience wrapper around the `pending()`
function, similar to `skip_fragile()` but more general-purpose.

Also remove `pending_win32` function as it can be replaced by
`skip(iswin())`.
2022-11-13 05:52:19 -08:00
Justin M. Keyes
3169fc54a1 refactor(treesitter): rename x_position => x_pos
"pos" has a long precedent as "position" in vim, and there is no reason
to use a verbose name here.
2022-09-25 13:46:15 +02:00
Christian Clason
9ec4b20be6
fix(treesitter): return full metadata for get_captures_at_position (#20203)
fix(treesitter): get_captures_at_position returns metadata

Return the full `metadata` table for the capture instead of just the
priority.

Further cleanup of related docs.
2022-09-16 09:05:05 +02:00
Christian Clason
97f38f0a9b fix(treesitter): do not link @error by default
The @error capture is used for tree-sitter's ERROR node, which indicates
a parsing error -- which can be quite frequent (and jarring) while typing.

Users can still manually `hi link @error Error` in their config.
2022-09-06 07:57:46 +02:00
Christian Clason
64cc78c9f3 feat(treesitter): add injections 2022-09-06 07:57:46 +02:00
Christian Clason
6254b0fd3b ci(tests): don't skip parsers on functionaltest
Treesitter parsers are now a mandatory part of the installation and
should be tested on all platforms. Remove `pending_c_parser` helper.
2022-09-06 07:57:46 +02:00
bfredl
b04ef7f6b9 fix(treesitter): make it get_captures_at_position 2022-08-26 13:57:31 +02:00
bfredl
030b422d1e feat(treesitter)!: use @foo.bar style highlight groups
This removes the support for defining links via
vim.treesitter.highlighter.hl_map (never documented, but plugins did
anyway), or the uppercase-only `@FooGroup.Bar` to `FooGroup` rule.

The fallback is now strictly `@foo.bar.lang` to `@foo.bar` to `@foo`,
and casing is irrelevant (as it already was outside of treesitter)

For compatibility, define default links to builting syntax groups
as defined by pre-existing color schemes
2022-08-26 13:57:31 +02:00
bfredl
73ee2b35d1 fix(tests): use pending_c_parser when needed 2022-08-25 20:49:27 +02:00
Quentin Rasmont
f57341a4b6 feat(treesitter): upstream node_length() as a node method
Util from the nvim-treesitter project.
2022-08-25 18:01:14 +02:00
Quentin Rasmont
baba43681e feat(treesitter): upstream get_root_for_node() as a node method
Util from the nvim-treesitter project.
2022-08-25 18:01:14 +02:00
Quentin Rasmont
a577fb778a feat(treesitter): upstream get_named_children() as a node method
Util from the nvim-treesitter project.
2022-08-25 18:01:14 +02:00
Quentin Rasmont
6b2d42eb03 feat(treesitter): add ability to retreive a tree/node given a range 2022-08-25 18:01:14 +02:00
Quentin Rasmont
3aba4ba378 feat(treesitter): upstream is_parent()
Util from the nvim-treesitter project.
Renamed is_parent to is_ancestor for clarity.
2022-08-25 18:01:14 +02:00
Thomas Vigouroux
26ebf67c39
test(treesitter): make internal lang test pending when necessary 2022-08-24 16:59:13 +02:00
Thomas Vigouroux
3c1d70f20b
feat(treesitter): allow customizing language symbol name 2022-08-22 15:34:10 +02:00
Justin M. Keyes
7b2b44bce4
fix(ci): noisy logs, unreliable test #19019
Problem:
1. CI logs have too many (40+) logs mentioning SIGHUP:
   ```
   WRN 2022-06-18T16:05:47.075 T3568.22499.0/c deadly_signal:177: got signal 1 (SIGHUP)
   WRN 2022-06-18T16:05:47.273 T3569.91095.0/c deadly_signal:177: got signal 1 (SIGHUP)
   WRN 2022-06-18T16:05:47.651 T3570.59545.0/c deadly_signal:177: got signal 1 (SIGHUP)
   ```
2. TS parser test still sometimes fails on BSD CI.
3. remote_spec test fails too often.

Solution:
1. Log deadly signals at INFO level. It hasn't been helpful in CI, and
   for local troubleshooting it's reasonable to adjust the loglevel as
   needed.
2. Adjust the TS parser test again. ref #18911
3. Skip the remote_spec test. The `--remote` feature was merged before
   it was fully formed and needs to be revisited.
2022-06-18 19:19:08 -07:00
Justin M. Keyes
9662cd7f48
fix(tests): unreliable parser_spec #18911
The "first run" has high variability. Looks like the test failures
correlate with 545dc82c1b
, which makes sense because that improves "first run" performance.

So the `1000*` factor of this test could be adjusted to e.g. `300*` or `500*`.

ref https://github.com/neovim/neovim/pull/16945
2022-06-09 08:07:54 -07:00
James McCoy
6b0595d7cc
test(ts): skip test if C parser is not available 2022-06-08 18:04:25 -04:00
Chinmay Dalal
116a3f4683
fix(treesitter): create new parser if language is not the same as cached parser (#18149) 2022-04-22 16:15:28 +02:00
bfredl
6eca9b69c4 feat(ui): allow conceal to be defined in decorations
Unlike syntax conceal, change highlight of concealed char

Can be used in tree-sitter using "conceal" metadata.
2022-03-20 18:02:41 +01:00
Stephan Seitz
8ab5ec4aaa
feat(tree-sitter): allow Atom-style capture fallbacks (#14196)
This allows falling back to `@definition` when we have no mapping
`@definition.fancy-specialization`.

This behavior is described in tree-sitter's documentation
(https://tree-sitter.github.io/tree-sitter/syntax-highlighting#theme).

Fixes https://github.com/nvim-treesitter/nvim-treesitter/issues/738
2022-02-16 19:38:19 +01:00
kevinhwang91
58d81efcb2 fix: append test for checking zero width node range 2022-02-11 20:59:47 +08:00
Lewis Russell
f9080b24c4
fix(ts): escape lang when loading parsers (#16668)
When trying to load a language parser, escape the value of
the language.

With language injection, the language might be picked up from the
buffer. If this value is erroneous it can cause `nvim_get_runtime_file`
to hard error.

E.g., the markdown expression `~~~{` will extract '{' as a language and
then try to get the parser using `parser/{*` as the pattern.
2022-01-27 10:42:59 +01:00
Stephan Seitz
9c26939f75 perf(treesitter): cache query parsing 2022-01-06 13:20:22 +01:00
Mathias Fußenegger
1f3c0593eb
feat(ts): add support for multiline nodes in get_node_text (#14999)
Based on https://github.com/neovim/neovim/pull/14445

This extends `vim.treesitter.query.get_node_text` to return the text
that spans a node's range even if start_row ~= end_row.
2021-12-12 12:05:39 +01:00
Christian Clason
5676edb86d fix(tests): adapt parser_spec test to tree-sitter bump
Change query to include anonymous nodes (`(_)` -> `[_]`) and
use new syntax (`{vim,lua}.match?`->`#{vim,lua}.match?`)
2021-11-23 18:38:22 +01:00
Dylan Kendal
140084180e
feat(treesitter): add next, prev sibling method
Add tsnode methods to change to the next, previous, named or unnamed
nodes.
2021-08-20 11:58:15 -04:00
Thomas Vigouroux
41912bf777
Merge pull request #15283 from jamessan/pending-c-parsers
test(treesitter): skip all parsers tests if parsers aren't installed
2021-08-06 10:53:18 +02:00
James McCoy
8336488ce1
test(treesitter): skip all parsers tests if parsers aren't installed 2021-08-05 21:41:55 -04:00
Stephan Seitz
b2a9afef6d treesitter: add query.list_directives 2021-07-24 20:57:45 +02:00
Stephan Seitz
242608e669 feat(treesitter): allow to set highlight priority for queries 2021-07-22 21:38:57 +02:00
Raymond W. Ko
7c95697026
treesitter: add predicate "any-of?" (#14344)
For the case of Clojure and other Lisp syntax highlighting, it is
necessary to create huge regexps consisting of hundreds of symbols with
the pipe (|) character. To make things more difficult, these Lisp
symbols sometimes consists of special characters that are themselves
part of special regexp characters like '*'. In addition to being
difficult to maintain, it's performance is suboptimal.

This patch introduces a new predicate to perform 'source' matching in
amortized constant time. This is accomplished by compiling a hash table
on the first use.
2021-05-14 17:41:20 +02:00
Stephan Seitz
5c9b4948d4
treesitter: do not escape in match? (#14382) 2021-05-11 11:52:47 +02:00
derekstride
44d4526e18
Ensure there is a nested table allocated for #set!
Fixes the following bug:
```
test/functional/helpers.lua:107: Error executing lua:
vim/treesitter/query.lua:256: attempt to index a nil value
```
2021-04-27 12:29:37 -02:30
Thomas Vigouroux
e652b2987a
Merge pull request #14046 from nvim-treesitter/feature/language-tree-directive-config
feat(treesitter): allow injections to be configured through directives
2021-04-14 05:36:05 +02:00
Thomas Vigouroux
e8bc3d717e
feat(ts): include parser ABI version in checkhealth 2021-04-06 19:02:58 +02:00
Steven Sojka
8bea39f372 feat(treesitter): allow injections to be configured through directives 2021-04-02 12:48:16 -05:00
TJ DeVries
d50f99f08b
fixup: remove unused test function 2021-03-31 18:09:03 +02:00
TJ DeVries
06c065469b
ts: Add per-language highlight links 2021-03-31 18:09:00 +02:00
TJ DeVries
d6209a7b84 fixup: Add a test and it is so pretty 2021-03-30 10:57:38 -04:00
“jvgrootveld”
a2818819bb treesitter: default start and end row when omitted
Add support for default start and end row when omitted in the
query:iter_captures and query:iter_matches functions.

When the start and end row values are omitted, the values of the given
node is used. The end row value is incremented by 1 to include the node end
row in the match.

Updated tests and docs accordingly.
2021-01-15 21:44:40 +01:00
Björn Linse
a58d96a7ed buffer updates: mark sorted region as changed.
NB: currently this invalidates all extmarks inside the region.
Later on within-line extmarks could be preserved
2021-01-03 14:47:37 +01:00
Björn Linse
adb01190c4 tests: make treesitter its own category 2021-01-03 14:47:29 +01:00