Add textobjects queries (#20924)
Co-Authored-By: Max <max@zed.dev> Release Notes: - vim: Added motions `[[`, `[]`, `]]`, `][` for navigating by section, `[m`, `]m`, `[M`, `]M` for navigating by method, and `[*`, `]*`, `[/`, `]/` for comments. These currently only work for languages built in to Zed, as they are powered by new tree-sitter queries. - vim: Added new text objects: `ic`, `ac` for inside/around classes, `if`,`af` for functions/methods, and `g c` for comments. These currently only work for languages built in to Zed, as they are powered by new tree-sitter queries. --------- Co-authored-by: Max <max@zed.dev>
This commit is contained in:
parent
c443307c19
commit
75c9dc179b
28 changed files with 1205 additions and 26 deletions
|
@ -69,6 +69,7 @@ several features:
|
|||
- Syntax overrides
|
||||
- Text redactions
|
||||
- Runnable code detection
|
||||
- Selecting classes, functions, etc.
|
||||
|
||||
The following sections elaborate on how [Tree-sitter queries](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax) enable these
|
||||
features in Zed, using [JSON syntax](https://www.json.org/json-en.html) as a guiding example.
|
||||
|
@ -259,6 +260,44 @@ For example, in JavaScript, we also disable auto-closing of single quotes within
|
|||
(comment) @comment.inclusive
|
||||
```
|
||||
|
||||
### Text objects
|
||||
|
||||
The `textobjects.scm` file defines rules for navigating by text objects. This was added in Zed v0.165 and is currently used only in Vim mode.
|
||||
|
||||
Vim provides two levels of granularity for navigating around files. Section-by-section with `[]` etc., and method-by-method with `]m` etc. Even languages that don't support functions and classes can work well by defining similar concepts. For example CSS defines a rule-set as a method, and a media-query as a class.
|
||||
|
||||
For languages with closures, these typically should not count as functions in Zed. This is best-effort however, as languages like Javascript do not syntactically differentiate syntactically between closures and top-level function declarations.
|
||||
|
||||
For languages with declarations like C, provide queries that match `@class.around` or `@function.around`. The `if` and `ic` text objects will default to these if there is no inside.
|
||||
|
||||
If you are not sure what to put in textobjects.scm, both [nvim-treesitter-textobjects](https://github.com/nvim-treesitter/nvim-treesitter-textobjects), and the [Helix editor](https://github.com/helix-editor/helix) have queries for many languages. You can refer to the Zed [built-in languages](https://github.com/zed-industries/zed/tree/main/crates/languages/src) to see how to adapt these.
|
||||
|
||||
| Capture | Description | Vim mode |
|
||||
| ---------------- | ----------------------------------------------------------------------- | ------------------------------------------------ |
|
||||
| @function.around | An entire function definition or equivalent small section of a file. | `[m`, `]m`, `[M`,`]M` motions. `af` text object |
|
||||
| @function.inside | The function body (the stuff within the braces). | `if` text object |
|
||||
| @class.around | An entire class definition or equivalent large section of a file. | `[[`, `]]`, `[]`, `][` motions. `ac` text object |
|
||||
| @class.inside | The contents of a class definition. | `ic` text object |
|
||||
| @comment.around | An entire comment (e.g. all adjacent line comments, or a block comment) | `gc` text object |
|
||||
| @comment.inside | The contents of a comment | `igc` text object (rarely supported) |
|
||||
|
||||
For example:
|
||||
|
||||
```scheme
|
||||
; include only the content of the method in the function
|
||||
(method_definition
|
||||
body: (_
|
||||
"{"
|
||||
(_)* @function.inside
|
||||
"}")) @function.around
|
||||
|
||||
; match function.around for declarations with no body
|
||||
(function_signature_item) @function.around
|
||||
|
||||
; join all adjacent comments into one
|
||||
(comment)+ @comment.around
|
||||
```
|
||||
|
||||
### Text redactions
|
||||
|
||||
The `redactions.scm` file defines text redaction rules. When collaborating and sharing your screen, it makes sure that certain syntax nodes are rendered in a redacted mode to avoid them from leaking.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue