-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document new treesitter queries extension point
- Loading branch information
1 parent
ae47cfb
commit d128eb2
Showing
4 changed files
with
171 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Language Queries | ||
|
||
Nvim-paredit works by executing treesitter queries over the nodes it is operating over and extracting known capture | ||
groups. It can then use these capture groups to understand the AST in a grammar agnostic manner. | ||
|
||
There are two query files that nvim-paredit reads: | ||
|
||
### - `queries/<language>/paredit/forms.scm` | ||
|
||
The `forms.scm` is always executed and is used for collecting general structural information about nodes being operated | ||
on. | ||
|
||
It should contain the following captures | ||
|
||
#### `@form` | ||
|
||
This should be appended to any node that can be considered a 'form'. A form would be defined as an s-exp that contains | ||
child nodes. An example from Clojure would be: | ||
|
||
```clojure | ||
{} ;; a map | ||
[] ;; a vector | ||
() ;; a list | ||
#{} ;; a set | ||
;; etc | ||
``` | ||
|
||
#### `@comment` | ||
|
||
This should be appended to any nodes that are considered comments. In general nvim-paredit operations explicitly skip | ||
over or ignore comment nodes. | ||
|
||
--- | ||
|
||
Here is an example from Clojure: | ||
|
||
```scm | ||
;; queries/clojure/paredit/forms.scm | ||
(list_lit) @form | ||
(map_lit) @form | ||
(comment) @comment | ||
``` | ||
|
||
See the [source file](../queries/clojure/paredit/forms.scm) for a full example. | ||
|
||
--- | ||
|
||
### - `queries/<language>/paredit/pairs.scm` | ||
|
||
The `pairs.scm` is only executed when performing pairwise dragging and is used to determine which elements within a form | ||
are 'paired'. | ||
|
||
It should contain the following captures: | ||
|
||
#### `@pair` | ||
|
||
This should be appended to elements within a form that are considered 'paired'. These elements are used to identify | ||
pairs when performing [pairwise dragging](../README.md#pairwise-dragging). | ||
|
||
--- | ||
|
||
Here is an example from Clojure: | ||
|
||
```scm | ||
;; queries/clojure/paredit/pairs.scm | ||
(list_lit | ||
(sym_lit) @fn-name | ||
(vec_lit | ||
(_) @pair) | ||
(#any-of? @fn-name "let" "loop" "binding" "with-open" "with-redefs")) | ||
``` | ||
|
||
See the [source file](../queries/clojure/paredit/pairs.scm) for a full example. | ||
|
||
--- | ||
|
||
## Developing | ||
|
||
These query files described above can be added locally to your neovim config queries directory, to a queries directory | ||
in a new plugin, or within the queries directory of nvim-paredit. | ||
|
||
If you add these query files to your own neovim config and want to extend or modify the existing queries for a supported | ||
language make sure to add the `;; extends` directive to the top of the file. If you don't specify this then any queries | ||
defined by nvim-paredit will be overridden. | ||
|
||
Only the `forms.scm` queries file is required for adding language support. The `pairs.scm` file is only needed to | ||
augment the implementation with [pairwise dragging](../README.md#pairwise-dragging). If this query file cannot be found | ||
then the default dragging behaviour will work just fine. | ||
|
||
> [!NOTE] | ||
> | ||
> If you add queries for a new language that doesn't already have built-in support in nvim-paredit you need to also | ||
> configure the `filetypes` table in nvim-paredit during setup. | ||
> | ||
> If you don't then paredit won't run on buffers not included in the `filetypes` table! | ||
```lua | ||
local paredit = require("nvim-paredit") | ||
paredit.setup({ | ||
filetypes = { "clojure", ..., "<your-new-language>" } | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters