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
//! Unified query surface across FileStore + ModuleIndex.
//!
//! All cross-file LSP queries (references, rename, workspace/symbol) route
//! through this module so that each handler is a one-liner against a single
//! role-masked function instead of reinventing the per-tier walk.
//!
//! As of phase 4, `refs_to` collects references to a named target across the
//! callers-visible set of files. `resolve_symbol` is left as future work — the
//! current handlers still use cursor-position tree resolution to identify the
//! target, then call into `refs_to` for the cross-file collection.
use std::path::PathBuf;
use tower_lsp::lsp_types::Url;
use crate::file_analysis::{AccessKind, FileAnalysis, HandlerOwner, RefKind, Span, SymKind};
use crate::file_store::{FileKey, FileStore};
use crate::module_index::ModuleIndex;
bitflags::bitflags! {
/// Which file roles a query should search. Handlers pick the mask that
/// fits their semantics: rename is EDITABLE (skip deps, they're read-only);
/// references is VISIBLE (include deps, read-only reads are fine).
pub struct RoleMask: u8 {
const OPEN = 1 << 0;
const WORKSPACE = 1 << 1;
const DEPENDENCY = 1 << 2;
const BUILTIN = 1 << 3;
const EDITABLE = Self::OPEN.bits() | Self::WORKSPACE.bits();
const VISIBLE = Self::OPEN.bits() | Self::WORKSPACE.bits() | Self::DEPENDENCY.bits() | Self::BUILTIN.bits();
}
}
/// Identifies what we're collecting references to.
#[derive(Debug, Clone)]
pub struct TargetRef {
pub name: String,
pub kind: TargetKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TargetKind {
/// A sub defined in a specific package. `None` = the sub has
/// no package context (top-level script). Matches `Sub`/`Method`
/// symbols whose `package` field equals this, and `FunctionCall`
/// refs whose `resolved_package` equals this. Package-scoping
/// mirrors method's class-scoping — name-only matching
/// cross-links `Foo::run` and `Bar::run`.
Sub { package: Option<String> },
/// A method on a specific class. Matches `Sub`/`Method` symbols
/// whose `package == class`, and `MethodCall` refs whose invocant
/// resolves to `class`.
Method { class: String },
/// A package/class/module name — matches PackageRef refs.
Package,
/// A hash key owned by a specific sub's return value. `package` is the
/// sub's defining package (or None for top-level / unpackaged subs);
/// matches `HashKeyOwner::Sub { package, name }` by structural equality.
HashKeyOfSub { package: Option<String>, name: String },
/// A hash key owned by a class (Moo `has` slots, DBIC columns on a Result class).
HashKeyOfClass(String),
/// A `Handler` symbol registered on a class (Mojo events, Dancer
/// routes, etc.). Both the definition (`Handler` symbol) and call
/// sites (`DispatchCall` refs) match; stacked registrations all
/// surface separately so features can enumerate every handler.
Handler {
owner: HandlerOwner,
name: String,
},
}
/// A located reference in some file.
#[derive(Debug, Clone)]
pub struct RefLocation {
pub key: FileKey,
pub span: Span,
/// Read/Write/Declaration — used by document_highlight callers that will
/// migrate to `refs_to` in a follow-up.
#[allow(dead_code)]
pub access: AccessKind,
}
impl RefLocation {
pub fn to_url(&self) -> Option<Url> {
match &self.key {
FileKey::Url(u) => Some(u.clone()),
FileKey::Path(p) => Url::from_file_path(p).ok(),
}
}
}
/// Collect every reference to `target` across the masked file set.
///
/// - `files` — open + workspace store
/// - `module_index` — dep cache (consulted only if mask includes Dependency)
pub fn refs_to(
files: &FileStore,
module_index: Option<&ModuleIndex>,
target: &TargetRef,
mask: RoleMask,
) -> Vec<RefLocation> {
let mut out = Vec::new();
// Open files (canonical — workspace entries for open paths are skipped).
let mut covered_paths: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
if mask.contains(RoleMask::OPEN) {
files.for_each_open_mut(|url, doc| {
let url = url.clone();
if let Ok(p) = url.to_file_path() {
covered_paths.insert(p);
}
collect_from_analysis(&FileKey::Url(url), &doc.analysis, target, module_index, &mut out);
});
} else {
// Even if open isn't in the mask, track the paths so a WORKSPACE walk
// doesn't duplicate them (an open file's pre-close state isn't meaningful).
files.for_each_open_mut(|url, _doc| {
if let Ok(p) = url.to_file_path() {
covered_paths.insert(p);
}
});
}
// Workspace files.
if mask.contains(RoleMask::WORKSPACE) {
for entry in files.workspace_raw().iter() {
if covered_paths.contains(entry.key()) {
continue;
}
collect_from_analysis(&FileKey::Path(entry.key().clone()), entry.value(), target, module_index, &mut out);
}
}
// Dependencies (read-only modules from @INC).
if mask.contains(RoleMask::DEPENDENCY) {
if let Some(idx) = module_index {
idx.for_each_cached(|_module_name, cached| {
let key = FileKey::Path(cached.path.clone());
collect_from_analysis(&key, &cached.analysis, target, module_index, &mut out);
});
}
}
// Sort for stable output, dedupe by (path, span).
out.sort_by(|a, b| {
key_for_sort(&a.key)
.cmp(&key_for_sort(&b.key))
.then_with(|| {
(a.span.start.row, a.span.start.column)
.cmp(&(b.span.start.row, b.span.start.column))
})
});
out.dedup_by(|a, b| file_key_eq(&a.key, &b.key) && a.span == b.span);
out
}
fn key_for_sort(k: &FileKey) -> PathBuf {
match k {
FileKey::Path(p) => p.clone(),
FileKey::Url(u) => u.to_file_path().unwrap_or_else(|_| PathBuf::from(u.as_str())),
}
}
fn file_key_eq(a: &FileKey, b: &FileKey) -> bool {
key_for_sort(a) == key_for_sort(b)
}
fn collect_from_analysis(
key: &FileKey,
analysis: &FileAnalysis,
target: &TargetRef,
module_index: Option<&ModuleIndex>,
out: &mut Vec<RefLocation>,
) {
use crate::file_analysis::{HashKeyOwner, SymbolDetail};
// Include declaration spans when this file defines the target.
for sym in &analysis.symbols {
if sym.name != target.name {
continue;
}
// Treat a sub and a method in the same package as the same
// callable — Perl's only distinction between them is call
// shape. `Sub { package }` matches exactly that scope (None
// = top-level script sub); `Method { class }` is the
// `Sub { package: Some(class) }` case with stricter intent.
let callable_scope: Option<Option<String>> = match &target.kind {
TargetKind::Sub { package } => Some(package.clone()),
TargetKind::Method { class } => Some(Some(class.clone())),
_ => None,
};
let matches_kind = match &target.kind {
TargetKind::Sub { .. } | TargetKind::Method { .. } => {
let scope = callable_scope.as_ref().unwrap();
matches!(sym.kind, SymKind::Sub | SymKind::Method)
&& sym.package == *scope
}
TargetKind::Package => matches!(
sym.kind,
SymKind::Package | SymKind::Class | SymKind::Module
),
TargetKind::HashKeyOfSub { package, name } => matches!(
&sym.detail,
SymbolDetail::HashKeyDef {
owner: HashKeyOwner::Sub { package: op, name: on },
..
} if op == package && on == name
),
TargetKind::HashKeyOfClass(wanted) => matches!(
&sym.detail,
SymbolDetail::HashKeyDef { owner: HashKeyOwner::Class(n), .. } if n == wanted
),
TargetKind::Handler { owner, name: hname } => {
sym.name == *hname && matches!(
&sym.detail,
SymbolDetail::Handler { owner: o, .. } if o == owner
)
}
};
if matches_kind {
out.push(RefLocation {
key: key.clone(),
span: sym.selection_span,
access: AccessKind::Declaration,
});
}
}
// Collect usage refs.
let callable_scope_for_refs: Option<Option<String>> = match &target.kind {
TargetKind::Sub { package } => Some(package.clone()),
TargetKind::Method { class } => Some(Some(class.clone())),
_ => None,
};
for r in &analysis.refs {
if r.target_name != target.name {
continue;
}
// Sub + Method both match any call into that scope — function
// or method shape — per the "same callable, two shapes"
// invariant. Filter is a single scope comparison.
let matches_kind = match (&target.kind, &r.kind) {
(TargetKind::Sub { .. } | TargetKind::Method { .. },
RefKind::FunctionCall { resolved_package }) => {
let scope = callable_scope_for_refs.as_ref().unwrap();
resolved_package == scope
}
(TargetKind::Sub { .. } | TargetKind::Method { .. },
RefKind::MethodCall { .. }) => {
// Method-shaped call: match only when the ref's
// invocant resolves to the target scope. The bag is
// the single resolver — `method_call_invocant_class`
// dispatches by invocant shape (variable / chain /
// bareword / `__PACKAGE__`) and queries the bag.
// Cross-file enrichment composes naturally because
// it pushes Variable witnesses the bag query reads.
// Class still has to be `Some` to match — package-
// less subs and genuinely unpinnable invocants stay
// out, no cross-linking.
let scope = callable_scope_for_refs.as_ref().unwrap();
match (analysis.method_call_invocant_class(r, module_index), scope) {
(Some(cn), Some(pkg)) => &cn == pkg,
_ => false,
}
}
(TargetKind::Package, RefKind::PackageRef) => true,
(
TargetKind::HashKeyOfSub { package, name },
RefKind::HashKeyAccess { owner, .. },
) => matches!(
owner,
Some(HashKeyOwner::Sub { package: op, name: on })
if op == package && on == name
),
(TargetKind::HashKeyOfClass(wanted), RefKind::HashKeyAccess { owner, .. }) => {
// `Class(wanted)` is the canonical shape for "this
// class's keys"; a `Sub { package: wanted, .. }`-
// owned access (constructor / search-arg /
// accessor) is *also* a hash-key access against
// `wanted`. Use `found_by` to admit both — same
// rule the in-file `hash_key_defs_for_owner`
// dispatcher uses, so cross-file `refs_to` and
// local find_definition agree on the set.
let target_owner = HashKeyOwner::Class(wanted.clone());
matches!(owner, Some(o) if o.found_by(&target_owner))
}
(TargetKind::Handler { owner, name: hname },
RefKind::DispatchCall { owner: ref_owner, .. }) => {
r.target_name == *hname
&& matches!(ref_owner, Some(o) if o == owner)
}
_ => false,
};
if matches_kind {
out.push(RefLocation {
key: key.clone(),
span: r.span,
access: r.access,
});
}
}
}
#[cfg(test)]
#[path = "resolve_tests.rs"]
mod tests;