Skip to content

Commit 046f384

Browse files
authored
Add textobjects.scm for Java (#111)
This adds a `textobjects.scm` file to enable Tree-sitter-based text objects for Java. Currently, Zed only supports this functionality in Vim mode. With this addition, you'll be able to treat several Java constructs as text objects and target them with any Vim operator like `d` (delete), `c` (change) or `y` (copy) or select them with `v`, as well as navigate code semantically. Here are the details: - Use `ic` / `ac` to work **i**nside or **a**round a `class`, `interface`, `enum`, `record` and `annotation` - Use `if` / `af` to work **i**nside or **a**round a `method`, `lambda` and `constructor` - Use `igc` / `agc` to work **i**nside or **a**round a `single-line comment`, `multi-line comment`and `javadoc comment` - Use `]m` / `[m` to go the next or previous `method` or `constructor` - Use `]]` / `[[`to go the next or previous class-like construct (a `class`, `interface`, etc.). - Use `]/` / `[/` to go to the next or previous `comment`. If you are interested in knowing more about these features, you can find more information in the official Zed documentation for Tree-sitter and for Text Objects. The Default Vim Bindings file in Zed is also a good resource. There are good built-in text objects like `i` for indentation, `b` for blocks or `[x` / `]x` for selecting greater or smaller syntax nodes.
1 parent 83802f4 commit 046f384

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

languages/java/textobjects.scm

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; methods
2+
(method_declaration) @function.around
3+
(method_declaration
4+
body: (block
5+
"{" (_)* @function.inside "}"
6+
))
7+
8+
; constructors
9+
(constructor_declaration) @function.around
10+
(constructor_declaration
11+
body: (constructor_body
12+
"{" (_)* @function.inside "}"
13+
))
14+
15+
; lambdas
16+
(lambda_expression) @function.around
17+
(lambda_expression
18+
body: (block
19+
"{" (_)* @function.inside "}"
20+
))
21+
(lambda_expression
22+
body: (_) @function.inside)
23+
24+
25+
; classes
26+
(class_declaration) @class.around
27+
(class_declaration
28+
body: (class_body
29+
"{" (_)* @class.inside "}"
30+
))
31+
32+
; interfaces
33+
(interface_declaration) @class.around
34+
(interface_declaration
35+
body: (interface_body
36+
"{" (_)* @class.inside "}"
37+
))
38+
39+
; enums
40+
(enum_declaration) @class.around
41+
(enum_declaration
42+
body: (enum_body
43+
"{"
44+
_* @class.inside
45+
"}"
46+
))
47+
48+
; records
49+
(record_declaration) @class.around
50+
(record_declaration
51+
body: (class_body
52+
"{" (_)* @class.inside "}"
53+
))
54+
55+
; annotations
56+
(annotation_type_declaration) @class.around
57+
(annotation_type_declaration
58+
(annotation_type_body
59+
"{" (_)* @class.inside "}"
60+
))
61+
62+
63+
; comments
64+
((line_comment)+) @comment.around
65+
(block_comment) @comment.around

0 commit comments

Comments
 (0)