You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/extensions/languages.md
+40Lines changed: 40 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -69,6 +69,7 @@ several features:
69
69
- Syntax overrides
70
70
- Text redactions
71
71
- Runnable code detection
72
+
- Selecting classes, functions, etc.
72
73
73
74
The following sections elaborate on how [Tree-sitter queries](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax) enable these
74
75
features in Zed, using [JSON syntax](https://www.json.org/json-en.html) as a guiding example.
@@ -259,6 +260,45 @@ For example, in JavaScript, we also disable auto-closing of single quotes within
259
260
(comment) @comment.inclusive
260
261
```
261
262
263
+
### Text objects
264
+
265
+
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.
266
+
267
+
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.
268
+
269
+
When there's only one level, use the function matchers instead of class as the `[[` family of motions will fall-back to methods if there are no classes defined. For example, Markdown defines a section as a method so that you can use `[[` to jump to the next heading.
270
+
271
+
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.
272
+
273
+
For languages with declarations like C, provide queries that match `@class.around` or `@function.around`. The `im` and `ic` text objects will default to these if there is no inside.
274
+
275
+
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 langauges. 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.
276
+
277
+
| Capture | Description | Vim mode |
278
+
|------------------|-------------|----------|
279
+
|@function.around | An entire function definition or equivalent small section of a file. |`[m`, `]m`, `[M`,`]M` motions. `am` text object |
280
+
|@function.inside | The function body (the stuff within the braces). |`im` text object |
281
+
|@class.around | An entire class definition or equivalent large section of a file. |`[[`, `]]`, `[]`, `][` motions. `ac` text object |
282
+
|@class.inside | The contents of a class definition. |`ic` text object |
283
+
|@comment.around | An entire comment (e.g. all adjacent line comments, or a block comment) |`gc` text object |
284
+
|@comment.inside | The contents of a comment |`igc` text object (rarely supported) |
285
+
286
+
For example:
287
+
```scheme
288
+
; include only the content of the method in the function
289
+
(method_definition
290
+
body: (_
291
+
"{"
292
+
(_)* @function.inside
293
+
"}")) @function.around
294
+
295
+
; match function.around for declarations with no body
296
+
(function_signature_item) @function.around
297
+
298
+
; join all adjacent comments into one
299
+
(comment)+ @comment.around
300
+
```
301
+
262
302
### Text redactions
263
303
264
304
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.
0 commit comments