Skip to content

Commit b58e22f

Browse files
authored
Improve the outline (#136)
This PR adds several changes to improve the current state of the outline for multiple Java pieces of code. This will not only be reflected in the outline panel, but also in the buffer symbols, which in my case, is what I prefer to navigate across all the symbols in the current file. At the beginning, my motivation was to solve an issue related to how the methods were appearing in the sticky scroll, but this led me a rabbit hole where I discovered that a lot more was missing in the outline, namely the following: Things that didn't appear in the outline before: 1. Constructors. 2. Any `class`, `interface`, `enum`, `record` and `annotation` (even if nested) that had no modifiers (access and non-access). 3. Fields and methods that had no modifiers (access and non-access). 4. Static blocks. 5. The types of the fields and the return type of methods. 6. Enum declaration. 7. Enum constants. 8. Annotation type declaration. 9. Annotation type elements. 10. The constant fields of an interface. 11. The parameters of a record constructor. And lastly, these changes have as consequence also the fixing of an issue related to the sticky scroll. The purpose of the sticky scroll is to give you context about the class and the method you are currently in, which can be very useful in multiple scenarios. However, there was an issue that when a method or class had an annotation, that annotation would appear in the sticky scroll instead of the whole method declaration, defeating the purpose of the sticky scroll. This feature was recently [introduced](zed-industries/zed#42242 (comment)) and can be enabled in `settings.json`: ```json "sticky_scroll": { "enabled": true } ``` **Limitations I found**: 1. Currently, there's always a space after a parameter type in the outline. Even before these changes, when a method had parameters, even though the parameter types were not shown, there was a space left inside the parenthesis. After trying to solve this with no success, I'm very inclined to believe this is probably an issue on Zed's side. 2. For records, the constructor parameters appear at the top level in the outline. To nest them under the record declaration, we need the entire `record_declaration` marked as `@item`, but marking the entire `record_declaration` as `@item` causes the sticky scroll to show the annotation (because it includes the entire text range starting with any annotation that is present). **Before**: https://github.com/user-attachments/assets/ba836bc7-3259-4e2b-b100-ce0e57c88b67 **Now**: https://github.com/user-attachments/assets/12604ee1-7631-4b75-a95f-a30aff87add8
1 parent cec444d commit b58e22f

File tree

1 file changed

+141
-9
lines changed

1 file changed

+141
-9
lines changed

languages/java/outline.scm

Lines changed: 141 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"final"
1111
"strictfp"
1212
"static"
13-
]* @context)
13+
]* @context)?
1414
"class" @context
15-
name: (_) @name) @item
15+
name: (_) @name
16+
body: (_) @item)
1617

1718
(record_declaration
1819
(modifiers
@@ -26,9 +27,10 @@
2627
"final"
2728
"strictfp"
2829
"static"
29-
]* @context)
30+
]* @context)?
3031
"record" @context
31-
name: (_) @name) @item
32+
name: (_) @name
33+
body: (_) @item)
3234

3335
(interface_declaration
3436
(modifiers
@@ -41,9 +43,66 @@
4143
"non-sealed"
4244
"strictfp"
4345
"static"
44-
]* @context)
46+
]* @context)?
4547
"interface" @context
46-
name: (_) @name) @item
48+
name: (_) @name
49+
body: (_) @item)
50+
51+
(enum_declaration
52+
(modifiers
53+
[
54+
"private"
55+
"public"
56+
"protected"
57+
"static"
58+
"final"
59+
"strictfp"
60+
]* @context)?
61+
"enum" @context
62+
name: (_) @name
63+
body: (_) @item)
64+
65+
(annotation_type_declaration
66+
(modifiers
67+
[
68+
"private"
69+
"public"
70+
"protected"
71+
"abstract"
72+
"static"
73+
"strictfp"
74+
]* @context)?
75+
"@interface" @context
76+
name: (_) @name
77+
body: (_) @item)
78+
79+
(enum_constant
80+
name: (identifier) @name) @item
81+
82+
(method_declaration
83+
(modifiers
84+
[
85+
"private"
86+
"public"
87+
"protected"
88+
"abstract"
89+
"static"
90+
"final"
91+
"native"
92+
"strictfp"
93+
"synchronized"
94+
]* @context)?
95+
type: (_) @context
96+
name: (_) @name
97+
parameters: (formal_parameters
98+
"(" @context
99+
(formal_parameter
100+
type: (_) @context)?
101+
("," @context
102+
(formal_parameter
103+
type: (_) @context)?)*
104+
")" @context)
105+
body: (_) @item)
47106

48107
(method_declaration
49108
(modifiers
@@ -57,11 +116,62 @@
57116
"native"
58117
"strictfp"
59118
"synchronized"
60-
]* @context)
119+
]* @context)?
120+
type: (_) @context
61121
name: (_) @name
62122
parameters: (formal_parameters
63123
"(" @context
64-
")" @context)) @item
124+
(formal_parameter
125+
type: (_) @context)?
126+
("," @context
127+
(formal_parameter
128+
type: (_) @context)?)*
129+
")" @context)
130+
";" @item)
131+
132+
(constructor_declaration
133+
(modifiers
134+
[
135+
"private"
136+
"public"
137+
"protected"
138+
"static"
139+
"final"
140+
]* @context)?
141+
name: (_) @name
142+
parameters: (formal_parameters
143+
"(" @context
144+
(formal_parameter
145+
type: (_) @context)?
146+
("," @context
147+
(formal_parameter
148+
type: (_) @context)?)*
149+
")" @context)
150+
body: (_) @item)
151+
152+
(compact_constructor_declaration
153+
(modifiers
154+
[
155+
"private"
156+
"public"
157+
"protected"
158+
]* @context)?
159+
name: (_) @name
160+
body: (_) @item)
161+
162+
(annotation_type_element_declaration
163+
(modifiers
164+
[
165+
"private"
166+
"public"
167+
"protected"
168+
"abstract"
169+
"static"
170+
]* @context)?
171+
type: (_) @context
172+
name: (_) @name
173+
"(" @context
174+
")" @context) @item
65175

66176
(field_declaration
67177
(modifiers
@@ -73,6 +183,28 @@
73183
"final"
74184
"transient"
75185
"volatile"
76-
]* @context)
186+
]* @context)?
187+
type: (_) @context
77188
declarator: (variable_declarator
78189
name: (_) @name)) @item
190+
191+
(constant_declaration
192+
(modifiers
193+
[
194+
"public"
195+
"static"
196+
"final"
197+
]* @context)?
198+
type: (_) @context
199+
declarator: (variable_declarator
200+
name: (_) @name)) @item
201+
202+
(static_initializer
203+
"static" @context
204+
(block) @item)
205+
206+
(record_declaration
207+
parameters: (formal_parameters
208+
(formal_parameter
209+
type: (_) @context
210+
name: (_) @name) @item))

0 commit comments

Comments
 (0)