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
356
357
358
359
//! Code Lens (`textDocument/codeLens`) support.
//!
//! Shows clickable annotations above methods that override a parent
//! method or implement an interface method, linking to the prototype
//! declaration.
use tower_lsp::lsp_types::*;
use crate::Backend;
use crate::definition::member::MemberKind;
use crate::types::{ClassInfo, ClassLikeKind, MAX_INHERITANCE_DEPTH};
use crate::util::offset_to_position;
/// Information about a prototype (ancestor) method that a local method
/// overrides or implements.
struct Prototype {
/// Display name of the ancestor class (short name).
ancestor_name: String,
/// Whether the ancestor is an interface.
is_interface: bool,
/// URI of the file containing the ancestor class.
file_uri: String,
/// Position of the method declaration in the ancestor's file.
position: Position,
}
impl Backend {
/// Handle a `textDocument/codeLens` request.
///
/// Returns a code lens for each method in the file that overrides
/// a parent class method or implements an interface method.
pub fn handle_code_lens(&self, uri: &str, content: &str) -> Option<Vec<CodeLens>> {
let classes = {
let map = self.ast_map.read();
map.get(uri)?.clone()
};
let mut lenses = Vec::new();
for class in &classes {
let class_fqn = class.fqn();
for method in &class.methods {
// Skip synthetic/stub methods with no real source position.
if method.name_offset == 0 {
continue;
}
// Skip virtual methods (injected via @method tags, not
// actually declared in source).
if method.is_virtual {
continue;
}
if let Some(proto) =
self.find_prototype(class, &class_fqn, &method.name, uri, content)
{
let pos = offset_to_position(content, method.name_offset as usize);
let range = Range {
start: Position {
line: pos.line,
character: 0,
},
end: Position {
line: pos.line,
character: 0,
},
};
let icon = if proto.is_interface { "◆" } else { "↑" };
let title = format!("{} {}::{}", icon, proto.ancestor_name, method.name);
// Build a URI with a fragment encoding the target
// line and column so that `vscode.open` jumps to the
// right position. This avoids the `instanceof`
// constraint errors that `editor.action.goToLocations`
// and `editor.action.showReferences` trigger when
// called from an LSP server without a companion
// extension to convert plain JSON into VS Code class
// instances.
let fragment = format!(
"L{},{}",
proto.position.line + 1,
proto.position.character + 1
);
let mut target_uri: Url = match proto.file_uri.parse() {
Ok(u) => u,
Err(_) => continue,
};
target_uri.set_fragment(Some(&fragment));
let command = Command {
title,
command: "vscode.open".to_string(),
arguments: Some(vec![serde_json::json!(target_uri)]),
};
lenses.push(CodeLens {
range,
command: Some(command),
data: None,
});
}
}
}
if lenses.is_empty() {
None
} else {
Some(lenses)
}
}
/// Search the inheritance hierarchy for the closest ancestor that
/// declares a method with the given name.
///
/// Priority order: parent class chain, then used traits, then
/// implemented interfaces. Returns `None` when no ancestor
/// declares the method.
fn find_prototype(
&self,
class: &ClassInfo,
_class_fqn: &str,
method_name: &str,
current_uri: &str,
current_content: &str,
) -> Option<Prototype> {
// ── 1. Walk the parent class chain ──────────────────────────────
let mut current = class.clone();
for _ in 0..MAX_INHERITANCE_DEPTH {
let parent_name = match current.parent_class.as_ref() {
Some(name) => name.clone(),
None => break,
};
let parent = match self.find_or_load_class(&parent_name) {
Some(p) => ClassInfo::clone(&p),
None => break,
};
// Check methods declared directly on this parent (not
// inherited) so we find the actual declaration site.
if parent
.methods
.iter()
.any(|m| m.name == method_name && !m.is_virtual)
&& let Some(proto) = self.build_prototype(
&parent_name,
&parent,
method_name,
false,
current_uri,
current_content,
)
{
return Some(proto);
}
current = parent;
}
// ── 2. Check used traits ────────────────────────────────────────
if let Some(proto) = self.find_prototype_in_traits(
&class.used_traits,
method_name,
current_uri,
current_content,
0,
) {
return Some(proto);
}
// ── 3. Check implemented interfaces ─────────────────────────────
if let Some(proto) =
self.find_prototype_in_interfaces(class, method_name, current_uri, current_content)
{
return Some(proto);
}
None
}
/// Search a list of traits for a method declaration.
///
/// Recursively checks traits used by each trait, up to a depth limit.
fn find_prototype_in_traits(
&self,
trait_names: &[String],
method_name: &str,
current_uri: &str,
current_content: &str,
depth: usize,
) -> Option<Prototype> {
if depth > MAX_INHERITANCE_DEPTH as usize {
return None;
}
for trait_name in trait_names {
let trait_info = match self.find_or_load_class(trait_name) {
Some(t) => t,
None => continue,
};
if trait_info
.methods
.iter()
.any(|m| m.name == method_name && !m.is_virtual)
&& let Some(proto) = self.build_prototype(
trait_name,
&trait_info,
method_name,
false,
current_uri,
current_content,
)
{
return Some(proto);
}
// Recurse into traits used by this trait.
if let Some(proto) = self.find_prototype_in_traits(
&trait_info.used_traits,
method_name,
current_uri,
current_content,
depth + 1,
) {
return Some(proto);
}
}
None
}
/// Search implemented interfaces (including those inherited from
/// parents) for a method declaration.
fn find_prototype_in_interfaces(
&self,
class: &ClassInfo,
method_name: &str,
current_uri: &str,
current_content: &str,
) -> Option<Prototype> {
// Collect all interface names from the class and its parent chain.
let mut all_iface_names: Vec<String> = class.interfaces.clone();
let mut current = class.clone();
for _ in 0..MAX_INHERITANCE_DEPTH {
let parent_name = match current.parent_class.as_ref() {
Some(name) => name.clone(),
None => break,
};
let parent = match self.find_or_load_class(&parent_name) {
Some(p) => ClassInfo::clone(&p),
None => break,
};
for iface in &parent.interfaces {
if !all_iface_names.contains(iface) {
all_iface_names.push(iface.clone());
}
}
current = parent;
}
for iface_name in &all_iface_names {
if let Some(proto) = self.find_prototype_in_interface(
iface_name,
method_name,
current_uri,
current_content,
) {
return Some(proto);
}
}
None
}
/// Check a single interface (and its own extends chain) for the
/// method declaration.
fn find_prototype_in_interface(
&self,
iface_name: &str,
method_name: &str,
current_uri: &str,
current_content: &str,
) -> Option<Prototype> {
let iface = self.find_or_load_class(iface_name)?;
if iface
.methods
.iter()
.any(|m| m.name == method_name && !m.is_virtual)
&& let Some(proto) = self.build_prototype(
iface_name,
&iface,
method_name,
true,
current_uri,
current_content,
)
{
return Some(proto);
}
// Walk the interface's own extends chain (interfaces can extend
// other interfaces via `parent_class` and `interfaces`).
for parent_iface in &iface.interfaces {
if let Some(proto) = self.find_prototype_in_interface(
parent_iface,
method_name,
current_uri,
current_content,
) {
return Some(proto);
}
}
if let Some(ref parent_name) = iface.parent_class
&& let Some(proto) = self.find_prototype_in_interface(
parent_name,
method_name,
current_uri,
current_content,
)
{
return Some(proto);
}
None
}
/// Build a `Prototype` by locating the method's position in the
/// ancestor's source file.
fn build_prototype(
&self,
ancestor_fqn: &str,
ancestor_class: &ClassInfo,
method_name: &str,
is_interface: bool,
current_uri: &str,
current_content: &str,
) -> Option<Prototype> {
let (file_uri, file_content) =
self.find_class_file_content(ancestor_fqn, current_uri, current_content)?;
let name_offset = ancestor_class.member_name_offset(method_name, "method");
let position = Self::find_member_position(
&file_content,
method_name,
MemberKind::Method,
name_offset,
)?;
// Determine whether to treat this as an interface based on the
// ancestor's kind (the caller's hint is a fallback).
let is_iface = ancestor_class.kind == ClassLikeKind::Interface || is_interface;
Some(Prototype {
ancestor_name: ancestor_class.name.clone(),
is_interface: is_iface,
file_uri,
position,
})
}
}