1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/// `textDocument/declaration` — jump to the abstract or interface declaration of a symbol.
///
/// In PHP the distinction between declaration and definition matters for:
/// - Interface methods (declared but never given a body)
/// - Abstract class methods
///
/// For concrete symbols with no abstract counterpart this falls back to the same
/// result as go-to-definition so the request is never empty-handed.
use std::sync::Arc;
use php_ast::{ClassMemberKind, EnumMemberKind, NamespaceBody, Stmt, StmtKind};
use tower_lsp::lsp_types::{Location, Position, Url};
use crate::ast::{ParsedDoc, SourceView};
use crate::util::{strip_variable_sigil, utf16_code_units, word_at_position};
/// Find the abstract or interface declaration of `word`.
/// Prefers abstract/interface declarations; falls back to any declaration.
pub fn goto_declaration(
source: &str,
all_docs: &[(Url, Arc<ParsedDoc>)],
position: Position,
) -> Option<Location> {
let word = word_at_position(source, position)?;
// First pass: look for an abstract or interface declaration
for (uri, doc) in all_docs {
let sv = doc.view();
if let Some(range) = find_abstract_declaration(sv, &doc.program().stmts, &word) {
return Some(Location {
uri: uri.clone(),
range,
});
}
}
// Second pass: any declaration (same as goto_definition)
for (uri, doc) in all_docs {
let sv = doc.view();
if let Some(range) = find_any_declaration(sv, &doc.program().stmts, &word) {
return Some(Location {
uri: uri.clone(),
range,
});
}
}
None
}
fn find_abstract_declaration(
sv: SourceView<'_>,
stmts: &[Stmt<'_, '_>],
word: &str,
) -> Option<tower_lsp::lsp_types::Range> {
for stmt in stmts {
match &stmt.kind {
StmtKind::Interface(i) => {
// Interface methods are declarations without bodies
for member in i.members.iter() {
if let ClassMemberKind::Method(m) = &member.kind
&& m.name == word
{
return Some(sv.name_range(&m.name.to_string()));
}
}
if i.name == word {
return Some(sv.name_range(&i.name.to_string()));
}
}
StmtKind::Class(c) => {
for member in c.members.iter() {
if let ClassMemberKind::Method(m) = &member.kind
&& m.is_abstract
&& m.name == word
{
return Some(sv.name_range(&m.name.to_string()));
}
}
}
StmtKind::Trait(t) => {
for member in t.members.iter() {
if let ClassMemberKind::Method(m) = &member.kind
&& m.is_abstract
&& m.name == word
{
return Some(sv.name_range(&m.name.to_string()));
}
}
}
StmtKind::Namespace(ns) => {
if let NamespaceBody::Braced(inner) = &ns.body
&& let Some(r) = find_abstract_declaration(sv, inner, word)
{
return Some(r);
}
}
_ => {}
}
}
None
}
fn find_any_declaration(
sv: SourceView<'_>,
stmts: &[Stmt<'_, '_>],
word: &str,
) -> Option<tower_lsp::lsp_types::Range> {
let bare = strip_variable_sigil(word);
for stmt in stmts {
match &stmt.kind {
StmtKind::Function(f) if f.name == word => {
return Some(sv.name_range(&f.name.to_string()));
}
StmtKind::Class(c)
if c.name.as_ref().map(|n| n.to_string()) == Some(word.to_string()) =>
{
return Some(
sv.name_range(
&c.name
.as_ref()
.map(|n| n.to_string())
.expect("match guard ensures Some"),
),
);
}
StmtKind::Class(c) => {
for member in c.members.iter() {
match &member.kind {
ClassMemberKind::Method(m) if m.name == word => {
return Some(sv.name_range(&m.name.to_string()));
}
ClassMemberKind::ClassConst(cc) if cc.name == word => {
return Some(sv.name_range(&cc.name.to_string()));
}
ClassMemberKind::Property(p) if p.name == bare => {
return Some(sv.name_range(&p.name.to_string()));
}
_ => {}
}
}
}
StmtKind::Interface(i) => {
if i.name == word {
return Some(sv.name_range(&i.name.to_string()));
}
for member in i.members.iter() {
match &member.kind {
ClassMemberKind::Method(m) if m.name == word => {
return Some(sv.name_range(&m.name.to_string()));
}
ClassMemberKind::ClassConst(cc) if cc.name == word => {
return Some(sv.name_range(&cc.name.to_string()));
}
_ => {}
}
}
}
StmtKind::Trait(t) => {
if t.name == word {
return Some(sv.name_range(&t.name.to_string()));
}
for member in t.members.iter() {
match &member.kind {
ClassMemberKind::Method(m) if m.name == word => {
return Some(sv.name_range(&m.name.to_string()));
}
ClassMemberKind::ClassConst(cc) if cc.name == word => {
return Some(sv.name_range(&cc.name.to_string()));
}
ClassMemberKind::Property(p) if p.name == bare => {
return Some(sv.name_range(&p.name.to_string()));
}
_ => {}
}
}
}
StmtKind::Enum(e) if e.name == word => {
return Some(sv.name_range(&e.name.to_string()));
}
StmtKind::Enum(e) => {
for member in e.members.iter() {
match &member.kind {
EnumMemberKind::Case(c) if c.name == word => {
return Some(sv.name_range(&c.name.to_string()));
}
EnumMemberKind::Method(m) if m.name == word => {
return Some(sv.name_range(&m.name.to_string()));
}
EnumMemberKind::ClassConst(cc) if cc.name == word => {
return Some(sv.name_range(&cc.name.to_string()));
}
_ => {}
}
}
}
StmtKind::Namespace(ns) => {
if let NamespaceBody::Braced(inner) = &ns.body
&& let Some(r) = find_any_declaration(sv, inner, word)
{
return Some(r);
}
}
_ => {}
}
}
None
}
/// Find abstract or interface declaration using `FileIndex` entries.
/// Returns line-only positions (character 0) for unopened files.
/// This is a limitation of the compact FileIndex — for opened files,
/// goto_declaration() provides precise name ranges.
pub fn goto_declaration_from_index(
source: &str,
indexes: &[(
tower_lsp::lsp_types::Url,
std::sync::Arc<crate::file_index::FileIndex>,
)],
position: tower_lsp::lsp_types::Position,
) -> Option<Location> {
use crate::file_index::ClassKind;
use crate::util::word_at_position;
let word = word_at_position(source, position)?;
let bare = strip_variable_sigil(&word);
let precise_range = |line: u32, name_char: u32, name: &str| -> tower_lsp::lsp_types::Range {
let end_char = name_char + utf16_code_units(name);
tower_lsp::lsp_types::Range {
start: tower_lsp::lsp_types::Position {
line,
character: name_char,
},
end: tower_lsp::lsp_types::Position {
line,
character: end_char,
},
}
};
// First pass: abstract/interface declarations.
for (uri, idx) in indexes {
for cls in &idx.classes {
match cls.kind {
ClassKind::Interface => {
// Interface itself.
if cls.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(cls.start_line, cls.name_char, &cls.name),
});
}
// Abstract method in interface.
for m in &cls.methods {
if m.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(m.start_line, m.name_char, &m.name),
});
}
}
}
ClassKind::Trait => {
// Trait abstract methods.
for m in &cls.methods {
if m.is_abstract && m.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(m.start_line, m.name_char, &m.name),
});
}
}
}
_ if cls.is_abstract => {
// Abstract methods in abstract classes.
for m in &cls.methods {
if m.is_abstract && m.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(m.start_line, m.name_char, &m.name),
});
}
}
}
_ => {}
}
}
}
// Second pass: any declaration.
for (uri, idx) in indexes {
// Top-level functions.
for f in &idx.functions {
if f.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(f.start_line, f.name_char, &f.name),
});
}
}
for cls in &idx.classes {
// Class/Interface/Trait/Enum declarations.
if cls.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(cls.start_line, cls.name_char, &cls.name),
});
}
// Methods.
for m in &cls.methods {
if m.name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(m.start_line, m.name_char, &m.name),
});
}
}
// Properties.
for p in &cls.properties {
if p.name.as_ref() == bare {
return Some(Location {
uri: uri.clone(),
range: precise_range(p.start_line, p.name_char, &p.name),
});
}
}
// Class/Interface/Trait/Enum constants.
for c in &cls.constants {
if c.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(cls.start_line, cls.name_char, &cls.name),
});
}
}
// Enum cases (stored in separate `cases` field).
if cls.kind == ClassKind::Enum {
for case_name in &cls.cases {
if case_name.as_ref() == word {
return Some(Location {
uri: uri.clone(),
range: precise_range(cls.start_line, cls.name_char, &cls.name),
});
}
}
}
}
}
None
}